Move OpenGL stuff out of Game class and into the Renderer class
This commit is contained in:
parent
1e7e9a4f9e
commit
ca546621f1
|
@ -9,6 +9,7 @@
|
||||||
#include "simpleengine/gfx/renderer.h"
|
#include "simpleengine/gfx/renderer.h"
|
||||||
#include "simpleengine/gfx/texture.h"
|
#include "simpleengine/gfx/texture.h"
|
||||||
#include "simpleengine/vector.h"
|
#include "simpleengine/vector.h"
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
#include <simpleengine/ecs/component/model_component.h>
|
#include <simpleengine/ecs/component/model_component.h>
|
||||||
#include <simpleengine/ecs/component/rotating_component.h>
|
#include <simpleengine/ecs/component/rotating_component.h>
|
||||||
#include <simpleengine/event/event.h>
|
#include <simpleengine/event/event.h>
|
||||||
|
@ -33,27 +34,67 @@
|
||||||
|
|
||||||
namespace se = simpleengine;
|
namespace se = simpleengine;
|
||||||
|
|
||||||
class FPSCounterEvent : public se::Event {
|
class FPSCounterEvent : public se::Renderable {
|
||||||
public:
|
public:
|
||||||
double last_frame_time;
|
double last_frame_time_input;
|
||||||
int frame_count;
|
int frame_count_input;
|
||||||
|
|
||||||
FPSCounterEvent() : se::Event() {
|
double last_frame_time_tps;
|
||||||
this->last_frame_time = glfwGetTime();
|
int frame_count_tps;
|
||||||
frame_count = 0;
|
|
||||||
|
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) {
|
virtual void update(const float &delta_time) {
|
||||||
double current_time = glfwGetTime();
|
double current_time = glfwGetTime();
|
||||||
frame_count++;
|
frame_count_tps++;
|
||||||
|
|
||||||
// Check if the last print was 1 second ago.
|
// Check if the last print was 1 second ago.
|
||||||
if (current_time - last_frame_time >= 1.0) {
|
if (current_time - last_frame_time_tps >= 1.0) {
|
||||||
double ms_per_frame = 1000 / (double)frame_count;
|
double ms_per_frame = 1000 / (double)frame_count_tps;
|
||||||
|
|
||||||
printf("%d fps, %f ms/frame\n", frame_count, ms_per_frame);
|
printf("Fixed update: %d tps, %f ms/frame\n", frame_count_tps, ms_per_frame);
|
||||||
frame_count = 0;
|
frame_count_tps = 0;
|
||||||
last_frame_time += 1.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);
|
game.add_event(light);
|
||||||
|
|
||||||
auto fps_counter = std::make_shared<FPSCounterEvent>();
|
auto fps_counter = std::make_shared<FPSCounterEvent>();
|
||||||
game.add_event(fps_counter);
|
game.add_renderable(fps_counter);
|
||||||
|
|
||||||
game.set_enable_vsync(true);
|
/* game.set_enable_vsync(false);
|
||||||
// game.set_fps_limit(120);
|
game.set_fps_limit(100); */
|
||||||
int res = game.run();
|
int res = game.run();
|
||||||
|
|
||||||
std::cout << "Engine result: " << res << std::endl;
|
std::cout << "Engine result: " << res << std::endl;
|
||||||
|
|
||||||
renderer->destroy();
|
renderer->destroy();
|
||||||
|
|
|
@ -32,9 +32,6 @@ namespace simpleengine {
|
||||||
const int& minor_version = 4, const bool& resizeable = false, const int& forward_compat = GL_TRUE);
|
const int& minor_version = 4, const bool& resizeable = false, const int& forward_compat = GL_TRUE);
|
||||||
virtual ~Game();
|
virtual ~Game();
|
||||||
|
|
||||||
void enable_default_gl_options() const;
|
|
||||||
void enable_gl_option(GLenum option) const;
|
|
||||||
|
|
||||||
void add_event(std::shared_ptr<simpleengine::Event> event);
|
void add_event(std::shared_ptr<simpleengine::Event> event);
|
||||||
void add_renderable(std::shared_ptr<simpleengine::Renderable> renderable_event);
|
void add_renderable(std::shared_ptr<simpleengine::Renderable> renderable_event);
|
||||||
void set_fps_limit(const int& fps_limit);
|
void set_fps_limit(const int& fps_limit);
|
||||||
|
@ -66,6 +63,7 @@ namespace simpleengine {
|
||||||
int fps_limit = -1;
|
int fps_limit = -1;
|
||||||
bool enable_vsync = true;
|
bool enable_vsync = true;
|
||||||
|
|
||||||
|
// Engine TPS related stuff
|
||||||
int max_engine_tps = 120; // The maximum engine TPS
|
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 fixed_delta_time = 1.f / (float) max_engine_tps; // The delta time from fixed timestep
|
||||||
float tps_accumulator = 0.f;
|
float tps_accumulator = 0.f;
|
||||||
|
|
|
@ -42,6 +42,7 @@ namespace simpleengine::gfx {
|
||||||
Renderer(GLFWwindow* window, GLuint shader_program, std::shared_ptr<Camera> camera);
|
Renderer(GLFWwindow* window, GLuint shader_program, std::shared_ptr<Camera> camera);
|
||||||
|
|
||||||
void enable_debug();
|
void enable_debug();
|
||||||
|
void enable_gl_option(GLenum option) const;
|
||||||
|
|
||||||
virtual void sort_jobs();
|
virtual void sort_jobs();
|
||||||
virtual void queue_job(RenderingType rendering_type, gfx::Mesh& mesh, glm::mat4 last_position, glm::mat4 position);
|
virtual void queue_job(RenderingType rendering_type, gfx::Mesh& mesh, glm::mat4 last_position, glm::mat4 position);
|
||||||
|
|
18
src/game.cpp
18
src/game.cpp
|
@ -10,11 +10,9 @@
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <GL/gl.h>
|
|
||||||
#else
|
#else
|
||||||
#include <gl/glew.h>
|
#include <gl/glew.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <gl/gl.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
simpleengine::Game::Game(int w, int h, const std::string& window_name, const int& gl_profile, const int& major_version,
|
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();
|
glfwTerminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
enable_default_gl_options();
|
update_enabled_vsync();
|
||||||
|
|
||||||
last_frame_time = std::chrono::high_resolution_clock::now();
|
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,
|
void simpleengine::Game::initialize(const int& gl_profile, const int& major_version, const int& minor_version,
|
||||||
const bool& resizeable, const int& forward_compat) {
|
const bool& resizeable, const int& forward_compat) {
|
||||||
glfwInit();
|
glfwInit();
|
||||||
|
@ -162,7 +149,8 @@ int simpleengine::Game::run() {
|
||||||
|
|
||||||
// Poll input events
|
// Poll input events
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
input_update(frame_time); // Update input on varying timestep
|
|
||||||
|
input_update(frame_time);
|
||||||
|
|
||||||
tps_accumulator += frame_time;
|
tps_accumulator += frame_time;
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,10 @@ namespace simpleengine::gfx {
|
||||||
glDebugMessageCallback(debug_message_callback, 0);
|
glDebugMessageCallback(debug_message_callback, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Renderer::enable_gl_option(GLenum option) const {
|
||||||
|
glEnable(option);
|
||||||
|
}
|
||||||
|
|
||||||
void Renderer::sort_jobs() {
|
void Renderer::sort_jobs() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -112,9 +116,10 @@ namespace simpleengine::gfx {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::initialize() {
|
void Renderer::initialize() {
|
||||||
glEnable(GL_DEPTH_TEST);
|
enable_gl_option(GL_DEPTH_TEST);
|
||||||
glEnable(GL_BLEND);
|
enable_gl_option(GL_BLEND);
|
||||||
glEnable(GL_CULL_FACE);
|
enable_gl_option(GL_CULL_FACE);
|
||||||
|
|
||||||
glCullFace(GL_BACK);
|
glCullFace(GL_BACK);
|
||||||
|
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
|
Loading…
Reference in New Issue