Timers :(

Started by
9 comments, last by The Lion King 19 years, 6 months ago
Can anybody point a perfect timer calss and its implementation to me? I searched a lot ... found a lot ... but cant get hold of any of them? Anyone?
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.
not necessarily perfect ... a simple would do.

I am using C++ and its for Windows .
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;}
thnx ... I need the implementation too ... how do I use it :)
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.
CTimer myTimer;

// Load
myTimer.Init();

// Game Loop
myTimer.Update();

mySprite.x += myTimer.GetFrameTime() * 2.0f;
and how do I lock the fps with it for the app.

And if I need to display a sprite 10 frames per second then?
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());
What happens inside UpdateAnimation ?

How does it handle the given time. For my app it is giving me 0.3 ... how do I use it?

This topic is closed to new replies.

Advertisement