Refactored text renderer

master
Jonatan Nilsson 2019-06-21 10:30:43 +00:00
parent b7ee801fa5
commit d98ff62872
5 changed files with 40 additions and 65 deletions

View File

@ -82,9 +82,10 @@ ClearWindow:
; @return hl - Workram position for text pointer
; @return de - Memory address to source string
StartText:
ld a, 10
ld [textwriter_curdelay], a
ld a, 0
ld [textwriter_curletter], a
ld [textwriter_curdelay], a
ld [textwriter_posx], a
ld [textwriter_posy], a
ld a, 1
@ -102,17 +103,16 @@ StartText:
DrawActiveText:
ld a, [textwriter_active] ; Check if text is currently active
and a
jp z, .exit
ret z
; Delay writing a letter by 10 frames
ld a, [textwriter_curdelay]
inc a
dec a
ld [textwriter_curdelay], a
cp 10
jr c, .exit
ret nz
; Reset the curdelay
ld a, 0
ld a, 10
ld [textwriter_curdelay], a
.startdrawtext
@ -124,34 +124,23 @@ DrawActiveText:
ld e, [hl] ; Store the next byte address to e
; We now have the current letter being drawn at [de]
ld hl, _SCRN1 ; Load the top-left map position to our map
ld a, [textwriter_posy]
ld c, a
; Get the current X position for the text on screen
ld hl, (_SCRN1 + $21) ; Load the top-left window position for our text
; Load the x position for current text position and
ld a, [textwriter_posx]
ld b, a
.moveyaxis
; Our first objective is to position the text to correct position
; We do so shifting the position by $20 for each y axis
; and position by $01 for each x axis that needs movement
ld a, c ; Load the y position to our accumilator
ld a, l
add b ; Add the x position
ld l, a
; Get the current Y position for the text on screen
ld a, [textwriter_posy]
and a ; Check if it's zero
jr z, .movexaxis ; Jump to x axis if it's zero and no moving necessary
ld a, l ; Load the first half of the destination position
add a, $20 ; Add $20 (one vertical line)
ld l, a ; Store the position back
ld a, h ; Grab the second part of the address
adc a, $00 ; Add only the accumilator
ld h, a ; Store the position back
dec c ; Decrement our y parameter
jr .moveyaxis ; Repeat
.movexaxis
; Now we check and move the position based on the x position
ld a, b ; Load the x position to our accimulator
and a ; Check if it's zero
jr z, .drawletter ; Jump to drawletter if it's zero already
inc hl ; Increment the map position by one since we need to move by x position
dec b ; Decrement our x parameter
jr .movexaxis ; Repeat
jr z, .drawletter
; If not, jump forward by a whole line.
ld a, l
add a, $20
ld l, a
.drawletter
ld a, [de] ; Load the first byte in the string
ld bc, textwriter_map ; Get the map for the ASCII->Tile map
@ -176,12 +165,12 @@ DrawActiveText:
; The byte we printed was a new line so we increment Y position
; and reset our X position.
ld a, 0 ; Usually we'd print into first position but since we're jumping
ld a, -1 ; Usually we'd print into first position but since we're jumping
; into continue, continue will automatically increment x for us.
ld [textwriter_posx], a
ld a, [textwriter_posy]
inc a
ld [textwriter_posy], a
ld a, 1
ld [textwriter_posy], a ; Set our Y position to 1
ld [textwriter_curdelay], a ; Also set curdelay to 1 as well
; We already know we printed empty character so we can skip checking
; if we need to repeat once again and just set the b flag
@ -195,7 +184,8 @@ DrawActiveText:
; Check if we printed an empty letter
cp a, $50
jr nz, .continue
ld b, 1
ld a, 1
ld [textwriter_curdelay], a ; Set curdelay to 1 so it immediately prints next character
.continue
; Time to update our work ram
@ -213,13 +203,6 @@ DrawActiveText:
; Update the current letter being drawn in ram
ld a, [textwriter_curletter]
inc a
; Check if we need to repeat whole thing due to printing empty character
ld a, b
and b
jr nz, .startdrawtext
; We didn't print an empty
ld [textwriter_curletter], a
.exit

BIN
main.o

Binary file not shown.

BIN
test.gb

Binary file not shown.

View File

@ -1,17 +1,13 @@
ROM Bank #0 (HOME):
SECTION: $0200-$027F ($0080 bytes) ["rom_textmap"]
$0200 = textwriter_map
SECTION: $1280-$137D ($00FE bytes) ["TextWriter"]
$137A = DrawActiveText.exit
$1326 = DrawActiveText.movexaxis
$1317 = DrawActiveText.moveyaxis
$132E = DrawActiveText.drawletter
$1342 = DrawActiveText.checkNewline
$1360 = DrawActiveText.continue
$1357 = DrawActiveText.checkRepeatOnce
$1306 = DrawActiveText.startdrawtext
SECTION: $1280-$136C ($00ED bytes) ["TextWriter"]
$131D = DrawActiveText.drawletter
$1331 = DrawActiveText.checkNewline
$1353 = DrawActiveText.continue
$1347 = DrawActiveText.checkRepeatOnce
$12D3 = StartText
$12EF = DrawActiveText
$12F1 = DrawActiveText
$1280 = InitWindow
SECTION: $0031-$003E ($000E bytes) ["Game Loop"]
$0037 = GameLoop.mainloop
@ -34,12 +30,12 @@ ROM Bank #0 (HOME):
SECTION: $0280-$127F ($1000 bytes) ["Font"]
$0280 = Page1
$1280 = Page1End
SLACK: $2DAD bytes
SLACK: $2DBE bytes
WRAM Bank #0:
SECTION: $C000-$C006 ($0007 bytes) ["ram_textwriter"]
$C002 = textwriter_curletter
$C005 = textwriter_curdelay
$C002 = textwriter_curletter
$C003 = textwriter_posx
$C004 = textwriter_posy
$C006 = textwriter_active

View File

@ -1,16 +1,12 @@
; File generated by rgblink
00:0200 textwriter_map
00:137A DrawActiveText.exit
00:1326 DrawActiveText.movexaxis
00:1317 DrawActiveText.moveyaxis
00:132E DrawActiveText.drawletter
00:1342 DrawActiveText.checkNewline
00:1360 DrawActiveText.continue
00:1357 DrawActiveText.checkRepeatOnce
00:1306 DrawActiveText.startdrawtext
00:131D DrawActiveText.drawletter
00:1331 DrawActiveText.checkNewline
00:1353 DrawActiveText.continue
00:1347 DrawActiveText.checkRepeatOnce
00:12D3 StartText
00:12EF DrawActiveText
00:12F1 DrawActiveText
00:1280 InitWindow
00:0037 GameLoop.mainloop
00:0031 GameLoop
@ -22,8 +18,8 @@
00:0061 Start
00:0280 Page1
00:1280 Page1End
00:C002 textwriter_curletter
00:C005 textwriter_curdelay
00:C002 textwriter_curletter
00:C003 textwriter_posx
00:C004 textwriter_posy
00:C006 textwriter_active