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.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;
|
||||
|
@ -5,6 +5,10 @@
|
||||
#include "GL\wglew.h"
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
_declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
|
||||
}
|
||||
|
||||
namespace nf {
|
||||
DEBUGINIT;
|
||||
|
||||
@ -37,10 +41,16 @@ namespace nf {
|
||||
}
|
||||
|
||||
void Application::startLoop() {
|
||||
showWindow(true);
|
||||
m_running = true;
|
||||
MSG msg = { };
|
||||
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)) {
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
@ -50,8 +60,18 @@ namespace nf {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Application::showWindow(bool show) {
|
||||
@ -65,6 +85,10 @@ namespace nf {
|
||||
return m_currentConfig;
|
||||
}
|
||||
|
||||
int Application::getFPS() {
|
||||
return m_FPS;
|
||||
}
|
||||
|
||||
void Application::registerWindowClass() {
|
||||
m_wclassName = L"NFClass";
|
||||
WNDCLASS wclass = { };
|
||||
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
|
||||
};
|
||||
}
|
@ -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
|
||||
|
Reference in New Issue
Block a user