Created
March 24, 2013 16:24
-
-
Save tmatth/5232538 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple GNU/Linux OpenGL framework for implementing: | |
// http://gafferongames.com/game-physics/fix-your-timestep/ | |
// | |
// Instructions: | |
// 1) Add the following to Timestep.cpp: | |
// #include "Linux.h" | |
// | |
// 2) Compile with: | |
// g++ -O2 -g -D__LINUX__ Timestep.cpp -o timestep -lglut -lGL -lGLU -lrt | |
#ifdef __LINUX__ | |
#ifndef LINUX_GUARD_H_ | |
#define LINUX_GUARD_H_ | |
#include <GL/freeglut.h> | |
#include <ctime> | |
void keyPressed(unsigned char key, int x, int y) | |
{ | |
if (key == 27) | |
onQuit(); | |
} | |
int window; | |
bool openDisplay(const char title[], int width, int height) | |
{ | |
// create window | |
// keep glut happy | |
static int argc = 1; | |
glutInit(&argc, NULL); | |
glutInitDisplayMode(GLUT_DOUBLE); | |
glutInitWindowSize(width, height); | |
window = glutCreateWindow(title); | |
// set the kpress callback, must be done | |
// AFTER the window has been created | |
glutKeyboardFunc(keyPressed); | |
return true; | |
} | |
void updateDisplay() | |
{ | |
// process pending events | |
glutMainLoopEvent(); | |
// Flush the OpenGL buffers to the window | |
glutSwapBuffers(); | |
} | |
void closeDisplay() | |
{ | |
glutDestroyWindow(window); | |
window = 0; | |
} | |
float time() | |
{ | |
static timespec start = {0, 0}; | |
if (start.tv_sec == 0 and start.tv_nsec == 0) { | |
clock_gettime(CLOCK_MONOTONIC, &start); | |
return 0.0f; | |
} | |
timespec counter = {0, 0}; | |
clock_gettime(CLOCK_MONOTONIC, &counter); | |
return (counter.tv_sec - start.tv_sec) + | |
((counter.tv_nsec - start.tv_nsec) / 1e9f); | |
} | |
#endif // LINUX_GUARD_H_ | |
#endif // __LINUX__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment