0

I am working on a basic tetris game for school. I am not sure how can i develop a game loop since this is my first time making a game.

I am using opengl to do the drawing. Do I make a main loop that will wait certain amount of time before it redrawn the scene?

LittleFunny
  • 8,155
  • 15
  • 87
  • 198
  • Most tutorials on beginning game development explain exactly how to write the game loop so that it handles timing issues. – Mark Hildreth Nov 05 '13 at 21:52

1 Answers1

1

Here's a basic (very high level pseudo-code) game loop:

void RunGameLoop()
{
  // record the frame time and calculate the time since the last frame
  float timeTemp = time();
  float deltaTime = timeTemp - lastFrameTime;
  lastFrameTime = timeTemp;

  // iterate through all units and let them update
  unitManager->update(deltaTime);

  // iterate through all units and let them draw
  drawManager->draw();
}

The purpose of passing deltaTime (the time since the last frame in seconds) to unitManager->update() is so that when the units are updating, they can multiply their movement by deltaTime so their values can be in units per second.

abstract class Unit
{
  public:
  abstract void update(float deltaTime);
}

FallingBlockUnit::update(float deltaTime)
{
  moveDown(fallSpeed * deltaTime);
}

The draw manager is going to be responsible for managing the draw buffers (I suggest double buffering to prevent screen flicker)

DrawManager::draw()
{
  // set the back buffer to a blank color
  backBuffer->clear();

  // draw all units here

  // limit the frame rate by sleeping until the next frame should be drawn
  // const float frameDuration = 1.0f / framesPerSecond;
  float sleepTime = lastDrawTime + frameDuration - time();
  sleep(sleepTime);
  lastDrawTime = time();

  // swap the back buffer to the front
  frontBuffer->draw(backBuffer);
}

For further research, here's a book that my Game Programming professor wrote about 2d game programming. http://www.amazon.com/Graphics-Programming-Games-John-Pile/dp/1466501898

Justin Ryder
  • 757
  • 9
  • 17
  • Will the animation be differ in different on different speed of computer – LittleFunny Nov 05 '13 at 23:58
  • 1
    Yes it will. I've added code for frame rate limiting in the draw manager. Ideally you wouldn't just sleep, you could start calculating the next frame, but for a basic game sleeping will be just fine. – Justin Ryder Nov 06 '13 at 15:00
  • I revisit my question. Why need to recalculate the next frame instead of sleep – LittleFunny Mar 06 '14 at 04:36
  • It's not necessary to start calculating the next frame ahead of time, but it can come in handy for large, complex game engines. You can never be sure exactly how long a frame will take to compute and render and any part of your game could take longer than expected for any number of reasons (more than usual number of objects on screen, lots of ai being run, extra shaders running, etc). If you compute a frame or two ahead of what you're displaying, then you have a buffer that will compensate for unexpected lag, because you have an extra frame or two's time to finish computing the lagging frame. – Justin Ryder Mar 13 '14 at 18:44