yahoo5000 yahoo5000 - 3 months ago 11
C++ Question

C++ timer in while loop

Hello i am trying to make a simple console game , spaceship and meteors who witch u have to dodge , i have draw ed a game map and a spaceship with a full control of him , and now ant to make a meteor spawn each second with a random coordinates and fly into a spaceship direction this is how my game map looks like :

####################
# #
#>|\ #
# ###> #
#>|/ #
# #
####################


of course its much bigger then i draw but thats are just a sample what i have done so far and now want to spawn meteor each second to fly into a spaceship direction for that i need a timer i ges , so now a coding part this is my main function :

int main()
{
setup(); // setup a game requered information and variables
while(!gamestart){ // draw a menu if game is not started yet
menu(); // draw menu window
input(); // inputs
movement(); // actual moving
}
while(gamestart && !gameover){// if game is started
gamedraw();// draw a game map and a spaceship
input();// inputs
movement(); // moving
meteorDraw(1000);// and here i am trying to draw a meteors each 1 sec
}
return 0;
}


so i have a structure with an "x" and "y" coordinates of meteor and and int meteordraw function i will probably would just make a random coordinates each second and then draw a meteor , but the problem is that my whole game slows down to 1 sec this is my meteorDraw function :

void meteorDraw(int time){
if(gamestart){
Sleep(time);
for(int a = 0; a <= mapsizey;a++){
for(int b = 0; b <= mapsizex;b++){

}
}
}
}


i havent done anything with a meteordraw function just want to make sure that its work as it should first but its slows down my whole game not only meteor spawn time
any help or advice how can i solve this ?

Answer

I assume that you want to be continuously calling gamedraw(), input() and movement() in order to run the game. The problem is that the Sleep call in meteorDraw() is called in the same (sole) thread in which the other functions are called, and it blocks (stops all execution in that thread) for the sleep duration. This means that gamedraw(), input() and movement() will not be called during the sleep.

So you have this currently happening, repeating within the second while loop:

  1. Call gamedraw() once - returns promptly.
  2. Call input() once - returns promptly.
  3. Call movement() once - returns promptly.
  4. Call meteorDraw(1000) - takes 1 second to return.

What you actually want is something like this:

  1. Call gamedraw() - returns promptly.
  2. Call input() - returns promptly.
  3. Call movement() - returns promptly.
  4. If more than 1 second has passed since meteorDraw() was last called, call it, otherwise do nothing. meteorDraw() should return promptly.

The following code is one possible implementation.

#include <time.h>

void meteorDraw();

int main()
{
    time_t last_meteor_time=0, time_now=0;
    setup(); // setup a game requered information and variables 
    while(!gamestart){ // draw a menu if game is not started yet 
        menu(); // draw menu window
        input(); // inputs 
        movement(); // actual moving
    }
    while(gamestart && !gameover){// if game is started
        gamedraw();// draw a game map and a spaceship
        input();// inputs 
        movement(); // moving 
        time(&time_now); // Get the current time.
        if (time_now != last_meteor_time){
            // Call only if clock has advanced 1 second since previous call.
            meteorDraw();
            last_meteor_time = time_now;
        }
    }
    return 0;
}

void meteorDraw(){
    if(gamestart){
        for(int a = 0; a <= mapsizey;a++){
            for(int b = 0; b <= mapsizex;b++){
                /* Draw meteor... */
            }
        }
    }
}

If you decide you want millisecond-resolution time, then you'll need a platform-specific call to get millisecond-resolution time. See this answer: Capturing a time in milliseconds

Or use clock from <ctime> for sub-second but not millisecond accuracy.