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 SECTION "rom_textmap", ROM0,ALIGN[8] textwriter_map: ; 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F db $50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50 ; 0 ; 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F db $50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50,$50 ; 16 ; 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F db $50,$41,$50,$50,$50,$50,$50,$43,$50,$50,$50,$50,$42,$50,$3F,$50 ; 32 ; 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F db $35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$50,$50,$50,$50,$50,$40 ; 48 ; 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F db $50,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F ; 64 ; 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F db $10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$1A,$50,$50,$50,$50,$50 ; 80 ; 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F db $50,$1B,$1C,$1D,$1E,$1F,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29 ; 96 ; 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F db $2A,$2B,$2C,$2D,$2E,$2F,$30,$31,$32,$33,$34,$50,$50,$50,$50,$50 ; 112 SECTION "TextWriter", ROM0 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 ClearWindow: ld hl, (_SCRN1 + $21) ld a, $50 ld b, $1E call Memfill ld hl, (_SCRN1 + $41) ld b, $1E call Memfill ; 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: ld a, 0 ld [textwriter_curletter], a ld [textwriter_curdelay], a ld [textwriter_posx], a ld [textwriter_posy], a ld a, 1 ld [textwriter_active], a 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 jp z, .exit ; Delay writing a letter by 10 frames ld a, [textwriter_curdelay] inc a ld [textwriter_curdelay], a cp 10 jr c, .exit ; Reset the curdelay ld a, 0 ld [textwriter_curdelay], a .startdrawtext ; 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] ld hl, _SCRN1 ; Load the top-left map position to our map ld a, [textwriter_posy] ld c, a 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 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 .drawletter ld a, [de] ; Load the first byte in the string 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 ld a, [de] and a ; Check if we reached the end jr nz, .checkNewline ; We have reached the end of the string, disable textwriter ld b, 0 ; Disable repeater ld a, 0 ld [textwriter_active], a 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. ld a, 0 ; 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 ; 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 ld b, 1 .continue ; Time to update our work ram inc de ; Increment de to next letter ; Load the position pointer of current text and update it to the next letter 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 ; 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 ld a, [textwriter_active] ret