Jump to content

Delay/timer? (other that Sleep();

- - - - -

  • Please log in to reply
72 replies to this topic

#1
TeenChristian

TeenChristian

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 639 posts
So I'm working on a game. Basically I need a piece of code that will activate something after a certain amount of time. Sleep() would work, but it not only waits, but halts the whole program, thus freezing the sprites moving in the background. So some code that waits a certain amount of time without halting the program would be great! ^^

Thanks in advanced!
My Personal Blog l Learning C++ l I'll be famous soon enough.

#2
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
Get the start time, and each loop check the current time until the difference between the current time and the start time is equal or greater to the gap you wanted to wait for.

#3
TeenChristian

TeenChristian

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 639 posts
I believe I found a command that does all of it for me...

dbTimer( void );

I know your not familiar with the Dark GDK, but you might be able to answer this question:

When I put this code into my program:
dbTimer( 5000 );


Instead of counting 5 seconds it gives me an error saying

Quote

Error 3 error C2660: 'dbTimer' : function does not take 1 arguments

Any way to fix that?
My Personal Blog l Learning C++ l I'll be famous soon enough.

#4
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
abzero's solution is typical for games, so do it like that. However, if simple timer is too coarse-grained for the purpose at hand, which is frequently the case in gaming, and you're coding on Windows, then consider using high-precision timer.

WINAPI functions QueryPerformanceCounter and QueryPerformanceFrequency are used for this. These functions are based on hardware counter. I think that all living computers today have counter available for OS, so I think that this WINAPI works on all computers. At least I've always used it and have never heard that it didn't work on someone's computer.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,083 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
dbTimer takes no parameters:

Quote

Prototype:
int dbTimer()
This command will get the internal system time, which continually increments at a thousand times a second. The system time is returned in milliseconds, where 1000 units represent 1 second.
DarkGDK.us - Online DarkGDK Reference

#6
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
dbTimer( void ) basically says the function doesn't take any parameters (maybe it's a callback??). So not to sure if it's what you want, Dark GDK probably includes what you want, you'll just have to find it.

#7
opwuaioc

opwuaioc

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 216 posts

zoranh said:

abzero's solution is typical for games, so do it like that. However, if simple timer is too coarse-grained for the purpose at hand, which is frequently the case in gaming, and you're coding on Windows, then consider using high-precision timer.

Is there an alternative for Linux?
Something witty here.

#8
TeenChristian

TeenChristian

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 639 posts
So what exactly is dbTimer() used for?
My Personal Blog l Learning C++ l I'll be famous soon enough.

#9
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
it seems that dbTimer just gives you very fine resolution timer. It can be used for your purposes,

somthing like this:

class Timer {
    public:
        Timer(int msec, callbackObject co ) : _msec(msec),_start(dbTimer()),_callback(co) { }
 
        bool Expired() { return (dbTimer()-_start)>_msec; }
        void callback() { _co(); }
   private :
       int _msec;
       int _start;
       callbackObject _co;
};

Then just have a list of timer objects, each time through your game loop, test the Expired method and if it returns true, remove it from the list, and run the callback method.

#10
TeenChristian

TeenChristian

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 639 posts
unfortunately I don't seem to understand much of what you've posted abzero. I'm still relatively new to C++. Apparently there isn't a single command to accomplish what I'm trying to do, or a sleep command that doesn't stop the program.

Is there a way to get the time using anything located in the <ctime> header? and just do something like:

int time1 = time; 
int time2 = time1;
if (time2 = time1 + 5000)
{
//stuff
} //5000 being milliseconds or seconds to wait

would anything that simple work? Thanks again!
My Personal Blog l Learning C++ l I'll be famous soon enough.

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
@abzero: Your first solution is called busy-waiting, which is a cheap yet CPU-inefficient way of doing things. TC, I suggest you look into the pthread library and think about using a separate thread for your action; then you can easily use the sleep() function without stopping the sprites or hogging the CPU. I'll walk you through it, if you like.
sudo rm -rf /

#12
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
@dargueta, true, it would be busy waiting if I had a single loop waiting. But since this is a game, I was suggesting that this was encorporated into the game loop rather than a seperate loop.
So the game loop would be modified like:

....
updatedTimers();
moveObjects();
detectCollisions();
grawObjects();

flipBuffers();

etc...


The accuray of the timer in this case would not be perfect, but it would work well and wouldn't have the cpu siting in a spining loop like a busy-wait normally would.

The thread idea would work, but then you'd need one thread for each timer; the creation of a thread probably is more work than the addition of a Timer object to a stack, and although it would be much more accurate it increases the complexity of the code as you then have to thing about making sure each function is reentrant proof and multithreading safe etc.
But it is a valid alternative.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users