Divided engine into two threads

This commit is contained in:
Grayson Riffe (Desktop) 2021-08-18 22:23:01 -05:00
parent 61768b00f9
commit 5bfcb1f163
2 changed files with 35 additions and 29 deletions

View File

@ -23,7 +23,6 @@ namespace nf {
m_defaultWindowStyle = GetWindowLong(m_window, GWL_STYLE); m_defaultWindowStyle = GetWindowLong(m_window, GWL_STYLE);
SetProp(m_window, L"App", this); SetProp(m_window, L"App", this);
if (m_currentConfig.fullscreen) toggleFullscreen(); if (m_currentConfig.fullscreen) toggleFullscreen();
createOpenGLContext();
} }
void Application::setWindowIcon(HANDLE hIcon) { void Application::setWindowIcon(HANDLE hIcon) {
@ -75,35 +74,18 @@ namespace nf {
showWindow(true); showWindow(true);
m_running = true; m_running = true;
MSG msg = { }; MSG msg = { };
std::thread mainThread(&Application::startMainThread, this);
while (m_running) { while (m_running) {
m_fpsDuration = std::chrono::steady_clock::now() - m_frameClock; while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
if (m_fpsDuration.count() >= m_minFrametime) { TranslateMessage(&msg);
m_deltaTime = m_fpsDuration.count(); DispatchMessage(&msg);
while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { if (msg.message == WM_QUIT) {
TranslateMessage(&msg); m_running = false;
DispatchMessage(&msg); break;
if (msg.message == WM_QUIT) {
m_running = false;
goto FrameEnd;
}
} }
glClear(GL_COLOR_BUFFER_BIT);
m_currentState->update();
m_currentState->render();
SwapBuffers(m_hdc);
m_frames++;
m_frameClock = std::chrono::steady_clock::now();
//TODO: Update and render current state
} }
m_fpsClock2 = std::chrono::steady_clock::now();
m_fpsDuration = m_fpsClock2 - m_fpsClock1;
if (m_fpsDuration.count() >= 1.0) {
m_fpsClock1 = std::chrono::steady_clock::now();
m_FPS = m_frames;
m_frames = 0;
}
FrameEnd:;
} }
mainThread.join();
} }
void Application::showWindow(bool show) { void Application::showWindow(bool show) {
@ -127,6 +109,32 @@ namespace nf {
m_currentState = m_sIntro; m_currentState = m_sIntro;
} }
void Application::startMainThread() {
createOpenGLContext();
while (m_running) {
m_fpsDuration = std::chrono::steady_clock::now() - m_frameClock;
if (m_fpsDuration.count() >= m_minFrametime) {
m_deltaTime = m_fpsDuration.count();
glClear(GL_COLOR_BUFFER_BIT);
m_currentState->update();
m_currentState->render();
SwapBuffers(m_hdc);
m_frames++;
m_frameClock = std::chrono::steady_clock::now();
}
m_fpsClock2 = std::chrono::steady_clock::now();
m_fpsDuration = m_fpsClock2 - m_fpsClock1;
if (m_fpsDuration.count() >= 1.0) {
m_fpsClock1 = std::chrono::steady_clock::now();
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
m_FPS = m_frames;
m_frames = 0;
Log((int)glGetError());
}
}
m_currentState->onExit();
}
void Application::registerWindowClass() { void Application::registerWindowClass() {
if (!FindWindow(L"NFClass", NULL)) { if (!FindWindow(L"NFClass", NULL)) {
m_wclassName = L"NFClass"; m_wclassName = L"NFClass";
@ -193,9 +201,6 @@ namespace nf {
return 0; return 0;
} }
case WM_DESTROY: { case WM_DESTROY: {
app->m_currentState->onExit();
app->m_currentState = nullptr;
//Unload anything else
PostQuitMessage(0); PostQuitMessage(0);
return 0; return 0;
} }

View File

@ -27,6 +27,7 @@ namespace nf {
~Application(); ~Application();
private: private:
void addIntroState(); void addIntroState();
void startMainThread();
void registerWindowClass(); void registerWindowClass();
void toggleFullscreen(); void toggleFullscreen();
RECT getWindowRect() const; RECT getWindowRect() const;