-2

I'm learning C language and very new to this. I'm trying to write a simple game that spawns an enemy after 3 secs from the start of the level. I tried clock() function and had the problem that while it spawns after given time it also freezes the game so it is unplayable.

void delay (clock_t n) {
    clock_t start = clock();
    while(clock() - start < n);
}

Also tried the get_current_time() method but the game freezes if I got to this level so my code must be wrong. Can anyone give me some solution on how to approach this?

void draw_hero( void ) {

    char * hero_image =
    /**/    "H   H"
    /**/    "H   H"
    /**/    "HHHHH"
    /**/    "H   H"
    /**/    "H   H";

    int hero_x = (screen_width() - HERO_WIDTH) / 2;
    int hero_y = (screen_height() - HERO_HEIGHT) / 2;
    hero = sprite_create(x, y, HERO_WIDTH, HERO_HEIGHT, hero_image);

    double lastTime = get_current_time();
    while (true){
          double current = get_current_time();
          double elapsed = current - lastTime;
          //lastTime = current;

          while (elapsed < lastTime + 500) ???
              sprite_draw(hero);
              show_screen();
    }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
lufee
  • 169
  • 2
  • 10
  • This raises some quite profound questions about how you are going to handle time in your program — making the question rather broad. What do you want to happen while the time has not elapsed? How do you handle other events that might be scheduled between now and then? What about unscheduled events that occur? Are you working single-threaded or multi-threaded? Which platform are you on? There's also the problem that your code is not a complete function, much less an MCVE ([MCVE]). – Jonathan Leffler Aug 23 '16 at 03:45
  • The [`clock`](http://en.cppreference.com/w/c/chrono/clock) function is a bad choice, as on some systems it's not even using the wall-clock but the CPU time of the running process. You should look into platform-specific functions to get time with enough precision. – Some programmer dude Aug 23 '16 at 03:50
  • As for your problem, most games have a main loop somewhere, to handle events and drawing. You might want to read about *event queues* and how to add a *timer event* to that, something you can check for in the main loop. You could also simply add a variable that is the timeout, and don't accept input in the main loop while it's non-zero, and decrement it in the main loop. – Some programmer dude Aug 23 '16 at 03:54
  • @JoachimPileborg the clock function is meant to count processor clocks, not time clocks. it's much more accurate than getting the actual time. The header includes a macro for calculating the number of clocks per second; though it's roughly an approximation. On systems where the processor speed can vary, you'll have to poll the processor speed and scale to that. – DeftlyHacked Aug 23 '16 at 07:16
  • @DeftlyHacked Might be true all that. But still, on e.g. POSIX systems the `clock` function will not work as a timer anyway since it won't count time the process isn't running. And if one have a multi-threaded process the time can advance faster than expected. – Some programmer dude Aug 23 '16 at 07:20

1 Answers1

-1

clock is not the correct function to use. Nor is spinning in a tight loop to "sleep" appropriate.

Here's a minimal game loop based on your code and it demonstrates how to create the enemy thing at 3 seconds into it. But a real game is a bit more complicated...

I'm using "true", which is technically a C++ concept, but since you were using it in your own code, I suspect some C compilers will allow it.

I don't know what get_current_time is, but I assume it returns value with more granularity that a second (e.g. millisecond resolution).

// initialize - create screen, spawn initial sprites, create map etc..
bool has_created_enemy = false;
double baseTime = get_current_time();  // used for normalizing time to 0
double lastTime = 0;                   // last timestamp of last frame
double currentTime = 0;                // timestamp of current frame

while (true)
{
     lastTime = currentTime;
     currentTime = get_current_time() - baseTime;
     double elapsed_since_last_frame = currentTime - lastTime;

     // keyboard input


     // collision detection


     // update game state
     if ((currentTime >= 3.0) && (!has_created_enemy))
     {
          // spawn enemy
          has_created_enemy = true;
     }

     // draw frame
}
selbie
  • 100,020
  • 15
  • 103
  • 173
  • See http://stackoverflow.com/questions/4767923/ - using 'true' and 'false' is not 'technically a C++ concept'. – flau Aug 23 '16 at 08:09