Added FPS cap, added Optimus support, and removed showWindow requirement
This commit is contained in:
parent
2eb8d55a1c
commit
efeec66fad
@ -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;
|
||||||
|
@ -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,20 +41,36 @@ 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
|
||||||
while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
|
|
||||||
TranslateMessage(&msg);
|
m_fpsDuration = std::chrono::steady_clock::now() - m_frameClock;
|
||||||
DispatchMessage(&msg);
|
|
||||||
if (msg.message == WM_QUIT)
|
if (m_fpsDuration.count() >= m_minFrametime) {
|
||||||
m_running = false;
|
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;
|
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 = { };
|
||||||
@ -107,30 +131,30 @@ namespace nf {
|
|||||||
LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
||||||
Application* app = (Application*)GetProp(hWnd, L"App");
|
Application* app = (Application*)GetProp(hWnd, L"App");
|
||||||
switch (uMsg) {
|
switch (uMsg) {
|
||||||
case WM_CREATE: {
|
case WM_CREATE: {
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
case WM_SYSKEYDOWN: {
|
||||||
|
if (GetKeyState(VK_RETURN) & 0x8000) {
|
||||||
|
app->toggleFullscreen();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
case WM_SYSKEYDOWN: {
|
break;
|
||||||
if (GetKeyState(VK_RETURN) & 0x8000) {
|
}
|
||||||
app->toggleFullscreen();
|
case WM_MENUCHAR: {
|
||||||
return 0;
|
return MNC_CLOSE << 16;
|
||||||
}
|
}
|
||||||
break;
|
case WM_CLOSE: {
|
||||||
}
|
//State onExit() in order
|
||||||
case WM_MENUCHAR: {
|
//unload anything else
|
||||||
return MNC_CLOSE << 16;
|
DestroyWindow(hWnd);
|
||||||
}
|
return 0;
|
||||||
case WM_CLOSE: {
|
}
|
||||||
//State onExit() in order
|
case WM_DESTROY: {
|
||||||
//unload anything else
|
PostQuitMessage(0);
|
||||||
DestroyWindow(hWnd);
|
return 0;
|
||||||
return 0;
|
}
|
||||||
}
|
|
||||||
case WM_DESTROY: {
|
|
||||||
PostQuitMessage(0);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return DefWindowProc(hWnd, uMsg, wParam, lParam);//TODO: Fill out events
|
return DefWindowProc(hWnd, uMsg, wParam, lParam);//TODO: Fill out events
|
||||||
}
|
}
|
||||||
@ -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);
|
||||||
|
@ -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);
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -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
|
||||||
|
Reference in New Issue
Block a user