diff --git a/.gitignore b/.gitignore index a00b467..79eacfe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ *.o *.2bpp +*.tilemap is.gb is.gb.sym png/map/*.s png/map/*.inc +dev clips/ diff --git a/Makefile b/Makefile index 3a3ea7f..d8ac8d1 100644 --- a/Makefile +++ b/Makefile @@ -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" diff --git a/inc/game.inc b/inc/game.inc new file mode 100644 index 0000000..74e105e --- /dev/null +++ b/inc/game.inc @@ -0,0 +1,2 @@ +DEF GAME_STATEF_TITLE EQU (1 << 0) +DEF GAME_STATEF_GAME EQU (1 << 1) diff --git a/png/bg/title.aseprite b/png/bg/title.aseprite new file mode 100644 index 0000000..cb144f6 Binary files /dev/null and b/png/bg/title.aseprite differ diff --git a/png/bg/title.png b/png/bg/title.png new file mode 100644 index 0000000..aeec6c7 Binary files /dev/null and b/png/bg/title.png differ diff --git a/src/game.s b/src/game.s new file mode 100644 index 0000000..f589300 --- /dev/null +++ b/src/game.s @@ -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 diff --git a/src/main.s b/src/main.s index 006a401..b790923 100644 --- a/src/main.s +++ b/src/main.s @@ -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 diff --git a/src/title.s b/src/title.s new file mode 100644 index 0000000..aff4067 --- /dev/null +++ b/src/title.s @@ -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 diff --git a/src/util.s b/src/util.s index c1a4882..84fea10 100644 --- a/src/util.s +++ b/src/util.s @@ -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