Karma Universe - demuynck.org Lesson 9
TI-82 ASM Corner :: Programming Guide :: Lesson 9 Back | Home | Search
Lesson 9 : Sprites

In this lesson, you will learn how to display sprites by using an existing sprite routine. You will learn how to write your own sprite routine in a next lesson.
In this lesson, we will use PutSprite by Jimmy Mardell. It is a routine that you just cut and paste in your source.

PutSprite Routine | GRAPH_MEM | Other routines

PutSprite Routine

Here is the code for the routine that we will use to display sprites. I just list it so you can copy and paste it in your source. Don't worry about how it works now, you will understand later.

PutSprite:      ; Puts a sprite stored at (HL)
 push 	bc      ; at B,C by Jimmy Mardell
 push 	de
 push 	hl
 push 	hl
 ld	a,63
 sub	c
 ld	c,a
 call 	FIND_PIXEL
 ld 	de,GRAPH_MEM
 add 	hl,de
 ex 	de,hl
 pop 	hl
 ld 	b,(hl)
 inc 	hl
 ld 	c,(hl)
 inc 	hl
 push 	hl
 pop 	ix
 ex 	de,hl
PS_NewRow:
 push 	bc
 ld 	d,(ix)
 inc 	ix
 push 	af
 push 	hl
PS_NewCol:
 rl 	d
 ld 	e,a
 jr 	nc,PS_NoPixel
 or 	(hl)
 ld 	(hl),a
 jr 	PS_NextPixel
PS_NoPixel:
 cpl
 and 	(hl)
 ld 	(hl),a
PS_NextPixel:
 ld 	a,e
 rrca
 jr 	nc,PS_SameByte
 inc 	hl
PS_SameByte:
 djnz 	PS_NewCol
 pop 	hl
 pop 	af
 ld 	de,12
 add 	hl,de
 pop 	bc
 dec 	c
 jr 	nz,PS_NewRow
 pop 	hl
 pop 	de
 pop 	bc
 ret
 

One problem with this routine is that the rom page has to be set to 4 before you call the routine. This is needed because otherwise the FIND_PIXEL call won't work and the calculator will crash. Setting the rompage to 4 is mostly done only ones in the program (in the beginning). The ROM page will stay at 4 then. The code to do this is:

 ld	a,$8c
 out	(2),a
 
This sends out a command to change the rom page to page 4 to port 2. More about ports in lesson 16.

How the GRAPH_MEM works

OK, how does displaying sprites work? Well, the sprite routines don't display the sprite immediately to the LCD, they are writing it in the GRAPH_MEM. You need to copy the GRAPH_MEM to the LCD before you can see the sprite. But don't worry, the ROM has an easy routine to do this: ROM_CALL(DISP_GRAPH). This routine is rather slow compared to the CR_GRBCopy, a routine that is included in CrASH. It can be called using call CR_GRBCopy. But remember that your program will NOT work with Ash 3.0 if you use that routine.

With the putsprite routine, you can use sprites from 1x1 to 8x255. Thus the maximum width of the sprites is 8 pixels. Other sprite routines will display larger sprites. PutSprite also does not have clipping. This means that if a sprite goes of the edge of the screen, it will appear on the other side.

You know that the screen has a width of 96 pixels and a height of 64. When each pixel is represented by one bit, each line takes 12 bytes. 12*64=768 and that is exactly the length of the GRAPH_MEM.

Example:

#include "ti82.h"

.org START_ADDR
.db "PutSprite Test",0

 ld	a,$8c           ; set rom page 4
 out	(2),a	        ; (needed for FIND_PIXEL)    

 ROM_CALL(CLEARLCD)
 call   Clear_GRAPH_MEM ; clear GRAPH_MEM 
 ld     hl,TestSprite   ; load address of sprite in HL
 ld     bc,44*256+28    ; b = 44 = x / c = 28 = y 
 call   PutSprite       ; put the sprite at (44,28)
 ROM_CALL(DISP_GRAPH)   ; copy GRAPH_MEM to LCD
 ROM_CALL(KEY_HAND)     ; wait for a key
 ret

Clear_GRAPH_MEM:        ; routine that clears
 ld     hl,GRAPH_MEM    ; the GRAPH_MEM (fills
 ld     de,GRAPH_MEM+1  ; it with zeros)
 ld     (hl),0
 ld     bc,767
 ldir
 ret

PutSprite:              ; PutSprite by Jimmy Mardell
 ... (PutSprite Code)
 ret

TestSprite:
.db 8,8                  ; 8x8 sprite
.db %11111111            ; each bit represents 
.db %11000011            ; a pixel
.db %10100101            ;  1 means pixel on
.db %10011001            ;  0 means pixel off
.db %10011001            ; this is a sprite of 
.db %10100101            ; a cross in a square
.db %11000011
.db %11111111

.end
 
Sprite Example Screenshot On the right, there is a screenshot of the output from the example program. The sprite is displayed in the middle of the screen.

The Clear_GRAPH_MEM routine is a routine that clears the GRAPH_MEM.
You don't have to worry about this routine, I'll explain it later (the LDIR instruction is explained in lesson 18).

Other Routines

Next to the PutSprite routine, there are a lot of other sprite routines available. Each of them has its own characteristics. Some are big, others are small. Some can handle big sprites, others not. Some support clipping, others not. etc...

These routines can be found in the ticalc.org archives.

Previous lesson | Contents | Next lesson