Skip to main content
gamedev.net
🔒 Locked

Timer Class

Started by MeeZ 2 replies 1k views
Original Post
MeeZ
MeeZ
G''Day! I am having a painful time trying to get my particle system rendering multiple effects at the same time. The reason being is because (i think) i need a decent timer class that can calculate elapsed time. Anyway to cut a long story short. I was hoping anyone could help me put together a timer class (in C++) that I can use to initialize counters and get elapsed times from various counters (various instances of the timer class). Or even if you could just point me in the direction of where i can find a good timer class on the net!? Anyway, I hope you can help me out. Cheers, MeeZ
MeeZ
Scourage
Scourage
I've written a simple timer class that's really just a wrapper for SDL's timer. Should work on any system you can get SDL on, but you gotta link with it. It's really basic. I think I based it on the one from the programming linux games book-it's been a while, I don't remember. You can have multiple timers, you might want to consider making this class a singleton and having all your objects reference it for thier time updates or passing the time change per frame to each object for it's own update function. Either way, this is what I've been using.

Also, I haven't tested the pause function (I wrote this last year). I think it doesn't work quite right. I might even look into it more tonight and fix it. If I do I'll post it on my website at: http://scourage.servebeer.com/programming/index.html

Header:


#ifndef C_TIMER
#define C_TIMER

#include <SDL/SDL.h>

typedef struct SDL_timer_type // Create A Structure For The Timer Information

{
unsigned int start;
unsigned int elapsed;
unsigned int last_called;
unsigned int this_frame;

} timer_type;

class c_timer
{
public:
c_timer();
~c_timer();
void start();
float time_this_frame();
float time_running();
void pause();
void unpause();

private:
timer_type time;
bool paused;
};

#endif


Source:

#include "timer.h"

c_timer::c_timer()
{
paused=false;
time.last_called=0;
time.start=0;
time.elapsed=0;
time.this_frame=0;
}

c_timer::~c_timer()
{
}

void c_timer::start()
{
time.start=SDL_GetTicks();
time.elapsed=0;
time.last_called=time.start;
time.this_frame=0;
paused=false;
}

float c_timer::time_this_frame()
{
unsigned int current_time;

if(paused)
{
current_time=time.last_called;
}
else
{
current_time=SDL_GetTicks();
}
time.this_frame=current_time-time.last_called;
time.last_called=current_time;
return (float)time.this_frame/1000;
}

float c_timer::time_running()
{
unsigned int current_time;

if(paused)
{
current_time=time.last_called;
}
else
{
current_time=SDL_GetTicks();
}
time.elapsed=current_time-time.start;
return (float)time.elapsed/1000;
}


void c_timer::Pause()
{
paused=true;
}

void c_timer::unpause()
{
paused=false;
}



----------------------------------
I'm not online

[edited by - scourage on January 27, 2004 6:02:43 AM]

[size="3"]Halfway down the trail to Hell...
MeeZ
MeeZ
Scourage,

Thanks very much for your reply! SOrry i didn''t get back to you last night, but i was totally buggered and went to bed. Anyway, I''ll have a go at implementing your timer that you have posted for me. I really appreciate that! Thanks again!

Cheers,

MeeZ
MeeZ
Scourage
Scourage
No sweat, I''ve had plenty of days like that. If you make any improvements in it, or find problems, let me know.

cheers,

Bob

----------------------------------
I''m not online

[size="3"]Halfway down the trail to Hell...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.