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:28-May-03
Views:6983
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

User Contributed Comments(1)
 [1] lisad | 2014-06-16 03:43:56

You made &#8203;&#8203;some good points, I learned many things here! http://www.friv2hi.com
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[105824]
2D Rotated Rectangles Collision Detection[79071]
Keyboard Hook[68618]
UDP[52685]
HTTP Proxy[35960]

::Top 5 Samples::
2D ColDet Rotated Rectangles[10591]
PS2 Mouse Driver[6084]
Wave Format Player[4969]
Reading FAT12[4856]
CodeGuru[4544]


All rights reserved to RageStorm © 2009