Sprites
One of the most basic components of any image based game are sprites, small images which move about the screen at high speeds. The mouse cursor is a sprite that Windows© redraws thousands of times a second on the screen. The TI86 doesn't have color, just black and white. A bit set in the Video Memory, as you know, represents a pixel turned on (black). A sprite is made up of many bits in a row. Each bit in the sprite data represents a pixel on the screen.
You can play around with making your own sprites right now using SpriteEdit. Just copy and paste the results right into your source!
Now that you know how to put single pixels on the screen, you can move onto putting whole pictures onto the screen. We won't use FindPixel this time because that would take too long for each and every pixel in a 64 pixel image (8x8 pixel image). Instead we will be manipulating whole bytes at a time.
When you play Super Mario on your old 2D Nintendo 8-Bit, there are several characters that are moving on the screen at once. (I'm using the old Nintendo as an example because it uses 2 dimensional graphics like the TI86 does.) Those fast moving images are called sprites because they are small and are used constantly. Stuff like trees are called tiles because they make up the background total image along with, say, part of a castle wall.
You can't always just copy a sprite onto the screen and be
done. Sometimes the sprite might overlap from one byte to the
next one the screen. Take for example this to be a part of the
video memory. Each 0
is a bit and each group of 8
0
's is a byte. These bytes extend in all directions
on the screen. The bits are zeros because it is clear, blank,
no pixels set.
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
Here is the assembler code for the sprite we want to work with.
.db
tells the assembler
the following is to be directly inputted into the program as bytes.
.db %00011000 .db %11011011 .db %01011010 .db %11011011 .db %01011010 .db %11011011 .db %00011000 .db %11100111
Here is a graphical representation of what the above will look like on the screen.
We are now going to copy the that sprite into the video memory where it will overlap two bytes. that it will overlap bytes
xxxxxx00 011000xx xxxxxxxx xxxxxx11 011011xx xxxxxxxx xxxxxx01 011010xx xxxxxxxx xxxxxx11 011011xx xxxxxxxx xxxxxx01 011010xx xxxxxxxx xxxxxx11 011011xx xxxxxxxx xxxxxx00 011000xx xxxxxxxx xxxxxx11 100111xx xxxxxxxx
We need to now cover the routine that handles the writing of the image onto the Video Memory. This routine needs to figure out if the image runs over two bytes of is in one byte, how much of an overlap there is if it spans over two bytes, and then write the image taking the overlap into consideration.
I emailed Jay Hellrung when he just released his Sprite Display Routines asking for some info on them. From his reply I have put together the following on the standard SDR8.h routine included in the zip.
On the Download page you can find a zip with several Sprite Routines packaged for you to choose from.
The routine is pasted here and referenced with the following table by the line numbers. Follow each line and try to picture in your head what it is doing. The easiest way to do this is to print out the SDR8 Code and then refer to the explanations.
- ↓SDR_8x8:
- ↓ ld a,63
- ↓ sub c
- ↓ ld c,a
- ↓ push hl
- ↓ call FindPixel
- ↓ ld c,8
- ↓ add a,a
- ↓ dec a
- ↓ ld e,a
- ↓ ld a,b
- ↓ and %00000111
- ↓ inc a
- ↓ ld b,a
- ↓SDR_8x8_NewRow:
- ↓ ex (sp),hl
- ↓ ld a,(hl)
- ↓ inc hl
- ↓ ex (sp),hl
- ↓ push bc
- ↓ rlca
- ↓SDR_8x8_PrepByte:
- ↓ rrca
- ↓ djnz SDR_8x8_PrepByte
- ↓ ld d,a
- ↓ and e
- ↓ or (hl)
- ↓ ld (hl),a
- ↓ inc hl
- ↓ ld a,e
- ↓ cpl
- ↓ and d
- ↓ or (hl)
- ↓ ld (hl),a
- ↓ ld c,$10-1
- ↓ add hl,bc
- ↓ pop bc
- ↓ dec c
- ↓ jr nz,SDR_8x8_NewRow
- ↓ pop hl
- ↓ ret
Line(s) | Explanation | Bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
↑1 | You need to use it like this:
ld hl,sprite ;location of sprite ld bc,$1414 ;coordinates call SDR_8x8 ;call sprite routine ret ;done PutSprite: ;the routine's label ;paste jay's or some other sprite ; routine here ;make sure to paste findpixel if ; it's needed Sprite_Label: ;the sprite label .db %01111110 ;first line .db %10011001 ;second line . . . . . . .db %01111110 ;eight line |
More from z80 » Graphics
Find a Pixel // Grayscale // Pixel Manipulation // The Screen // Sprites // SDR8 Routine // Tile Maps // TileGen Routine