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 Double Routine

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

bgtiled.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. We have an overall row counter, which counts 16 loops.

Each loop draws a full row of the top two tiles of the 2x2 tile, and then draws a full row of the bottom two tiles of the 2x2 tile.

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_double
;
; Author          : Marc Gale (Xalthorn or Eldara)
;
; Description     : Fill the background with a 2x2 tile
;
; On Entry        : hl holds the start of the background data
;                   a holds the bank number of the tile
;                   b holds the starting sprite of the tile
;                   c holds the palette number (0-7)
;
; During          : a is used heavily
;                 : d is used as a loop counter
;                 : e is used as a loop counter
;
; On Exit         : Nothing intentional
;************************************************************
bgtile_double:
 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     e,16            ; initialise the y counter for 16 rows
.yloop
 ld     d,16            ; initialise the x counter for 16 tiles
.xloop1
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc    b               ; increase the sprite number
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 dec    b               ; decrease the sprite number 

 dec    d               ; decrease the x counter 
 jr     nz,.xloop1      ; if we haven't hit 0, go back and carry on
 ld     d,16            ; reset the x counter

 inc    b               ; increase the sprite number...
 inc    b               ; ... to the next row
.xloop2
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc    b               ; increase the sprite number
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 dec    b               ; decrease the sprite number

 dec    d               ; decrease the x counter 
 jr     nz,.xloop2      ; if we haven't hit 0, go back and carry on

 dec    b               ; decrease the sprite number...
 dec    b               ; ...back to the start

 dec    e               ; decrease the y counter 
 jr     nz,.yloop       ; if we haven't hit 0, go back and 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             ; grab the bank 1 data

 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         ; write the bank 1 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

 ret                    ; we're done here, return

Buttons created using the interactive button maker at Coolarchive