Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ZadrraS

[SDL] Sprite animation. How?

This topic is 5231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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
______________________________________________________________

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!