dmgtris/src/main.asm

116 lines
2.5 KiB
NASM
Raw Normal View History

2023-10-16 05:47:11 +00:00
IF !DEF(MAIN_ASM)
DEF MAIN_ASM EQU 1
2023-10-10 08:32:24 +00:00
INCLUDE "globals.asm"
INCLUDE "res/tiles.inc"
INCLUDE "res/gameplay_map.inc"
2023-10-16 05:47:11 +00:00
SECTION "Globals", WRAM0
wStateEventHandler:: ds 2
wStateVBlankHandler:: ds 2
2023-10-10 08:32:24 +00:00
SECTION "Code Entry Point", ROM0
2023-10-13 06:38:10 +00:00
Main::
2023-10-10 08:32:24 +00:00
; Turn off LCD during initialization.
2023-10-13 06:38:10 +00:00
wait_vram
xor a, a
ldh [rLCDC], a
2023-10-11 06:18:12 +00:00
2023-10-10 08:32:24 +00:00
; We use a single set of tiles for the entire game, so we copy it at the start.
ld de, Tiles
2023-10-11 06:18:12 +00:00
ld hl, _VRAM
2023-10-10 08:32:24 +00:00
ld bc, TilesEnd - Tiles
call UnsafeMemCopy
2023-10-11 06:18:12 +00:00
; Also to the second bank of tile data.
ld de, Tiles
ld hl, _VRAM + $800
ld bc, TilesEnd - Tiles
call UnsafeMemCopy
; Make sure both sprites and bg use the same tile data.
2023-10-13 06:38:10 +00:00
ldh a, [rLCDC]
or LCDCF_BLK01
ldh [rLCDC], a
2023-10-11 06:18:12 +00:00
; Clear OAM.
call ClearOAM
call CopyOAMHandler
; Set up the palettes.
2023-10-10 08:32:24 +00:00
ld a, PALETTE_REGULAR
2023-10-13 09:20:28 +00:00
set_bg_palette
set_obj0_palette
ld a, PALETTE_LIGHTER_1
set_obj1_palette
2023-10-10 08:32:24 +00:00
2023-10-13 06:38:10 +00:00
; Get the timer going. It's used for RNG.
xor a, a
ldh [rTMA], a
ld a, TACF_262KHZ | TACF_START
2023-10-11 06:18:12 +00:00
; Zero out the ram where needed.
2023-10-10 08:32:24 +00:00
call InitializeVariables
2023-10-11 06:18:12 +00:00
; Set up the interrupt handlers.
2023-10-10 08:32:24 +00:00
call InitializeLCDCInterrupt
2023-10-16 05:47:11 +00:00
; Switch to gameplay state.
call SwitchToGameplay
2023-10-13 06:38:10 +00:00
2023-10-10 08:32:24 +00:00
2023-10-16 05:47:11 +00:00
EventLoop::
2023-10-16 11:27:08 +00:00
; Play the sound effect, if any.
call SFXPlay
2023-10-16 05:47:11 +00:00
; Wrangle inputs and timers at the start of every frame.
2023-10-10 08:32:24 +00:00
call GetInput
call HandleTimers
2023-10-16 05:47:11 +00:00
; Call the current state's event handler.
ld a, [wStateEventHandler]
ld l, a
ld a, [wStateEventHandler + 1]
ld h, a
jp hl
EventLoopPostHandler::
2023-10-10 08:32:24 +00:00
2023-10-16 05:47:11 +00:00
; Wait for vblank and update OAM.
2023-10-10 08:32:24 +00:00
wait_vblank
2023-10-11 06:18:12 +00:00
call hOAMDMA
2023-10-16 05:47:11 +00:00
; Call the current state's vblank handler.
ld a, [wStateVBlankHandler]
ld l, a
ld a, [wStateVBlankHandler + 1]
ld h, a
jp hl
EventLoopPostVBlankHandler::
; Jump back to the start of the event loop.
jr EventLoop
2023-10-10 08:32:24 +00:00
; *****************************************************************************
; * *
; * Functions *
; * *
; *****************************************************************************
SECTION "Functions", ROM0
InitializeVariables:
2023-10-13 09:20:28 +00:00
call TimeInit
call IntrInit
call InputInit
call ScoreInit
call LevelInit
call FieldInit
2023-10-16 11:27:08 +00:00
call SFXInit
2023-10-10 08:32:24 +00:00
ret
2023-10-16 05:47:11 +00:00
ENDC