Sprite is an image, which is actually an array of numbers,
each number in the array represents a color.
The array may be 1d or 2d, it doesn't really matter,
but in this tutorial I'll use 1d array as if it were a 2d array.
This is how the sprite looks in memory(array):

It's really simple, isn't it?
Ok, now for the drawing routine:
void drawsprite(int x, int y, int w, int h, unsigned char *sprt)
{
for (int iy = 0; iy < h; iy++)
for (int ix = 0; ix < w; ix++)
if (sprt[iy *w +ix] != 0) put_pixel(x +ix, y +iy, sprt[iy *w +ix]);
}
|
Well I'll explain what it does,
First, we get the position we want to draw the sprite at.
Second, we get the Width and Height of the sprite,
don't let it confuse you, it is a 1d array but we draw it on the screen
as if it were a 2d array(Remember how the screen works??
It's the same way with sprites)
And then we get the pointer of the sprite which is just a normal array.
Ok so we scan the sprite and we draw all pixels but BLACK ones.(This make the sprite transparent)
You ask what is "sprt[iy *w +ix]"??
You still don't remember how the screen works huh?
"iy * w" directs us to the begining of the Y line.
For example 1[iy] * 7[w] = 7 means we are in the second line(According to the sprite above).
We add the X pos to the Y line so it becomes 7[iy * w] + 2[ix] = 9, and this directs us exactly to the point in the array we want...
Btw-This is how 2d arrays work..
This is how it looks on the screen:

In the source you'll find more functions:
DrawSpriteCol(...) - Draws the sprite in one color, instead of the array colors, check it out.
DrawSpriteTrans(...) - Draws the sprite with a transparent color that you choose.(In the example above the transparent color is 0)
GetSprite(...) - Gets a sprite image from the screen memory to an array.(Useful to save sprites into files, etc..)
Source Code:
DJGPP - sprites.c
Turbo C - sprites.cpp