Skip to content

Instantly share code, notes, and snippets.

@ISSOtm
Last active October 9, 2018 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ISSOtm/6142e86c16c856a7506803dc5d6d4a58 to your computer and use it in GitHub Desktop.
Save ISSOtm/6142e86c16c856a7506803dc5d6d4a58 to your computer and use it in GitHub Desktop.
A variable-width font engine for the Game Boy
SECTION "VWF engine",ROM0
; Prints text pointed to by DE
; Text is null-terminated
PrintVWFText::
ld a, 3
jr .startupColorID
.specialChar
add a, " " - 1
add a, a
add a, LOW(.funcs)
ld l, a
adc a, HIGH(.funcs)
sub a, l
ld h, a
ld a, [hli]
ld h, [hl]
ld l, a
jp hl
.funcs
dw .setColorID
.setColorID
ld a, [de]
and 3
inc de
.startupColorID
ld [wColorID], a
.printOneChar
ld a, [wCurTileID]
swap a
ld l, a
ld h, HIGH(wTileBuffer)
ld a, [de]
and a
ret z
inc de
sub a, " "
jr c, .specialChar
push de
ld d, HIGH(FontTiles) >> 2
add a, a
add a, a
rl d
add a, a
rl d
ld e, a
ld c, 7
.printOneLine
ld a, [wCurPixelOffset]
ld b, a
and a
ld a, [de]
push de
ld e, 0
jr z, .doneShifting
.shiftRight
rra ; Carry will always be clear (`and a` above, or 0 bit shifted from `rr e`)
rr e
dec b
jr nz, .shiftRight
.doneShifting
ld d, a
ld a, [wColorID]
rra
jr nc, .noLSB
ld a, d
or [hl]
ld [hl], a
push hl
ld a, l
add a, $10
ld l, a
ld a, e
or [hl]
ld [hl], a
pop hl
.noLSB
inc hl
ld a, [wColorID]
and 2
jr z, .noMSB
ld a, d
or [hl]
ld [hl], a
push hl
ld a, l
add a, $10
ld l, a
ld a, e
or [hl]
ld [hl], a
pop hl
.noMSB
inc hl
pop de
inc de
dec c
jr nz, .printOneLine
ld a, [de]
ld e, a
ld a, [wCurPixelOffset]
add a, e
cp 8
jr c, .stillSameTile
ld hl, wCurTileID
inc [hl]
and 7
.stillSameTile
ld [wCurPixelOffset], a
pop de
jr .printOneChar
SECTION "Font",ROM0,ALIGN[10]
; Font taken from another project (https://github.com/ISSOtm/Aevilia-GB)
; Made by Kai/kaikun97
; (Very rough, it used to be a fixed-width font that I barely modified)
; Format : 7 bytes make the tile itself, then the eigth byte gives the character's length in pixels
; Note : widths > 8px may not function correctly
FontTiles:: ; These correspond to ASCII characters (starting at $20)
db $00, $00, $00, $00, $00, $00, $00, 1 ; Space
; Symbols 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
; Uppercase
db $30, $48, $84, $84, $FC, $84, $84, 8
db $F8, $84, $84, $F8, $84, $84, $F8, 8
db $3C, $40, $80, $80, $80, $40, $3C, 8
db $F0, $88, $84, $84, $84, $88, $F0, 8
db $FC, $80, $80, $FC, $80, $80, $FC, 8
db $FC, $80, $80, $FC, $80, $80, $80, 8
db $7C, $80, $80, $BC, $84, $84, $78, 8
db $84, $84, $84, $FC, $84, $84, $84, 8
db $7C, $10, $10, $10, $10, $10, $7C, 8
db $04, $04, $04, $04, $04, $04, $F8, 8
db $84, $88, $90, $A0, $E0, $90, $8C, 8
db $80, $80, $80, $80, $80, $80, $FC, 8
db $84, $CC, $B4, $84, $84, $84, $84, 8
db $84, $C4, $A4, $94, $8C, $84, $84, 8
db $78, $84, $84, $84, $84, $84, $78, 8
db $F8, $84, $84, $F8, $80, $80, $80, 8
db $78, $84, $84, $84, $A4, $98, $6C, 8
db $F8, $84, $84, $F8, $90, $88, $84, 8
db $7C, $80, $80, $78, $04, $84, $78, 8
db $7C, $10, $10, $10, $10, $10, $10, 8
db $84, $84, $84, $84, $84, $84, $78, 8
db $84, $84, $84, $84, $84, $48, $30, 8
db $84, $84, $84, $84, $B4, $CC, $84, 8
db $84, $84, $48, $30, $48, $84, $84, 8
db $44, $44, $44, $28, $10, $10, $10, 8
db $FC, $04, $08, $10, $20, $40, $FC, 8
; Symbols 2, all empty
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
db $00, $00, $00, $00, $00, $00, $00, 1
; Lowercase
db $00, $00, $78, $04, $7C, $84, $78, 7
db $80, $80, $80, $F8, $84, $84, $78, 7
db $00, $00, $7C, $80, $80, $80, $7C, 7
db $04, $04, $04, $7C, $84, $84, $78, 7
db $00, $00, $78, $84, $F8, $80, $7C, 7
db $00, $3C, $40, $FC, $40, $40, $40, 7
db $00, $00, $78, $84, $7C, $04, $F8, 7
db $80, $80, $F8, $84, $84, $84, $84, 7
db $00, $80, $00, $80, $80, $80, $80, 2
db $00, $20, $00, $20, $20, $20, $C0, 4
db $80, $80, $84, $98, $E0, $98, $84, 7
db $10, $10, $10, $10, $10, $10, $10, 7
db $00, $00, $68, $94, $94, $94, $94, 7
db $00, $00, $78, $84, $84, $84, $84, 7
db $00, $00, $78, $84, $84, $84, $78, 7
db $00, $00, $78, $84, $84, $F8, $80, 7
db $00, $00, $78, $84, $84, $7C, $04, 7
db $00, $00, $BC, $C0, $80, $80, $80, 7
db $00, $00, $7C, $80, $78, $04, $F8, 7
db $00, $40, $F8, $40, $40, $40, $3C, 7
db $00, $00, $84, $84, $84, $84, $78, 7
db $00, $00, $84, $84, $48, $48, $30, 7
db $00, $00, $84, $84, $84, $A4, $58, 7
db $00, $00, $8C, $50, $20, $50, $8C, 7
db $00, $00, $84, $84, $7C, $04, $F8, 7
db $00, $00, $FC, $08, $30, $40, $FC, 7
SECTION "Tile buffer",WRAM0,ALIGN[8]
; Copy to VRAM once text has finished printing
wTileBuffer::
ds $10 * 9
; ID of the tile being drawn
wCurTileID::
db
; ID of the current pixel within the current tile
wCurPixelOffset::
db
; ID of the color used in the palette to draw the letters
wColorID::
db
wTileBufferEnd:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment