added queuing push and pop logic and having fun with c# integration

This commit is contained in:
Erris
2026-01-22 09:33:35 +01:00
parent 14134c7d2f
commit 58ea4554c7
34 changed files with 338 additions and 126 deletions

View File

@@ -12,12 +12,8 @@
#include "open_engine/input/keycodes.hpp"
#include "open_engine/renderer/buffer.hpp"
#include "open_engine/renderer/shader.hpp"
#include "open_engine/opengl/opengl_shader.hpp"
#include "open_engine/renderer/texture.hpp"
#include "open_engine/orthographic_camera_controller.hpp"
// Entry Point -------------------------
#include "open_engine/entry_point.hpp"
// -------------------------------------
#include "open_engine/renderer/renderer2d.hpp"
#endif // OPEN_ENGINE_HPP

View File

@@ -15,14 +15,21 @@ namespace OpenEngine {
{
public:
Application();
virtual ~Application() = default;
~Application();
void Run();
virtual void OnEvent(Event& event);
void PushLayer(Layer* layer);
void PushOverlay(Layer* overlay);
//void PushOverlay(Ref<Layer> overlay);
//void PopOverlay(Ref<Layer> overlay);
//void PushLayer(Ref<Layer> layer);
//void PopLayer();
void QueueLayerPush(Ref<Layer> layer);
void QueueLayerPop(Ref<Layer> layer);
void QueueOverlayPush(Ref<Layer> layer);
void QueueOverlayPop(Ref<Layer> layer);
inline static Application& Get() { return *instance; }
@@ -38,7 +45,7 @@ namespace OpenEngine {
bool running = true;
std::unique_ptr<Window> window;
ImGuiLayer* imgui_layer;
Ref<ImGuiLayer> imgui_layer;
LayerStack layer_stack;
};

View File

@@ -28,9 +28,19 @@
namespace OpenEngine {
template<typename T>
using Scope = std::unique_ptr<T>;
template<typename T, typename ... Args>
constexpr Scope<T> CreateScope(Args&& ... args)
{
return std::make_unique<T>(std::forward<Args>(args)...);
}
template<typename T>
using Ref = std::shared_ptr<T>;
template<typename T, typename ... Args>
constexpr Ref<T> CreateRef(Args&& ... args)
{
return std::make_shared<T>(std::forward<Args>(args)...);
}
}
#endif // CORE_HPP

View File

@@ -11,6 +11,7 @@ int main(int argc, char** argv)
auto app = OpenEngine::CreateApplication();
app->Run();
delete app;
}

View File

@@ -9,7 +9,7 @@ namespace OpenEngine {
{
public:
ImGuiLayer();
~ImGuiLayer() = default;
~ImGuiLayer();
virtual void OnAttach() override;
virtual void OnDetach() override;

View File

@@ -54,7 +54,7 @@ namespace OpenEngine {
private:
inline static const std::string GetJoystickName(unsigned int joystick) { return instance->GetJoystickNameImpl(joystick); };
static Input* instance;
static Scope<Input> instance;
};
}

View File

@@ -25,8 +25,6 @@ namespace OpenEngine {
virtual unsigned int GetJoystickAxesCountImpl(unsigned int joystick) override;
virtual bool IsJoystickButtonPressedImpl(unsigned int joystick, unsigned int button) override;
private:
};
}

View File

@@ -3,6 +3,7 @@
#include "events/event.hpp"
#include "core.hpp"
#include "open_engine/logging.hpp"
namespace OpenEngine {
class OE_API Layer

View File

@@ -13,15 +13,23 @@ namespace OpenEngine {
LayerStack();
~LayerStack();
void PushLayer(Layer* layer);
void PopLayer(Layer* layer);
void PushOverlay(Layer* overlay);
void PopOverlay(Layer* overlay);
void QueueLayerPush(Ref<Layer> layer);
void QueueLayerPop(Ref<Layer> layer);
void QueueOverlayPush(Ref<Layer> layer);
void QueueOverlayPop(Ref<Layer> layer);
void UpdateLayers();
std::vector<Ref<Layer>>::iterator begin() { return layers.begin(); }
std::vector<Ref<Layer>>::iterator end() { return layers.end(); }
std::vector<Layer*>::iterator begin() { return layers.begin(); }
std::vector<Layer*>::iterator end() { return layers.end(); }
private:
std::vector<Layer*> layers;
std::vector<Ref<Layer>> layers;
std::vector<Ref<Layer>> layers_to_pop;
std::vector<Ref<Layer>> layers_to_push;
std::vector<Ref<Layer>> overlay_to_pop;
std::vector<Ref<Layer>> overlay_to_push;
unsigned int layer_insert_index = 0;
};
}

View File

@@ -22,12 +22,19 @@ namespace OpenEngine {
virtual void Unbind() const override;
// utility uniform functions
virtual void SetBool(const std::string &name, bool value) const;
virtual void SetInt(const std::string &name, int value) const;
virtual void SetFloat(const std::string &name, float value) const;
virtual void SetMat4(const std::string &name, const glm::mat4& value) const;
virtual void SetVec3(const std::string &name, const glm::vec3& value) const;
void UploadBool(const std::string &name, bool value) const;
void UploadInt(const std::string &name, int value) const;
void UploadFloat(const std::string &name, float value) const;
void UploadMat4(const std::string &name, const glm::mat4& value) const;
void UploadVec3(const std::string &name, const glm::vec3& value) const;
void UploadVec4(const std::string &name, const glm::vec4& value) const;
virtual void SetBool(const std::string &name, bool value) const override;
virtual void SetInt(const std::string &name, int value) const override;
virtual void SetFloat(const std::string &name, float value) const override;
virtual void SetMat4(const std::string &name, const glm::mat4& value) const override;
virtual void SetVec3(const std::string &name, const glm::vec3& value) const override;
virtual void SetVec4(const std::string &name, const glm::vec4& value) const override;
private:
std::string ReadFile(const std::string& shader_path);

View File

@@ -5,6 +5,8 @@ namespace OpenEngine {
class GraphicsContext
{
public:
// TODO: Check this!
virtual ~GraphicsContext() = default;
virtual void Init() = 0;
virtual void SwapBuffers() = 0;
};

View File

@@ -3,6 +3,7 @@
#include "../renderer/renderer_api.hpp"
#include "../opengl/opengl_renderer_api.hpp"
#include "open_engine/core.hpp"
#include <cstdint>
namespace OpenEngine {
@@ -35,7 +36,7 @@ namespace OpenEngine {
}
private:
static inline RendererAPI* api = new OpenGLRendererAPI();
static inline Scope<RendererAPI> api = CreateScope<OpenGLRendererAPI>();
};
}

View File

@@ -1,9 +1,10 @@
#ifndef RENDERER_HPP
#define RENDERER_HPP
#include "../renderer/renderer_api.hpp"
#include "../renderer/vertex_array.hpp"
#include "../orthographic_camera.hpp"
#include "open_engine/renderer/renderer_api.hpp"
#include "open_engine/renderer/vertex_array.hpp"
#include "open_engine/orthographic_camera.hpp"
#include "open_engine/core.hpp"
#include <open_engine/renderer/shader.hpp>
#include <glm/glm.hpp>
@@ -32,7 +33,7 @@ namespace OpenEngine {
glm::mat4 view_projection_matrix;
};
inline static SceneData* scene_data = new SceneData;
inline static Scope<SceneData> scene_data = CreateScope<SceneData>();
};
}

