various cleanup

This commit is contained in:
Erris
2026-02-10 09:42:59 +01:00
parent a53150d228
commit 86392e0790
13 changed files with 181 additions and 155 deletions

View File

@@ -16,6 +16,7 @@
#include "open_engine/input/keycodes.hpp"
#include "open_engine/renderer/render_command.hpp"
#include "open_engine/renderer/subtexture2d.hpp"
#include "open_engine/renderer/framebuffer.hpp"
#include "open_engine/renderer/renderer2d.hpp"
#include "open_engine/renderer/renderer.hpp"

View File

@@ -13,7 +13,7 @@ namespace OpenEngine {
class Application
{
public:
Application();
Application(const std::string& name = "OpenEngine Project");
~Application();
virtual void OnEvent(Event& event);
@@ -26,7 +26,8 @@ namespace OpenEngine {
inline static Application& Get() { return *instance; };
inline Window& GetWindow() { return *window; };
inline void StopRunning() { running = false; };
void Close();
private:
void Run();
@@ -34,6 +35,7 @@ namespace OpenEngine {
bool OnWindowResize(WindowResizeEvent& event);
private:
const std::string name;
std::unique_ptr<Window> window;
inline static Application* instance;

View File

@@ -14,52 +14,25 @@ namespace OpenEngine {
class Input
{
public:
virtual ~Input() = default;
Input(const Input&) = delete;
Input& operator=(const Input&) = delete;
static bool IsKeyPressed(KeyCode keycode);
inline static bool IsKeyPressed(KeyCode keycode) { return instance->IsKeyPressedImpl(keycode); };
static bool IsMouseButtonPressed(MouseCode button);
inline static bool IsMouseButtonPressed(MouseCode button) { return instance->IsMouseButtonPressedImpl(button); };
static std::pair<float, float> GetMousePosition();
static float GetMouseX();
static float GetMouseY();
inline static std::pair<float, float> GetMousePosition() { return instance->GetMousePositionImpl(); };
inline static bool GetMouseX() { return instance->GetMouseXImpl(); };
inline static bool GetMouseY() { return instance->GetMouseYImpl(); };
static bool JoystickExists(unsigned int joystick);
static std::map<unsigned int, std::string> GetJoystickList();
inline static bool JoystickExists(unsigned int joystick) { return instance->JoystickExistsImpl(joystick); };
inline static std::map<unsigned int, std::string> GetJoystickList() { return instance->GetJoystickListImpl(); };
static float GetJoystickAxis(unsigned int joystick, unsigned int axis);
static const float* GetJoystickAxes(unsigned int joystick);
static unsigned int GetJoystickAxesCount(unsigned int joystick);
inline static float GetJoystickAxis(unsigned int joystick, unsigned int axis) { return instance->GetJoystickAxisImpl(joystick, axis); };
inline static const float* GetJoystickAxes(unsigned int joystick) { return instance->GetJoystickAxesImpl(joystick); };
inline static unsigned int GetJoystickAxesCount(unsigned int joystick) { return instance->GetJoystickAxesCountImpl(joystick); };
inline static bool IsJoystickButtonPressed(unsigned int joystick, unsigned int button) { return instance->IsJoystickButtonPressedImpl(joystick, button); };
protected:
Input() = default;
virtual bool IsKeyPressedImpl(KeyCode keycode) = 0;
virtual bool IsMouseButtonPressedImpl(MouseCode button) = 0;
virtual std::pair<float, float> GetMousePositionImpl() = 0;
virtual float GetMouseXImpl() = 0;
virtual float GetMouseYImpl() = 0;
virtual bool JoystickExistsImpl(unsigned int joystick) = 0;
virtual std::map<unsigned int, std::string> GetJoystickListImpl() = 0;
virtual const std::string GetJoystickNameImpl(unsigned int joystick) = 0;
virtual float GetJoystickAxisImpl(unsigned int joystick, unsigned int axis) = 0;
virtual const float* GetJoystickAxesImpl(unsigned int joystick) = 0;
virtual unsigned int GetJoystickAxesCountImpl(unsigned int joystick) = 0;
virtual bool IsJoystickButtonPressedImpl(unsigned int joystick, unsigned int button) = 0;
static bool IsJoystickButtonPressed(unsigned int joystick, unsigned int button);
private:
inline static const std::string GetJoystickName(unsigned int joystick) { return instance->GetJoystickNameImpl(joystick); };
static Scope<Input> instance;
static const std::string GetJoystickName(unsigned int joystick);
};
}

View File

@@ -1,32 +0,0 @@
#ifndef LINUX_INPUT_HPP
#define LINUX_INPUT_HPP
#include "open_engine/input/input_system.hpp"
#include "open_engine/input/keycodes.hpp"
namespace OpenEngine {
class LinuxInput : public Input
{
protected:
virtual bool IsKeyPressedImpl(KeyCode keycode) override;
virtual bool IsMouseButtonPressedImpl(MouseCode button) override;
virtual std::pair<float, float> GetMousePositionImpl() override;
virtual float GetMouseXImpl() override;
virtual float GetMouseYImpl() override;
virtual bool JoystickExistsImpl(unsigned int joystick) override;
virtual float GetJoystickAxisImpl(unsigned int joystick, unsigned int axis) override;
virtual const std::string GetJoystickNameImpl(unsigned int joystick) override;
virtual std::map<unsigned int, std::string> GetJoystickListImpl() override;
virtual const float* GetJoystickAxesImpl(unsigned int joystick) override;
virtual unsigned int GetJoystickAxesCountImpl(unsigned int joystick) override;
virtual bool IsJoystickButtonPressedImpl(unsigned int joystick, unsigned int button) override;
};
}
#endif // LINUX_INPUT_HPP

View File

@@ -2,6 +2,7 @@
#define RENDERER2D_HPP
#include "open_engine/orthographic_camera.hpp"
#include "open_engine/renderer/subtexture2d.hpp"
#include "open_engine/renderer/texture.hpp"
#include "open_engine/ref_scope.hpp"
@@ -36,6 +37,7 @@ namespace OpenEngine {
static void DrawQuad(const Transform& transform_data, const glm::vec4& color);
static void DrawQuad(const Transform& transform_data, const Ref<Texture2D>& texture, float tiling_factor = 1.0f);
static void DrawQuad(const Transform& transform_data, const Ref<Subtexture2D>& subtexture, float tiling_factor = 1.0f);
static void ResetStats();
static const Statistics& GetStats();

View File

@@ -0,0 +1,28 @@
#ifndef SUBTEXTURE2D_HPP
#define SUBTEXTURE2D_HPP
#include "open_engine/renderer/texture.hpp"
#include <glm/glm.hpp>
namespace OpenEngine {
class Subtexture2D
{
public:
Subtexture2D(const Ref<Texture2D>& texture, glm::vec2& min, glm::vec2& max);
const Ref<Texture2D>& GetTexture() { return atlas; };
const glm::vec2* GetCoords() { return coords; };
static Ref<Subtexture2D> CreateFromCoords(const Ref<Texture2D> texture,
const glm::vec2& coordinates,
const glm::vec2& cell_size,
const glm::vec2& sprite_size = { 1.0f, 1.0f } );
private:
Ref<Texture2D> atlas;
glm::vec2 coords[4];
};
}
#endif // SUBTEXTURE2D_HPP

View File

@@ -10,12 +10,12 @@ namespace OpenEngine {
struct WindowProps
{
std::string title;
unsigned int width;
unsigned int height;
uint32_t width;
uint32_t height;
WindowProps(const std::string& title = "OpenEngine",
unsigned int width = 1280,
unsigned int height = 729)
uint32_t width = 1280,
uint32_t height = 729)
:title(title), width(width), height(height)
{
}
@@ -29,7 +29,7 @@ namespace OpenEngine {
struct WindowData
{
std::string title;
unsigned int width, height;
uint32_t width, height;
bool vsync;
EventCallbackFunction event_callback;
@@ -39,8 +39,8 @@ namespace OpenEngine {
virtual void OnUpdate() = 0;
virtual unsigned int GetWidth() const = 0;
virtual unsigned int GetHeight() const = 0;
virtual uint32_t GetWidth() const = 0;
virtual uint32_t GetHeight() const = 0;
virtual void SetEventCallback(const EventCallbackFunction& callback) = 0;
virtual void SetVSync(bool enabled) = 0;

View File

@@ -13,14 +13,15 @@
#include <imgui.h>
namespace OpenEngine {
Application::Application()
Application::Application(const std::string& name)
: name(name)
{
OE_PROFILE_FUNCTION();
OE_CORE_ASSERT(!instance, "Application already exists!");
instance = this;
window = Window::Create();
window = Window::Create(WindowProps(name));
window->SetEventCallback(BIND_EVENT_FN(Application::OnEvent));
{
@@ -90,11 +91,16 @@ namespace OpenEngine {
bool Application::OnWindowClose(WindowCloseEvent& event)
{
running = false;
Close();
return true;
}
void Application::Close()
{
running = false;
}
bool Application::OnWindowResize(WindowResizeEvent& event)
{
OE_PROFILE_FUNCTION();

View File

@@ -340,8 +340,10 @@ namespace OpenEngine {
void ImGuiLayer::OnEvent(Event& event)
{
/*
ImGuiIO& io = ImGui::GetIO();
event.handled |= event.IsInCategory(EventCategoryMouse) & io.WantCaptureMouse;
event.handled |= event.IsInCategory(EventCategoryKeyboard) & io.WantCaptureKeyboard;
*/
}
}

View File

@@ -1,7 +1,7 @@
#include <pch.hpp>
#include <core.hpp>
#include <input/linux_input.hpp>
#include <input/input_system.hpp>
#include <input/mouse_codes.hpp>
#include <input/keycodes.hpp>
#include <application.hpp>
@@ -11,9 +11,7 @@
namespace OpenEngine {
Scope<Input> Input::instance = CreateScope<LinuxInput>();
bool LinuxInput::IsKeyPressedImpl(KeyCode keycode)
bool Input::IsKeyPressed(KeyCode keycode)
{
auto window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow());
auto state = glfwGetKey(window, static_cast<int32_t>(keycode));
@@ -21,7 +19,7 @@ namespace OpenEngine {
return state == GLFW_PRESS || state == GLFW_REPEAT;
}
bool LinuxInput::IsMouseButtonPressedImpl(MouseCode keycode)
bool Input::IsMouseButtonPressed(MouseCode keycode)
{
auto window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow());
auto state = glfwGetMouseButton(window, static_cast<int32_t>(keycode));
@@ -29,7 +27,7 @@ namespace OpenEngine {
return state == GLFW_PRESS;
}
std::pair<float, float> LinuxInput::GetMousePositionImpl()
std::pair<float, float> Input::GetMousePosition()
{
auto window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow());
@@ -39,16 +37,16 @@ namespace OpenEngine {
return {(float)x, (float)y};
}
float LinuxInput::GetMouseXImpl()
float Input::GetMouseX()
{
auto [x, y] = GetMousePositionImpl();
auto [x, y] = GetMousePosition();
return (float)x;
}
float LinuxInput::GetMouseYImpl()
float Input::GetMouseY()
{
auto [x, y] = GetMousePositionImpl();
auto [x, y] = GetMousePosition();
return (float)y;
@@ -56,16 +54,16 @@ namespace OpenEngine {
auto axes = glfwGetJoystickAxes(0, &count);
}
bool LinuxInput::JoystickExistsImpl(unsigned int joystick)
bool Input::JoystickExists(unsigned int joystick)
{
bool status = glfwJoystickPresent(joystick);
return status;
}
const float* LinuxInput::GetJoystickAxesImpl(unsigned int joystick)
const float* Input::GetJoystickAxes(unsigned int joystick)
{
if (!JoystickExistsImpl(joystick)) {
if (!JoystickExists(joystick)) {
OE_CORE_WARN("Joystick number {} is not present.", joystick);
return nullptr;
}
@@ -76,9 +74,9 @@ namespace OpenEngine {
return axes;
}
float LinuxInput::GetJoystickAxisImpl(unsigned int joystick, unsigned int axis)
float Input::GetJoystickAxis(unsigned int joystick, unsigned int axis)
{
if (!JoystickExistsImpl(joystick)) {
if (!JoystickExists(joystick)) {
OE_CORE_WARN("Joystick number {} is not present.", joystick);
return 0.0f;
}
@@ -94,7 +92,7 @@ namespace OpenEngine {
return axes[axis];
}
const std::string LinuxInput::GetJoystickNameImpl(unsigned int joystick)
const std::string Input::GetJoystickName(unsigned int joystick)
{
auto name = glfwGetJoystickName(joystick);
@@ -106,7 +104,7 @@ namespace OpenEngine {
return name;
}
std::map<unsigned int, std::string> LinuxInput::GetJoystickListImpl()
std::map<unsigned int, std::string> Input::GetJoystickList()
{
std::map<unsigned int, std::string> joysticks;
@@ -120,9 +118,9 @@ namespace OpenEngine {
return joysticks;
}
unsigned int LinuxInput::GetJoystickAxesCountImpl(unsigned int joystick)
unsigned int Input::GetJoystickAxesCount(unsigned int joystick)
{
if (!JoystickExistsImpl(joystick)) {
if (!JoystickExists(joystick)) {
OE_CORE_WARN("Joystick number {} is not present.", joystick);
return 0;
}
@@ -133,9 +131,9 @@ namespace OpenEngine {
return count;
}
bool LinuxInput::IsJoystickButtonPressedImpl(unsigned int joystick, unsigned int button)
bool Input::IsJoystickButtonPressed(unsigned int joystick, unsigned int button)
{
if (!JoystickExistsImpl(joystick))
if (!JoystickExists(joystick))
OE_CORE_WARN("Joystick number {} is not present.", joystick);
int count;

View File

@@ -164,6 +164,9 @@ namespace OpenEngine {
{
OE_PROFILE_FUNCTION();
constexpr size_t quad_vertex_count = 4;
constexpr glm::vec2 texture_coords[] = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, { 0.0f, 1.0f }};
if (renderer_data.quad_index_count >= Renderer2DData::max_indices)
FlushAndReset();
@@ -193,33 +196,64 @@ namespace OpenEngine {
* glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), { 0.0f, 0.0f, 1.0f })
* glm::scale(glm::mat4(1.0f), transform_data.size);
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[0];
for (size_t i = 0; i < quad_vertex_count; i++) {
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[i];
renderer_data.quad_vertex_ptr->color = color;
renderer_data.quad_vertex_ptr->tex_coord = { 0.0f, 0.0f };
renderer_data.quad_vertex_ptr->tex_coord = texture_coords[i];
renderer_data.quad_vertex_ptr->tex_index = texture_index;
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
renderer_data.quad_vertex_ptr++;
}
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[1];
renderer_data.quad_vertex_ptr->color = color;
renderer_data.quad_vertex_ptr->tex_coord = {1.0f, 0.0f};
renderer_data.quad_vertex_ptr->tex_index = texture_index;
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
renderer_data.quad_vertex_ptr++;
renderer_data.quad_index_count += 6;
renderer_data.stats.quad_count++;
}
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[2];
renderer_data.quad_vertex_ptr->color = color;
renderer_data.quad_vertex_ptr->tex_coord = {1.0f, 1.0f};
renderer_data.quad_vertex_ptr->tex_index = texture_index;
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
renderer_data.quad_vertex_ptr++;
void Renderer2D::DrawQuad(const Transform& transform_data, const Ref<Subtexture2D>& subtexture, float tiling_factor)
{
OE_PROFILE_FUNCTION();
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[3];
constexpr size_t quad_vertex_count = 4;
const glm::vec2* texture_coords = subtexture->GetCoords();
const Ref<Texture2D>& texture = subtexture->GetTexture();
if (renderer_data.quad_index_count >= Renderer2DData::max_indices)
FlushAndReset();
constexpr glm::vec4 color = glm::vec4(1.0f);
glm::vec3 position = transform_data.position;
glm::vec3 size = transform_data.size;
float texture_index = 0;
for (uint32_t i = 1; i < renderer_data.texture_slot_index; i++) {
if (*renderer_data.texture_slots[i].get() == *texture.get()) {
texture_index = (float)i;
break;
}
}
if (texture_index == 0) {
if (renderer_data.texture_slot_index >= Renderer2DData::max_texture_slots)
FlushAndReset();
texture_index = (float)renderer_data.texture_slot_index;
renderer_data.texture_slots[renderer_data.texture_slot_index] = texture;
renderer_data.texture_slot_index++;
}
glm::mat4 transform = glm::translate(glm::mat4(1.0f), transform_data.position)
* glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), { 0.0f, 0.0f, 1.0f })
* glm::scale(glm::mat4(1.0f), transform_data.size);
for (size_t i = 0; i < quad_vertex_count; i++) {
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[i];
renderer_data.quad_vertex_ptr->color = color;
renderer_data.quad_vertex_ptr->tex_coord = {0.0f, 1.0f};
renderer_data.quad_vertex_ptr->tex_coord = texture_coords[i];
renderer_data.quad_vertex_ptr->tex_index = texture_index;
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
renderer_data.quad_vertex_ptr++;
}
renderer_data.quad_index_count += 6;
renderer_data.stats.quad_count++;
@@ -229,6 +263,9 @@ namespace OpenEngine {
{
OE_PROFILE_FUNCTION();
constexpr size_t quad_vertex_count = 4;
constexpr glm::vec2 texture_coords[] = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, { 0.0f, 1.0f }};
if (renderer_data.quad_index_count >= Renderer2DData::max_indices)
FlushAndReset();
@@ -239,33 +276,15 @@ namespace OpenEngine {
* glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), { 0.0f, 0.0f, 1.0f })
* glm::scale(glm::mat4(1.0f), transform_data.size);
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[0];
renderer_data.quad_vertex_ptr->color = color;
renderer_data.quad_vertex_ptr->tex_coord = { 0.0f, 0.0f };
renderer_data.quad_vertex_ptr->tex_index = 0;
renderer_data.quad_vertex_ptr->tiling_factor = 0;
renderer_data.quad_vertex_ptr++;
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[1];
for (size_t i = 0; i < quad_vertex_count; i++) {
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[i];
renderer_data.quad_vertex_ptr->color = color;
renderer_data.quad_vertex_ptr->tex_coord = {1.0f, 0.0f};
renderer_data.quad_vertex_ptr->tex_coord = texture_coords[i];
renderer_data.quad_vertex_ptr->tex_index = 0;
renderer_data.quad_vertex_ptr->tiling_factor = 0;
renderer_data.quad_vertex_ptr++;
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[2];
renderer_data.quad_vertex_ptr->color = color;
renderer_data.quad_vertex_ptr->tex_coord = {1.0f, 1.0f};
renderer_data.quad_vertex_ptr->tex_index = 0;
renderer_data.quad_vertex_ptr->tiling_factor = 0;
renderer_data.quad_vertex_ptr++;
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[3];
renderer_data.quad_vertex_ptr->color = color;
renderer_data.quad_vertex_ptr->tex_coord = {0.0f, 1.0f};
renderer_data.quad_vertex_ptr->tex_index = 0;
renderer_data.quad_vertex_ptr->tiling_factor = 0;
renderer_data.quad_vertex_ptr->tiling_factor = 1.0f;
renderer_data.quad_vertex_ptr++;
}
renderer_data.quad_index_count += 6;
renderer_data.stats.quad_count++;

View File

@@ -0,0 +1,27 @@
#include <pch.hpp>
#include <renderer/subtexture2d.hpp>
#include <ref_scope.hpp>
namespace OpenEngine {
Subtexture2D::Subtexture2D(const Ref<Texture2D>& texture, glm::vec2& min, glm::vec2& max)
: atlas(texture)
{
coords[0] = { min.x, min.y };
coords[1] = { max.x, min.y };
coords[2] = { max.x, max.y };
coords[3] = { min.x, max.y };
}
Ref<Subtexture2D> Subtexture2D::CreateFromCoords(const Ref<Texture2D> texture,
const glm::vec2& coordinates,
const glm::vec2& cell_size,
const glm::vec2& sprite_size)
{
glm::vec2 min = { (coordinates.x * cell_size.x) / texture->GetWidth(), (coordinates.y * cell_size.y) / texture->GetHeight() };
glm::vec2 max = { ((coordinates.x + sprite_size.x) * cell_size.x) / texture->GetWidth(), ((coordinates.y + sprite_size.y) * cell_size.y) / texture->GetHeight() };
return CreateRef<Subtexture2D>(texture, min, max);
}
}

View File

@@ -121,7 +121,7 @@ namespace OpenEngine {
}
});
glfwSetCharCallback(gl_window, [] (GLFWwindow* window, unsigned int keycode)
glfwSetCharCallback(gl_window, [] (GLFWwindow* window, uint32_t keycode)
{
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);