From 7b4950dda040f8c330d88ad83f91a605fb13a26c Mon Sep 17 00:00:00 2001 From: Erris Date: Sat, 31 Jan 2026 10:27:40 +0100 Subject: [PATCH] too tired to think --- open_engine/include/open_engine.hpp | 2 + .../include/open_engine/application.hpp | 9 +- open_engine/include/open_engine/core.hpp | 20 ---- .../include/open_engine/entry_point.hpp | 2 +- .../include/open_engine/instrumentor.hpp | 96 +++++++++++++------ open_engine/include/open_engine/logging.hpp | 4 +- .../orthographic_camera_controller.hpp | 2 +- open_engine/include/open_engine/ref_scope.hpp | 24 +++++ .../open_engine/renderer/render_command.hpp | 3 +- .../open_engine/renderer/renderer2d.hpp | 15 ++- .../include/open_engine/renderer/shader.hpp | 2 +- open_engine/src/open_engine/application.cpp | 1 - .../src/open_engine/imgui/imgui_layer.cpp | 2 +- open_engine/src/open_engine/logging.cpp | 1 + .../src/open_engine/opengl/opengl_buffer.cpp | 1 + .../src/open_engine/opengl/opengl_context.cpp | 1 + .../opengl/opengl_renderer_api.cpp | 2 +- .../src/open_engine/opengl/opengl_shader.cpp | 18 +++- .../src/open_engine/renderer/renderer2d.cpp | 34 +++---- .../src/open_engine/window/linux_window.cpp | 13 ++- 20 files changed, 160 insertions(+), 92 deletions(-) create mode 100644 open_engine/include/open_engine/ref_scope.hpp diff --git a/open_engine/include/open_engine.hpp b/open_engine/include/open_engine.hpp index 55ad6f8..89de61b 100644 --- a/open_engine/include/open_engine.hpp +++ b/open_engine/include/open_engine.hpp @@ -2,6 +2,7 @@ #define OPEN_ENGINE_HPP #include "open_engine/core.hpp" +#include "open_engine/ref_scope.hpp" #include "open_engine/application.hpp" #include "open_engine/logging.hpp" #include "open_engine/events/key_event.hpp" @@ -10,6 +11,7 @@ #include "open_engine/renderer/renderer.hpp" #include "open_engine/core/time.hpp" #include "open_engine/input/input_system.hpp" +#include "open_engine/input/mouse_codes.hpp" #include "open_engine/input/keycodes.hpp" #include "open_engine/renderer/buffer.hpp" #include "open_engine/renderer/shader.hpp" diff --git a/open_engine/include/open_engine/application.hpp b/open_engine/include/open_engine/application.hpp index 3491b34..b3221ce 100644 --- a/open_engine/include/open_engine/application.hpp +++ b/open_engine/include/open_engine/application.hpp @@ -1,13 +1,14 @@ #ifndef APPLICATION_HPP #define APPLICATION_HPP -#include "open_engine/core.hpp" #include "open_engine/events/application_event.hpp" #include "open_engine/imgui/imgui_layer.hpp" #include "open_engine/window/window.hpp" #include "open_engine/layer_stack.hpp" #include "open_engine/layer.hpp" +int main(int argc, char **argv); + namespace OpenEngine { class Application { @@ -15,8 +16,6 @@ namespace OpenEngine { Application(); ~Application(); - void Run(); - virtual void OnEvent(Event& event); void QueueLayerPush(Ref layer); @@ -30,9 +29,11 @@ namespace OpenEngine { inline void StopRunning() { running = false; }; private: + void Run(); bool OnWindowClose(WindowCloseEvent& event); bool OnWindowResize(WindowResizeEvent& event); + private: inline static Application* instance; bool running = true; @@ -40,6 +41,8 @@ namespace OpenEngine { Ref imgui_layer; LayerStack layer_stack; + + friend int ::main(int argc, char **argv); }; // Is defined by client diff --git a/open_engine/include/open_engine/core.hpp b/open_engine/include/open_engine/core.hpp index 1f608c1..6091ce0 100644 --- a/open_engine/include/open_engine/core.hpp +++ b/open_engine/include/open_engine/core.hpp @@ -3,8 +3,6 @@ #include "open_engine/instrumentor.hpp" -#include - #ifdef OE_ENABLE_ASSERTS #include #define OE_ASSERT(x, ...) { if (!(x)) { OE_ERROR("Assertion Failed: {0}", __VA_ARGS__); raise(SIGTRAP); } } @@ -18,22 +16,4 @@ #define BIND_EVENT_FN(function) std::bind(&function, this, std::placeholders::_1) -namespace OpenEngine { - template - using Scope = std::unique_ptr; - template - constexpr Scope CreateScope(Args&& ... args) - { - return std::make_unique(std::forward(args)...); - } - - template - using Ref = std::shared_ptr; - template - constexpr Ref CreateRef(Args&& ... args) - { - return std::make_shared(std::forward(args)...); - } -} - #endif // CORE_HPP diff --git a/open_engine/include/open_engine/entry_point.hpp b/open_engine/include/open_engine/entry_point.hpp index 4dd21e3..5658be2 100755 --- a/open_engine/include/open_engine/entry_point.hpp +++ b/open_engine/include/open_engine/entry_point.hpp @@ -1,7 +1,7 @@ #ifndef ENTRY_POINT_HPP #define ENTRY_POINT_HPP -#include "open_engine/instrumentor.hpp" +#include "open_engine/core.hpp" #include "open_engine/application.hpp" #include "open_engine/logging.hpp" diff --git a/open_engine/include/open_engine/instrumentor.hpp b/open_engine/include/open_engine/instrumentor.hpp index 795e7be..41ffb73 100644 --- a/open_engine/include/open_engine/instrumentor.hpp +++ b/open_engine/include/open_engine/instrumentor.hpp @@ -1,6 +1,10 @@ #ifndef INSTRUMENTOR_HPP #define INSTRUMENTOR_HPP +#include "open_engine/logging.hpp" + +#include +#include #include #include #include @@ -8,11 +12,15 @@ #include namespace OpenEngine { + + using FloatingPointMicroseconds = std::chrono::duration; + struct ProfileResult { std::string name; - long long start, end; - uint32_t thread_id; + FloatingPointMicroseconds start; + std::chrono::microseconds elapsed_time; + std::thread::id thread_id; }; struct InstrumentationSession @@ -30,45 +38,53 @@ namespace OpenEngine { void BeginSession(const char* name, const std::string& filepath = "results.json") { + std::lock_guard lock(mutex); + if (current_session) { + if (Logger::GetCoreLogger()) + OE_CORE_ERROR("Instrumentor::BeginSession({}), when session {} already exists", name, current_session->name); + InternalEndSession(); + } + output_stream.open(filepath); - WriteHeader(); + if(output_stream.is_open()) { + current_session = new InstrumentationSession(name); + WriteHeader(); + } else { + if (Logger::GetCoreLogger()) + OE_CORE_ERROR("Instrumentor could not open results file: {}", filepath); + } }; void EndSession() { - WriteFooter(); - output_stream.flush(); - output_stream.close(); - profile_count = 0; + std::lock_guard lock(mutex); + InternalEndSession(); }; void WriteProfile(const ProfileResult& result) { - if (profile_count++ > 0) - output_stream << ","; + std::stringstream json; std::string name = result.name; std::replace(name.begin(), name.end(), '"', '\''); - output_stream << "{"; - output_stream << "\"cat\":\"function\","; - output_stream << "\"dur\":" << (result.end - result.start) << ','; - output_stream << "\"name\":\"" << name << "\","; - output_stream << "\"ph\":\"X\","; - output_stream << "\"pid\":0,"; - output_stream << "\"tid\":" << result.thread_id << ","; - output_stream << "\"ts\":" << result.start; - output_stream << "}"; - }; + json << std::setprecision(3) << std::fixed; + json << ",{"; + json << "\"cat\":\"function\","; + json << "\"dur\":" << (result.elapsed_time.count()) << ','; + json << "\"name\":\"" << name << "\","; + json << "\"ph\":\"X\","; + json << "\"pid\":0,"; + json << "\"tid\":" << result.thread_id << ","; + json << "\"ts\":" << result.start.count(); + json << "}"; - void WriteHeader() - { - output_stream << "{\"otherData\": {},\"traceEvents\":["; - }; + std::lock_guard lock(mutex); - void WriteFooter() - { - output_stream << "]}"; + if (current_session) { + output_stream << json.str(); + output_stream.flush(); + } }; static Instrumentor& Get() @@ -78,11 +94,32 @@ namespace OpenEngine { }; private: + void WriteHeader() + { + output_stream << "{\"otherData\": {},\"traceEvents\":[{}"; + }; + + void WriteFooter() + { + output_stream << "]}"; + }; + + void InternalEndSession() { + if (current_session) { + WriteFooter(); + output_stream.close(); + delete current_session; + current_session = nullptr; + } + } + Instrumentor() { }; private: + std::mutex mutex; + InstrumentationSession* current_session = nullptr; std::ofstream output_stream; int profile_count = 0; }; @@ -106,11 +143,12 @@ namespace OpenEngine { { auto end_timepoint = std::chrono::high_resolution_clock::now(); - long long start = std::chrono::time_point_cast(start_timepoint).time_since_epoch().count(); - long long end = std::chrono::time_point_cast(end_timepoint).time_since_epoch().count(); + auto start = FloatingPointMicroseconds{start_timepoint.time_since_epoch()}; + auto elapsed_time = std::chrono::time_point_cast(end_timepoint).time_since_epoch() + - std::chrono::time_point_cast(start_timepoint).time_since_epoch(); uint32_t thread_id = std::hash{}(std::this_thread::get_id()); - Instrumentor::Get().WriteProfile({ name, start, end, thread_id }); + Instrumentor::Get().WriteProfile({ name, start, elapsed_time, std::this_thread::get_id() }); stopped = true; }; diff --git a/open_engine/include/open_engine/logging.hpp b/open_engine/include/open_engine/logging.hpp index c86fd12..a93b36a 100644 --- a/open_engine/include/open_engine/logging.hpp +++ b/open_engine/include/open_engine/logging.hpp @@ -1,12 +1,14 @@ #ifndef LOGGING_HPP #define LOGGING_HPP -#include "open_engine/core.hpp" +#include "open_engine/ref_scope.hpp" #include #include #include + + namespace OpenEngine { spdlog::level::level_enum stringToLogLevel(std::string level_str); int setupMultisinkLogger(const std::string &file_path); diff --git a/open_engine/include/open_engine/orthographic_camera_controller.hpp b/open_engine/include/open_engine/orthographic_camera_controller.hpp index 519fd7d..00bf9ac 100644 --- a/open_engine/include/open_engine/orthographic_camera_controller.hpp +++ b/open_engine/include/open_engine/orthographic_camera_controller.hpp @@ -18,7 +18,7 @@ namespace OpenEngine { void OnEvent(Event& e); const OrthographicCamera& GetCamera() const { return camera; }; - OrthographicCamera GetCamera() { return camera;}; + OrthographicCamera& GetCamera() { return camera; }; float GetZoom() const { return zoom; } void SetZoom(float level) { zoom = level; }; diff --git a/open_engine/include/open_engine/ref_scope.hpp b/open_engine/include/open_engine/ref_scope.hpp new file mode 100644 index 0000000..31f5140 --- /dev/null +++ b/open_engine/include/open_engine/ref_scope.hpp @@ -0,0 +1,24 @@ +#ifndef REF_SCOPE_HPP +#define REF_SCOPE_HPP + +#include + +namespace OpenEngine { + template + using Scope = std::unique_ptr; + template + constexpr Scope CreateScope(Args&& ... args) + { + return std::make_unique(std::forward(args)...); + } + + template + using Ref = std::shared_ptr; + template + constexpr Ref CreateRef(Args&& ... args) + { + return std::make_shared(std::forward(args)...); + } +} + +#endif // REF_SCOPE_HPP diff --git a/open_engine/include/open_engine/renderer/render_command.hpp b/open_engine/include/open_engine/renderer/render_command.hpp index c6c6a8c..284ce43 100644 --- a/open_engine/include/open_engine/renderer/render_command.hpp +++ b/open_engine/include/open_engine/renderer/render_command.hpp @@ -2,8 +2,9 @@ #define RENDER_COMMAND_HPP #include "open_engine/core.hpp" -#include "open_engine/renderer/renderer_api.hpp" #include "open_engine/opengl/opengl_renderer_api.hpp" +#include "open_engine/renderer/renderer_api.hpp" +#include "open_engine/ref_scope.hpp" #include diff --git a/open_engine/include/open_engine/renderer/renderer2d.hpp b/open_engine/include/open_engine/renderer/renderer2d.hpp index 884483b..78545d6 100644 --- a/open_engine/include/open_engine/renderer/renderer2d.hpp +++ b/open_engine/include/open_engine/renderer/renderer2d.hpp @@ -1,13 +1,20 @@ #ifndef RENDERER2D_HPP #define RENDERER2D_HPP -#include "open_engine/core.hpp" #include "open_engine/orthographic_camera.hpp" #include "open_engine/renderer/texture.hpp" +#include "open_engine/ref_scope.hpp" #include namespace OpenEngine { + struct Transform + { + glm::vec3 position = {0.0f, 0.0f, 0.0f}; + glm::vec3 size = {1.0f, 1.0f, 1.0f}; + float rotation = 0.0f; + }; + class Renderer2D { public: @@ -17,10 +24,8 @@ namespace OpenEngine { static void BeginScene(const OrthographicCamera& camera); static void EndScene(); - static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color); - static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color); - static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref& texture); - static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& texture); + static void DrawQuad(const Transform& transform_data, const glm::vec4& color); + static void DrawQuad(const Transform& transform_data, const Ref& texture, float tiling_factor = 1.0f); }; } diff --git a/open_engine/include/open_engine/renderer/shader.hpp b/open_engine/include/open_engine/renderer/shader.hpp index 9cb6cb8..7060800 100644 --- a/open_engine/include/open_engine/renderer/shader.hpp +++ b/open_engine/include/open_engine/renderer/shader.hpp @@ -1,7 +1,7 @@ #ifndef SHADER_HPP #define SHADER_HPP -#include +#include #include #include diff --git a/open_engine/src/open_engine/application.cpp b/open_engine/src/open_engine/application.cpp index 2cdb9e7..05e1e1f 100755 --- a/open_engine/src/open_engine/application.cpp +++ b/open_engine/src/open_engine/application.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include diff --git a/open_engine/src/open_engine/imgui/imgui_layer.cpp b/open_engine/src/open_engine/imgui/imgui_layer.cpp index f6b7305..b9229cb 100644 --- a/open_engine/src/open_engine/imgui/imgui_layer.cpp +++ b/open_engine/src/open_engine/imgui/imgui_layer.cpp @@ -332,7 +332,7 @@ namespace OpenEngine { { OE_PROFILE_FUNCTION(); - static bool show = true; + static bool show = false; if (show) ImGui::ShowDemoWindow(&show); } diff --git a/open_engine/src/open_engine/logging.cpp b/open_engine/src/open_engine/logging.cpp index 614ac3e..65fe0e6 100644 --- a/open_engine/src/open_engine/logging.cpp +++ b/open_engine/src/open_engine/logging.cpp @@ -1,5 +1,6 @@ #include +#include #include #include diff --git a/open_engine/src/open_engine/opengl/opengl_buffer.cpp b/open_engine/src/open_engine/opengl/opengl_buffer.cpp index 81c3430..ff01f0d 100644 --- a/open_engine/src/open_engine/opengl/opengl_buffer.cpp +++ b/open_engine/src/open_engine/opengl/opengl_buffer.cpp @@ -1,5 +1,6 @@ #include +#include #include #include diff --git a/open_engine/src/open_engine/opengl/opengl_context.cpp b/open_engine/src/open_engine/opengl/opengl_context.cpp index 49f14fb..5eb1907 100644 --- a/open_engine/src/open_engine/opengl/opengl_context.cpp +++ b/open_engine/src/open_engine/opengl/opengl_context.cpp @@ -1,5 +1,6 @@ #include +#include #include #include diff --git a/open_engine/src/open_engine/opengl/opengl_renderer_api.cpp b/open_engine/src/open_engine/opengl/opengl_renderer_api.cpp index 2c1aa43..80682e0 100644 --- a/open_engine/src/open_engine/opengl/opengl_renderer_api.cpp +++ b/open_engine/src/open_engine/opengl/opengl_renderer_api.cpp @@ -30,7 +30,7 @@ namespace OpenEngine { { OE_PROFILE_FUNCTION(); -#ifdef OE_DEBUG +#ifdef OE_DEBUG_TOOLS glEnable(GL_DEBUG_OUTPUT); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); glDebugMessageCallback(OpenGLMessageCallback, nullptr); diff --git a/open_engine/src/open_engine/opengl/opengl_shader.cpp b/open_engine/src/open_engine/opengl/opengl_shader.cpp index 0f94b1f..1e69f6a 100644 --- a/open_engine/src/open_engine/opengl/opengl_shader.cpp +++ b/open_engine/src/open_engine/opengl/opengl_shader.cpp @@ -2,10 +2,12 @@ #include +#include #include #include -#include #include + +#include #include #include #include @@ -194,30 +196,44 @@ namespace OpenEngine { void OpenGLShader::SetBool(const std::string &name, bool value) const { + OE_PROFILE_FUNCTION(); + UploadBool(name, value); } void OpenGLShader::SetInt(const std::string &name, int value) const { + OE_PROFILE_FUNCTION(); + UploadInt(name, value); } void OpenGLShader::SetFloat(const std::string &name, float value) const { + OE_PROFILE_FUNCTION(); + UploadFloat(name, value); } void OpenGLShader::SetMat4(const std::string &name, const glm::mat4& value) const { + OE_PROFILE_FUNCTION(); + UploadMat4(name, value); } void OpenGLShader::SetVec2(const std::string &name, const glm::vec2& value) const { + OE_PROFILE_FUNCTION(); + UploadVec2(name, value); } void OpenGLShader::SetVec3(const std::string &name, const glm::vec3& value) const { + OE_PROFILE_FUNCTION(); + UploadVec3(name, value); } void OpenGLShader::SetVec4(const std::string &name, const glm::vec4& value) const { + OE_PROFILE_FUNCTION(); + UploadVec4(name, value); } diff --git a/open_engine/src/open_engine/renderer/renderer2d.cpp b/open_engine/src/open_engine/renderer/renderer2d.cpp index c58d043..908422e 100755 --- a/open_engine/src/open_engine/renderer/renderer2d.cpp +++ b/open_engine/src/open_engine/renderer/renderer2d.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include @@ -73,49 +75,43 @@ namespace OpenEngine { renderer_data->texture_shader->Bind(); renderer_data->texture_shader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix()); - renderer_data->texture_shader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix()); } void Renderer2D::EndScene() { } - void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref& texture) - { - OE_PROFILE_FUNCTION(); - - DrawQuad(glm::vec3(position, 0.0f), size, texture); - } - void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& texture) + void Renderer2D::DrawQuad(const Transform& transform_data, const Ref& texture, float tiling_factor) { OE_PROFILE_FUNCTION(); renderer_data->texture_shader->SetVec4("u_Color", glm::vec4(1.0f)); + renderer_data->texture_shader->SetFloat("u_TilingFactor", tiling_factor); texture->Bind(); - glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * - glm::scale(glm::mat4(1.0f), {size.x, size.y, 1.0f}); + glm::mat4 transform = glm::translate(glm::mat4(1.0f), transform_data.position) + * ((transform_data.rotation != 0) ? + glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), {0.0f, 0.0f, 1.0f}) + : 1.0f) + * glm::scale(glm::mat4(1.0f), transform_data.size); renderer_data->texture_shader->SetMat4("u_Transform", transform); renderer_data->vertex_array->Bind(); RenderCommand::DrawIndexed(renderer_data->vertex_array); } - void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color) - { - OE_PROFILE_FUNCTION(); - - DrawQuad(glm::vec3(position, 0.0f), size, color); - } - void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color) + void Renderer2D::DrawQuad(const Transform& transform_data, const glm::vec4& color) { OE_PROFILE_FUNCTION(); renderer_data->texture_shader->SetVec4("u_Color", color); renderer_data->white_texture->Bind(); - glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * - glm::scale(glm::mat4(1.0f), {size.x, size.y, 1.0f}); + glm::mat4 transform = glm::translate(glm::mat4(1.0f), transform_data.position) + * ((transform_data.rotation != 0) ? + glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), {0.0f, 0.0f, 1.0f}) + : 1.0f) + * glm::scale(glm::mat4(1.0f), transform_data.size); renderer_data->texture_shader->SetMat4("u_Transform", transform); renderer_data->vertex_array->Bind(); diff --git a/open_engine/src/open_engine/window/linux_window.cpp b/open_engine/src/open_engine/window/linux_window.cpp index a6920c1..fa76187 100644 --- a/open_engine/src/open_engine/window/linux_window.cpp +++ b/open_engine/src/open_engine/window/linux_window.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -103,19 +102,19 @@ namespace OpenEngine { switch (action) { case GLFW_PRESS: { - KeyPressedEvent event(key, scancode, 0, mods); + KeyPressedEvent event(static_cast(key), scancode, 0, mods); data.event_callback(event); break; } case GLFW_RELEASE: { - KeyReleasedEvent event(key, scancode, mods); + KeyReleasedEvent event(static_cast(key), scancode, mods); data.event_callback(event); break; } case GLFW_REPEAT: { - KeyPressedEvent event(key, scancode, 1, mods); + KeyPressedEvent event(static_cast(key), scancode, 1, mods); data.event_callback(event); break; } @@ -126,7 +125,7 @@ namespace OpenEngine { { WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window); - KeyTypedEvent event(keycode); + KeyTypedEvent event(static_cast(keycode)); data.event_callback(event); }); @@ -137,13 +136,13 @@ namespace OpenEngine { switch (action) { case GLFW_PRESS: { - MouseButtonPressedEvent event(button); + MouseButtonPressedEvent event(static_cast(button)); data.event_callback(event); break; } case GLFW_RELEASE: { - MouseButtonReleasedEvent event(button); + MouseButtonReleasedEvent event(static_cast(button)); data.event_callback(event); break; }