Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Tutorial - Sprites
Author:Arkon
Category:DOS Graphics
Uploaded at:29-May-03
Views:7919
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

NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[117079]
2D Rotated Rectangles Collision Detection[88997]
Keyboard Hook[77343]
UDP[65891]
HTTP Proxy[41227]

::Top 5 Samples::
2D ColDet Rotated Rectangles[11562]
PS2 Mouse Driver[6960]
Wave Format Player[5793]
Reading FAT12[5621]
CodeGuru[5361]


All rights reserved to RageStorm © 2009