#include #include #ifdef __linux__ #include #endif #ifdef _WIN32 #include #endif #include "Controller.h" Controller::Controller() { if(!glfwInit()) exit(-1); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwSetErrorCallback(error_callback); gameWindow = new Window(); gameEventHandler = new EventHandler(); } Controller::~Controller() { delete gameWindow; delete gameEventHandler; glfwTerminate(); } void Controller::run() { bool running = 1; while(running) { // Timing limit_framerate(); std::cout << 1/deltaTime << std::endl; // Update game // Render and buffer swap glfwSwapBuffers(gameWindow->getGLFWwindow()); // Check events, handle input gameEventHandler->handleEvents(gameWindow->getGLFWwindow()); if(gameEventHandler->gameShouldTerminate) running = 0; } } void Controller::error_callback(int error, const char* description) { (void)error; fprintf(stderr, "Error: %s\n", description); } void Controller::limit_framerate() { static double startingTime = 0.0; static double lastTime = 0.0; lastTime = glfwGetTime() - startingTime; double frameTime = 1/(double)MAX_FPS; if(frameTime > lastTime) { #ifdef __linux__ usleep((frameTime - lastTime) * 1000000); #endif #ifdef _WIN32 Sleep((frameTime - lastTime) * 1000); #endif } deltaTime = glfwGetTime() - startingTime; startingTime = glfwGetTime(); }