Jump to content
  • Advertisement
Sign in to follow this  
adam_o

How to tell the computer to wait...

This topic is 4223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, I have been programming for a little while now, and I need to find a way to tell the computer to wait for a little while. I've been using the wait command described at http://www.cplusplus.com/reference/clibrary/ctime/clock.html , but now I need a way to stop the "wait" by pressing a key or something. Basically, is there a way to abort the wait sequence that I have, or is there a better wait sequence? All help is appreciated and thanks in advance. I use C++, OpenGL, and GLUT.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by dpadam450
system("pause"); will stop your program until a key is pressed.


I'm sorry, I failed to specify that I have a mac. Is this generic for C++ or is it a windows thing?

Share this post


Link to post
Share on other sites
You could use a while loop but I don't know how to get the current tick count on a mac


int maxTime = 5000; // 5000ms
int startRef = GetCurrentTime(); // GetCurrentTime() returns number of ms

while ((GetCurrentTime() - startRef) < maxTime)
{
if (IsKeyboardHit())
break;
}





I came up with this quickly, it should work provided you know of a function to get the clock tick in ms. Replace GetCurrentTime() with a mac version that returns number of ms. You should replace the IsKeyboardHit() function with a more appropriate mac version too.

Also, system("pause") may work on a mac however you'll find you'll be waiting forever until someone presses a key.

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
You could use a while loop but I don't know how to get the current tick count on a mac

*** Source Snippet Removed ***

I came up with this quickly, it should work provided you know of a function to get the clock tick in ms. Replace GetCurrentTime() with a mac version that returns number of ms. You should replace the IsKeyboardHit() function with a more appropriate mac version too.

Also, system("pause") may work on a mac however you'll find you'll be waiting forever until someone presses a key.


Thank you so much for this help. I should have thought of it, just a while statement. Oh, and I do know the clock tick thing, it's CLOCKS_PER_SEC (I don't know if that's the same as windows) The only thing that I'm not so sure about is the IsKeyboardHit() function... I use the GLUT glutKeyboardFunc(), which runs "whenever possible", so I'm wondering if I can just tell it that when a key is hit, that it should set a variable (say key_hit) to one, which would be the IsKeyboardHit() replacement... I'll see what I can do with this code. Thanks again. :) You made my debugging day :P

[Edited by - adam_o on January 28, 2007 10:35:40 PM]

Share this post


Link to post
Share on other sites
Upon closer review of the code, I found that it looked surprisingly similar to the code that I have (see the while section). If you could clarify a little, that would be great...


void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {/*I tried stuff here but it doesn't work*/}
}



vs


int maxTime = 5000; // 5000ms
int startRef = GetCurrentTime(); // GetCurrentTime() returns number of ms

while ((GetCurrentTime() - startRef) < maxTime)
{
if (IsKeyboardHit())
break;
}

Share this post


Link to post
Share on other sites
Your while version looks like it will end up doing the same thing... You can use yours because it doesn't have to do the subtraction everytime but in the end it doesn't matter which version to use.

If you're using GLUT then setting it should be quite easy, but you may have to process the event queue in your wait function so that the keyboard events can be handled


// Global key up/down variable
bool g_bKeyPress = false

// Handlers called from GLUT
void KeyboardFunc()
{
g_bKeyPress = true;
}

void KeyboardUpFunc()
{
g_bKeyPress = false;
}

void Wait(int seconds)
{
clock_t endwait = clock() + seconds * CLOCKS_PER_SEC;
while (clock() < endwait)
{
// Process GLUT event queue?

// Check for keypress
if (g_bKeyPress)
break;
}
}



Try and find out if there is a function for processing the queue... Otherwise you may have to find a function which checks to see if the keyboard has been pressed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!