News Downloads Projects Documents/tutorials Staff

JE-SYSTEMS

Sprites  


How sprite works.

Why use sprites?
Sprites are very powerful, and fast as well. A sprite is a picture/icon with a width of 8,16 or 32 and height x. Perhaps you think that the limit of 32 pixels whide sux, but it's often much more than you'll ever need. The big power with sprites is that you can with one command place/remove a sprite. Sprites are used for many things, but the most used sprite, is the one you're using when you surf on your PC. The Mouse pointer..

Two ways of defining sprites
There's two ways of defining sprites, Hexadecimal and binary form. Those format looks like this:
...,0xAF,... (Hexadecimal)
...,0b10101111,... (Binary)

Using the Binary form is much easier to use. The Hexadecimal form is just binary form converted to hexadecimal. The TIGCC documentation explains this very good in those lines:
    Starting from TIGCC v0.91, you can use binary numbers to define your sprites. On the one hand, this is clearly not recommended since it makes the program incompatible with other C dialects as well as previous versions of TIGCC. On the other hand, it makes designing a sprite much easier and also allows for editing the sprite at a later time without having to reconvert it.
So, therefor we start with how to use and define sprites in the Hexadecimal way. I highly recommend you to read threw this section, even if you're not intrested in how to define sprites hexadecimal way.

How do you define sprites
Depending of the size of the sprite, you define it something like this:
static unsigned char cursor[] = {0xC0,0xF0,0x6C,0x73,0x39,0x3E,0x1C,0x18};

We set this variable cursor as a static variable, cause we don't want it to change. Then you can call the sprite function (in this case it has 8 blocks = 8 pixels high, and each number/char after the 0x.. format, is 4 pixels, so it's 8x8 pixels. Here's how to call it:
Sprite8 (x, y, height, cursor, LCD_MEM, mode);

Mode can be SPRT_XOR (which means, if there's a cursor sprite at x,y, then delete it, if not, place it) SPRT_OR (place it) SPRT_AND (delete everything that is not set (that is everything that is defined as '0' in binary mode)

How to declare a sprite (hexadecimal way)
Sprites is in compressed form, or binary form, which we recalculate to Hexadecimal form. If we want a sprite, that looks like an O, or something like this:

    1111
  1       1
1           1
1           1
1           1
1           1
  1       1
    1111

Okay, here we have 1 where we want pixels, let's add zeros where we don't want pixels:

00111100
01000010
10000001
10000001
10000001
10000001
01000010
00111100

You still see the O shape? Good. I said sprite is compressed. When we compress this binary format to hexadecimal we get 4 binary values to one hexadecimal (0001 = 1 .. 0010 = 2 and so on). So, let's split this sprite into 4 binary value columns:

0011 1100
0100 0010
1000 0001
1000 0001
1000 0001
1000 0001
0100 0010
0011 1100

It's a little harder to see that 0 shape now. Okay, now it's time to convert to hexadecimal. Here's a small list how to convert it:

Binary Decimal Hexadecimal
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1011 13 D
1110 14 E
1111 15 F


Time to convert. The first row of our sprite looks like this: 0011 1100. We start to convert the 0011, and look in the table above, and we c that it's 3 in hexadecimal form, the otherone is 1100, and that's C, so we get 3C. A sprite will not understand just 3C, so we have to type this as 0x3C. This means our line for the defination of this sprite looks like this:
static unsigned char my_sprite[] = {0x3C,.....};

How to declare a sprite (binary way)
To define a sprite the binary way, you just need to change the definition of the sprite. You will use it the exactly same way as a normal sprite. Let's take a look at that shape of that O we got before. It looks like this, right:
00111100
01000010
10000001
10000001
10000001
10000001
01000010
00111100

To declare this one as a binary sprite, we just simply do that by adding 0b infront of it (instead of usinx 0x(hex) as we did for the hexadecimal form). So the line for this sprite would look like this:
static unsigned char cursor[] = {0b00111100,0b01000010,0b10000001,0b10000001, 0b10000001,0b10000001,0b01000010,0b00111100};

Or if we put the number under eachother, it looks like this:
static unsigned char cursor[] = {
0b00111100,
0b01000010,
0b10000001,
0b10000001,
0b10000001,
0b10000001,
0b01000010,
0b00111100};

As you can se, we got our shape right there. So this way is very easy to use, but it's not recommended. You use this sprite exactly the same way you use a hexadecimal defined sprite.

I hope you have found some help with clearing out how sprite works. If you have any question about this tutorial, be sure to drop me a mail
Created to be compatible with Microsoft Internet Explorer at 800x600.
Site designed by Jon Eriksson © JE-Systems, 2002