Jump to content
  • Advertisement
Sign in to follow this  
The Lion King

Timers :(

This topic is 5405 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

Advertisement
You couldn't get ahold of one because it doesn't exist. There is no perfect timer. There may be a timer that's good for what you want, though. What language do you want it in? Should it be cross-platform?

I have a timer class with a built-in FPS counter in C# that works with Linux and Windows (different hi-res timer for each; it decides at runtime), but you may need something different.

Share this post


Link to post
Share on other sites
I dont know if this is a perfect timer class but it works


class CTimer
{

protected:

double m_Frequency;

__int64 m_StartClock;

float m_FrameTime;
float m_FrameStart;
float m_FrameEnd;

public:

float GetFrameTime() { return m_FrameTime; }

double GetTime();

void Init();
void Update();

};




#include <windows.h>
#include "timer.h"

double CTimer::GetTime()
{
__int64 EndClock;

QueryPerformanceCounter((LARGE_INTEGER*)&EndClock);

return (double)(EndClock-m_StartClock)*m_Frequency;
}

void CTimer::Init()
{
__int64 rate;

// Get the performance frequency
QueryPerformanceFrequency((LARGE_INTEGER*)&rate);

// Invert it so we can multiply instead of divide
m_Frequency = 1.0/(double)rate;

// Get the start time
QueryPerformanceCounter((LARGE_INTEGER*)&m_StartClock);

m_FrameTime = 0.0f;
m_FrameStart = (float)GetTime();
m_FrameEnd = 0.0f;
}

void CTimer::Update()
{
// Update the timing
m_FrameEnd = (float)GetTime();
m_FrameTime = m_FrameEnd - m_FrameStart;
m_FrameStart = m_FrameEnd;
}

Share this post


Link to post
Share on other sites
With his, you would just call CTimer::Init() at the start of the program, call CTimer::Update() once a frame, and CTimer::GetFrameTime() whenever you need to know the time.

Share this post


Link to post
Share on other sites
CTimer myTimer;

// Load
myTimer.Init();

// Game Loop
myTimer.Update();

mySprite.x += myTimer.GetFrameTime() * 2.0f;

Share this post


Link to post
Share on other sites
I have no idea how to lock the frame rate

this is how i would use it to update an animated sprite at 10 frames per second

mySprite.UpdateAnimation(10, myTimer.GetFrameTime());

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!