diff --git a/inc/oam.inc b/inc/oam.inc new file mode 100644 index 0000000..59633e2 --- /dev/null +++ b/inc/oam.inc @@ -0,0 +1,47 @@ +INCLUDE "hardware.inc" + +; The address of an internal OAM entry +; @param \1 16-bit register +; @param \2 OAM index +; @param \3 offset +MACRO ld_OAM + ld \1, _OAM + \2 * sizeof_OAM_ATTRS + \3 +ENDM + +; The address of an internal OAM entry's Y coordinate +MACRO ld_OAM_y + ld_OAM \1, \2, 0 +ENDM + +; The address of an internal OAM entry's X coordinate +MACRO ld_OAM_x + ld_OAM \1, \2, 1 +ENDM + +; The address of an internal OAM entry's tile index +MACRO ld_OAM_tile + ld_OAM \1, \2, 2 +ENDM + +; The address of an internal OAM entry's attributes +MACRO ld_OAM_attr + ld_OAM \1, \2, 3 +ENDM + +; Set an entry in the internal OAM (copied on next DMA) +; @param \1 OAM index +; @param \2 X coordinate +; @param \3 Y coordinate +; @param \4 Tile index +; @param \5 Attributes +MACRO OAM_set + ld_OAM_y hl, \1 + ld a, \3 + ld [hli], a + ld a, \2 + ld [hli], a + ld a, \4 + ld [hli], a + ld a, \5 + ld [hl], a +ENDM diff --git a/src/oam.s b/src/oam.s index 74da1b8..34f8d39 100644 --- a/src/oam.s +++ b/src/oam.s @@ -2,7 +2,7 @@ INCLUDE "hardware.inc" SECTION "OAM Mirror", WRAM0, ALIGN[8] -_OAM:: ds 40 * 4 +_OAM:: ds OAM_COUNT * sizeof_OAM_ATTRS _OAM_end: SECTION "OAM DMA routine", ROM0 diff --git a/src/player.s b/src/player.s index a57eb7f..b8d3578 100644 --- a/src/player.s +++ b/src/player.s @@ -1,5 +1,6 @@ INCLUDE "hardware.inc" INCLUDE "input.inc" +INCLUDE "oam.inc" Section "Player Data", WRAM0 @@ -11,7 +12,8 @@ Section "Player Code", ROM0 spriteData: INCBIN "png/sprite/player.2bpp" -DEF SPRITE_IDX EQU 32 +DEF SPRITE_OAM_IDX EQU 0 +DEF SPRITE_IDX EQU 32 ; tile index, rename later DEF SPRITE_WIDTH EQU 2 DEF SPRITE_HEIGHT EQU 2 @@ -25,46 +27,10 @@ Player_Init:: ld d, 16 * (SPRITE_WIDTH * SPRITE_HEIGHT) call memcpy - ; top left - ld hl, _OAM - ld a, 144-8-8 - ld [hli], a - ld a, 8 - ld [hli], a - ld a, SPRITE_IDX - ld [hli], a - xor a - ld [hli], a - - ; top right - ld a, 144-8-8 - ld [hli], a - ld a, 8 + 8 - ld [hli], a - ld a, SPRITE_IDX + 1 - ld [hli], a - xor a - ld [hli], a - - ; bottom left - ld a, 144-8 - ld [hli], a - ld a, 8 - ld [hli], a - ld a, SPRITE_IDX + 2 - ld [hli], a - xor a - ld [hli], a - - ; bottom right - ld a, 144-8 - ld [hli], a - ld a, 8 + 8 - ld [hli], a - ld a, SPRITE_IDX + 3 - ld [hli], a - xor a - ld [hli], a + OAM_set SPRITE_OAM_IDX + 0, 8, 144-8-8, SPRITE_IDX, 0 + OAM_set SPRITE_OAM_IDX + 1, 16, 144-8-8, SPRITE_IDX + 1, 0 + OAM_set SPRITE_OAM_IDX + 2, 8, 144-8, SPRITE_IDX + 2, 0 + OAM_set SPRITE_OAM_IDX + 3, 16, 144-8, SPRITE_IDX + 3, 0 ld bc, _OAMRAM ld hl, _OAM @@ -79,7 +45,7 @@ Player_Update:: ld a, [hl] and %1 cp %1 - jr nz, .done + ret nz ld hl, keys ld b, [hl] @@ -90,17 +56,40 @@ Player_Update:: jr z, .left ; check for right boundary - ld hl, _OAM + 1 + ld_OAM_x hl, SPRITE_OAM_IDX + 1 ld a, [hl] - cp 160 - 8 - jr nc, .done + cp SCRN_X + jr nc, .left + + ; TODO: check collision + call move_right + +.left: + ; check for left button + ld a, b + and BTN_LEFT + ret z + + ; check for left boundary + ld_OAM_x hl, SPRITE_OAM_IDX + ld a, [hl] + cp 9 + ret c + + ; TODO: check collision + call move_left + + ret + +move_right: + ld_OAM_x hl, SPRITE_OAM_IDX ; update top left sprite inc [hl] inc hl ld [hl], SPRITE_IDX inc hl - res 5, [hl] + res OAMB_XFLIP, [hl] ; update top right sprite inc hl @@ -109,7 +98,7 @@ Player_Update:: inc hl ld [hl], SPRITE_IDX + 1 inc hl - res 5, [hl] + res OAMB_XFLIP, [hl] ; update bottom left sprite inc hl @@ -118,7 +107,7 @@ Player_Update:: inc hl ld [hl], SPRITE_IDX + 2 inc hl - res 5, [hl] + res OAMB_XFLIP, [hl] ; update bottom right sprite inc hl @@ -127,53 +116,45 @@ Player_Update:: inc hl ld [hl], SPRITE_IDX + 3 inc hl - res 5, [hl] + res OAMB_XFLIP, [hl] -.left: - ; check for left button - ld a, b - and BTN_LEFT - jr z, .done + ret - ; check for left boundary - ld hl, _OAM + 1 - ld a, [hl] - cp 9 - jr c, .done +move_left: + ld_OAM_x hl, SPRITE_OAM_IDX - ; update top left sprite + ; top left dec [hl] inc hl ld [hl], SPRITE_IDX + 1 inc hl - set 5, [hl] + set OAMB_XFLIP, [hl] - ; update top right sprite + ; top right inc hl inc hl dec [hl] inc hl ld [hl], SPRITE_IDX inc hl - set 5, [hl] + set OAMB_XFLIP, [hl] - ; update bottom left sprite + ; bottom left inc hl inc hl dec [hl] inc hl ld [hl], SPRITE_IDX + 3 inc hl - set 5, [hl] + set OAMB_XFLIP, [hl] - ; update bottom right sprite + ; bottom right inc hl inc hl dec [hl] inc hl ld [hl], SPRITE_IDX + 2 inc hl - set 5, [hl] + set OAMB_XFLIP, [hl] -.done: ret