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.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;

View File

@ -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);

View File

@ -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<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) {
std::chrono::duration<float> time = getCurrentTime();
HANDLE cmd = GetStdHandle(STD_OUTPUT_HANDLE);

View File

@ -1,6 +1,8 @@
#pragma once
#include "Config.h"
#include <Windows.h>
#include <chrono>
#include <vector>
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<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:
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