fix: make new entity system wrapper work

This commit is contained in:
2025-10-23 15:29:04 +02:00
parent 37d35f990d
commit a427fb7099
36 changed files with 70 additions and 66 deletions

View File

@ -3,7 +3,7 @@
#include <string>
namespace Engine {
namespace Core {
class FileManager
{

View File

@ -1,7 +1,7 @@
#ifndef PARSER_H_
#define PARSER_H_
namespace Engine {
namespace Core {
// Very fast OBJ/MTL line parser
class Parser {

View File

@ -9,10 +9,10 @@
#include "engine/app/app.h"
#include "engine/renderer/core.h"
extern Engine::IApplication* CreateApplication();
extern Core::IApplication* CreateApplication();
int main() {
auto engine = Engine::Engine::GetInstance();
engine->Run(std::unique_ptr<Engine::IApplication>(CreateApplication()));
auto engine = Core::Engine::GetInstance();
engine->Run(std::unique_ptr<Core::IApplication>(CreateApplication()));
return 0;
}

View File

@ -5,7 +5,7 @@
#include "engine/window/event.hpp"
#include "engine/export.h"
namespace Engine {
namespace Core {
class ENGINE_API IApplication {
public:
virtual ~IApplication() = default;

View File

@ -4,7 +4,7 @@
#include <glm/mat4x4.hpp>
#include "engine/export.h"
namespace Engine {
namespace Core {
// requires mesh component
struct ENGINE_API batch {
friend class Renderer;

View File

@ -3,7 +3,7 @@
#include "engine/export.h"
namespace Engine {
namespace Core {
struct ENGINE_API camera {};
}

View File

@ -6,7 +6,7 @@
#include "engine/renderer/renderer.h"
#include "engine/export.h"
namespace Engine {
namespace Core {
struct ENGINE_API light {
friend class Renderer;
public:

View File

@ -6,7 +6,7 @@
#include "engine/renderer/wavefront.h"
#include "engine/export.h"
namespace Engine {
namespace Core {
struct ENGINE_API mesh {
std::shared_ptr<Object> object;
};

View File

@ -3,7 +3,7 @@
#include "engine/export.h"
namespace Engine {
namespace Core {
struct ENGINE_API rotate {};
}

View File

@ -4,7 +4,7 @@
#include <glm/glm.hpp>
#include "engine/export.h"
namespace Engine {
namespace Core {
struct ENGINE_API transform {
glm::vec3 position;
glm::vec3 rotation;

View File

@ -3,7 +3,7 @@
#include <glm/glm.hpp>
namespace Engine {
namespace Core {
class Vertex {
friend class Mesh;

View File

@ -13,7 +13,7 @@
#include "engine/app/app.h"
#include "engine/export.h"
namespace Engine {
namespace Core {
class ENGINE_API Engine : public EventHandler {
public:

View File

@ -3,7 +3,7 @@
#include <GL/glew.h>
namespace Engine {
namespace Core {
void MessageCallback(GLenum source,
GLenum type,

View File

@ -6,7 +6,7 @@
#include "engine/renderer/texture.h"
namespace Engine {
namespace Core {
class Material {
private:

View File

@ -7,7 +7,7 @@
#include "engine/renderer/basics.h"
namespace Engine {
namespace Core {
class Mesh {
public: // TODO: abstract away

View File

@ -8,7 +8,7 @@
#include "engine/export.h"
#include "engine/components/light.h"
namespace Engine {
namespace Core {
// TODO: make static or singleton
class ENGINE_API Renderer {

View File

@ -7,7 +7,7 @@
#include "engine/export.h"
namespace Engine {
namespace Core {
class ENGINE_API Shader
{

View File

@ -3,7 +3,7 @@
#include <string>
#include <memory>
namespace Engine {
namespace Core {
class Texture {
public:

View File

@ -14,7 +14,7 @@
#include "engine/export.h"
namespace Engine {
namespace Core {
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };

View File

@ -3,12 +3,11 @@
#include <entt/entt.hpp>
#include "engine/export.h"
namespace Engine {
namespace Core {
class Entity;
class ENGINE_API Scene {
friend class Entity;
public:
Scene() = default;
@ -16,6 +15,7 @@ public:
private:
entt::registry m_registry;
friend class Renderer;
friend class Entity;
};
class ENGINE_API Entity {
@ -25,16 +25,24 @@ public:
Entity(const Entity& other) = default;
template<typename Type, typename... Args>
auto AddComponent(Args &&...args) {
assert(this->m_entity != entt::null && "Entity is empty");
assert(this->m_scene != nullptr && "Scene has not been assigned to the entity");
return m_scene->m_registry.emplace<Type>(m_entity, std::forward<Args>(args)...);
void AddComponent(Args&&... args) {
assert(m_entity != entt::null && "Entity is empty");
assert(m_scene && "Scene has not been assigned to the entity");
m_scene->m_registry.emplace<Type>(m_entity, std::forward<Args>(args)...);
}
template<typename Type>
[[nodiscard]] auto GetComponent() const {
assert(this->m_entity != entt::null && "Entity is empty");
assert(this->m_scene != nullptr && "Scene has not been assigned to the entity");
[[nodiscard]] Type& GetComponent() {
assert(m_entity != entt::null && "Entity is empty");
assert(m_scene && "Scene has not been assigned to the entity");
return m_scene->m_registry.get<Type>(m_entity);
}
template<typename Type>
[[nodiscard]] const Type& GetComponent() const {
assert(m_entity != entt::null && "Entity is empty");
assert(m_scene && "Scene has not been assigned to the entity");
return m_scene->m_registry.get<Type>(m_entity);
}
@ -47,4 +55,4 @@ private:
Scene *m_scene = nullptr;
};
} // namespace Engine
} // namespace Core

View File

@ -7,7 +7,7 @@
#include <unordered_map>
#include <vector>
namespace Engine {
namespace Core {
enum class EventType {
WINDOW_RESIZE,
@ -49,13 +49,13 @@ public:
EventEmitter() = default;
Handle Subscribe2(EventHandler* handler) {
Handle Subscribe(EventHandler* handler) {
auto slot = Slot{ m_next_id++, handler };
m_subs.push_back(slot);
return Handle{ slot.id };
}
void Unsubscribe2(const Handle& h) {
void UnSubscribe(const Handle& h) {
m_subs.erase(std::remove_if(m_subs.begin(), m_subs.end(),
[&](const Slot& s){ return s.id == h.id; }),
m_subs.end());

View File

@ -3,7 +3,7 @@
#include "engine/window/event.hpp"
namespace Engine {
namespace Core {
class WindowEvent : public Event {
public:

View File

@ -13,7 +13,7 @@
#define DEFAULT_WIDTH 1024
#define DEFAULT_HEIGHT 768
namespace Engine {
namespace Core {
class Window : public EventEmitter {
friend class Engine;

View File

@ -4,7 +4,7 @@
#include <iostream>
#include <sstream>
namespace Engine {
namespace Core {
FileManager::FileManager()
{

View File

@ -4,7 +4,7 @@
#include "engine/IO/parser.h"
namespace Engine {
namespace Core {
// Skip whitespace
void Parser::SkipSpaces() {

View File

@ -2,7 +2,7 @@
#include "engine/components/batch.h"
namespace Engine {
namespace Core {
unsigned int batch::LastID = 0;

View File

@ -5,7 +5,7 @@
#include "engine/window/event.hpp"
#include "engine/renderer/wavefront.h"
namespace Engine {
namespace Core {
Engine* Engine::s_instance = nullptr;
@ -19,17 +19,7 @@ void Engine::Run(std::unique_ptr<IApplication> app) {
m_app->OnInit(m_scene);
m_renderer->Init();
// m_window->Subscribe<WindowCloseEvent>([&](const WindowCloseEvent& e) {
// m_app->OnEvent(e);
// });
// m_window->Subscribe<WindowResizeEvent>([&](const WindowResizeEvent& e) {
// m_renderer->OnWindowResized(e.GetWidth(), e.GetHeight());
// m_app->OnEvent(e);
// });
m_window->Subscribe2(this);
m_window->Subscribe(this);
while (m_running) {
m_window->ProcessEvents();

View File

@ -2,7 +2,7 @@
#include <iostream>
namespace Engine {
namespace Core {
void MessageCallback(GLenum source,
GLenum type,

View File

@ -2,7 +2,7 @@
#include "engine/renderer/mesh.h"
namespace Engine {
namespace Core {
Mesh::Mesh() {
m_vao = 0;

View File

@ -8,6 +8,8 @@
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtx/string_cast.hpp>
#include "engine/renderer/renderer.h"
#include "engine/window/window.h"
#include "engine/IO/file_manager.h"
@ -18,7 +20,7 @@
#include "engine/components/mesh.h"
#include "engine/components/batch.h"
namespace Engine {
namespace Core {
Renderer::Renderer(std::shared_ptr<Scene> scene) : m_scene(scene)
{
@ -156,7 +158,7 @@ void Renderer::RenderScene(Shader &shader) {
auto prevInstanceVBO = b.m_instance_vbo;
b.prepare(models.data(), models.size());
if (prevInstanceVBO <= 0) {
std::cout << "[DEBUG] enabling batch"<<std::endl;
std::cout << "[DEBUG] enabling batch" << std::endl;
m.object->EnableBatch(b.m_instance_vbo);
}
m.object->Render(shader, batchItems.size());

View File

@ -2,7 +2,7 @@
#include <GL/glew.h>
#include "engine/renderer/shader.h"
namespace Engine {
namespace Core {
Shader::Shader()
{

View File

@ -7,7 +7,7 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
namespace Engine {
namespace Core {
std::unique_ptr<Texture> Texture::LoadFile(const std::string& filename) {
auto texture = std::make_unique<Texture>();

View File

@ -12,7 +12,7 @@
#define DEFAULT_MATERIAL_NAME "default"
namespace Engine {
namespace Core {
// ObjElement toElement(const std::string &s) {
// if (s == "#") return ObjElement::OHASH;

View File

@ -2,7 +2,7 @@
#include <iostream>
namespace Engine {
namespace Core {
Entity Scene::CreateEntity() {
Entity entity = { m_registry.create(), this };

View File

@ -8,7 +8,7 @@
#include "engine/renderer/debug.h"
namespace Engine {
namespace Core {
std::shared_ptr<Window> Window::s_instance = nullptr;

View File

@ -23,10 +23,11 @@
#include "engine/api.h"
using namespace Engine;
using namespace Core;
class Game : public IApplication {
public:
Game() = default;
~Game() override {}
void OnInit(std::shared_ptr<Scene> scene) override {
@ -42,6 +43,8 @@ public:
cameraEntity = scene->CreateEntity();
cameraEntity.AddComponent<camera>();
cameraEntity.AddComponent<transform>(glm::vec3(0.f, 2.f, 2.f));
assert(cameraEntity.HasComponent<camera>() && "Camera doesn't have required 'camera' component");
assert(cameraEntity.HasComponent<transform>() && "Camera doesn't have 'transform' component");
Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj");
modelEntity = scene->CreateEntity();
@ -58,7 +61,8 @@ public:
// Cube template (use shared object to avoid reloading 1000 times)
std::shared_ptr<Object> cubeObj = std::shared_ptr<Object>(Object::LoadFile("./assets/grass_block/grass_block.obj"));
auto batchEntt = scene->CreateEntity();
auto cubeBatch = batchEntt.AddComponent<batch>();
batchEntt.AddComponent<batch>();
auto& cubeBatch = batchEntt.GetComponent<batch>();
batchEntt.AddComponent<mesh>(cubeObj);
assert(batchEntt.HasComponent<batch>() && "batch doesn't have any batch component!");
assert(batchEntt.HasComponent<mesh>() && "batch doesn't have any mesh component!");
@ -138,7 +142,7 @@ public:
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
auto camTransform = cameraEntity.GetComponent<transform>();
auto& camTransform = cameraEntity.GetComponent<transform>();
camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
camTransform.rotation = cameraViewDirection;
@ -173,8 +177,8 @@ public:
glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor);
// Update the directional light in the registry
auto l = lightEntity.GetComponent<light>();
auto t = lightEntity.GetComponent<transform>();
auto& l = lightEntity.GetComponent<light>();
auto& t = lightEntity.GetComponent<transform>();
if (l.type == light::LightType::DIRECTIONAL) {
// "position" for directional light often stores direction vector
// If your system instead uses transform.rotation, adjust accordingly