xtreampb Posted January 5, 2013 Share Posted January 5, 2013 I'm trying to figure out how to create and use a timer. Basically when five seconds pass i want to fire a function. Does anyone have any ideas on how i can do this? Quote bool Life() { while(death=false) { if(death==true) return death; } } I have found the secret to infinite life Did I help you out? Like my post! Link to comment Share on other sites More sharing options...
Josh Posted January 5, 2013 Share Posted January 5, 2013 class Timer { Int starttime Int timeinterval void Update() { If CurrentTime()-starttime>timeinterval CallFunction() } } Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
xtreampb Posted January 5, 2013 Author Share Posted January 5, 2013 can i get a little more background. I want to try to incorporate this into a class that already has models and such. Is CurrentTime() an LE built-in function? How is update being called Quote bool Life() { while(death=false) { if(death==true) return death; } } I have found the secret to infinite life Did I help you out? Like my post! Link to comment Share on other sites More sharing options...
Rick Posted January 5, 2013 Share Posted January 5, 2013 #pragma once #include <windows.h> #include "Event.h" class Timer { private: float _interval; unsigned __int64 _lastInterval; double _freq; bool _enabled; public: Timer(float interval); ~Timer(void); Event0 OnTick; void SetInterval(float interval); float GetInterval(); void Enable(); void Disable(); bool IsEnabled(); void Update(); }; #include "Timer.h" Timer::Timer(float interval) { _enabled = false; _interval = interval; unsigned __int64 pf; QueryPerformanceFrequency((LARGE_INTEGER*)&pf); _freq = 1.0 / (double)pf; } Timer::~Timer(void) { } void Timer::Disable() { _enabled = false; } void Timer::Enable() { _enabled = true; QueryPerformanceCounter((LARGE_INTEGER*)&_lastInterval); } void Timer::SetInterval(float interval) { _interval = interval; } float Timer::GetInterval() { return _interval; } bool Timer::IsEnabled() { return _enabled; } void Timer::Update() { if(_enabled) { unsigned __int64 temp; QueryPerformanceCounter((LARGE_INTEGER*)&temp); // get the time in ms float lastTime = ((temp - _lastInterval) * _freq) * 1000.0f; if(lastTime > _interval) { // fire the event OnTick.Raise(); _lastInterval = temp; } } } Get Event.h here http://www.leadwerks...le/367-c-event/ Usage would be: class MyClass { private: Timer* tmrTest; public: // this will be our callback void tmrTest_OnTick(Timer* sender) { } MyClass(){ tmrTest = new Timer(5000); // 5 seconds // bind the timer on tick event to a class method tmrTest.OnTick.Bind(this, &MyClass::tmrTest_OnTick); } }; Be sure to call tmrTest->Update() each frame 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.