View File

@@ -16,6 +16,7 @@ namespace OpenEngine {
};
virtual void Init() = 0;
virtual ~RendererAPI() = default;
virtual void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) = 0;

View File

@@ -19,6 +19,13 @@ namespace OpenEngine {
virtual void Bind() const = 0;
virtual void Unbind() const = 0;
virtual void SetBool(const std::string &name, bool value) const = 0;
virtual void SetInt(const std::string &name, int value) const = 0;
virtual void SetFloat(const std::string &name, float value) const = 0;
virtual void SetMat4(const std::string &name, const glm::mat4& value) const = 0;
virtual void SetVec3(const std::string &name, const glm::vec3& value) const = 0;
virtual void SetVec4(const std::string &name, const glm::vec4& value) const = 0;
};
class ShaderLibrary

View File

@@ -1,7 +1,8 @@
#ifndef VERTEX_ARRAY_HPP
#define VERTEX_ARRAY_HPP
#include "../renderer/buffer.hpp"
#include "open_engine/core.hpp"
#include "open_engine/renderer/buffer.hpp"
#include <memory>
@@ -20,7 +21,7 @@ namespace OpenEngine {
virtual const std::vector<std::shared_ptr<VertexBuffer>>& GetVertexBuffers() const = 0;
virtual const std::shared_ptr<IndexBuffer>& GetIndexBuffer() const = 0;
static VertexArray* Create();
static Ref<VertexArray> Create();
};
}

