[A83] Re: scrolling


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

[A83] Re: scrolling




>From: "Nick Palladino" <nickps1@hotmail.com>
>
>Hello everyone I need a little help with scrolling. By the way
>thanks for the information on tile maps, I can now display a 12*8
>tilemap on my calculators screen. Anyways I've seen tutorials on
>scrolling the tilemaps but I don't really understand a couple things.
>If I wanted to scroll a tilemap does it have to be a certain size? By
>this I mean can it be 16*16 or 32*32 or whatever size or is there only
>descinated size tilemaps you can scroll? Also I don't know how to
>apply the scrolling code to the tilemap. By this I mean if i had a
>tilemap and i wanted to press the arrow keys and make it scroll what
>code would I use to do this. Maby someone could write me a good
>tutorial like last time and get me on my way to scrolling tilemaps.
>Thank You very very very much!!

A normal tilemap has a fixed size which depends on the size of the tiles. 
(e.g. 16x16 tiles -> 6x4 tilemap) Tilemaps used as levels for games can have 
any size.


( Chapter I Displaying a picture )
( Chapter II Displaying a tilemap )
Chapter III Simple tilemap scrolling


-------------------------------------------
Chapter III Simple tilemap scrolling
-------------------------------------------
______________
* Left-Right *
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
Scrolling only left-right means our tilemap should be 8 tiles high. The 
length can be as big as you want. In this example I'll take 16.
So what makes a tilemap scroll? Well, you need a tilemap routine that is a 
bit more complicated. It must have an extra input, a coordinate which tells 
the routine the position where to start drawing. In our example, which is a 
relatively small tilemap, we can only scroll 4 places (16-12). By 
incrementing/decrementing the coordinate each time a key is pressed, the 
tilemap routine draws different parts of the tilemap. When the coordinate is 
zero, it draws the first 12 columns of the map. Increment the coordinate and 
it draws columns 2-13, and so on.

( The code which takes care of the key input has been included as example 
and is probably useless in a game )

.org progStart

	xor	a		; reset coordinate
updateTileMap:
	push	af
	ld	hl,tilemap
	ld	de,tiles
	call	drawTileMap
	call	fastCopy
keyloop:
	call	_getcsc		; replace call by bcall on 83+
	halt
	cp	15		; pressing clear quits
	jr	z,quit
	sub	2
	jr	z,moveLeft
	dec	a
	jr	nz,keyloop
moveRight:
	pop	af
	cp	16-12		; don't scroll further than 4
	jr	z,updateTileMap ;   =tilemap length - 12
	inc	a
	jr	updateTileMap
moveLeft:
	pop	af
	or	a		; don't scroll further than 0
	jr	z,updateTileMap
	dec	a
	jr	updateTileMap
quit:
	pop	af
	ret

;================
; a = coordinate
; hl -> tilemap
; de -> tiles
;================

drawTileMap:
	ld	b,0		; get the place of where to
	ld	c,a		; start drawing
	add	hl,bc		;
	push	hl
	pop	ix
	ld	hl,8E29h	; or 9340h on the 83+
	ld	bc,8*256+12
drawTileMapLoop:
	push	bc
	ld	b,c
drawLineLoop:
	push	bc
	push	hl
	ld	h,0
	ld	l,(ix)
	inc	ix
	add	hl,hl
	add	hl,hl
	add	hl,hl
	add	hl,de
	ex	de,hl
	ex	(sp),hl
	ld	b,8
drawTileLoop:
	ld	a,(de)
	ld	(hl),a
	inc	de
	ld	a,b
	ld	b,0
	add	hl,bc
	ld	b,a
	djnz	drawTileLoop
	ld	de,-(8*12)+1
	add	hl,de
	pop	de
	pop	bc
	djnz	drawLineLoop
	ld	c,7*12
	add	hl,bc
	ld	c,16-12		; skip a few bytes of the tilemap
	add	ix,bc		;   =tilemap length - 12
	pop	bc
	djnz	drawTileMapLoop
	ret

tilemap:
	.db	1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
	.db	0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2
	.db	1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
	.db	0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2
	.db	1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
	.db	0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2
	.db	1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
	.db	0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2
tiles:
	.db	0,0,0,0,0,0,0,0

	.db	00000000b
	.db	00011100b
	.db	00111110b
	.db	01101011b
	.db	01111111b
	.db	01011101b
	.db	00100010b
	.db	00011100b

	.db	00000000b
	.db	00011100b
	.db	00111110b
	.db	01101011b
	.db	01111111b
	.db	01100011b
	.db	00111110b
	.db	00011100b
