Advertisement

SDL Sprites

Started by December 22, 2004 02:10 PM
2 comments, last by Drew_Benton 20 years, 2 months ago
Hey, I've been trying to follow some SDL sprite tutorials, but i'm having trouble grasping the concept of working with sprites. What I don't understand is, how does changing the frame typically work? All the tutorials I've seen use a struct: typedef struct { SDL_Surface *Frame; int pause; } Sframe; where Frame is a pointer to the frame, and pause is the amount of time to wait until changing it. Is it conventional, with each iteration of the game/message loop, to check each sprite and compare it with the current system time? and then change the frame if necisary? Or is there another way? Thanks a lot :)
I implemented a basic tile map and managed to load a sprite on it, so I'll briefly explain what I did.


1) Assume you have a sprite class that holds all the sprite frames *and* the direction the sprite is currently facing.

class Sprite {  int facing; // 0 = north, 1 = west, ...etc.  vector <*SDL_Surface> north_frames;  vector <*SDL_Surface> south_frames;  vector <*SDL_Surface> west_frames;  vector <*SDL_Surface> east_frames;};


2) When the sprite is not moving, you should be displaying a frame where the sprite is standing still (duh)

3) When you recieve a user command to move the sprite in some direction, start with the first index in the appropriate direction vector.

4) Now for your step size, animate thru the vector (example: assume we are doing 32x32 tiles and a sprite takes 4 steps (8 pixels) to move to a new tile. Then we'd start with drawing vector[0], then move the sprite 4 pixels and blit vector[1], next 4 pixels is vector[2], then vector[3], then back to vector[0]).


That is a really dumbed-down, simple example, but I hope you can follow it somewhat. There isn't any one "right" way to animate a sprite, you can do it anyway you like.

If you want to see a complete example, go here: http://sourceforge.net/projects/allacrost/ Click "browse CVS" and look in module_01/src for hoa_map.h and hoa_map.cpp. Those are two files I wrote a few months ago and with that code I was able to get a sprite walking around the map as a short demo. I commented it pretty good as well. So I hope either what I said helps, or this source code helps. Good luck!

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

Advertisement
Quote:
Is it conventional, with each iteration of the game/message loop, to check each sprite and compare it with the current system time? and then change the frame if necisary? Or is there another way?

That's exactly how I do it... checking the SDL timer is a fairly inexpensive operation. No need to worry!


Ryan
--Visit the Game Programming Wiki!
You can also get some more ideas about sprites with Cone3D's tuts. A lot of people feel that they are not that good - as I've read from feedback through out the forum - but they still serve as a 'different view'.

This topic is closed to new replies.

Advertisement