Skip to content

Commit

Permalink
consistency and optimization improvement (#87)
Browse files Browse the repository at this point in the history
* Update GalacticArmada.asm - optimizations and consistency

* Update gameplay-background.asm

* Update gameplay-background.asm

* Update gameplay-state.asm

* Update hud.asm

* Update hud.asm

* Update interrupts.asm

* Update player.asm

* Update enemies.asm

* Update bullets.asm

* Update enemy-player-collision.asm

* Update enemy-bullet-collision.asm

* Update entry-point.md

* Update story-state.asm

* Update story-state.asm

* Update title-screen-state.asm

* Update vblank-utils.asm

* Update background-utils.asm

* Update collision-utils.asm

* Update input-utils.asm

* Update math.asm

* Update memory-utils.asm

* Update metasprites.asm

* Update sprites-utils.asm

* Update text-utils.asm
  • Loading branch information
starleaf-luna committed Apr 17, 2024
1 parent 34b165c commit db9eb64
Show file tree
Hide file tree
Showing 22 changed files with 263 additions and 285 deletions.
33 changes: 15 additions & 18 deletions galactic-armada/src/main/GalacticArmada.asm
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ EntryPoint:
; ANCHOR: entry-point-end
; Shut down audio circuitry
ld a, 0
xor a
ld [rNR52], a

ld a, 0
; We don't actually need another xor a here, because the value of A doesn't change between these two instructions
ld [wGameState], a

; Wait for the vertical blank phase before initiating the library
Expand All @@ -34,7 +33,7 @@ EntryPoint:
call InitSprObjLibWrapper

; Turn the LCD off
ld a, 0
xor a
ld [rLCDC], a

; Load our common text font into VRAM
Expand All @@ -47,7 +46,6 @@ EntryPoint:
; During the first (blank) frame, initialize display registers
ld a, %11100100
ld [rBGP], a
ld a, %11100100
ld [rOBP0], a

; ANCHOR_END: entry-point-end
Expand All @@ -58,18 +56,17 @@ NextGameState::
; Do not turn the LCD off outside of VBlank
call WaitForOneVBlank

call ClearBackground;
call ClearBackground


; Turn the LCD off
ld a, 0
xor a
ld [rLCDC], a

ld a, 0
ld [rSCX],a
ld [rSCY],a
ld [rWX],a
ld [rWY],a
ld [rSCX], a
ld [rSCY], a
ld [rWX], a
ld [rWY], a
; disable interrupts
call DisableInterrupts
Expand All @@ -78,21 +75,21 @@ NextGameState::

; Initiate the next state
ld a, [wGameState]
cp a, 2 ; 2 = Gameplay
cp 2 ; 2 = Gameplay
call z, InitGameplayState
ld a, [wGameState]
cp a, 1 ; 1 = Story
cp 1 ; 1 = Story
call z, InitStoryState
ld a, [wGameState]
cp a, 0 ; 0 = Menu
and a ; 0 = Menu
call z, InitTitleScreenState

; Update the next state
ld a, [wGameState]
cp a, 2 ; 2 = Gameplay
cp 2 ; 2 = Gameplay
jp z, UpdateGameplayState
cp a, 1 ; 1 = Story
cp 1 ; 1 = Story
jp z, UpdateStoryState
jp UpdateTitleScreenState

; ANCHOR_END: next-game-state
; ANCHOR_END: next-game-state
27 changes: 12 additions & 15 deletions galactic-armada/src/main/states/gameplay/gameplay-background.asm
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ InitializeBackground::
ld bc, starFieldMapEnd - starFieldMap
call CopyDEintoMemoryAtHL_With52Offset

ld a, 0
ld [mBackgroundScroll+0],a
ld a, 0
ld [mBackgroundScroll+1],a

xor a
ld [mBackgroundScroll], a
ld [mBackgroundScroll+1], a
ret
; ANCHOR_END: gameplay-background-initialize

Expand All @@ -42,13 +40,13 @@ UpdateBackground::

; Increase our scaled integer by 5
; Get our true (non-scaled) value, and save it for later usage in bc
ld a , [mBackgroundScroll+0]
add a , 5
ld b,a
ld [mBackgroundScroll+0], a
ld a , [mBackgroundScroll+1]
adc a , 0
ld c,a
ld a, [mBackgroundScroll]
add a, 5
ld b, a
ld [mBackgroundScroll], a
ld a, [mBackgroundScroll+1]
adc 0
ld c, a
ld [mBackgroundScroll+1], a
; ANCHOR_END: gameplay-background-update-start

Expand All @@ -65,8 +63,7 @@ UpdateBackground::
rr b

; Use the de-scaled low byte as the backgrounds position
ld a,b
ld a, b
ld [rSCY], a

ret
; ANCHOR_END: gameplay-background-update-end
; ANCHOR_END: gameplay-background-update-end
14 changes: 7 additions & 7 deletions galactic-armada/src/main/states/gameplay/gameplay-state.asm
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ wLivesText:: db "lives", 255
InitGameplayState::

ld a, 3
ld [wLives+0], a
ld [wLives], a

ld a, 0
ld [wScore+0], a
xor a
ld [wScore], a
ld [wScore+1], a
ld [wScore+2], a
ld [wScore+3], a
Expand All @@ -44,7 +44,7 @@ InitGameplayState::
call DrawTextTilesLoop

; Call Our function that draws text onto background/window tiles
ld de, $9c0D
ld de, $9c0d
ld hl, wLivesText
call DrawTextTilesLoop
Expand All @@ -64,7 +64,7 @@ InitGameplayState::
ld a, LCDCF_ON | LCDCF_BGON|LCDCF_OBJON | LCDCF_OBJ16 | LCDCF_WINON | LCDCF_WIN9C00|LCDCF_BG9800
ld [rLCDC], a

ret;
ret
; ANCHOR_END: init-gameplay-state
; ANCHOR: update-gameplay-state-start
Expand Down Expand Up @@ -102,7 +102,7 @@ UpdateGameplayState::

; ANCHOR: update-gameplay-end-update
ld a, [wLives]
cp a, 250
cp 250
jp nc, EndGameplay

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand All @@ -127,4 +127,4 @@ EndGameplay:
ld a, 0
ld [wGameState],a
jp NextGameState
; ANCHOR_END: update-gameplay-end-update
; ANCHOR_END: update-gameplay-end-update
19 changes: 8 additions & 11 deletions galactic-armada/src/main/states/gameplay/hud.asm
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ IncreaseScore_Loop:
ld [hl], a

; Stop if it hasn't gone past 0
cp a, 9
cp 9
ret c

; If it HAS gone past 9
IncreaseScore_Next:

; Increase a counter so we can not go out of our scores bounds
inc c
ld a, c
inc a
ld c, a

; Check if we've gone our o our scores bounds
cp a, 6
; Check if we've gone over our scores bounds
cp 6
ret z

; Reset the current digit to zero
Expand All @@ -50,7 +49,7 @@ DrawLives::
ld de, $9C13 ; The window tilemap starts at $9C00

ld a, [hl]
add a, 10 ; our numeric tiles start at tile 10, so add to 10 to each bytes value
add 10 ; our numeric tiles start at tile 10, so add 10 to each bytes value
ld [de], a

ret
Expand All @@ -68,13 +67,11 @@ DrawScore::
DrawScore_Loop:

ld a, [hli]
add a, 10 ; our numeric tiles start at tile 10, so add to 10 to each bytes value
add 10 ; our numeric tiles start at tile 10, so add to 10 to each bytes value
ld [de], a

; Decrease how many numbers we have drawn
ld a, c
dec a
ld c, a
dec c
; Stop when we've drawn all the numbers
ret z
Expand All @@ -83,4 +80,4 @@ DrawScore_Loop:
inc de

jp DrawScore_Loop
; ANCHOR_END: hud-draw-score
; ANCHOR_END: hud-draw-score
12 changes: 6 additions & 6 deletions galactic-armada/src/main/states/gameplay/interrupts.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ INCLUDE "src/main/utils/hardware.inc"
SECTION "Interrupts", ROM0

DisableInterrupts::
ld a, 0
xor a
ldh [rSTAT], a
di
ret
Expand All @@ -14,7 +14,7 @@ InitStatInterrupts::

ld a, IEF_STAT
ldh [rIE], a
xor a, a ; This is equivalent to `ld a, 0`!
xor a
ldh [rIF], a
ei

Expand All @@ -24,7 +24,7 @@ InitStatInterrupts::

; We'll start with the first scanline
; The first stat interrupt will call the next time rLY = 0
ld a, 0
xor a
ldh [rLYC], a

ret
Expand All @@ -39,13 +39,13 @@ StatInterrupt:

; Check if we are on the first scanline
ldh a, [rLYC]
cp 0
and a
jp z, LYCEqualsZero

LYCEquals8:

; Don't call the next stat interrupt until scanline 8
ld a, 0
xor a
ldh [rLYC], a

; Turn the LCD on including sprites. But no window
Expand All @@ -70,4 +70,4 @@ EndStatInterrupts:
pop af

reti;
; ANCHOR_END: interrupts-section
; ANCHOR_END: interrupts-section
Loading

6 comments on commit db9eb64

@ZheTheBear
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some change made here broke the rom, game doesn't go through start screen

@ISSOtm
Copy link
Member

@ISSOtm ISSOtm commented on db9eb64 Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @win-ini

@starleaf-luna
Copy link
Contributor Author

@starleaf-luna starleaf-luna commented on db9eb64 Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some change made here broke the rom, game doesn't go through start screen

that must have been an error I've made then, let's compare the changes from this commit and the repo's state from the previous one... but I can't find any change from here that would change how the game would load the states, maybe my change of cp a, 0 to and a which seems to affect the carry flag made an impact here?

until this issue is resolved, is there a way to revert a commit? I have the copy of this commit on my GitHub profile, where we can resolve the issue and then re-push the commit.

@ZheTheBear
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some change made here broke the rom, game doesn't go through start screen

that must have been an error I've made then, let's compare the changes from this commit and the repo's state from the previous one... but I can't find any change from here that would change how the game would load the states, maybe my change of cp a, 0 to and a which seems to affect the carry flag made an impact here?

until this issue is resolved, is there a way to revert a commit? I have the copy of this commit on my GitHub profile, where we can resolve the issue and then re-push the commit.

Hello @win-ini!

Thank you for answering so soon. I've tried with multiple versions of make (Which I think doesn't make any difference, but had to try) since I thought that you had tried to compile the rom and ran it without issue.

