Commit 0267a3df authored by Philip Trettner's avatar Philip Trettner

modularized main loop

parent 9c9bdc80
......@@ -7,9 +7,10 @@
#include <cassert>
#include <chrono>
#include <iostream>
#include <thread>
#include <chrono>
#include <glow/gl.hh>
#include <GLFW/glfw3.h>
......@@ -322,28 +323,7 @@ void GlfwApp::mainLoop()
double cpuTime = 0;
while (!shouldClose())
{
// update cursor mode
switch (mCursorMode)
{
case CursorMode::Normal:
glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
break;
case CursorMode::Hidden:
glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
break;
case CursorMode::Disabled:
glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
break;
}
// vsync
glfwSwapInterval(mVSync ? 1 : 0);
// Poll for and process events
glfwPollEvents();
// viewport
glViewport(0, 0, mWindowWidth, mWindowHeight);
updateInput();
// Update
auto maxTicks = mMaxFrameSkip;
......@@ -364,6 +344,8 @@ void GlfwApp::mainLoop()
mCurrentTime += dt;
}
beginRender();
// Render here
{
if (mQueryStats && mPrimitiveQuery)
......@@ -384,12 +366,7 @@ void GlfwApp::mainLoop()
}
}
// draw the tweak bar(s)
if (mDrawTweakbars)
TwDraw();
// Swap front and back buffers
glfwSwapBuffers(mWindow);
endRender();
// timing
auto now = glfwGetTime();
......@@ -442,6 +419,50 @@ void GlfwApp::internalOnMouseButton(double x, double y, int button, int action,
onMouseButton(x, y, button, action, mods, mClickCount);
}
void GlfwApp::updateInput()
{
// update cursor mode
switch (mCursorMode)
{
case CursorMode::Normal:
glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
break;
case CursorMode::Hidden:
glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
break;
case CursorMode::Disabled:
glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
break;
}
// Poll for and process events
glfwPollEvents();
}
void GlfwApp::beginRender()
{
// vsync
glfwSwapInterval(mVSync ? 1 : 0);
// viewport
glViewport(0, 0, mWindowWidth, mWindowHeight);
}
void GlfwApp::endRender()
{
// draw the tweak bar(s)
if (mDrawTweakbars)
TwDraw();
// Swap front and back buffers
glfwSwapBuffers(mWindow);
}
void GlfwApp::sleepSeconds(double seconds) const
{
std::this_thread::sleep_for(std::chrono::milliseconds((int)(seconds * 1000)));
}
int GlfwApp::run(int argc, char *argv[])
{
static GlfwApp *currApp = nullptr;
......
......@@ -228,6 +228,19 @@ protected:
private:
void internalOnMouseButton(double x, double y, int button, int action, int mods);
protected:
/// performs glfw polling
void updateInput();
/// should be called before rendering
void beginRender();
/// should be called after rendering
/// calls swapBuffers
void endRender();
/// Blocks the thread for a given number of milliseconds
void sleepSeconds(double seconds) const;
public:
/// Initializes GLFW and GLOW, and runs until window is closed
int run(int argc, char* argv[]);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment