c++ - 控制帧率

标签 c++ winapi opengl glut frame-rate

我正在 VS 上使用 C++,使用 OpenGL 渲染/移动形状,使用 Win32 进行窗口显示等(从过剩显示中移出)

如何控制帧率?

我看到很多使用 dt 和某种形式的帧刷新的例子,但我不确定如何实现这个...有什么东西可以作为 Win32 的一部分使用吗?有没有更简单的方法?

此外,这可能是一个愚蠢的问题,但是,如果什么都没有实现,那么默认帧速率是多少?还是没有?

最佳答案

我的 USB 驱动器上有一些非常旧的代码,它们使用旧的 OpenGL 和过剩,相同的计时原则甚至适用于更现代的版本,但绘制代码会有所不同。该代码用于不精确的计时,但足以说明如何粗略地实现设定的 FPS:

// throttle the drawing rate to a fixed FPS
//compile with: g++ yourfilenamehere.cpp -lGL -lglut
#include <cstdlib>
#include <iostream>

#include <GL/gl.h>
#include <GL/glut.h>

GLint FPS = 0;

void FPS(void) {
  static GLint frameCounter = 0;         // frames averaged over 1000mS
  static GLuint currentClock;             // [milliSeconds]
  static GLuint previousClock = 0; // [milliSeconds]
  static GLuint nextClock = 0;     // [milliSeconds]

  ++frameCounter;
  currentClock = glutGet(GLUT_ELAPSED_TIME); //has limited resolution, so average over 1000mS
  if ( currentClock < nextClock ) return;

  FPS = frameCounter/1; // store the averaged number of frames per second

  previousClock = currentClock;
  nextClock = currentClock+1000; // set the next clock to aim for as 1 second in the future (1000 ms)
  frameCounter=0;
}

void idle() {
  static GLuint previousClock=glutGet(GLUT_ELAPSED_TIME);
  static GLuint currentClock=glutGet(GLUT_ELAPSED_TIME);
  static GLfloat deltaT;

  currentClock = glutGet(GLUT_ELAPSED_TIME);
  deltaT=currentClock-previousClock;
  if (deltaT < 35) {return;} else {previousClock=currentClock;}

  // put your idle code here, and it will run at the designated fps (or as close as the machine can get

  printf(".");
  //end your idle code here

  FPS(); //only call once per frame loop 
  glutPostRedisplay();
}

void display() {
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glClear(GL_COLOR_BUFFER_BIT);

  // Set the drawing color (RGB: WHITE)
  printf("FPS %d\n",FPS);
  glColor3f(1.0,1.0,1.0);

  glBegin(GL_LINE_STRIP); {
     glVertex3f(0.25,0.25,0.0);
     glVertex3f(0.75,0.25,0.0);
     glVertex3f(0.75,0.75,0.0);
     glVertex3f(0.25,0.75,0.0);
     glVertex3f(0.25,0.25,0.0);
  }
  glEnd(); 

  glutSwapBuffers();
}

void init() {
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0); 
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:  // escape key
         exit(0);
         break;
      default:
         break;
   }
}

int main(int argc, char** argv) {
   glutInit(&amp;argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
   glutCreateWindow("FPS test");

   glutIdleFunc(idle);
   glutDisplayFunc(display);
   glutKeyboardFunc(keyboard);

   init();

   glutMainLoop();
   return 0;
}

希望这对您有所帮助 :) 如果您需要更多信息,请告诉我。

关于c++ - 控制帧率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20217776/

相关文章:

c++ - 我正在尝试创建类来封装工具栏,但背景变黑了。由 C++ win32api

c++ - 调用DrawText时DT_NOFULLWIDTHCHARBREAK有什么作用?

OpenGL:为计算着色器使用多个全局工作组有好处吗

c# - P/invoke 函数采用指向结构的指针

c++ - GLSL 构建错误

c++ - OpenGL 代码不会显示任何内容

c++ - 使用 Unicode 字符串文字 Stringify 宏

c++ - 在 Windows 中使用 C++ 将 Unicode 输出到控制台

c++ - 创建一个结构数组但稍后定义大小?

c++ - Boost 无法打开文件, 'libboost_filesystem-vc100-mt-gd-1_47.lib'