View File

@@ -35,7 +35,7 @@ namespace OpenEngine {
WindowData data;
GLFWwindow* gl_window;
GraphicsContext* context;
Scope<GraphicsContext> context;
};
}

View File

@@ -49,7 +49,7 @@ namespace OpenEngine {
virtual void* GetNativeWindow() const = 0;
static Window* Create(const WindowProps& props = WindowProps());
static std::unique_ptr<Window> Create(const WindowProps& props = WindowProps());
};
}

View File

@@ -1,42 +1,51 @@
#include <pch.hpp>
#include <core.hpp>
#include <events/application_event.hpp>
#include <imgui/imgui_layer.hpp>
#include <core/time.hpp>
#include <renderer/renderer.hpp>
#include <application.hpp>
#include <input/input_system.hpp>
#include <core.hpp>
#include <core/time.hpp>
#include <events/application_event.hpp>
#include <renderer/renderer2d.hpp>
#include <renderer/renderer.hpp>
#include <input/input_system.hpp>
#include <imgui/imgui_layer.hpp>
#include <memory>
#include <imgui.h>
namespace OpenEngine {
Application::Application()
{
OE_CORE_ASSERT(!instance, "Application already exists!");
instance = this;
window = std::unique_ptr<Window>(Window::Create());
window = Window::Create();
window->SetEventCallback(BIND_EVENT_FN(Application::OnEvent));
Renderer::Init();
Renderer2D::Init();
imgui_layer = new ImGuiLayer();
PushOverlay(imgui_layer);
imgui_layer = std::make_shared<ImGuiLayer>();
QueueOverlayPush(imgui_layer);
}
Application::~Application()
{
Renderer2D::Shutdown();
}
void Application::Run()
{
while(running) {
Time::Get().Update();
layer_stack.UpdateLayers();
for (Layer* layer : layer_stack)
for (auto layer : layer_stack)
layer->OnUpdate();
imgui_layer->Begin();
for (Layer* layer : layer_stack)
for (auto layer : layer_stack)
layer->OnImGuiRender();
imgui_layer->End();
@@ -72,13 +81,23 @@ namespace OpenEngine {
return false;
}
void Application::PushLayer(Layer* layer)
void Application::QueueLayerPush(Ref<Layer> layer)
{
layer_stack.PushLayer(layer);
layer_stack.QueueLayerPush(layer);
}
void Application::PushOverlay(Layer* overlay)
void Application::QueueLayerPop(Ref<Layer> layer)
{
layer_stack.PushOverlay(overlay);
layer_stack.QueueLayerPop(layer);
}
void Application::QueueOverlayPush(Ref<Layer> overlay)
{
layer_stack.QueueOverlayPush(overlay);
}
void Application::QueueOverlayPop(Ref<Layer> overlay)
{
layer_stack.QueueOverlayPop(overlay);
}
}

View File

@@ -253,6 +253,10 @@ namespace OpenEngine {
{
}
ImGuiLayer::~ImGuiLayer()
{
}
void ImGuiLayer::OnAttach()
{
IMGUI_CHECKVERSION();

View File

@@ -1,14 +1,15 @@
#include <pch.hpp>
#include "application.hpp"
#include "core.hpp"
#include "logging.hpp"
#include <input/linux_input.hpp>
#include <GLFW/glfw3.h>
namespace OpenEngine {
Input* Input::instance = new LinuxInput();
Scope<Input> Input::instance = CreateScope<LinuxInput>();
bool LinuxInput::IsKeyPressedImpl(int keycode)
{

View File

@@ -1,3 +1,4 @@
#include "logging.hpp"
#include <layer_stack.hpp>
namespace OpenEngine {
@@ -7,39 +8,71 @@ namespace OpenEngine {
LayerStack::~LayerStack()
{
for (Layer* layer : layers)
delete layer;
}
void LayerStack::PushLayer(Layer* layer)
{
layers.emplace(layers.begin() + layer_insert_index, layer);
layer_insert_index++;
layer->OnAttach();
}
void LayerStack::PushOverlay(Layer* overlay)
{
layers.emplace_back(overlay);
overlay->OnAttach();
}
void LayerStack::PopLayer(Layer* layer)
{
auto it = std::find(layers.begin(), layers.begin() + layer_insert_index, layer);
if (it != layers.end()) {
for (auto& layer : layers)
layer->OnDetach();
layers.erase(it);
layer_insert_index--;
}
}
void LayerStack::PopOverlay(Layer* overlay)
void LayerStack::QueueLayerPush(Ref<Layer> layer)
{
auto it = std::find(layers.begin() + layer_insert_index, layers.end(), overlay);
if (it != layers.end()) {
overlay->OnDetach();
layers.erase(it);
layers_to_push.emplace_back(layer);
}
void LayerStack::QueueLayerPop(Ref<Layer> layer)
{
layers_to_pop.emplace_back(layer);
}
void LayerStack::QueueOverlayPush(Ref<Layer> overlay)
{
overlay_to_push.emplace_back(overlay);
}
void LayerStack::QueueOverlayPop(Ref<Layer> overlay)
{
overlay_to_pop.emplace_back(overlay);
}
void LayerStack::UpdateLayers()
{
// processing pop queue layers
for (auto& layer : layers_to_pop) {
auto it = std::find(layers.begin(), layers.begin() + layer_insert_index, layer);
if (it != layers.end()) {
layer->OnDetach();
layers.erase(it);
layer_insert_index--;
}
}
layers_to_pop.clear();
// processing pop queue overlays
for (auto& overlay : overlay_to_pop) {
auto it = std::find(layers.begin() + layer_insert_index, layers.end(), overlay);
if (it != layers.end()) {
overlay->OnDetach();
layers.erase(it);
}
}
overlay_to_pop.clear();
// processing push queue layers
for (auto& layer : layers_to_push) {
layers.emplace(layers.begin() + layer_insert_index, layer);
layer_insert_index++;
layer->OnAttach();
OE_CORE_DEBUG("Added layer: {}", layer->GetName());
}
layers_to_push.clear();
// processing push queue overlays
for (auto& overlay : overlay_to_push) {
layers.emplace_back(overlay);
overlay->OnAttach();
}
overlay_to_push.clear();
}
}

View File

@@ -10,6 +10,8 @@ namespace OpenEngine {
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
}
void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height)

View File

@@ -114,8 +114,10 @@ namespace OpenEngine {
return;
}
for (int i = 0; i < sources.size(); i++)
for (int i = 0; i < sources.size(); i++) {
glDetachShader(program, gl_shader_ids[i]);
glDeleteShader(gl_shader_ids[i]);
}
}
std::unordered_map<GLenum, std::string> OpenGLShader::PreProcess(const std::string& shader_source)
@@ -173,27 +175,54 @@ namespace OpenEngine {
}
void OpenGLShader::SetBool(const std::string &name, bool value) const
{
glUniform1i(glGetUniformLocation(id, name.c_str()), (int)value);
{
UploadBool(name, value);
}
void OpenGLShader::SetInt(const std::string &name, int value) const
{
glUniform1i(glGetUniformLocation(id, name.c_str()), value);
{
UploadInt(name, value);
}
void OpenGLShader::SetFloat(const std::string &name, float value) const
{
glUniform1f(glGetUniformLocation(id, name.c_str()), value);
{
UploadFloat(name, value);
}
void OpenGLShader::SetMat4(const std::string &name, const glm::mat4& value) const
{
glUniformMatrix4fv(glGetUniformLocation(id, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
UploadMat4(name, value);
}
void OpenGLShader::SetVec3(const std::string &name, const glm::vec3& value) const
{
UploadVec3(name, value);
}
void OpenGLShader::SetVec4(const std::string &name, const glm::vec4& value) const
{
UploadVec4(name, value);
}
void OpenGLShader::UploadBool(const std::string &name, bool value) const
{
glUniform1i(glGetUniformLocation(id, name.c_str()), (int)value);
}
void OpenGLShader::UploadInt(const std::string &name, int value) const
{
glUniform1i(glGetUniformLocation(id, name.c_str()), value);
}
void OpenGLShader::UploadFloat(const std::string &name, float value) const
{
glUniform1f(glGetUniformLocation(id, name.c_str()), value);
}
void OpenGLShader::UploadMat4(const std::string &name, const glm::mat4& value) const
{
glUniformMatrix4fv(glGetUniformLocation(id, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
}
void OpenGLShader::UploadVec3(const std::string &name, const glm::vec3& value) const
{
glUniform3fv(glGetUniformLocation(id, name.c_str()), 1, glm::value_ptr(value));
}
void OpenGLShader::UploadVec4(const std::string &name, const glm::vec4& value) const
{
glUniform4fv(glGetUniformLocation(id, name.c_str()), 1, glm::value_ptr(value));
}
void OpenGLShader::CheckCompileErrors(unsigned int shader, const std::string& type)
{

View File

@@ -14,8 +14,8 @@ namespace OpenEngine {
: path(path)
{
int image_width, image_height, channels;
stbi_uc* data = stbi_load(path.c_str(), &image_width, &image_height, &channels, 0);
stbi_set_flip_vertically_on_load(1);
stbi_uc* data = stbi_load(path.c_str(), &image_width, &image_height, &channels, 0);
OE_CORE_ASSERT(data, "Image could not be loaded!");
width = image_width;

View File

@@ -1,9 +1,9 @@
#include <memory>
#include <pch.hpp>
#include <renderer/renderer.hpp>
#include "opengl/opengl_shader.hpp"
#include "renderer/render_command.hpp"
#include "open_engine/renderer/renderer.hpp"
#include "open_engine/renderer/render_command.hpp"
#include <memory>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
@@ -32,8 +32,8 @@ namespace OpenEngine {
const glm::mat4& transform)
{
shader->Bind();
std::dynamic_pointer_cast<OpenGLShader>(shader)->SetMat4("u_ViewProjection", scene_data->view_projection_matrix);
std::dynamic_pointer_cast<OpenGLShader>(shader)->SetMat4("u_Transform", transform);
shader->SetMat4("u_ViewProjection", scene_data->view_projection_matrix);
shader->SetMat4("u_Transform", transform);
vertex_array->Bind();
RenderCommand::DrawIndexed(vertex_array);

View File

@@ -2,15 +2,16 @@
#include "opengl/opengl_vertex_array.hpp"
#include "renderer/renderer.hpp"
#include <renderer/vertex_array.hpp>
#include <memory>
namespace OpenEngine {
VertexArray* VertexArray::Create()
Ref<VertexArray> VertexArray::Create()
{
switch (Renderer::GetApi()) {
case RendererAPI::API::None : OE_CORE_ASSERT(false, "No render API selected!"); return nullptr;
case RendererAPI::API::OpenGL : return new OpenGLVertexArray();
case RendererAPI::API::OpenGL : return std::make_shared<OpenGLVertexArray>();
}
OE_CORE_ASSERT(false, "Selected API not supported");

View File

@@ -5,6 +5,7 @@
#include "logging.hpp"
#include <GLFW/glfw3.h>
#include <memory>
#include <window/linux_window.hpp>
#include <opengl/opengl_context.hpp>
#include "renderer/graphics_context.hpp"
@@ -17,9 +18,9 @@ namespace OpenEngine {
OE_CORE_ERROR("GLFW Error {}: {}", error, description);
}
Window* Window::Create(const WindowProps& props)
std::unique_ptr<Window> Window::Create(const WindowProps& props)
{
return new LinuxWindow(props);
return std::make_unique<LinuxWindow>(props);
}
LinuxWindow::LinuxWindow(const WindowProps& props)
@@ -56,7 +57,7 @@ namespace OpenEngine {
gl_window = glfwCreateWindow((int)props.width, (int)props.height, props.title.c_str(), nullptr, nullptr);
context = new OpenGLContext(gl_window);
context = CreateScope<OpenGLContext>(gl_window);
context->Init();
glfwSetWindowUserPointer(gl_window, &data);
@@ -156,6 +157,7 @@ namespace OpenEngine {
void LinuxWindow::Shutdown()
{
glfwDestroyWindow(gl_window);
gl_window = nullptr;
}
void LinuxWindow::OnUpdate()