asynchronous function.

Started by
5 comments, last by homeboye 18 years, 6 months ago
Hi. I am working on a game and i need to have a clock in it. The clock will be shown on the screen the whole time, and every second it has to refresh to show the new value. But the clock(function) has to be independant on the other program. How would i do that? Any literature/examples would be nice. Ty
Advertisement
You'd just render the clock the same way as any other moving objects, in your game loop.

Look at the source code for any game - and I mean *any* game - everything will be rendered inside a loop, with some things being rendered every frame. Some games use dirty rectangles or other lazy rendering methods which mean that certain items are only drawn when necessary, but generally, everything is drawn every frame.

In 3d games, or games which use 3d hardware acceleration, everything is typically drawn every frame.

Mark
yeah, but i would use the _sleep() function to do that.
and if i use that, then the whole program will be delayed for a whole second.
well it can never be really independant, not unless maybe you have multiple CPUs. But you can approximate it and you don't need sleep. What you want is some type of timing function, of course an accurate one. This will depend on what operating system, language, and/or compiler you use. But let's say you have a function called tick() which returns the current millisecond the program has been running. Note that I made up tick so don't go looking for it anywhere. Some example code using c++(but pretty easy to translate to another language):
int start = tick();while(true){  if(tick() - start > 1000)  {    cout << "one second passed" << endl;    start = tick();  }  if(exit condition)  {    break;  }  // Other code here}

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

You could always have an Update() function in the clock class which would look a bit like:
void Clock::Update(){    if (GetTickCount() - startTick > 1000)    {        sec += 1;        // Check if sec is > 59, and update the min/hours accordingly            startTick -= 1000;    }}


Alternatively, you could make the clock function threaded, but that would be overkill from my POV and it would bring a lot of problems with it.

Toolmaker

Quote:Original post by Toolmaker
Alternatively, you could make the clock function threaded, but that would be overkill from my POV and it would bring a lot of problems with it.

Toolmaker

Not only that, but it wouldn't make much sense either, because the only time you're ever going to be showing your user the clock (except in log files, etc, and in those cases, ~1 second doesn't matter too much) is when rendering occurs, so you'll be bound to that as an update speed anyway, no matter how many times you update your clock in between. Since as far as the user can tell, things are only being updated on a per-frame basis, why should the clock go any faster?
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
ok, so it seems that i will have to go old school :)
ty anyways.

This topic is closed to new replies.

Advertisement