DEF vSelectedSpreadIndex EQU VARIABLES_START DEF vPreviousSpreadIndex EQU vSelectedSpreadIndex + 1 def vSelectedSpreadCard equ vPreviousSpreadIndex + 1 ScreenSpreadSelect: dw SpreadSelectSetup dw SpreadSelectUpdate dw SpreadSelectDraw dw SpreadSelectTeardown SpreadSelectSetup: ld a, 0 ldh [vSelectedCardInSpread], a ldh [vPreviousSpreadIndex], a ldh [vSelectedSpreadIndex], a call UpdateCurrentSpread ld hl, SpreadSelectTilemap ld de, $9800 ld b, 18 ld c, 20 ld a, LOW(CopyTilesToMapThreadsafe) ld [vAsyncNext], a ld a, HIGH(CopyTilesToMapThreadsafe) ld [vAsyncNext+1], a ld a, LOW(.loadUITiles) ld [vAsyncAfter], a ld a, HIGH(.loadUITiles) ld [vAsyncAfter+1], a call DoInAsyncVBlank ret .loadUITiles ld hl, UITiles ; source ld de, $8000 + $80*16; destination of copy ld bc, UITilesEnd - UITiles ; length to copy ld a, LOW(CopyRangeUnsafe) ld [vAsyncNext], a ld a, HIGH(CopyRangeUnsafe) ld [vAsyncNext+1], a ld a, LOW(.zeroOutOAM) ld [vAsyncAfter], a ld a, HIGH(.zeroOutOAM) ld [vAsyncAfter+1], a ret .zeroOutOAM ld a, LOW(RunDMA) ld [vAsyncNext], a ld a, HIGH(RunDMA) ld [vAsyncNext+1], a ld a, LOW(DrawSpreadAsync) ld [vAsyncAfter], a ld a, HIGH(DrawSpreadAsync) ld [vAsyncAfter+1], a ld a, HIGH(ZEROES) ld de, $ffc0 ; arguments to the first following async call ret SpreadSelectUpdate: ld hl, rMYBTNP bit 4, [hl] jp z, .doneB ld hl, ScreenMainMenu call ChangeScene ret .doneB ld a, [vSelectedSpreadIndex] bit 3, [hl] ; select the down key jp z, .doneDown ; skip the following code if down is not pressed dec a ; increment when they press down because the deck has card 0 at the top .doneDown bit 2, [hl] ; select up key jp z, .doneUp ; skip the following code if up is not pressed inc a ; decrement when they press up because the deck has card 0 at the top .doneUp ld hl, Spreads call ArrayClampLooping ldh [vSelectedSpreadIndex], a ; load current selected tile index ld hl, vTime ldh a, [rDELTAT] ld b, a ldh a, [vTime] add a, b ldh [vTime], a ldh a, [vTime+1] adc a, 0 ldh [vTime+1], a ; increment time. when the 16bit time register is greater ; than 4096 ($10_00) then one second has passed. so that's satisfied when ; vTime+1 is equal to or greater than $10 ldh a, [vTime+1] cp a, $01 jp c, .doneUpdate ; if the timer is less than $0100, skip to end ;otherwise reset the timer ld a, 0 ldh [vTime], a ldh [vTime+1], a ld hl, SquaresTiles ldh a, [vFrameCountSquares] inc a call ArrayClampLooping ldh [vFrameCountSquares], a ldh a, [vSelectedSpreadIndex] ld hl, vPreviousSpreadIndex cp a, [hl] ret z ; we're done if previous index is same as new index call UpdateCurrentSpread ; execute an async call to DrawSpreadAsync. ld a, LOW(DrawSpreadAsync) ld [vAsyncNext], a ld a, HIGH(DrawSpreadAsync) ld [vAsyncNext+1], a call DoInAsyncVBlank .doneUpdate ret ; return from main thread. go back to looping. UpdateCurrentSpread: ; find the correct spread address ld hl, Spreads inc hl ; skip length of spreads ld a, [vSelectedSpreadIndex] inc a ; we're decing at the start so we inc first to prepare... ld d, 0 .findSpread dec a jp z, .spreadFound ld e, [hl] ; skip cards in spread inc hl add hl, de ld e, [hl] ; skip title of spread inc hl add hl, de ld e, [hl] ; skip description inc hl add hl, de jp .findSpread .spreadFound ld a, l ld [vCurrentSpread], a ld a, h ld [vCurrentSpread+1], a ; save the current spread (hl) into vcurrentspread. ret DrawSpreadAsync: ldh a, [vSelectedSpreadIndex] ldh [vPreviousSpreadIndex], a ld de, $9800 + 32*5 + 5 ld hl, ONES ld b, 8 ld c, 10 ld a, LOW(CopyTilesToMapThreadsafe) ld [vAsyncNext], a ld a, HIGH(CopyTilesToMapThreadsafe) ld [vAsyncNext+1], a ld a, LOW(.afterClear) ld [vAsyncAfter], a ld a, HIGH(.afterClear) ld [vAsyncAfter+1], a ret ; return from async execution now that we've registered desire ; to call copytilestomapthreadsafe ; and then drawspread .afterClear ld a, [vCurrentSpread] ld l, a ld a, [vCurrentSpread+1] ld h, a ld d, 0 ld e, [hl] inc e add hl, de ld de, $9800 + 32 + 1 ld a, LOW(PrintString) ld [vAsyncNext], a ld a, HIGH(PrintString) ld [vAsyncNext+1], a ld a, LOW(.afterTitle) ld [vAsyncAfter], a ld a, HIGH(.afterTitle) ld [vAsyncAfter+1], a ret .afterTitle ld de, $9800 + (32*2) + 1 ld a, LOW(PrintString) ld [vAsyncNext], a ld a, HIGH(PrintString) ld [vAsyncNext+1], a ld a, LOW(.afterDescription) ld [vAsyncAfter], a ld a, HIGH(.afterDescription) ld [vAsyncAfter+1], a ret .afterDescription ld hl, $9800 + 32*5 + 5 ldh a, [vSelectedSpreadCard] ld e, a ; e contains the selected index ld a, LOW(DrawSpreadBigAndThreadsafe) ld [vAsyncNext], a ld a, HIGH(DrawSpreadBigAndThreadsafe) ld [vAsyncNext+1], a ld a, LOW(.finishAsyncUpdate) ld [vAsyncAfter], a ld a, HIGH(.finishAsyncUpdate) ld [vAsyncAfter+1], a ret .finishAsyncUpdate ; signal that we're done with execution of the async thread ld a, 0 ld [vAsyncNext], a ld [vAsyncNext+1], a ld [vAsyncAfter], a ld [vAsyncAfter+1], a ret SpreadSelectDraw: di ld hl, SquaresTiles + 1 ld b, 0 ldh a, [vFrameCountSquares] ld c, a add hl, bc add hl, bc ld c, [hl] inc hl ld b, [hl] ld h, b ld l, c ld de, $8010 ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a inc de ld a, [hl+] ld [de], a reti SpreadSelectTeardown: ret DrawSpreadBigAndThreadsafe: ; hl for location on screen ; bc for current spread address ; actually holding that in vCurrentSpread ; e for index of selected card di ld a, [vCurrentSpread] ld c, a ld a, [vCurrentSpread+1] ld b, a ld a, [bc] cp a, 0 ; length of spread jp z, .doneDrawingSpread ; return early if the spread is empty ld d, a ; length of spread in d inc d .drawCards ei nop nop di dec d jp z, .doneWithSpread ; if we're drawing zero remaining cards, stop drawing inc bc ; step forward push hl ; need this bc drawsmallcard changes hl ld a, [bc] call DrawBigCard pop hl jp .drawCards .doneWithSpread ; stack has spread address ; d is zero ; e should have currently selected index ld b, h ld c, l ; stash hl in bc for a mo; this should be a location in vram ld a, [vCurrentSpread] ld l, a ld a, [vCurrentSpread+1] ld h, a ;spread address in hl inc e ; skip the tiles length add hl, de ld a, [hl] ; load card descriptor into a ld h, b ld l, c; retrieve vram address ei nop di nop nop call DrawBigCardSelected .doneDrawingSpread ei nop nop ret DrawBigCard: ; starting from screen location hl, draw a card at ;the location described in a ; saves de and is therefore not threadsafe push de ld d, a swap a and a, %0000_1111 sla a ld e, a ld a, d and a, %0000_1111 ld d, 0 add hl, de ; step right ld e, 64 jp z, .drawCard .stepDown add hl, de ; step down dec a jp nz, .stepDown .drawCard ld de, 32 ld [hl], $a inc hl ld [hl], $e add hl, de ld [hl], $f dec hl ld [hl], $b add hl, de ld [hl], $b inc hl ld [hl], $f add hl, de ld [hl], $1b dec hl ld [hl], $17 pop de ret DrawBigCardSelected: ; starting from screen location hl, draw a card at ;the location described in a push de push af swap a ld d, 0 and a, %0000_1111 sla a ld e, a add hl, de ; step right ld e, 64 pop af and a, %0000_1111 jp z, .drawCard .stepDown add hl, de ; step down dec a jp nz, .stepDown .drawCard ld de, 32 ld [hl], $12 inc hl ld [hl], $1e add hl, de ld [hl], $1f dec hl ld [hl], $13 add hl, de ld [hl], $13 inc hl ld [hl], $1f add hl, de ld [hl], $23 dec hl ld [hl], $17 pop de ret DrawSpreadMinimap: ; hl for location on screen ; bc for current spread address ; a for index of selected card push af push bc ld a, [bc] ld d, 0 ld e, a ; length of spread in de .drawCards ld a, e cp a, 0 jp z, .doneWithSpread ; if we're drawing zero remaining cards, stop drawing dec e inc bc ; step forward push hl ; need this bc drawsmallcard changes hl push bc ld a, [bc] ld bc, $8384 call DrawSmallCard pop bc pop hl jp .drawCards .doneWithSpread ; stack has hl, bc, af at the time of jumping here pop bc ; stack: af pop af ld d, 0 ld e, a ; index of selected card in spread inc e ; skip the length push hl ; stack: hl ld h, b ld l, c ; put bc in hl temporarily so we can add to hl add hl, de ld a, [hl] pop hl ; stack: empty ld bc, $8586 call DrawSmallCard ret DrawSmallCard: ; starting from screen location hl, draw a card at ;the location described in a, top and bottom sprite in b and c push de push af swap a ld d, 0 and a, %0000_1111 ld e, a add hl, de ; step right ld e, 32 pop af and a, %0000_1111 jp z, .drawCard : dec a add hl, de ; step down cp a, 0 jp nz, :- .drawCard ld [hl], b add hl, de ld [hl], c pop de ret SpreadSelectTilemap: db $09, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $08 db $03, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $04 db $03, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $04 db $06, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $07 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 db $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01 SpreadSelectTilemapEnd: