TEST_F(FpsTest, update) { FPS fps; fps.next_ticks = SDL_GetTicks() + 1000; for (int i = 0; i < 9; ++i) { SDL_Delay(100); fps.update(); EXPECT_EQ(static_cast<uint32_t>(i + 1), fps.frames); EXPECT_EQ(0U, fps.latest_frames); } SDL_Delay(100 + 50); fps.update(); EXPECT_EQ(0U, fps.frames); EXPECT_EQ(10U, fps.latest_frames); }
void Game::loop() { if (!running) { running = true; FPS fps; try { while (running) { float delta = (float)fps.update(); update(delta); render(); } } catch (InternalException &e) { std::cout << "warning: " << e.what() << "\n"; } catch (...) { throw; } } }
GLvoid render(){ fps.measure(); camera.recalculate(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt( camera.pos[0], camera.pos[1], camera.pos[2], camera.lookAt[0], camera.lookAt[1], camera.lookAt[2], 0.0f, 1.0f, 0.0f ); glEnable(GL_DEPTH_TEST); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glVertexPointer(3, GL_FLOAT, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glColorPointer(3, GL_FLOAT, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); glNormalPointer(GL_FLOAT, 0, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, &indices); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glDisable(GL_DEPTH_TEST); glutSwapBuffers(); fps.setLastFrameNow(); if(fps.timeToPrintFPS()){ //cout << fps; fps.setLastPrintNow(); } }
void Mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON) { if (state == GLUT_DOWN) { mouseX = x; mouseY = y; mouseState = 1; } else { mouseState = 0; } } else if (button == GLUT_RIGHT_BUTTON) { if (state == GLUT_DOWN) { //printf("(%0.2f, %.02f, %.02f) (%.02f, %.02f, %.02f)\n", tx, ty, tz, ax, ay, az); printf("rfps=%lf, sfps=%lf, load=%lf\n", fps.GetRenderFPS(), fps.GetSkipedFPS(), fps.GetLoad()); } } }
GLvoid init(int* argc, char** argv){ //Glut glutInit(argc, argv); glutInitWindowPosition(100, 100); glutInitWindowSize(500, 500); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Arne Schreuder OpenGLF"); //Glew //printGLInfo(); GLenum error = glewInit(); if(error != GLEW_OK){ printf("Glew error: %s\n", glewGetErrorString(error)); return; } //Shader program initShaderProgram(vsFile, fsFile); glGenBuffers(3, &vbo[0]); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW); fps.start(); //Callbacks glutDisplayFunc(render); glutReshapeFunc(reshape); glutIdleFunc(idle); glutKeyboardFunc(keyboardNormalPressed); glutKeyboardUpFunc(keyboardNormalReleased); glutMouseFunc(mousePress); glutPassiveMotionFunc(mousePassiveMotion); //Mainloop glutMainLoop(); }
GLvoid idle(){ if(fps.capFPS()){ glutPostRedisplay(); } }
int main( int argc, char* args[]) { //player Player myPlayer; //fps FPS fps; //init if(init() == false) { return 1; } //load if(loadFiles() != false) { loadLevel(lvlScene); } else { return 1; } //while no quit while(quit == false) { //start timer fps.startFPS(); while(SDL_PollEvent(&event)) { //player event myPlayer.getInputPlayer(); //x out window if(event.type == SDL_QUIT) { quit = true; } } //lvlupdate updateLevel(); //player controls myPlayer.movePlayer(); myPlayer.showPlayer(pAlpha); //SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 0x77, 0x77, 0x77 ) ); //nextlvl /*if(nextlvl == true) { cleanUp(); nextlvl == false; }*/ //update if(SDL_Flip(screen) == -1) { return 1; } //cap if(fps.getT() < 1000 / fpsNum) { SDL_Delay((1000 / fpsNum) - fps.getT()); } } cleanUp(); return 0; }
void Idle(void) { if (fps.Step() != FPS::stateRender) { return; } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Load identity matrix glLoadIdentity(); // Multiply in translation matrix glTranslatef(tx, ty, tz); // Multiply in rotation matrix glRotatef(ax, 1, 0, 0); glRotatef(ay, 0, 1, 0); glRotatef(az, 0, 0, 1); #ifdef __TEST_2D__ SetTo2D(); glBegin(GL_QUADS); glColor3f(1.0, 0.0f, 0.0f); glVertex3f(100, 200, 0); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(200, 200, 0); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(200, 100, 0); glColor3f(1.0f, 1.0f, 0.0f); glVertex3f(100, 100, 0); glColor3f(1.0, 0.0f, 0.0f); glVertex3f(100, 200, -100); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(200, 200, -100); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(200, 100, -100); glColor3f(1.0f, 1.0f, 0.0f); glVertex3f(100, 100, -100); glEnd(); if (perspective) { SetTo3D(); } #endif // Render colored quad if (perspective) { DrawGrid(10, 1); glBegin(GL_QUADS); glColor3f(1.0, 0.0f, 0.0f); glVertex2f(-1, 1); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(1, 1); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(1, -1); glColor3f(1.0f, 1.0f, 0.0f); glVertex2f(-1, -1); glEnd(); DrawPoints(fps.GetThisCycleFrameCount()); } else { int x = width < height ? height : width; x = x / 60 * 60; DrawGrid((float)(x/2), 30); glBegin(GL_QUADS); glColor3f(1.0, 0.0f, 0.0f); glVertex2f(-50, 50); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(50, 50); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(50, -50); glColor3f(1.0f, 1.0f, 0.0f); glVertex2f(-50, -50); glEnd(); } // Swap buffers (color buffers, makes previous render visible) glutSwapBuffers(); if (!stopRotate) { ay += 1.0f; } }