GBC-like Graphical Engine
Posted by: ISSOtm
Date: 2015-06-11 09:53:09
I am currently programming a game on my TI 84 Plus, and I wanted to display graphics in the same manner as a GBC.
I wrote some code, but it doesn't work that well…
; arguments :
; bc -> pointer to tile map (tile ID list)
; ix -> pointer to tile data (see tile structure)
GBClikeEngine:
ld de, appBackupScreen
ld hl, plotSScreen
ld a, 192 ; there are 384 tiles, but we are only going to draw half of them
push bc
drawEvenTiles:
push ix ; we will later need to restore ix to load another tile pointer
push af
ld a, (bc) ; get the first tile's ID
push bc
add a, a ; a contains the current tile's ID
add a, a ; four bytes per tile
ld b, 0
ld c, a
add ix, bc ; now ix contains a pointer to the tile being drawn
drawEvenTile: ; for some reason, evenly-numbered tiles must be drawn in one go, then all odd-numbered tiles will be.
ld b, 4 ; we are going to draw 4 lines
evenTileLoop:
ld a, (ix)
rld ; load to main buffer. This is placed incorrectly, but "drawOddTiles" will rotate this into a correct position
and $F0
ld (de), a ; load to back-buffer
push bc ; save counter
ld bc, 12
add hl, bc ; go 12 bytes later (one screen line)
ex de, hl
add hl, bc ; same
ex de, hl
inc ix ; go to next byte of current tile data
pop bc ; restore counter
djnz evenTileloop
pop bc
inc bc ; we will draw the next tile
pop af
pop ix ; restore the base tile pointer
dec a
jr nz, drawEvenTiles
pop bc
inc bc ; we are going to draw the odd-numbered tiles, so add an offset of 1
ld de, L3
ld hl, L6
ld a, 192
drawOddTiles:
push ix ; we will later need to restore ix to load another tile pointer
push af
ld a, (bc) ; get the first tile's ID
push bc
add a, a ; a contains the current tile's ID
add a, a ; four bytes per tile
ld b, 0
ld c, a
add ix, bc ; now ix contains a pointer to the tile being drawn
drawOddTile:
ld b, 4 ; we are going to draw 4 lines
oddTileLoop:
ld a, (ix)
rld ; load to main buffer. This rotates the previously incorrectly-placed nibble into a correct position
and $F0
rrca
rrca
rrca
rrca
ld c, a
ld a, (de)
or c
ld (de), a ; load to back-buffer
push bc ; save counter
ld bc, 12
add hl, bc ; go 12 bytes later (one screen line)
ex de, hl
add hl, bc ; same
ex de, hl
inc ix ; go to next byte of current tile data
pop bc ; restore counter
djnz oddTileloop
pop bc
inc bc ; we will draw the next tile
pop af
pop ix ; restore the base tile pointer
dec a
jr nz, drawOddTiles
ret
(appBackupScreen and plotSScreen are two RAM areas within the TI 84+)
The way it should be used is so :
Create some tile data, which is composed of four bytes per tile (one per line). The first byte goes to appBackupScreen, the second to plotSScreen.
Create some tile map, which is composed of 384 bytes (because the TI's screen is 96 * 64 pixels large, divided by 4 * 4 pixels per tile -> 384 tiles). Each byte holds the ID of the corresponding tile.
Call GBClikeEngine, with ix pointing to the tile data, and bc pointing to the tile map.
As I said, its objective is to reproduce the way the GBC screen works, on a TI 84 Plus (which as a B/W only display)
Could someone help me ? Thanks.