Page 1 of 1

SDL Animation

Posted: Sat Feb 06, 2010 12:44 am
by Sp3ke
Well I'm still new to SDL and working on a first game with a friend. However, what would be an efficient way of adding an Animation timer for the Character sprites? At the moment the animation is running way too fast and I've got a few theorys on how to slow it down, but no actual code snippets. (Using C++)

Any help will be appreciated.

Re: SDL Animation

Posted: Sat Feb 06, 2010 10:12 am
by hurstshifter

Re: SDL Animation

Posted: Sat Feb 06, 2010 12:19 pm
by Busy_V

Re: SDL Animation

Posted: Sat Feb 06, 2010 2:34 pm
by Skullman
A method you could try is: say you have a sprite with six animations in a cycle and you want to animate them to go though that cycle every two seconds. You think to yourself: that's three of the frames in a second, which is 1000/3 = ~333ms. So you could set your animation function to wait until 333ms have elapsed on the timer then change the animation accordingly.

You could repeat it for however many animations you want for whatever sprites you use. Two animations in a second is a change every 500ms; three animations in three seconds is once every second or 1000ms, 10 animations in 3 seconds is every 300ms et cetera.

Re: SDL Animation

Posted: Sat Feb 06, 2010 3:28 pm
by Lord Pingas
Lets put it like this...

Our frame rate is at 60 beats per second (So every 60 beats count as a second).

We have 3 frames, frame 1, frame 2 and frame 3.

The code will be a little something like this:

Code: Select all

if(frame_counter == 60) // If we have passed one second
    // Blit frame 1 to player's x and y position

if(frame_counter == 120) // If we have passed two seconds
    // Blit frame 2 to player's x and y position

if(frame_counter == 180) // If we have passed three seconds
    // Blit frame 3 to player's x and y position

if(frame_counter > 180) // If our frame counter is greater than 180
    // Reset our frame counter to 0, restarting the loop
I'm pretty sure that's how animation would go.

Re: SDL Animation

Posted: Sun Feb 14, 2010 4:11 am
by Sp3ke
Sorry only checked back to this topic just now. I've read all the comments, and made a timer for the animation however it dosen't seem to be working. Any ideas?

Code: Select all

void Characterz::Show()
{
	if(xVel < 0)
	{
		status = CHARACTER_LEFT;
		frame++;
	}
	else if(xVel > 0)
	{
		status = CHARACTER_RIGHT;
		frame++;
	}
	else if(yVel < 0)
	{
		status = CHARACTER_UP;
		frame2++;
	}
	else if(yVel > 0)
	{
		status = CHARACTER_DOWN;
		frame2++;
	}
	else
	{
		frame = 0;
		frame2 = 0;
	}


//THIS IS WHERE I TRYED TO ANIMATE THE CHARACTER MOVING RIGHT
if(status == CHARACTER_RIGHT)
{
	if(frame == 0 || frame == 1)
	{
		frame = 2;
		AnimationStart = SDL_GetTicks();
	}
	else if(SDL_GetTicks() == 50)
	{
		++frame;
		AnimationStart = SDL_GetTicks();
	}
}


	//Loop the animation
    if(frame >= 4)
    {
        frame = 0;
    }
	if(frame2 >= 4)
	{
		frame2 = 0;
	}


	do{
	apply_surface(CollStick.x, CollStick.y, Stick, Buffer, NULL);
	if(Collision.check_collision(CollCharacter,CollStick))
	{
	Mix_PlayChannel( -1, StickSound, 0 );

	CollStick.x = ((int) rand()% 608);
	CollStick.y =  ((int) rand()% 448);
	
	while(CollStick.y < 55)
	{
		CollStick.y =  ((int) rand()% 448);
	}
	while(CollStick.x > 270 && CollStick.x < 340 )
	{
		CollStick.x = ((int) rand()% 608);
	}

	apply_surface(CollStick.x, CollStick.y, Stick, Buffer, NULL);
	}

		}
		while(Collision.check_collision(CollCharacter, CollStick));

    //Show the Character
    if( status == CHARACTER_RIGHT )
    {
        apply_surface( CollCharacter.x, CollCharacter.y, Character, Buffer, &ClipRight[ frame ] );
    }
	else if( status == CHARACTER_LEFT )
    {
        apply_surface( CollCharacter.x, CollCharacter.y, Character, Buffer, &ClipLeft[ frame ] );
    }
	else if( status == CHARACTER_UP )
    {
        apply_surface( CollCharacter.x, CollCharacter.y, Character, Buffer, &ClipUp[ frame2 ] );
    }
	else if( status == CHARACTER_DOWN)
	{
	   apply_surface( CollCharacter.x, CollCharacter.y, Character, Buffer, &ClipDown[ frame2 ] );
	}
}