From ca546621f1d8a68ec3b3fbad77f2624e7969422e Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Tue, 18 Oct 2022 16:36:34 -0400 Subject: [PATCH] Move OpenGL stuff out of Game class and into the Renderer class --- examples/dev_testing/src/main.cpp | 72 +++++++++++++++++++++++------ include/simpleengine/game.h | 4 +- include/simpleengine/gfx/renderer.h | 1 + src/game.cpp | 18 ++------ src/gfx/renderer.cpp | 11 +++-- 5 files changed, 70 insertions(+), 36 deletions(-) diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index 8ab98d6..7cdc2ba 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -9,6 +9,7 @@ #include "simpleengine/gfx/renderer.h" #include "simpleengine/gfx/texture.h" #include "simpleengine/vector.h" +#include #include #include #include @@ -33,27 +34,67 @@ namespace se = simpleengine; -class FPSCounterEvent : public se::Event { +class FPSCounterEvent : public se::Renderable { public: - double last_frame_time; - int frame_count; + double last_frame_time_input; + int frame_count_input; - FPSCounterEvent() : se::Event() { - this->last_frame_time = glfwGetTime(); - frame_count = 0; + double last_frame_time_tps; + int frame_count_tps; + + double last_frame_time_render; + int frame_count_render; + + FPSCounterEvent() : se::Renderable() { + last_frame_time_input = glfwGetTime(); + frame_count_input = 0; + + last_frame_time_tps = glfwGetTime(); + frame_count_tps = 0; + + last_frame_time_render = glfwGetTime(); + frame_count_render = 0; } virtual void update(const float &delta_time) { double current_time = glfwGetTime(); - frame_count++; + frame_count_tps++; // Check if the last print was 1 second ago. - if (current_time - last_frame_time >= 1.0) { - double ms_per_frame = 1000 / (double)frame_count; + if (current_time - last_frame_time_tps >= 1.0) { + double ms_per_frame = 1000 / (double)frame_count_tps; - printf("%d fps, %f ms/frame\n", frame_count, ms_per_frame); - frame_count = 0; - last_frame_time += 1.0; + printf("Fixed update: %d tps, %f ms/frame\n", frame_count_tps, ms_per_frame); + frame_count_tps = 0; + last_frame_time_tps += 1.0; + } + } + + virtual void input_update(const float &delta_time) { + double current_time = glfwGetTime(); + frame_count_input++; + + // Check if the last print was 1 second ago. + if (current_time - last_frame_time_input >= 1.0) { + double ms_per_frame = 1000 / (double)frame_count_input; + + printf("Input: %d tps, %f ms/frame\n", frame_count_input, ms_per_frame); + frame_count_input = 0; + last_frame_time_input += 1.0; + } + } + + virtual void render(const float& interpolate_alpha, const float& frame_time) { + double current_time = glfwGetTime(); + frame_count_render++; + + // Check if the last print was 1 second ago. + if (current_time - last_frame_time_render >= 1.0) { + double ms_per_frame = 1000 / (double)frame_count_render; + + printf("Render: %d fps, %f ms/frame\n\n", frame_count_render, ms_per_frame); + frame_count_render = 0; + last_frame_time_render += 1.0; } } }; @@ -103,11 +144,12 @@ int main(int argc, char *argv[]) { game.add_event(light); auto fps_counter = std::make_shared(); - game.add_event(fps_counter); + game.add_renderable(fps_counter); - game.set_enable_vsync(true); - // game.set_fps_limit(120); + /* game.set_enable_vsync(false); + game.set_fps_limit(100); */ int res = game.run(); + std::cout << "Engine result: " << res << std::endl; renderer->destroy(); diff --git a/include/simpleengine/game.h b/include/simpleengine/game.h index e3bcd55..3c8803d 100644 --- a/include/simpleengine/game.h +++ b/include/simpleengine/game.h @@ -32,9 +32,6 @@ namespace simpleengine { const int& minor_version = 4, const bool& resizeable = false, const int& forward_compat = GL_TRUE); virtual ~Game(); - void enable_default_gl_options() const; - void enable_gl_option(GLenum option) const; - void add_event(std::shared_ptr event); void add_renderable(std::shared_ptr renderable_event); void set_fps_limit(const int& fps_limit); @@ -66,6 +63,7 @@ namespace simpleengine { int fps_limit = -1; bool enable_vsync = true; + // Engine TPS related stuff int max_engine_tps = 120; // The maximum engine TPS float fixed_delta_time = 1.f / (float) max_engine_tps; // The delta time from fixed timestep float tps_accumulator = 0.f; diff --git a/include/simpleengine/gfx/renderer.h b/include/simpleengine/gfx/renderer.h index 8536602..436c95d 100644 --- a/include/simpleengine/gfx/renderer.h +++ b/include/simpleengine/gfx/renderer.h @@ -42,6 +42,7 @@ namespace simpleengine::gfx { Renderer(GLFWwindow* window, GLuint shader_program, std::shared_ptr camera); void enable_debug(); + void enable_gl_option(GLenum option) const; virtual void sort_jobs(); virtual void queue_job(RenderingType rendering_type, gfx::Mesh& mesh, glm::mat4 last_position, glm::mat4 position); diff --git a/src/game.cpp b/src/game.cpp index bab41da..b5e6e12 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -10,11 +10,9 @@ #ifdef __linux__ #include #include -#include #else #include #include -#include #endif simpleengine::Game::Game(int w, int h, const std::string& window_name, const int& gl_profile, const int& major_version, @@ -44,22 +42,11 @@ simpleengine::Game::Game(int w, int h, const std::string& window_name, const int glfwTerminate(); } - enable_default_gl_options(); + update_enabled_vsync(); last_frame_time = std::chrono::high_resolution_clock::now(); } -void simpleengine::Game::enable_default_gl_options() const { - - //glFrontFace(GL_CW); - - update_enabled_vsync(); -} - -void simpleengine::Game::enable_gl_option(GLenum option) const { - glEnable(option); -} - void simpleengine::Game::initialize(const int& gl_profile, const int& major_version, const int& minor_version, const bool& resizeable, const int& forward_compat) { glfwInit(); @@ -162,7 +149,8 @@ int simpleengine::Game::run() { // Poll input events glfwPollEvents(); - input_update(frame_time); // Update input on varying timestep + + input_update(frame_time); tps_accumulator += frame_time; diff --git a/src/gfx/renderer.cpp b/src/gfx/renderer.cpp index 9ab6c81..eb3ad47 100644 --- a/src/gfx/renderer.cpp +++ b/src/gfx/renderer.cpp @@ -47,6 +47,10 @@ namespace simpleengine::gfx { glDebugMessageCallback(debug_message_callback, 0); } + void Renderer::enable_gl_option(GLenum option) const { + glEnable(option); + } + void Renderer::sort_jobs() { } @@ -112,9 +116,10 @@ namespace simpleengine::gfx { } void Renderer::initialize() { - glEnable(GL_DEPTH_TEST); - glEnable(GL_BLEND); - glEnable(GL_CULL_FACE); + enable_gl_option(GL_DEPTH_TEST); + enable_gl_option(GL_BLEND); + enable_gl_option(GL_CULL_FACE); + glCullFace(GL_BACK); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);