Timers :(
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?
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.
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.
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;}
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;
// 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?
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());
this is how i would use it to update an animated sprite at 10 frames per second
mySprite.UpdateAnimation(10, myTimer.GetFrameTime());
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement