Opengl:如何实现基于时间的俄罗斯方块游戏?

Opengl:如何实现基于时间的俄罗斯方块游戏?,opengl,game-loop,tetris,Opengl,Game Loop,Tetris,我正在为学校做一个基本的俄罗斯方块游戏。由于这是我第一次制作游戏,我不确定如何开发游戏循环 我正在用opengl来画图。我是否制作了一个主循环,在它重新绘制场景之前会等待一定的时间 下面是一个基本(非常高级的伪代码)游戏循环: void RunGameLoop() { // record the frame time and calculate the time since the last frame float timeTemp = time(); float deltaTime

我正在为学校做一个基本的俄罗斯方块游戏。由于这是我第一次制作游戏,我不确定如何开发游戏循环

我正在用opengl来画图。我是否制作了一个主循环,在它重新绘制场景之前会等待一定的时间

下面是一个基本(非常高级的伪代码)游戏循环:

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();
}
将deltaTime(从最后一帧开始的时间,以秒为单位)传递给unitManager->update()的目的是,当单位更新时,它们可以将移动乘以deltaTime,这样它们的值可以以每秒单位为单位

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

FallingBlockUnit::update(float deltaTime)
{
  moveDown(fallSpeed * deltaTime);
}
绘图管理器将负责管理绘图缓冲区(我建议使用双缓冲区以防止屏幕闪烁)

为了进一步研究,我的游戏编程教授写了一本关于2d游戏编程的书。

大多数关于游戏开发开始的教程都精确地解释了如何编写游戏循环,以便它处理计时问题。动画在不同的计算机速度下是否会有所不同是的。我在draw manager中添加了帧速率限制代码。理想情况下,你不会只是睡觉,你可以开始计算下一帧,但是对于一个基本的游戏,睡觉就可以了。为什么需要重新计算下一帧而不是休眠?没有必要提前开始计算下一帧,但是对于大型复杂的游戏引擎来说,它可以派上用场。你永远无法确定计算和渲染一帧需要多长时间,游戏的任何部分都可能需要比预期更长的时间,原因有很多(屏幕上的对象数量、运行大量ai、运行额外的着色器等等)。如果您在显示之前计算一到两帧,那么您有一个缓冲区来补偿意外的延迟,因为您还有一到两帧的时间来完成滞后帧的计算。
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);
}