.end

___________
* Up-Down *
ŻŻŻŻŻŻŻŻŻŻŻ
Scrolling only up-down means our tilemap should be 12 tiles long. The height 
can be as big as you want. In this example I'll take 10.
The code is almost the same as above.

( The code which takes care of the key input has been included as example 
and is probably useless in a game )

.org progStart

	xor	a		; reset coordinate
updateTileMap:
	push	af
	ld	hl,tilemap
	ld	de,tiles
	call	drawTileMap
	call	fastCopy
keyloop:
	call	_getcsc		; replace call by bcall on 83+
	halt
	cp	15		; pressing clear quits
	jr	z,quit
	dec	a
	jr	z,moveDown
	cp	4-1
	jr	nz,keyloop
moveUp:
	pop	af
	or	a		; don't scroll further than 0
	jr	z,updateTileMap
	dec	a
	jr	updateTileMap
moveLeft:
	pop	af
	cp	10-8		; don't scroll further than 2
	jr	z,updateTileMap ;   =tilemap height - 8
	inc	a
	jr	updateTileMap
quit:
	pop	af
	ret

;================
; a = coordinate
; hl -> tilemap
; de -> tiles
;================

drawTileMap:
	ld	bc,12		; get the place of where to
	or	a		; start drawing
	jr	z,drawTileMap1
drawTileMapGetStartLoop:
	add	hl,bc
	dec	a
	jr	nz,drawTileMapGetStartLoop
drawTileMap1:
	push	hl
	pop	ix
	ld	hl,8E29h	; or 9340h on the 83+
	ld	b,8
drawTileMapLoop:
	push	bc
	ld	b,c
drawLineLoop:
	push	bc
	push	hl
	ld	h,0
	ld	l,(ix)
	inc	ix
	add	hl,hl
	add	hl,hl
	add	hl,hl
	add	hl,de
	ex	de,hl
	ex	(sp),hl
	ld	b,8
drawTileLoop:
	ld	a,(de)
	ld	(hl),a
	inc	de
	ld	a,b
	ld	b,0
	add	hl,bc
	ld	b,a
	djnz	drawTileLoop
	ld	de,-(8*12)+1
	add	hl,de
	pop	de
	pop	bc
	djnz	drawLineLoop
	ld	c,7*12
	add	hl,bc
	pop	bc
	djnz	drawTileMapLoop
	ret

tilemap:
	.db	1,0,1,0,1,0,1,0,1,0,1,0
	.db	0,2,0,2,0,2,0,2,0,2,0,2
	.db	1,0,1,0,1,0,1,0,1,0,1,0
	.db	0,2,0,2,0,2,0,2,0,2,0,2
	.db	1,0,1,0,1,0,1,0,1,0,1,0
	.db	0,2,0,2,0,2,0,2,0,2,0,2
	.db	1,0,1,0,1,0,1,0,1,0,1,0
	.db	0,2,0,2,0,2,0,2,0,2,0,2
	.db	1,0,1,0,1,0,1,0,1,0,1,0
	.db	0,2,0,2,0,2,0,2,0,2,0,2
tiles:
	.db	0,0,0,0,0,0,0,0

	.db	00000000b
	.db	00011100b
	.db	00111110b
	.db	01101011b
	.db	01111111b
	.db	01011101b
	.db	00100010b
	.db	00011100b

	.db	00000000b
	.db	00011100b
	.db	00111110b
	.db	01101011b
	.db	01111111b
	.db	01100011b
	.db	00111110b
	.db	00011100b
.end

____
*  *
ŻŻŻŻ
Now that you understand the basics about tilemap scrolling, I think (if you 
find a decent routine) you should be able to understand simple 
up-down-left-right scrolling, which has two input coordinates instead of 
one.
Smooth scrolling is again a bit more complicated, but has the same basics. 
You have a tilemap, the tiles and one/two coordinate(s). The tilemap routine 
should do the rest.
You must also know that there are many sorts of tilemap routines. Almost 
every (tilemap based) game has its own routine adapted/optimized to the 
needs of that game.

Tijl Coosemans

_________________________________________________________________
Download MSN Explorer gratis van http://explorer.msn.nl/intl.asp