Jump to content

Recommended Posts

Posted

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?

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!

Posted
class Timer
{
   Int starttime
   Int timeinterval

   void Update()
   {
       If CurrentTime()-starttime>timeinterval CallFunction()
   }
}

My job is to make tools you love, with the features you want, and performance you can't live without.

Posted

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

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!

Posted

#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

  • Upvote 1

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...