Sign in to follow this  
homeboye

asynchronous function.

Recommended Posts

homeboye    115
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

Share this post


Link to post
Share on other sites
markr    1692
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

Share this post


Link to post
Share on other sites
nobodynews    3126
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
}

Share this post


Link to post
Share on other sites
Toolmaker    967
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

Share this post


Link to post
Share on other sites
SirLuthor    364
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this