Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Spawn 2 enemies per second in OpenGL

  1. #1
    Newbie Newbie
    Join Date
    Nov 2017
    Posts
    1

    Spawn 2 enemies per second in OpenGL

    I'm making a game in openGL and I want to spawn 2 enemies per second on the screen. The problem is, I'm calling the spawn function inside idle:

    Code :
    idle
    {
        // ...
        world.spawnEnemy();
    }

    And then, in the spawnEnemy function:

    Code :
    void World::spawnEnemy()
    {
        Enemy newEnemy, newEnemy2;
     
        float start;
     
        start = glutGet(GLUT_ELAPSED_TIME);
     
        // 1/2 a second passes
        while ( (glutGet(GLUT_ELAPSED_TIME) - start) <= 500 )
        {
            ;
        }
     
        // create enemy
        newEnemy();
        pushToEnemyList(newEnemy);
     
        // another 1/2 second passes
        while ( (glutGet(GLUT_ELAPSED_TIME) - start) <= 1000 )
        {
            ;
        }
     
        // create enemy
        newEnemy2();
        pushToEnemyList(newEnemy2);
    }

    And this is not working, of course. Probably because idle is called everytime and I'm calling a function inside idle that waits for a certain amount of time to pass and then everything starts going wrong. The solution is to do the spawning in the idle function (without calling any function, doing all the logic inside idle), but I don't like this idea. I need to do more things in a certain number of time, for example, my enemies have guns and they will have to shoot n times every second. And if I do all of these directly inside of idle it will become a mess. So, basically, how do I make this work:

    Code :
    idle
    {
        // ...
        world.spawnEnemy();
        world.enemyShoot();
        // another functionalities that depend on time
    }

    instead of this:

    Code :
    idle
    {
       // ...
       // logic of spawnEnemy directly here in the idle function
       // logic of enemyShoot directly here in the idle function
       // logic of another functionalities that depend on time directly here in the idle function
    }

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,516
    The simplest solution is to use glutTimerFunc().

    The other approach, if you know that the idle callback will be invoked regularly, is to implement your own timers there. The main thing is that if no actions are ready to be executed, just return from the idle function; don't busy-wait until something is ready. It's entirely possible that the value returned by glutGet(GLUT_ELAPSED_TIME) is only updated by the event loop; if that's the case, busy-waiting on it within the idle callback will wait for ever.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •