🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Implementing a standard Timer function

Started by
3 comments, last by Endurion 4 years, 5 months ago

Hey guys, I'm wondering why the heck I'm unable to simply use one timer function in my game. Instead, I have to create copies of the exact same function over and over again in order for the time to sync or work at all.

I've created the function in a simple class(I made the class so that I didn't have to have 1000 timer functions):

asdf

class Timer
{
public:
bool WaitMilliSeconds(long int seconds) //Now THIS is how you do a fucking timer
{
 static unsigned long secondsPassed;
 static unsigned long startTime = GetTickCount();
 static bool initTime = false;
 static bool timePassed = false;
 if (!initTime)
 {
  startTime = GetTickCount();
  initTime = true;
 }
 secondsPassed = (GetTickCount() - startTime);

 if (secondsPassed > seconds){
  secondsPassed = 0;
  startTime = GetTickCount();
  timePassed = true;
  initTime = false;
  return true;

 }
 else
  return false;
}
};
//I implement the timer as follows:
Timer t;
  if (t.WaitMilliSeconds(10))
  vTempDir += (vDiff / 2.0f);


and it works fine. I was hoping, in my render() function within my game loop, that I could just do

Timer t2;

t2.WaitMilliSeconds(10);

etc etc. Since they're all different classes, they shouldn't interfere with one another. But they do. I even tried

Timer t = new Timer;

t→WaitMilliSeconds(10);

But it literally still stops working if used more than once. I'm forced to make WaitMilliSeconds1, WaitMilliSeconds2, etc, why is the memory working this way? I thought when creating a pointer to a class and allocating memory for it on the stack, there'd be no way it would interfere with the function of the same class type but somewhere else in memory.

This isn't one of those brick-wall problems, I can get around this, but it's annoying, and it goes against what I learned about memory.

Any idea on how to create a function within a class that I can use over and over to measure time?

Thanks guys!

Advertisement

static unsigned long secondsPassed;

A static local variable is shared between all calls of the same function. Plus their initialization-assignment is only called once.

store the variables as non-static member variables. of course that requires an entirely different usage than what you are doing now. Eigrhee use a state-machine like approach for checking the timer (regularely in an update-loop), or have it call a callback when it finishes.

I need to be able to implement my code in this manner. I can't rely on win32 Threads anymore, I'm writing a game engine, and those who have done that know that you cannot rely on CreateThread() within classes if you're going OOP. Could you please edit my code a bit to show me how I might implement it properly?

you cannot rely on CreateThread() within classes if you're going OOP

That's not right. You mave have to put an mediator function that calls the instance or your class, but that's it.

For a timer you shouldn't need CreateThread at all though.

This might do it (you may have to add a constructor):

class Timer
{
  private:
  
    unsigned long secondsPassed;
    unsigned long startTime = GetTickCount();
    bool initTime = false;
    bool timePassed = false;
  
  public:
  
    bool WaitMilliSeconds( long int seconds )
    {
      if ( !initTime )
      {
        startTime = GetTickCount();
        initTime = true;
        timePassed = false;
      }
    
      secondsPassed = ( GetTickCount() - startTime );

      if ( secondsPassed > seconds )
      {
        secondsPassed = 0;
        startTime = GetTickCount();
        timePassed = true;
        initTime = false;
        return true;
      }
      return false;
    }
    
};

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement