~nytpu/ankano unlisted

89435165fbef98772e571f9fd89f2cf3f375789c — nytpu 4 months ago 7e30f41
update key handlers, allow dynamic key routines by loading them into WRAM
2 files changed, 86 insertions(+), 61 deletions(-)

M src/keys.asm
M src/loop.asm
M src/keys.asm => src/keys.asm +72 -60
@@ 6,73 6,85 @@ INCLUDE "defines.asm"

SECTION "Keys Handler", ROM0
; handle keypad input
; @destroy hl, b
; @destroy hl, a
; TODO: convert key checks to macro
KeyHandler::
	; use b so a is available for key handlers
	ld a, [hHeldKeys]
	and a ; cp 0
	jr z, .keypadDone ; no input this frame
	ld [wSaveA], a
	and $0F ; only d-pad is pressed
	ld hl, hHeldKeys
	xor a ; ld a, 0
	cp [hl]
	ret z ; no input this frame

	ld a, $0F
	cp [hl]
	; only d-pad is pressed, lets us skip half the bit checks most of the time
	jr z, .dPadOnly

	ld a, [wSaveA]
	and PADF_A
	jr z, .aDone
	; do stuff that should be done for a
	; jump to .aDone if
	bit PADB_A, [hl]
	call nz, wKeyHandlers.a

	bit PADB_B, [hl]
	call nz, wKeyHandlers.b

	bit PADB_SELECT, [hl]
	call nz, wKeyHandlers.select

	bit PADB_START, [hl]
	call nz, wKeyHandlers.start

.dPadOnly
	bit PADB_RIGHT, [hl]
	call nz, wKeyHandlers.right

	bit PADB_LEFT, [hl]
	call nz, wKeyHandlers.left

	; not using up and down for this game
	;bit PADB_UP, [hl]
	;call nz, wKeyHandlers.up

	;bit PADB_DOWN, [hl]
	;call nz, wKeyHandlers.down
	ret


SECTION "Key Handlers ROM", ROMX
gameKeyHandlers::
.a
	ld a, %00011011
	ld [hBGP], a
	ld a, [wSaveA]
.aDone

	ld a, [wSaveA]
	and PADF_B
	jr z, .bDone
	; stuff for b
	ldh [hBGP], a
	ret
.aEnd

.b
	ld a, %11100100
	ld [hBGP], a
.bDone
	ldh [hBGP], a
	ret
.bEnd

	ld a, [wSaveA]
	and PADF_SELECT
	jr z, .selDone
	; stuff for select
.selDone
.select
	ret
.selectEnd

	ld a, [wSaveA]
	and PADF_START
	jr z, .startDone
	; stuff for start
.startDone
.start
	ret
.startEnd

.dPadOnly
	ld a, [wSaveA]
	and PADF_RIGHT
	jr z, .rightDone
	; stuff for right
.rightDone

	ld a, [wSaveA]
	and PADF_LEFT
	jr z, .leftDone
	; stuff for left
.leftDone

	ld a, [wSaveA]
	and PADF_UP
	jr z, .upDone
	; stuff for up
.upDone

	ld a, [wSaveA]
	and PADF_DOWN
	jr z, .downDone
	; stuff for down
.downDone
	; cleanup, if needed
.keypadDone
.right
	ret
.rightEnd

.left
	ret
.leftEnd
.end::

SECTION "Keys Scratch", WRAM0
wSaveA: db
SECTION "Key Handlers", WRAM0
wKeyHandlers::
.a ds gameKeyHandlers.aEnd - gameKeyHandlers.a
.b ds gameKeyHandlers.bEnd - gameKeyHandlers.b
.select ds gameKeyHandlers.selectEnd - gameKeyHandlers.select
.start ds gameKeyHandlers.startEnd - gameKeyHandlers.start
.right ds gameKeyHandlers.rightEnd - gameKeyHandlers.right
.left ds gameKeyHandlers.leftEnd - gameKeyHandlers.left
;.up ds gameKeyHandlers.upEnd - gameKeyHandlers.up
;.down ds gameKeyHandlers.downEnd - gameKeyHandlers.down

M src/loop.asm => src/loop.asm +14 -1
@@ 41,12 41,25 @@ INCLUDE "res/spritemap.2bpp.pb16.size"
	PURGE NB_PB16_BLOCKS
	call pb16_unpack_block

	; load palette
	ld a, %11100100
	ld [hBGP], a

	ld a, LCDCF_ON | LCDCF_BGON | LCDCF_BG9800 | LCDCF_BG8800
	; load key handlers for game
	ld a, BANK(gameKeyHandlers)
	ldh [hCurROMBank], a
	ld [rROMB0], a
	ld de, gameKeyHandlers
	ld hl, wKeyHandlers
	ld c, gameKeyHandlers.end - gameKeyHandlers
	rst MemcpySmall

	; enable lcd, enable background, disable window, use $9800 for background
	; map, use $8800 for bg tileset, enable objects
	ld a, LCDCF_ON | LCDCF_BGON | LCDCF_BG9800 | LCDCF_BG8800 | LCDCF_OBJON
	ldh [rLCDC], a

	; enable interrupts (primarily vblank)
	ei
	; soft reset during gameplay is fine
	ld a, 1