2 Commits

Author SHA1 Message Date
  Forest Belton 71906064da Begin work on title screen 2 years ago
  Forest Belton fee20378ee Fix jumping and collisions on map boundary 2 years ago
11 changed files with 153 additions and 59 deletions
Split View
  1. +2
    -0
      .gitignore
  2. +8
    -2
      Makefile
  3. +2
    -0
      inc/game.inc
  4. BIN
      png/bg/title.aseprite
  5. BIN
      png/bg/title.png
  6. +20
    -15
      src/collision.s
  7. +25
    -0
      src/game.s
  8. +16
    -24
      src/main.s
  9. +18
    -18
      src/player.s
  10. +44
    -0
      src/title.s
  11. +18
    -0
      src/util.s

+ 2
- 0
.gitignore View File

@ -1,6 +1,8 @@
*.o
*.2bpp
*.tilemap
is.gb
is.gb.sym
png/map/*.s
png/map/*.inc
dev clips/

+ 8
- 2
Makefile View File

@ -2,7 +2,7 @@ PYTHON ?= python
# Graphics
SPRITE_PNG := $(shell find png/sprite -type f -name '*.png')
BG_PNG := $(shell find png/bg -type f -name '*.png')
MAP_PNG := $(shell find png/map -type f -name '*.png' -not -name '*_coll.png')
MAP_COLL := $(shell find png/map -type f -name '*_coll.png')
@ -10,6 +10,7 @@ MAP_COLL := $(shell find png/map -type f -name '*_coll.png')
MAP_S := $(MAP_PNG:%.png=%.s)
MAP_INC := $(MAP_PNG:%.png=%.inc)
SPRITE_2BPP := $(SPRITE_PNG:%.png=%.2bpp)
BG_DATA := $(BG_PNG:%.png=%.2bpp) $(BG_PNG:%.png=%.tilemap)
# Code
SFILES := $(shell find src -type f -name '*.s')
@ -24,6 +25,7 @@ is.gb is.gb.sym: $(OFILES)
$(OFILES): $(MAP_INC)
$(OFILES): $(SPRITE_2BPP)
$(OFILES): $(BG_DATA)
%.o: %.s
@echo "[ASSEMBLE] $<"
@ -37,6 +39,10 @@ png/sprite/%.2bpp: png/sprite/%.png
@echo "[SPRITE] $<"
@rgbgfx -o $@ $<
png/bg/%.2bpp png/bg/%.tilemap: png/bg/%.png
@echo "[BG] $<"
@rgbgfx -T -u -o "$(shell dirname $<)/$(shell basename --suffix=.png $<).2bpp" $<
clean:
@rm -f is.gb is.gb.sym $(OFILES) $(MAP_S) $(MAP_INC) $(SPRITE_2BPP)
@rm -f is.gb is.gb.sym $(OFILES) $(MAP_S) $(MAP_INC) $(SPRITE_2BPP) $(BG_DATA)
@echo "All build files removed"

+ 2
- 0
inc/game.inc View File

@ -0,0 +1,2 @@
DEF GAME_STATEF_TITLE EQU (1 << 0)
DEF GAME_STATEF_GAME EQU (1 << 1)

BIN
png/bg/title.aseprite View File


BIN
png/bg/title.png View File

Before After
Width: 160  |  Height: 144  |  Size: 2.2 KiB

+ 20
- 15
src/collision.s View File

@ -2,8 +2,12 @@ INCLUDE "oam.inc"
SECTION "Collision", ROM0
DEF COLLF_WALK EQU (1 << 0)
DEF COLLF_LADDER EQU (1 << 1)
DEF COLLF_PORTAL EQU (1 << 2)
; Determines if player has hit the background
; @return NZ = true, Z = false
; @return NZ = false, Z = true
player_bg_collides::
; C = MAP X = PLAYER X / 8 + CAMERA X
ld a, [PLAYER_X]
@ -15,16 +19,16 @@ player_bg_collides::
ld c, a
; C < 0
bit 7, c
ret nz
ld a, c
cpl
bit 7, a
ret z
; C >= MAP WIDTH
ld a, [CURRENT_MAP_WIDTH]
cp c
jr nz, .load_y
; Set NZ
or $ff
ret
.load_y:
@ -38,8 +42,10 @@ player_bg_collides::
ld b, a
; B < 0
bit 7, b
ret nz
ld a, b
cpl
bit 7, a
ret z
; B >= MAP HEIGHT
ld a, [CURRENT_MAP_HEIGHT]
@ -53,18 +59,18 @@ player_bg_collides::
.check_tiles:
; top left
call can_move_to
ret nz
ret z
; top right
inc c
call can_move_to
ret nz
ret z
; bottom left
dec c
inc b
call can_move_to
ret nz
ret z
; bottom right
inc c
@ -73,7 +79,7 @@ player_bg_collides::
ret
; Check if the player is in mid-air
; @note NZ = false, Z = true
; @note NZ = true, Z = false
player_in_air::
; c = x % 8 == 0 ? x/8 : x/8 + 1
ld a, [PLAYER_X]
@ -97,7 +103,7 @@ player_in_air::
inc b
call can_move_to
ret nz
ret z
inc c
call can_move_to
@ -108,7 +114,7 @@ player_in_air::
; @param b y-coordinate
; @param c x-coordinate
; @destroy a, d, e
; @note NZ = false, Z = true
; @note NZ = true, Z = false
can_move_to:
push bc
push hl
@ -129,9 +135,8 @@ can_move_to:
jr nz, .mul_y_width
add hl, bc
; check [hl] = 1
ld a, [hl]
cp 1
and COLLF_WALK
pop hl
pop bc

+ 25
- 0
src/game.s View File

@ -0,0 +1,25 @@
SECTION "Game Loop", ROM0
Game_Start::
ld hl, intro_Data
call Map_Load
call Player_Init
.loop:
ld hl, frame
inc [hl]
call Keys_Update
call Player_Update
; wait for vblank
halt
call Map_Update
; ~160 cycles
ld a, HIGH(_OAM)
call DMA_Start
jr .loop

+ 16
- 24
src/main.s View File

@ -1,3 +1,4 @@
INCLUDE "game.inc"
INCLUDE "hardware.inc"
INCLUDE "util.inc"
@ -11,9 +12,10 @@ rept $150 - _entry
db 0
endr
SECTION "Frame", WRAM0
SECTION "Game State", WRAM0
frame:: db
state:: db
SECTION "VBlank", ROM0[$40]
@ -23,19 +25,17 @@ handle_vblank:
SECTION "Code", ROM0
start:
; init/disable lcd
ld hl, rLCDC
res 7, [hl]
res 2, [hl]
; enable vblank
ld hl, rIE
ld [hl], IEF_VBLANK
ld hl, intro_Data
call Map_Load
call OAM_Init
call Keys_Init
call Player_Init
; set palette
ld a, %11100100
@ -44,26 +44,18 @@ start:
ld hl, rBGP
ld [hl], a
; enable lcd, sprites and interrupts
ld hl, rLCDC
set 7, [hl]
set 1, [hl]
ei
ld a, GAME_STATEF_TITLE
ld [state], a
.loop:
ld hl, frame
inc [hl]
call Keys_Update
call Player_Update
; wait for vblank
halt
call Map_Update
ld a, [state]
cp GAME_STATEF_TITLE
jr nz, .check_game_state
; ~160 cycles
ld a, HIGH(_OAM)
call DMA_Start
call Title_Start
jr .loop
jp .loop
; only two states atm, no need to check anything yet
.check_game_state:
call Game_Start
jr .loop

+ 18
- 18
src/player.s View File

@ -13,7 +13,7 @@ PLAYER_DIR:: db
PLAYER_STATE:: db
PLAYER_VY:: dw
PLAYER_INV:: DS (2 * PLAYER_INV_SIZE)
PLAYER_INV:: ds (2 * PLAYER_INV_SIZE)
Section "Player Code", ROM0
@ -62,7 +62,7 @@ Player_Update::
; initialize jump state if not already jumping
ld a, [PLAYER_STATE]
cp PLAYER_STATEF_JUMP
and PLAYER_STATEF_JUMP
jr nz, .jump_update
ld a, PLAYER_STATEF_JUMP
@ -74,7 +74,7 @@ Player_Update::
; todo: deduplicate
.jump_update_check:
ld a, [PLAYER_STATE]
cp PLAYER_STATEF_JUMP
and PLAYER_STATEF_JUMP
jr z, .right
.jump_update:
@ -92,17 +92,17 @@ Player_Update::
sbc HIGH(GRAVITY)
ld b, a
; we only need to clamp when moving down
bit 7, b
jr z, .no_clamp
; ; we only need to clamp when moving down
; bit 7, b
; jr z, .no_clamp
; vy = MIN(MAX_VY, vy)
ld a, b
cp HIGH(MAX_VY)
jr c, .no_clamp
; ; vy = MIN(MAX_VY, vy)
; ld a, b
; cp HIGH(MAX_VY)
; jr c, .no_clamp
ld b, HIGH(MAX_VY)
ld c, LOW(MAX_VY)
; ld b, HIGH(MAX_VY)
; ld c, LOW(MAX_VY)
.no_clamp:
ld hl, PLAYER_VY
@ -119,7 +119,7 @@ Player_Update::
; roll back jump if there was a collision
call player_bg_collides
jr nz, .jump_rollback
jr nz, .jump_Player_UpdateOAM
; TODO: Enable once scroll zones are implemented
; ld a, [PLAYER_VY]
@ -139,7 +139,7 @@ Player_Update::
.jump_rollback_check:
call player_bg_collides
jr nz, .jump_rollback
jr z, .jump_rollback
; set PLAYER_STATE = PLAYER_STATEF_WALK if VY < 0 (fell into ground)
ld hl, PLAYER_VY
@ -177,7 +177,7 @@ Player_Update::
ld [PLAYER_X], a
call player_bg_collides
jr nz, .right_rollback
jr z, .right_rollback
; TODO: Enable once scroll zones are implemented
; ld d, PLAYER_SPEED
@ -211,7 +211,7 @@ Player_Update::
ld [PLAYER_X], a
call player_bg_collides
jr nz, .left_rollback
jr z, .left_rollback
; TODO: Enable once scroll zones are implemented
; ld d, PLAYER_SPEED
@ -230,12 +230,12 @@ Player_Update::
.fall:
call player_in_air
ret nz
ret z
; only set jump state if not already jumping
ld hl, PLAYER_STATE
ld a, [hl]
or PLAYER_STATEF_JUMP
and PLAYER_STATEF_JUMP
ret nz
ld [hl], PLAYER_STATEF_JUMP

+ 44
- 0
src/title.s View File

@ -0,0 +1,44 @@
INCLUDE "hardware.inc"
SECTION "Title Loop", ROM0
titleTiles: INCBIN "png/bg/title.2bpp"
titleMap: INCBIN "png/bg/title.tilemap"
titleMapEnd:
DEF TITLE_TILES_SIZE EQUS "titleMap - titleTiles"
DEF TITLE_MAP_SIZE EQUS "titleMapEnd - titleMap"
Title_Start::
; load title screen
ld bc, _VRAM
ld hl, titleTiles
ld de, TITLE_TILES_SIZE
call memcpy16
; could probably be sped up...
FOR Y, 18
ld bc, _SCRN0 + 32 * Y
ld hl, titleMap + 20 * Y
ld d, 20
call memcpy
ENDR
; enable lcd, sprites and interrupts
ld hl, rLCDC
set 7, [hl]
set 1, [hl]
ei
.loop:
ld hl, frame
inc [hl]
call Keys_Update
; todo: check for start press and transition to game state
; wait for vblank
halt
jr .loop

+ 18
- 0
src/util.s View File

@ -21,6 +21,24 @@ memcpy::
jr nz, memcpy ; 12/8
ret ; 16
; Copy data between two regions (takes TODO cycles). Only use when size > 255
; @param bc Pointer to the destination region
; @param hl Pointer to the source region
; @param de Size (in bytes) to copy. Must be >0
; @destroy a, b, c, d, e, h, l
memcpy16::
ld a, [hli] ; 8
ld [bc], a ; 8
inc bc ; 8
dec de ; 8
xor a ; 4
or d ; 4
or e ; 4
jr nz, memcpy16 ; 12/8
ret ; 16
; Fill a memory region with a value
; @param hl Pointer to the destination region
; @param a Byte to fill with

Loading…
Cancel
Save