Refactored game loop to use less CPU

This commit is contained in:
Grayson Riffe (Desktop) 2021-08-23 01:13:53 -05:00
parent c3dd5a22b1
commit e238e35bb2
5 changed files with 13 additions and 13 deletions

View File

@ -140,8 +140,11 @@ namespace nf {
void Application::startMainThread() { void Application::startMainThread() {
m_renderer = new Renderer(this); m_renderer = new Renderer(this);
startIntroState(); startIntroState();
std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
const std::chrono::duration<double> wait_time = std::chrono::nanoseconds(1000000000 / 60);
auto next_time = start_time + wait_time;
while (m_running) { while (m_running) {
m_deltaTime = m_fpsDuration.count(); start_time = std::chrono::steady_clock::now();
m_currentState->update(); m_currentState->update();
m_currentState->render(); m_currentState->render();
m_renderer->doFrame(); m_renderer->doFrame();
@ -149,16 +152,14 @@ namespace nf {
m_fpsClock2 = std::chrono::steady_clock::now(); m_fpsClock2 = std::chrono::steady_clock::now();
m_fpsDuration = m_fpsClock2 - m_fpsClock1; m_fpsDuration = m_fpsClock2 - m_fpsClock1;
if (m_fpsDuration.count() >= 1.0) { if (m_fpsDuration.count() >= 1.0) {
m_fpsClock1 = std::chrono::steady_clock::now();
m_FPS = m_frames; m_FPS = m_frames;
m_frames = 0; m_frames = 0;
Log("FPS: " + std::to_string(m_FPS)); Log("FPS: " + std::to_string(m_FPS));
m_fpsClock1 = std::chrono::steady_clock::now();
} }
m_fpsDuration = std::chrono::steady_clock::now() - m_frameClock; std::this_thread::sleep_until(next_time);
while (m_fpsDuration.count() < m_minFrametime) { m_deltaTime = (std::chrono::steady_clock::now() - start_time).count();
m_fpsDuration = std::chrono::steady_clock::now() - m_frameClock; next_time += wait_time;
}
m_frameClock = std::chrono::steady_clock::now();
} }
m_currentState->onExit(); m_currentState->onExit();
delete m_renderer; delete m_renderer;

View File

@ -5,7 +5,7 @@ namespace nf {
m_count = count; m_count = count;
glGenBuffers(1, &m_id); glGenBuffers(1, &m_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW);//TODO: See if I need to change this to dynamic glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW);
} }
void IndexBuffer::bind() const { void IndexBuffer::bind() const {

View File

@ -1,4 +1,3 @@
//TODO: File IO functions
#include <Windows.h> #include <Windows.h>
#include "Utility.h" #include "Utility.h"
@ -72,7 +71,7 @@ namespace nf {
out.close(); out.close();
return true; return true;
} }
//TODO: XOR encryption
std::string readFile(const char* filename) { std::string readFile(const char* filename) {
std::ifstream in; std::ifstream in;
in.open(filename); in.open(filename);

View File

@ -50,11 +50,10 @@ namespace nf {
LONG m_defaultWindowStyle; LONG m_defaultWindowStyle;
WINDOWPLACEMENT m_wndPlacement; WINDOWPLACEMENT m_wndPlacement;
std::chrono::steady_clock::time_point m_frameClock = std::chrono::steady_clock::now();
std::chrono::duration<double> m_fpsDuration; std::chrono::duration<double> m_fpsDuration;
double m_deltaTime; double m_deltaTime;
std::chrono::steady_clock::time_point m_fpsClock1 = m_frameClock; std::chrono::steady_clock::time_point m_fpsClock1 = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point m_fpsClock2 = m_frameClock; std::chrono::steady_clock::time_point m_fpsClock2 = m_fpsClock1;
int m_frames; int m_frames;
const int m_targetFPS = 60; const int m_targetFPS = 60;
const double m_minFrametime = 1.0 / m_targetFPS; const double m_minFrametime = 1.0 / m_targetFPS;

View File

@ -6,6 +6,7 @@
namespace nf { namespace nf {
class Application; class Application;
class Renderer { class Renderer {
public: public:
Renderer(Application* app); Renderer(Application* app);