From efeec66fadb7e0a6ef81951617c82465710804f3 Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Laptop)" Date: Mon, 16 Aug 2021 14:07:20 -0500 Subject: [PATCH] Added FPS cap, added Optimus support, and removed showWindow requirement --- Game/src/Game.cpp | 1 - NothinFancy/src/Application.cpp | 92 +++++++++++++++++---------- NothinFancy/src/Utility.cpp | 10 +++ NothinFancy/src/include/Application.h | 15 +++++ NothinFancy/src/include/Utility.h | 2 + 5 files changed, 85 insertions(+), 35 deletions(-) diff --git a/Game/src/Game.cpp b/Game/src/Game.cpp index c9bb61c..4065de6 100644 --- a/Game/src/Game.cpp +++ b/Game/src/Game.cpp @@ -11,7 +11,6 @@ int main(int argc, char* argv[]) { //app.setWindowIcon(...); // app.setWindowCursor(...); //Create game states, load some assets, launch loop that continually updates and renders the current state and switches states when appropriate. - app.showWindow(true); app.startLoop(); return 0; diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index 8faca0e..b6a7d2b 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -5,6 +5,10 @@ #include "GL\wglew.h" #endif +extern "C" { + _declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; +} + namespace nf { DEBUGINIT; @@ -22,7 +26,7 @@ namespace nf { m_window = CreateWindowEx(NULL, m_wclassName, toWide(m_currentConfig.title), WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 1280, windowSize.bottom, NULL, NULL, m_hInst, NULL); SetProp(m_window, L"App", this); - if(m_currentConfig.fullscreen) toggleFullscreen(); + if (m_currentConfig.fullscreen) toggleFullscreen(); createOpenGLContext(); } @@ -37,20 +41,36 @@ namespace nf { } void Application::startLoop() { + showWindow(true); m_running = true; MSG msg = { }; while (m_running) { - //TODO: FPS and delta timing - while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { - TranslateMessage(&msg); - DispatchMessage(&msg); - if (msg.message == WM_QUIT) - m_running = false; + //TODO: delta timing + + m_fpsDuration = std::chrono::steady_clock::now() - m_frameClock; + + if (m_fpsDuration.count() >= m_minFrametime) { + m_deltaTime = m_fpsDuration.count(); + while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + if (msg.message == WM_QUIT) + m_running = false; + } + glClearColor(1.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + 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; } - glClearColor(1.0f, 0.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - SwapBuffers(m_hdc); - //TODO: Update and render current state } } @@ -65,6 +85,10 @@ namespace nf { return m_currentConfig; } + int Application::getFPS() { + return m_FPS; + } + void Application::registerWindowClass() { m_wclassName = L"NFClass"; WNDCLASS wclass = { }; @@ -107,30 +131,30 @@ namespace nf { LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { Application* app = (Application*)GetProp(hWnd, L"App"); switch (uMsg) { - case WM_CREATE: { - - return 0; - } - case WM_SYSKEYDOWN: { - if (GetKeyState(VK_RETURN) & 0x8000) { - app->toggleFullscreen(); - return 0; - } - break; - } - case WM_MENUCHAR: { - return MNC_CLOSE << 16; - } - case WM_CLOSE: { - //State onExit() in order - //unload anything else - DestroyWindow(hWnd); - return 0; - } - case WM_DESTROY: { - PostQuitMessage(0); + case WM_CREATE: { + + return 0; + } + case WM_SYSKEYDOWN: { + if (GetKeyState(VK_RETURN) & 0x8000) { + app->toggleFullscreen(); return 0; } + break; + } + case WM_MENUCHAR: { + return MNC_CLOSE << 16; + } + case WM_CLOSE: { + //State onExit() in order + //unload anything else + DestroyWindow(hWnd); + return 0; + } + case WM_DESTROY: { + PostQuitMessage(0); + return 0; + } } return DefWindowProc(hWnd, uMsg, wParam, lParam);//TODO: Fill out events } @@ -172,7 +196,7 @@ namespace nf { wglDeleteContext(m_hglrc); m_hglrc = wglCreateContextAttribsARB(m_hdc, NULL, attrib); wglMakeCurrent(m_hdc, m_hglrc); - Log((char*)glGetString(GL_VERSION)); + Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION))); GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); diff --git a/NothinFancy/src/Utility.cpp b/NothinFancy/src/Utility.cpp index a100c50..f98b7bd 100644 --- a/NothinFancy/src/Utility.cpp +++ b/NothinFancy/src/Utility.cpp @@ -16,6 +16,16 @@ namespace nf { std::printf("[%.4f] Debug: %s\n", time.count(), in.c_str()); } + void Debug::LogImp(int in) { + std::chrono::duration time = getCurrentTime(); + std::printf("[%.4f] Debug: %i\n", time.count(), in); + } + + void Debug::LogImp(double in) { + std::chrono::duration time = getCurrentTime(); + std::printf("[%.4f] Debug: %.4f\n", time.count(), in); + } + void Debug::ErrorImp(const char* in, const char* filename, int line) { std::chrono::duration time = getCurrentTime(); HANDLE cmd = GetStdHandle(STD_OUTPUT_HANDLE); diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index a286ae8..f0554cf 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -1,6 +1,8 @@ #pragma once #include "Config.h" #include +#include +#include namespace nf { class Application { @@ -14,6 +16,7 @@ namespace nf { void startLoop(); void showWindow(bool show); Config& getConfig(); + int getFPS(); ~Application(); private: @@ -33,5 +36,17 @@ namespace nf { WINDOWPLACEMENT m_wndPlacement; HDC m_hdc; HGLRC m_hglrc; + + std::chrono::steady_clock::time_point m_frameClock = std::chrono::steady_clock::now(); + std::chrono::duration m_fpsDuration; + double m_deltaTime; + std::chrono::steady_clock::time_point m_fpsClock1 = m_frameClock; + std::chrono::steady_clock::time_point m_fpsClock2 = m_frameClock; + int m_frames; + const int m_targetFPS = 60; + const double m_minFrametime = 1.0 / m_targetFPS; + int m_FPS; + + }; } \ No newline at end of file diff --git a/NothinFancy/src/include/Utility.h b/NothinFancy/src/include/Utility.h index ec66379..2a610c8 100644 --- a/NothinFancy/src/include/Utility.h +++ b/NothinFancy/src/include/Utility.h @@ -21,6 +21,8 @@ DebugBreak(); public: static void LogImp(const char* in); static void LogImp(const std::string& in); + static void LogImp(int in); + static void LogImp(double in); static void ErrorImp(const char* in, const char* filename, int line); }; #else