I can confirm that i've successfully built this with make from 3.75 to 4.4 all versions included and none of them worked so that doesn't seem to be the issue. My RGBDS version is the latest you can download from their webpage: 0.7.0 on all tools.

I ran the ROM on both emulicious and bgb and both would launch the rom correctly but no response to "A" button which should trigger the "history" text. When debugged the game seemed to get stuck on a loop (Should be any kind of condition check?)

Yes, you can revert commits. You should first create a branch from where it is now and push it, then, you have to use "git revert" on main branch, and a commit will be made for "undoing" the changes (which I think it's better than reset --hard in this situation). The last commit that ran correctly was "7ea2ab3642ca0d2253495bd32878b9b0ce6b7c42".

I'll be waiting for news :)

@starleaf-luna
Copy link
Contributor Author

@starleaf-luna starleaf-luna commented on db9eb64 Jun 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that you had tried to compile the rom and ran it without issue.

well, my thought process was if the changes are so inconsequential it had to have run still, even with my changes, flawlessly, right? well, lesson learned :P

When debugged the game seemed to get stuck on a loop (Should be any kind of condition check?)

when building a fresh ROM and opening the debugger, I see this code block:

ld a, [$FF44]
cp $90
jp c, $1C23

this seems to be part of the VBlank waiting function: (specifically not the exact duplicate of the code found a little later in the file, because of the surroundings of this code)

WaitForVBlankFunction_Loop::
	ld a, [rLY] ; Copy the vertical line to a
	cp 144 ; Check if the vertical line (in a) is 0
	jp c, WaitForVBlankFunction_Loop ; A conditional jump. The condition is that 'c' is set, the last operation overflowed

so, after setting a breakpoint on $0A2E (the address of UpdateTitleScreenState), it seems that the function is only called at boot, never again.
it seems it gets stuck on WaitForKeyFunction, as when stepping over it in the debugger it never goes forward.
...oh

and a, b
jp WaitForKeyFunction_NotPressed

I have somehow managed to delete the z, from the jump. how did it even occur? and yes, readding it and building the ROM does fix the problem.
obraz
I'm just mad at myself at not only somehow removing the conditional from the jump, breaking the game, and not even testing it, but also for skimming past it, somehow!
so, I was the issue again, and I will never make commits without testing ever again. I'm sorry for the havoc I've caused.

edit: I've now opened a pull request fixing the issue

edit 2: I think I now understand the mistake I've made.
the original code looked like this:

and a, b
jp z, WaitForKeyFunction_NotPressed

in my hassle to delete the a, part from the and (like I've done for the other instructions as well), I must've gotten confused and accidentally deleted the z, from the jp. I should've tested nonetheless, however, the fault is all on me.

@ZheTheBear
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that you had tried to compile the rom and ran it without issue.

well, my thought process was if the changes are so inconsequential it had to have run still, even with my changes, flawlessly, right? well, lesson learned :P...

Such a wonderful explanation 😄, don't worry I don't think nobody had the time to notice (At least it's not some kind of military software lol). That's the reason why this is an open source project, so everyone can share their knowledge. Your optimizations are very good and demonstrate that you have a good understanding of what's going on. The good thing is we all learned something from this.

Thank you very much for contributing to this project and making it smoother!

Please sign in to comment.