Welcome! Log In Create A New Profile

Advanced

Timing Animation

Posted by Sinman 
Timing Animation
January 16, 2009 12:01AM
Can anyone recommend a good library for timing animation? I'm looking for something along the lines of JavaScript's setInterval() (repeating) and setTimeout() (one time) methods. Or is there a more correct way to do this in C?

Edit: Would a better way be to create a new thread and then sleep that thread for an interval of time? Examples would be greatly appreciated.



Edited 1 time(s). Last edit at 01/16/2009 12:14AM by Sinman.
Re: Timing Animation
January 16, 2009 08:00AM
You'll probably need need to write your own or find an open source game a long the lines of what you want to do. I'm currently working on a game and I have a class or two that handles animation and allows you to set a time per animation frame.

I would not advice using threads for this as you'll end up in a right old mess.

If you'll tell me a bit more info on what you are trying to do maybe I can give you a snippet to help.
Re: Timing Animation
January 16, 2009 08:16AM
Given that the framerate is supposed to be fixed, just sync to that.
Re: Timing Animation
January 16, 2009 03:18PM
You can check out my game Horror Vacui for a clearer example of what I'm trying to do if the following explanation doesn't make any sense.

When a player places a card on the board it results in all (affected) adjacent cards flipping in a clockwise order ending with the flipping of the just placed card. The card in the top position starts its flip first and after a short delay is followed by the right, delay, bottom, and so on. The flipping animation is a tileset-based 5 frame animation that I would like to run slower than the main while loop. So I guess I'm trying to achieve two things: specify a delay before performing an action (which I would also use to simulate a CPU opponent "thinking" and moving their cursor) and run an animation independent of the speed of the main while loop.

The board holds 25 cards with up to 24 of them flipping around the same time (during a wild card event) so abstracting the animation away from any one use would be better than my current solution of maintaining the status of each card in multiple arrays.



Edited 1 time(s). Last edit at 01/16/2009 07:46PM by Sinman.
Re: Timing Animation
January 16, 2009 06:52PM
I think the best thing to do would be to have some sort of counter, and have the sprites animate once every 3 frames or something like that. Also just having a counter before the computer player responds might work too. But it sounds like you don't want to do that. But I really think that would be preferrable to using threads.
Re: Timing Animation
January 16, 2009 06:56PM
I get you. Here's something I am using for my animations. Also I use a set of sprites and specify the source x,y,width,height when initializing the frame.

This is just pseudo code so you get the idea and plug in what you need.


struct animation_frames
{
	xRECT	m_GraphicSource;
       //has a whole bunch of other stuff that you'll not need here
};

struct animations
{
	animations()
	{
		m_pframes_graphics = NULL;
		freeze = false;
		lastframe = get_tick_count();
	};
	~animations()
	{
		if (m_pframes_graphics) {
			delete [] m_pframes_graphics;
			m_pframes_graphics =NULL;
		}

	};

	void SetNumberOfFrames(int num) {
		frames = num;
		m_pframes_graphics = new animation_frames[frames];
	};

	void SetSource(int frame, int x, int y, int width, int height) {
		m_pframes_graphics[frame].m_GraphicSource.top = y;
		m_pframes_graphics[frame].m_GraphicSource.left = x;
		m_pframes_graphics[frame].m_GraphicSource.right = width;
		m_pframes_graphics[frame].m_GraphicSource.bottom = height;
	};

	int		            frames;
	float	                    time;
	bool	                    repeat;
	bool	                    freeze;
	Uint64                 lastframe;
	animation_frames*   m_pframes_graphics;

};

#define MAX_ANIMATIONS (10)
#define FRAME_TIME (30) // in ms
animations  animeng[MAX_ANIMATIONS];
int current_animation = 0;
int current_frame = 0;
//settting everything up

animeng[0].SetNumberOfFrames(2);  //set number of frame for animation
animeng[0].SetSource(0, 0,0,64,60);  //frame and source on the image (x,y,w,h)
animeng[0].SetSource(1, 65,0,64,60); //frame and source on the image (x,y,w,h)
animeng[0].time = 3; // time of animation ... basically a count

animeng[1].SetNumberOfFrames ....... // and so on


Uint64 get_tick_count()
{
	const Uint64 ticks = gettime();
	const Uint64 ms = ticks / TB_TIMER_CLOCK;
	return ms;
}

// Update code

void update()
{
      Uint64 dwTime = get_tick_count(); // timer function returns the tick count

	if ((dwTime - animeng[current_animation].lastframe) > (animeng[current_animation].time * FRAME_TIME))
       {
             animeng[current_animation].lastframe = get_tick_count();
            current_frame++; // next frame

            //animation time reached ....  either move onto next animation or set to start.

        }
	

}

//drawing
void draw()
{
//blit to screen based on current animation (current_animation) and current frame of that animation

...your source rect would be
animeng[current_animation].m_pframes_graphics[current_frame].m_GraphicSource.top;
animeng[current_animation].m_pframes_graphics[current_frame].m_GraphicSource.left;
animeng[current_animation].m_pframes_graphics[current_frame].m_GraphicSource.right;
animeng[current_animation].m_pframes_graphics[current_frame].m_GraphicSource.bottom;



}

void main()
{
   while(gameloop)
   {
      update();
      draw();
   }
}



Edited 1 time(s). Last edit at 01/16/2009 07:04PM by scanff.
Sorry, only registered users may post in this forum.

Click here to login