Added FPS cap, added Optimus support, and removed showWindow requirement

This commit is contained in:
Grayson Riffe (Laptop) 2021-08-16 14:07:20 -05:00
parent 2eb8d55a1c
commit efeec66fad
5 changed files with 85 additions and 35 deletions

View File

@ -11,7 +11,6 @@ int main(int argc, char* argv[]) {
//app.setWindowIcon(...); //app.setWindowIcon(...);
// app.setWindowCursor(...); // app.setWindowCursor(...);
//Create game states, load some assets, launch loop that continually updates and renders the current state and switches states when appropriate. //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(); app.startLoop();
return 0; return 0;

View File

@ -5,6 +5,10 @@
#include "GL\wglew.h" #include "GL\wglew.h"
#endif #endif
extern "C" {
_declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
}
namespace nf { namespace nf {
DEBUGINIT; 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); 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); SetProp(m_window, L"App", this);
if(m_currentConfig.fullscreen) toggleFullscreen(); if (m_currentConfig.fullscreen) toggleFullscreen();
createOpenGLContext(); createOpenGLContext();
} }
@ -37,10 +41,16 @@ namespace nf {
} }
void Application::startLoop() { void Application::startLoop() {
showWindow(true);
m_running = true; m_running = true;
MSG msg = { }; MSG msg = { };
while (m_running) { while (m_running) {
//TODO: FPS and delta timing //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)) { while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
@ -50,8 +60,18 @@ namespace nf {
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
SwapBuffers(m_hdc); SwapBuffers(m_hdc);
m_frames++;
m_frameClock = std::chrono::steady_clock::now();
//TODO: Update and render current state //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;
}
}
} }
void Application::showWindow(bool show) { void Application::showWindow(bool show) {
@ -65,6 +85,10 @@ namespace nf {
return m_currentConfig; return m_currentConfig;
} }
int Application::getFPS() {
return m_FPS;
}
void Application::registerWindowClass() { void Application::registerWindowClass() {
m_wclassName = L"NFClass"; m_wclassName = L"NFClass";
WNDCLASS wclass = { }; WNDCLASS wclass = { };
@ -172,7 +196,7 @@ namespace nf {
wglDeleteContext(m_hglrc); wglDeleteContext(m_hglrc);
m_hglrc = wglCreateContextAttribsARB(m_hdc, NULL, attrib); m_hglrc = wglCreateContextAttribsARB(m_hdc, NULL, attrib);
wglMakeCurrent(m_hdc, m_hglrc); wglMakeCurrent(m_hdc, m_hglrc);
Log((char*)glGetString(GL_VERSION)); Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION)));
GLuint vao; GLuint vao;
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &vao);
glBindVertexArray(vao); glBindVertexArray(vao);

View File

@ -16,6 +16,16 @@ namespace nf {
std::printf("[%.4f] Debug: %s\n", time.count(), in.c_str()); std::printf("[%.4f] Debug: %s\n", time.count(), in.c_str());
} }
void Debug::LogImp(int in) {
std::chrono::duration<float> time = getCurrentTime();
std::printf("[%.4f] Debug: %i\n", time.count(), in);
}
void Debug::LogImp(double in) {
std::chrono::duration<float> time = getCurrentTime();
std::printf("[%.4f] Debug: %.4f\n", time.count(), in);
}
void Debug::ErrorImp(const char* in, const char* filename, int line) { void Debug::ErrorImp(const char* in, const char* filename, int line) {
std::chrono::duration<float> time = getCurrentTime(); std::chrono::duration<float> time = getCurrentTime();
HANDLE cmd = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE cmd = GetStdHandle(STD_OUTPUT_HANDLE);

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include "Config.h" #include "Config.h"
#include <Windows.h> #include <Windows.h>
#include <chrono>
#include <vector>
namespace nf { namespace nf {
class Application { class Application {
@ -14,6 +16,7 @@ namespace nf {
void startLoop(); void startLoop();
void showWindow(bool show); void showWindow(bool show);
Config& getConfig(); Config& getConfig();
int getFPS();
~Application(); ~Application();
private: private:
@ -33,5 +36,17 @@ namespace nf {
WINDOWPLACEMENT m_wndPlacement; WINDOWPLACEMENT m_wndPlacement;
HDC m_hdc; HDC m_hdc;
HGLRC m_hglrc; HGLRC m_hglrc;
std::chrono::steady_clock::time_point m_frameClock = std::chrono::steady_clock::now();
std::chrono::duration<double> 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;
}; };
} }

View File

@ -21,6 +21,8 @@ DebugBreak();
public: public:
static void LogImp(const char* in); static void LogImp(const char* in);
static void LogImp(const std::string& 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); static void ErrorImp(const char* in, const char* filename, int line);
}; };
#else #else