[SDL] Sprite animation. How?

Started by
14 comments, last by ZadrraS 20 years, 1 month ago
How do i do sprite animation. Cone3D, tutorials, are harder as they could be. All i need to know is how do i play some kind of action, if i have, for example, 4 sprites of running character.
Advertisement
Cone3d'' sprite tutorial is horrible I feel, the way that I wrote my SDL animated sprite class was to store each frame of the animation in individual files - its perfectly possible to store all frames in a single file but it was simpler to use separate files to get started.

If you do go for the separate file approach then I would recommend using a different folder for the different sprites.

The way that the animation was achieved was through using a vector of SDL_Surfaces - one entry for each frame. When you load in the sprite all you need to do is loop through the number of frames and store the sprite frame image in the vector.

Now if you keep an internal frame counter to keep track of what frame the sprite is currently on you can increment this counter each time you draw the sprite, then when the counter is greater than the number of frames you reset it back to 1.
More surfaces.... My Ping-pong had 8 surfaces, is it normal to have LOTS of surfaces ? If a game is a more advanced 2D game it should have LOTS of surface if done that way you described...
And another Q so i wouldn''t have to make another thread. How do i make that if player presses left, the cube would go left 40pixels but if the player holds left it would only go 40 pixels left and wouldn''t move until you press the key again.
I am not sure if it is covered in the book, but there is a free copy of ''Programming Linux Games'' which uses SDL.

Yes, you probably don''t use linux BUT, SDL is cross-platform, so you should run into little trouble...

Programming Linux Games


------------------------------------------------------------
// TODO: Insert clever comment here.
------------------------------------------------------------// TODO: Insert clever comment here.
I need an array structure thingie, but it doesn''t work..:

struct Tiles {
int x;
int y;
};

then:

int num;
Tiles tiles[num];

But it doesn''t work, i get these errors:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: ''tile'' : unknown size

if i change "int num" to "const int num" it works but i get an error:

error C2105: ''++'' needs l-value

when i do:

num++;

What to do?
1. Statically declared arrays (such as that one there) in C++ must have a constant size. That is, the exact size of the array must be known before you compile the code.

The problem when you used const int was that a const int, is... well... constant. It can''t be changed using conventional (ie: normal) ways. So ++ is out.

To do what you want, you''re looking for this:
int num;
Tiles *tiles = new Tiles [num];

And when you are done with tiles, you have to do this:
delete tiles;

2. Get a book on C++. This problem is elementary enough to be attracting flames around here. Dynamic memory allocation is usually covered very early on in C++ books.

-Auron
Oh, and if you're using C and not C++, change the line:
Tiles *tiles = new Tiles [num];

to:
Tiles *tiles = (Tiles *)malloc (sizeof (Tiles) * num);

Change:
delete tiles;

To:
free (tiles);

And change all references to C++ to C.

-Auron

[edited by - Auron on March 21, 2004 12:20:59 PM]
There is a problem about c++ books here where i live.. theres only few of them and theyre extremely hard to find, so, the only way to learn is through the internet...
quote:Original post by ZadrraS
And another Q so i wouldn''t have to make another thread. How do i make that if player presses left, the cube would go left 40pixels but if the player holds left it would only go 40 pixels left and wouldn''t move until you press the key again.


At the end of the frame, use a static or global variable to record whether the button is down. Then, in the next frame, check if the button is down and that it wasn''t down last frame. Then and only then do you move the cube. Rinse and repeat.

Pseudo-code:
void frame () {  static bool leftHeld = false;  if (keyDown[LEFT] && !leftHeld) MoveCube();  leftHeld = keyDown[LEFT];}


You get the idea...

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement