[A83] Re: Tilemap


[Prev][Next][Index][Thread]

[A83] Re: Tilemap




This *should* be a working scrollable tilemap routine...
I haven't tested it, so it's likely to have some bugs.

Provided a sprite routine which writes an 8x8 sprite at (hl)..(hl+7) 
to the screen where the upper left corner is specified by d(x) and e(y),
and is called by _dosprite,
this routine should work for writing a scrollable tilemap to the screen at
once.

Operation:
 the tilemap should be at "tilemap:"
 the width of the tilemap should be defined as a word (.dw xxxx)
  at "map_width"
 the 2 user-variables y_origin and x_origin provide a means to set the 
  upper left corner of the piece of the map you need.
  You need to adjust these for scrolling.
 there is no check to see if x_origin and y_origin are in the right bounds
 the routine destroys a, b, de and hl.

 you need to push all registers (except hl) which are destroyed in the 
  sprite routine (one [modified to work with] with inputs defined above!!!) 
  at the places defined in the code.

Hope it works
--Peter-Martijn

> Next, we need the map
> 
map_width:
  .dw 12 ; width of map as a word!
tilemap:
  .db 1,1,1,1,1,1,1,1,1,1,1,1
  .db 1,0,0,2,3,0,0,0,0,0,0,1
  .db 1,0,0,4,5,0,0,0,0,0,0,1
  .db 1,0,0,0,0,0,0,0,0,0,0,1
  .db 1,0,0,0,0,0,0,0,0,0,0,1
  .db 1,0,0,0,0,0,0,0,0,0,0,1
  .db 1,0,0,0,0,0,0,0,0,0,0,1
  .db 1,1,1,1,1,1,1,1,1,1,1,1


  ld a,0
  ld (x_origin),a
  ld (y_origin),a

writemap:
  ld hl, tilemap
  ld de,(map_width)

  ld a,(y_origin)
  ld b,a
y_shiftloop:
  add hl,de
  djnz y_shiftloop

  ld a,(x_origin);
  ld d,0
  ld e,a            
  add hl,de       ; add x_origin to hl to point to the right byte.

  ld b,8

  ld de,00h

wmaploop_row:
  push bc


  ld b,12
  ld d,0
wmaploop_col:
  ld a,(hl)
  inc hl

  push hl
  ld h,0
  ld l,a

  add hl,hl
  add hl,hl
  add hl,hl   ; a now contains a * 8 ( ((a*2)*2)*2 = a* 2*2*2 = a*8)
              ; this is the offset from the "tiles" label
  push de
  ld de,tiles
  add hl,de
  pop de     


  ; here you should push all registers destroyed by the sprite routine except
  ; hl 

  call _dosprite

  ; here you should pop all the registers you pushed above

  pop hl
  
  ld a,8   ; add 8 to d;
  add a,d
  ld d,a
  
  djnz wmaploop_col

  ;now we need to decrement hl by 12, and increment it by (map_width)
  
  push de
  ld de, $FFFF - 12
  add hl,de
  ex de,hl
  ld hl,(map_width)
  add hl,de
  pop de

  ; to add 8 to e:
  ld a,8  
  add a,e
  ld e,a
    
  pop bc
  djnz wmaploop_row;

ret


> See how that is 12 numbers wide, and 8 numbers high? Now we need the tile 
> set. These tiles should be under the label 'tiles'.
> 
> tiles:
> 
> floor:           ;0
> .db  %00000000
> .db  %00000000
> .db  %00000000
> .db  %00000000
> .db  %00000000
> .db  %00000000
> .db  %00000000
> .db  %00000000
> 
> wall:          ;1
> .db  %11111111
> .db  %11111111
> .db  %11111111
> .db  %11111111
> .db  %11111111
> .db  %11111111
> .db  %11111111
> .db  %11111111
> 
> tabletopl:       ;2
> .db  %11111111
> .db  %10000000
> .db  %10000000
> .db  %10000000
> .db  %10000000
> .db  %10000000
> .db  %10000000
> .db  %10000000
> 
> tabletopr:      ;3
> .db  %11111111
> .db  %00000001
> .db  %00000001
> .db  %00000001
> .db  %00000001
> .db  %00000001
> .db  %00000001
> .db  %00000001
> 
> tablebotl:        ;4
> .db  %10000000
> .db  %10000000
> .db  %10000000
> .db  %10000000
> .db  %11111111
> .db  %00110000
> .db  %00110000
> .db  %00110000
> .db  %00110000
> 
> tablebotr:      ;5
> .db  %00000001
> .db  %00000001
> .db  %00000001
> .db  %00000001
> .db  %11111111
> .db  %00001100
> .db  %00001100
> .db  %00001100
> .db  %00001100
> 
> Okay... those tiles are actually 8x8 sprites, which when set up 8 up and 
> down, and 12 left and right, fills the entire screen. The first tile under 
> the tiles label is called 'floor'. This tile is totally blank, and the 
> character sprite can walk on this. It is tile value 0, and on the map, 
> wherever there is a 0, this floor tile will show up. Any tile after the 0 is 
> not 'walkable' unless specifically modified. Wherever there is a 1 on the 
> map, the tile with the value 1 will show up. Wherever there is a 2 on the 
> map, the tile with the value 2 will show up. 
> 
> I really hope this helps. Feel free to ask again if you need more help, or 
> want me to write a demo.
> 
> darkfire139@aol.com
> 
> 
> 





References: