Two funcs at once

Started by
3 comments, last by boltthrower 24 years ago
This program requires the user to input a few characters within two seconds to keep from failing the game. I know how to call a sleep function and set it for however many seconds. During these two seconds I put in a getc() function, but of course this crashes because the sleep() is only doing what it's told and can't with the getc() func inside waiting for its characters. But I do'nt know how to satisfy both requirements. I realize that I could call clock() before and after user input, subtract the two values, and find out AFTER how long it took, but this isn't consistent with the context of this part of the game. I'm using C and a 16-bit Borland Compiler. Edited by - boltthrower on 4/17/00 7:27:15 PM Edited by - boltthrower on 4/17/00 7:30:43 PM
Advertisement
If you''re programming a Windows app, you can use the SetTimer function and set up a callback function that will get executed when the time is up. If you are developing for DOS and comfortable with interrupts, one way is to use interrupt 0x1A functions 06 (set alarm) and 07 (reset alarm), which will call interrupt handler you provide.

aig
aig
Why is using clock() not consistent. It is much better for a number of reasons (A) it is more accurate (B) it allows you to do other things while still gaining how long it toke.

If you were to do that I suggest you create two functions, one for the start of the game loop and one for the end of the game loop.

//----------------------------------------------------//
// Example.cpp ---------------------------------------//
// ---------------------------------------------------//

int StartingTime=0; // used to hold the starting time


DWORD Start_The_Clock(void)
{
return(StartingTime = GetTickCount());
}


DWORD Wait_The_Time(DWORD HowLong)
{
while((GetTickCount() - StartingTime) < HowLong);

return(GetTickCount());
}

int main()
{
Start_The_Clock();

// process all game code

Wait_The_Time(30); // this will make the game run at about 30 fps

return(0);

}

// end of program

//-------------------------------------------------------//

Hope that helped








~Spike

You can contact me at
luke_howard@dingoblue.net.au
~SpikeYou can contact me at luke_howard@dingoblue.net.au
NO, I put at the bottom that I''m using a 16-bit compiler. That''s only text, isn''t it?

At least that''s what I was told. I''m new to this.

Anyways, it''s not a graphics game, but a text game. The user is supposed to put two characters into getc() before two seconds is up.
unsigned int start = clock();

while (clock()-start < 2000)
if (kbhit())
ch = getc();

something of the sort...

..-=gLaDiAtOr=-..

This topic is closed to new replies.

Advertisement