SDL Animation

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Sp3ke
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Sat Feb 06, 2010 12:39 am

SDL Animation

Post 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.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: SDL Animation

Post by hurstshifter »

"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
Busy_V
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 28
Joined: Mon Jan 04, 2010 5:29 am
Programming Language of Choice: C/C++

Re: SDL Animation

Post by Busy_V »

RyanPridgeon wrote:If you wanna go there, you go for it man. Nobody is stopping you so just work hard and achieve your goals.
User avatar
Skullman
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 10
Joined: Sun Oct 18, 2009 8:46 am
Current Project: Random
Favorite Gaming Platforms: Mega Drive, SNES, Dreamcast, Gameboy, GBA
Programming Language of Choice: Any and all
Location: England
Contact:

Re: SDL Animation

Post 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.
User avatar
Lord Pingas
Chaos Rift Regular
Chaos Rift Regular
Posts: 178
Joined: Thu Dec 31, 2009 9:33 am
Favorite Gaming Platforms: NES, SNES, Nintendo 64, Dreamcast, Wii
Programming Language of Choice: C++
Location: Hiding In My Mum's Basement With My Pokemon Cards

Re: SDL Animation

Post 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.
Sp3ke
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Sat Feb 06, 2010 12:39 am

Re: SDL Animation

Post 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 ] );
	}
}
Post Reply