The Lush Engine








Introduction
Background
Introduction
bgtiles.asm
bgtiled.asm
bgtilem.asm
putstile.asm
putdtile.asm
puthtile.asm
putvtile.asm
putmtile.asm
wallmeld.asm
Math
Objects
Palettes
Tiles
Utilities

Background Tiled Single Routine

The following file is a routine to fill the background with a single tile. You can download the file as well if you don't want to cut and paste the text.

bgtiles.asm

To download the file using a PC, right-click the link and select the option "Save Target As" then save the file as you would any other file from windows.

Explanation

The first thing we do is prepare the data for the second bank of background data. This data holds the bank number and the palette used by the tile placed. Once we have this data sorted out, we put it to one side.

The two banks of data are dealt with seperately to keep the speed up. There is no need to constantly switch between the banks of data as we are doing an entire screen full. We simply draw the background on the first bank and then set the second bank of data.

So, we need to preserve the start address of the background as once we've finished drawing the background we need to start again with the second bank of data.

We now draw the background data. This is a simple loop system that ensures we write 32 rows of 32 tiles, or 1024 tiles. 1024 is 4x256, so 4 loops of 256 makes our life easy.

Once that has all been done, we restore the starting address in HL, set the bank selection register to point to the second bank, and then work through the whole background (32x32 tiles) setting the second bank data to set the palette and tile bank used by the background.

Source

; **************************************************************
; Procedure Name  : bgtile_single
; 
; Author          : Marc Gale (Xalthorn or Eldara)
;
; Description     : Fill the background with a single tile
;
; On Entry        : hl holds the start of the background data
;                 : a holds the bank number of the tile
;                 : b holds the tile number
;                 : c holds the palette number (0-7)
;
; During          : a is used heavily
;                 : d and e are used as loop counters
;
; On Exit         : nothing intentional
;
; **************************************************************
bgtile_single:
 and    1               ; mask out all but the bit we want
 sla    a               ; move the bank
 sla    a               ; number to the
 sla    a               ; correct bit
 ld     d,a             ; stick it in d for a mo
 ld     a,c             ; get the palette number
 and    7               ; mask out all but the bits we want
 or     d               ; combine it with the bank number
 ld     c,a             ; store the result in c

; now we place the tile entries into the background map

 ld     a,0
 ldh    [$4F],a         ; make sure we're writing to bank 0

 push   hl              ; preserve the start address

 ld     a,b             ; put the tile number in A
 ld     d,4             ; we want to write 4x256=1024 bytes
.small_loop
 ld     e,0             ; start at 0, which means we'll get 256
.large_loop
 ld     [hli],a         ; stick it in the map data
 dec    e               ; drop the large counter by one
 jr     nz,.large_loop  ; if we haven't hit 0, carry on
 dec    d               ; drop the small counter by one
 jr     nz,.small_loop  ; if we haven't hit 0, carry on

; now that's done, let's set the bank and palette numbers

 pop    hl              ; restore the start address
 ld     a,1
 ldh    [$4F],a         ; make sure we're writing to bank 1

 ld	a,c             ; get the bank 1 data

 ld     d,4             ; we want to write 4x256=1024 bytes
.small_loop2
 ld     e,0             ; start at 0, which means we'll get 256
.large_loop2
 ld     [hli],a         ; write the bank 1 data
 dec    e               ; drop the large counter by one
 jr     nz,.large_loop2 ; if we haven't hit 0, carry on
 dec    d               ; drop the small counter by one
 jr     nz,.small_loop2 ; if we haven't hit 0, carry on

 ret                    ; we're done here, return

Buttons created using the interactive button maker at Coolarchive