gameboy/game/text.asm

211 lines
6.3 KiB
NASM
Raw Normal View History

2019-06-18 09:08:14 +00:00
SECTION "ram_textwriter", WRAM0
textwriter_curtext: ds 2
textwriter_curletter: ds 1
textwriter_posx: ds 1
textwriter_posy: ds 1
textwriter_curdelay: ds 1
textwriter_active: ds 1
2019-06-21 10:09:57 +00:00
SECTION "rom_textmap", ROM0,ALIGN[8]
2019-06-18 09:08:14 +00:00
textwriter_map:
; 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
2019-06-19 15:07:02 +00:00
db $50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50 ; 0
2019-06-18 09:08:14 +00:00
; 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
2019-06-19 15:07:02 +00:00
db $50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50 ; 16
2019-06-18 09:08:14 +00:00
; 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
2019-06-19 15:07:02 +00:00
db $50,$41,$50,$50,$50,$50,$50,$43,$50,$50,$50,$50,$42,$50,$3F,$50 ; 32
2019-06-18 09:08:14 +00:00
; 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
2019-06-19 15:07:02 +00:00
db $35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$50,$50,$50,$50,$50,$40 ; 48
2019-06-18 09:08:14 +00:00
; 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F
2019-06-19 15:07:02 +00:00
db $50,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F ; 64
2019-06-18 09:08:14 +00:00
; 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F
2019-06-19 15:07:02 +00:00
db $10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$1A,$50,$50,$50,$50,$50 ; 80
2019-06-18 09:08:14 +00:00
; 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F
2019-06-19 15:07:02 +00:00
db $50,$1B,$1C,$1D,$1E,$1F,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29 ; 96
2019-06-18 09:08:14 +00:00
; 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F
2019-06-19 15:07:02 +00:00
db $2A,$2B,$2C,$2D,$2E,$2F,$30,$31,$32,$33,$34,$50,$50,$50,$50,$50 ; 112
2019-06-18 09:08:14 +00:00
SECTION "TextWriter", ROM0
2019-06-19 15:07:02 +00:00
InitWindow:
; Fill first line in window with straight line
ld hl, _SCRN1
ld b, $20
ld a, $52
call Memfill
; Fill rest of the area with dark bg
ld b, $40
ld a, $50
call Memfill
; Fill last line in window with straight line
ld b, $20
ld a, $57
call Memfill
; Top left corner
ld hl, _SCRN1
ld [hl], $51
; Top right corner
ld hl, _SCRN1 + 19
ld [hl], $53
; Bottom Left corner
ld hl, _SCRN1 + $60
ld [hl], $56
; Bottom Right corner
ld hl, _SCRN1 + $60 + 19
ld [hl], $58
; Left line
ld hl, _SCRN1 + $20
ld [hl], $54
ld hl, _SCRN1 + $40
ld [hl], $54
; Right line
ld hl, _SCRN1 + $20 + 19
ld [hl], $55
ld hl, _SCRN1 + $40 + 19
ld [hl], $55
ret ; Return
2019-06-21 10:09:57 +00:00
ClearWindow:
ld hl, (_SCRN1 + $21)
ld a, $50
ld b, $1E
call Memfill
ld hl, (_SCRN1 + $41)
ld b, $1E
call Memfill
2019-06-18 09:08:14 +00:00
; Start to print a string on screen
; @param de - Address source string
; @return a - garbage
; @return b - PosX on where to draw text
; @return c - PosY on where to draw text
; @return hl - Workram position for text pointer
; @return de - Memory address to source string
StartText:
2019-06-21 10:30:43 +00:00
ld a, 10
ld [textwriter_curdelay], a
2019-06-18 09:08:14 +00:00
ld a, 0
ld [textwriter_curletter], a
ld [textwriter_posx], a
ld [textwriter_posy], a
2019-06-21 10:09:57 +00:00
ld a, 1
ld [textwriter_active], a
2019-06-18 09:08:14 +00:00
ld hl, textwriter_curtext
ld a, d
ld [hli], a
ld a, e
ld [hl], a
dec hl
ret
; Draw text if one is currently active
; @return a - 0 if not active, 1 if active
DrawActiveText:
ld a, [textwriter_active] ; Check if text is currently active
and a
2019-06-21 10:30:43 +00:00
ret z
2019-06-18 09:08:14 +00:00
; Delay writing a letter by 10 frames
ld a, [textwriter_curdelay]
2019-06-21 10:30:43 +00:00
dec a
2019-06-18 09:08:14 +00:00
ld [textwriter_curdelay], a
2019-06-21 10:30:43 +00:00
ret nz
2019-06-18 09:08:14 +00:00
; Reset the curdelay
2019-06-21 10:30:43 +00:00
ld a, 10
2019-06-18 09:08:14 +00:00
ld [textwriter_curdelay], a
2019-06-19 15:07:02 +00:00
.startdrawtext
2019-06-18 09:08:14 +00:00
; Get the memory address pointing to the text currently being
; displayed. This is stored in textwriter_curtext
ld hl, textwriter_curtext ; Get the memory address of the pointer
ld d, [hl] ; Store the first byte address to d
inc hl ; Move to next byte
ld e, [hl] ; Store the next byte address to e
; We now have the current letter being drawn at [de]
2019-06-21 10:30:43 +00:00
; 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
2019-06-18 09:08:14 +00:00
ld a, [textwriter_posx]
ld b, a
2019-06-21 10:30:43 +00:00
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]
2019-06-18 09:08:14 +00:00
and a ; Check if it's zero
2019-06-21 10:30:43 +00:00
jr z, .drawletter
; If not, jump forward by a whole line.
ld a, l
add a, $20
ld l, a
2019-06-18 09:08:14 +00:00
.drawletter
ld a, [de] ; Load the first byte in the string
2019-06-19 15:07:02 +00:00
ld bc, textwriter_map ; Get the map for the ASCII->Tile map
ld c, a ; Set the position of C to the ASCII byte of current character
ld a, [bc] ; Get the tile byte for the ASCII from the map
ld [hl], a ; Print the byte to the map
2019-06-18 09:08:14 +00:00
ld a, [de]
and a ; Check if we reached the end
2019-06-19 15:07:02 +00:00
jr nz, .checkNewline
2019-06-18 09:08:14 +00:00
; We have reached the end of the string, disable textwriter
2019-06-19 15:07:02 +00:00
ld b, 0 ; Disable repeater
2019-06-18 09:08:14 +00:00
ld a, 0
ld [textwriter_active], a
2019-06-19 15:07:02 +00:00
jr .continue ; Jump straight to continue. No need to check anything
.checkNewline
ld a, [de] ; Regrab the ascii byte we printed
cp a, $0A ; Check if it was a new line
jr nz, .checkRepeatOnce ; Jump to checkRepeatOnce if it wasn't a new line
; The byte we printed was a new line so we increment Y position
; and reset our X position.
2019-06-21 10:30:43 +00:00
ld a, -1 ; Usually we'd print into first position but since we're jumping
2019-06-19 15:07:02 +00:00
; into continue, continue will automatically increment x for us.
ld [textwriter_posx], a
2019-06-21 10:30:43 +00:00
ld a, 1
ld [textwriter_posy], a ; Set our Y position to 1
ld [textwriter_curdelay], a ; Also set curdelay to 1 as well
2019-06-19 15:07:02 +00:00
; 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
ld b, 1
jr .continue
.checkRepeatOnce
ld a, [bc] ; Regrab the tile we printer
ld b, 0 ; Use b as our marker if we should repeat once
; Check if we printed an empty letter
cp a, $50
jr nz, .continue
2019-06-21 10:30:43 +00:00
ld a, 1
ld [textwriter_curdelay], a ; Set curdelay to 1 so it immediately prints next character
2019-06-19 15:07:02 +00:00
2019-06-18 09:08:14 +00:00
.continue
; Time to update our work ram
2019-06-19 15:07:02 +00:00
inc de ; Increment de to next letter
; Load the position pointer of current text and update it to the next letter
2019-06-18 09:08:14 +00:00
ld hl, textwriter_curtext
ld a, d
ld [hli], a
ld a, e
ld [hl], a
; Update the x position in ram
ld a, [textwriter_posx]
inc a
ld [textwriter_posx], a
; Update the current letter being drawn in ram
ld a, [textwriter_curletter]
inc a
ld [textwriter_curletter], a
.exit
ld a, [textwriter_active]
ret