Compare commits

...

2 Commits

Author SHA1 Message Date
4798c52e51 feat: scene + entity class 2025-10-22 15:23:51 +02:00
ea593feb8d feat: refactoring 2025-10-22 14:25:02 +02:00
37 changed files with 221 additions and 77 deletions

View File

@ -5,9 +5,8 @@ set(SOURCES
src/IO/file_manager.cpp src/IO/file_manager.cpp
src/renderer/debug.cpp src/renderer/debug.cpp
src/window/window.cpp
src/scene/scene.cpp src/scene/scene.cpp
src/window/window.cpp
src/components/batch.cpp src/components/batch.cpp
src/renderer/mesh.cpp src/renderer/mesh.cpp

View File

@ -3,6 +3,8 @@
#include <string> #include <string>
namespace Engine {
class FileManager class FileManager
{ {
public: public:
@ -12,4 +14,6 @@ public:
static std::string read(const std::string &filename); static std::string read(const std::string &filename);
}; };
}
#endif // FILE_MANAGER_H #endif // FILE_MANAGER_H

View File

@ -1,6 +1,8 @@
#ifndef PARSER_H_ #ifndef PARSER_H_
#define PARSER_H_ #define PARSER_H_
namespace Engine {
// Very fast OBJ/MTL line parser // Very fast OBJ/MTL line parser
class Parser { class Parser {
private: private:
@ -17,4 +19,6 @@ public:
int TakeIndex(int baseCount); int TakeIndex(int baseCount);
}; };
}
#endif // PARSER_H_ #endif // PARSER_H_

View File

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

View File

@ -5,8 +5,9 @@
#include "engine/window/event.hpp" #include "engine/window/event.hpp"
#include "engine/export.h" #include "engine/export.h"
class ENGINE_API IApplication { namespace Engine {
public: class ENGINE_API IApplication {
public:
virtual ~IApplication() = default; virtual ~IApplication() = default;
virtual void OnInit(std::shared_ptr<Scene> scene) {}; virtual void OnInit(std::shared_ptr<Scene> scene) {};
@ -14,6 +15,7 @@ public:
virtual void OnShutdown() {}; virtual void OnShutdown() {};
virtual void OnEvent(const Event& event) {}; virtual void OnEvent(const Event& event) {};
}; };
}
#endif // APPLICATION_H_ #endif // APPLICATION_H_

View File

@ -4,6 +4,7 @@
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include "engine/export.h" #include "engine/export.h"
namespace Engine {
// requires mesh component // requires mesh component
struct ENGINE_API batch { struct ENGINE_API batch {
friend class Renderer; friend class Renderer;
@ -25,5 +26,6 @@ private:
private: private:
void prepare(glm::mat4 *instances, unsigned int count); void prepare(glm::mat4 *instances, unsigned int count);
}; };
}
#endif // COMPONENT_BATCH_H_ #endif // COMPONENT_BATCH_H_

View File

@ -3,6 +3,8 @@
#include "engine/export.h" #include "engine/export.h"
namespace Engine {
struct ENGINE_API camera {}; struct ENGINE_API camera {};
}
#endif // COMPONENTS_PLAYER_H_ #endif // COMPONENTS_PLAYER_H_

View File

@ -6,6 +6,7 @@
#include "engine/renderer/renderer.h" #include "engine/renderer/renderer.h"
#include "engine/export.h" #include "engine/export.h"
namespace Engine {
struct ENGINE_API light { struct ENGINE_API light {
friend class Renderer; friend class Renderer;
public: public:
@ -25,5 +26,6 @@ private:
glm::mat4 lightSpace; glm::mat4 lightSpace;
int shadowRes{1024}; int shadowRes{1024};
}; };
}
#endif // COMPONENTS_LIGHT_H_ #endif // COMPONENTS_LIGHT_H_

View File

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

View File

@ -3,6 +3,8 @@
#include "engine/export.h" #include "engine/export.h"
namespace Engine {
struct ENGINE_API rotate {}; struct ENGINE_API rotate {};
}
#endif // COMPONENT_ROTATE_H_ #endif // COMPONENT_ROTATE_H_

View File

@ -4,10 +4,12 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "engine/export.h" #include "engine/export.h"
namespace Engine {
struct ENGINE_API transform { struct ENGINE_API transform {
glm::vec3 position; glm::vec3 position;
glm::vec3 rotation; glm::vec3 rotation;
glm::vec3 scale; glm::vec3 scale;
}; };
}
#endif // COMPONENTS_TRANSFORM_H_ #endif // COMPONENTS_TRANSFORM_H_

View File

@ -3,6 +3,8 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
namespace Engine {
class Vertex { class Vertex {
friend class Mesh; friend class Mesh;
private: private:
@ -14,4 +16,6 @@ public:
: m_position(position), m_normal(normal), m_texCoord(texCoord) {} : m_position(position), m_normal(normal), m_texCoord(texCoord) {}
}; };
}
#endif // RENDERER_BASICS_H #endif // RENDERER_BASICS_H

View File

@ -13,6 +13,8 @@
#include "engine/app/app.h" #include "engine/app/app.h"
#include "engine/export.h" #include "engine/export.h"
namespace Engine {
class ENGINE_API Engine : public EventHandler { class ENGINE_API Engine : public EventHandler {
public: public:
static Engine* GetInstance(); static Engine* GetInstance();
@ -31,5 +33,6 @@ private:
bool m_running; bool m_running;
}; };
}
#endif // ENGINE_H_ #endif // ENGINE_H_

View File

@ -3,6 +3,8 @@
#include <GL/glew.h> #include <GL/glew.h>
namespace Engine {
void MessageCallback(GLenum source, void MessageCallback(GLenum source,
GLenum type, GLenum type,
GLuint id, GLuint id,
@ -11,4 +13,6 @@ void MessageCallback(GLenum source,
const GLchar* message, const GLchar* message,
const void* userParam); const void* userParam);
}
#endif // RENDERER_DEBUG_ #endif // RENDERER_DEBUG_

View File

@ -6,6 +6,8 @@
#include "engine/renderer/texture.h" #include "engine/renderer/texture.h"
namespace Engine {
class Material { class Material {
private: private:
glm::vec3 m_ambient { 0.2f, 0.2f, 0.2f }; glm::vec3 m_ambient { 0.2f, 0.2f, 0.2f };
@ -39,4 +41,6 @@ public:
inline void SetIllumination(float illum) { m_illum = illum; } inline void SetIllumination(float illum) { m_illum = illum; }
}; };
}
#endif // MATERIAL_H_ #endif // MATERIAL_H_

View File

@ -7,6 +7,8 @@
#include "engine/renderer/basics.h" #include "engine/renderer/basics.h"
namespace Engine {
class Mesh { class Mesh {
public: // TODO: abstract away public: // TODO: abstract away
unsigned int m_vao, m_vbo, m_ebo; unsigned int m_vao, m_vbo, m_ebo;
@ -24,4 +26,6 @@ public:
void Render(unsigned int count); void Render(unsigned int count);
}; };
}
#endif // MESH_H_ #endif // MESH_H_

View File

@ -8,6 +8,8 @@
#include "engine/export.h" #include "engine/export.h"
#include "engine/components/light.h" #include "engine/components/light.h"
namespace Engine {
// TODO: make static or singleton // TODO: make static or singleton
class ENGINE_API Renderer { class ENGINE_API Renderer {
public: public:
@ -37,4 +39,6 @@ private:
glm::mat4 m_view; glm::mat4 m_view;
}; };
}
#endif // RENDERER_H_ #endif // RENDERER_H_

View File

@ -7,6 +7,8 @@
#include "engine/export.h" #include "engine/export.h"
namespace Engine {
class ENGINE_API Shader class ENGINE_API Shader
{ {
public: public:
@ -45,4 +47,6 @@ private:
void checkLinkingError(); void checkLinkingError();
}; };
}
#endif // SHADER_H #endif // SHADER_H

View File

@ -3,6 +3,8 @@
#include <string> #include <string>
#include <memory> #include <memory>
namespace Engine {
class Texture { class Texture {
public: public:
Texture() : m_id(0) {} Texture() : m_id(0) {}
@ -13,4 +15,6 @@ private:
unsigned int m_id; unsigned int m_id;
}; };
}
#endif // TEXTURE_H_ #endif // TEXTURE_H_

View File

@ -14,6 +14,8 @@
#include "engine/export.h" #include "engine/export.h"
namespace Engine {
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN }; 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 }; enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
@ -53,4 +55,6 @@ private:
std::unordered_map<std::string, std::shared_ptr<Material>> m_materials; std::unordered_map<std::string, std::shared_ptr<Material>> m_materials;
}; };
}
#endif // MODEL_H_ #endif // MODEL_H_

View File

@ -1,15 +1,44 @@
#ifndef ENGINE_SCENE_H_ #pragma once
#define ENGINE_SCENE_H_
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include <memory>
namespace Engine {
class Entity;
class Scene { class Scene {
private:
friend class Entity;
public: public:
Scene(); Scene() = default;
Entity CreateEntity();
private: private:
entt::registry m_registry; entt::registry m_registry;
friend class Renderer; friend class Renderer;
friend class Game;
}; };
#endif // ENGINE_SCENE_H_ class Entity {
public:
Entity() = default;
Entity(entt::entity entity, Scene* scene) : m_entity(entity), m_scene(scene) {}
Entity(const Entity& other) = default;
template<typename Type, typename... Args>
inline auto AddComponent(Args &&...args) {
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)...);
}
template<typename Type>
[[nodiscard]] inline auto GetComponent() {
assert(this->m_scene != nullptr && "Scene has not been assigned to the entity");
return m_scene->m_registry.get<Type>(m_entity);
}
private:
entt::entity m_entity { 0 };
Scene *m_scene = nullptr;
};
} // namespace Engine

View File

@ -7,6 +7,8 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
namespace Engine {
enum class EventType { enum class EventType {
WINDOW_RESIZE, WINDOW_RESIZE,
WINDOW_CLOSE, WINDOW_CLOSE,
@ -71,4 +73,6 @@ private:
std::size_t m_next_id = 1; std::size_t m_next_id = 1;
}; };
}
#endif // EVENT_H_ #endif // EVENT_H_

View File

@ -3,6 +3,8 @@
#include "engine/window/event.hpp" #include "engine/window/event.hpp"
namespace Engine {
class WindowEvent : public Event { class WindowEvent : public Event {
public: public:
WindowEvent() : Event(Event::EventCategory::WINDOW) {} WindowEvent() : Event(Event::EventCategory::WINDOW) {}
@ -27,4 +29,6 @@ public:
WindowCloseEvent() {} WindowCloseEvent() {}
}; };
}
#endif // WINDOW_EVENTS_H_ #endif // WINDOW_EVENTS_H_

View File

@ -13,6 +13,8 @@
#define DEFAULT_WIDTH 1024 #define DEFAULT_WIDTH 1024
#define DEFAULT_HEIGHT 768 #define DEFAULT_HEIGHT 768
namespace Engine {
class Window : public EventEmitter { class Window : public EventEmitter {
friend class Engine; friend class Engine;
private: private:
@ -48,4 +50,6 @@ private:
int m_height; int m_height;
}; };
}
#endif //WINDOW_H_ #endif //WINDOW_H_

View File

@ -4,6 +4,8 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
namespace Engine {
FileManager::FileManager() FileManager::FileManager()
{ {
} }
@ -31,3 +33,5 @@ std::string FileManager::read(const std::string &filename)
return fileStream.str(); return fileStream.str();
} }
}

View File

@ -4,6 +4,8 @@
#include "engine/IO/parser.h" #include "engine/IO/parser.h"
namespace Engine {
// Skip whitespace // Skip whitespace
void Parser::SkipSpaces() { void Parser::SkipSpaces() {
while (*m_sv == ' ' || *m_sv == '\t') ++m_sv; while (*m_sv == ' ' || *m_sv == '\t') ++m_sv;
@ -121,3 +123,5 @@ bool Parser::TakeFaceIndices(int &vi, int &ti, int &ni) {
// Do NOT mutate indices (leave them raw). Let NormalizeIndex handle conversion. // Do NOT mutate indices (leave them raw). Let NormalizeIndex handle conversion.
return true; return true;
} }
}

View File

@ -2,6 +2,8 @@
#include "engine/components/batch.h" #include "engine/components/batch.h"
namespace Engine {
unsigned int batch::LastID = 0; unsigned int batch::LastID = 0;
batch::batch() { batch::batch() {
@ -28,3 +30,5 @@ void batch::prepare(glm::mat4 *instances, unsigned int count) {
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::mat4) * count, instances); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::mat4) * count, instances);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
}

View File

@ -5,6 +5,8 @@
#include "engine/window/event.hpp" #include "engine/window/event.hpp"
#include "engine/renderer/wavefront.h" #include "engine/renderer/wavefront.h"
namespace Engine {
Engine* Engine::s_instance = nullptr; Engine* Engine::s_instance = nullptr;
void Engine::Run(std::unique_ptr<IApplication> app) { void Engine::Run(std::unique_ptr<IApplication> app) {
@ -66,3 +68,4 @@ Engine* Engine::GetInstance() {
return s_instance; return s_instance;
} }
}

View File

@ -2,6 +2,8 @@
#include <iostream> #include <iostream>
namespace Engine {
void MessageCallback(GLenum source, void MessageCallback(GLenum source,
GLenum type, GLenum type,
GLuint id, GLuint id,
@ -59,3 +61,5 @@ void MessageCallback(GLenum source,
// (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), // (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
// type, severity, message); // type, severity, message);
} }
}

View File

@ -2,6 +2,8 @@
#include "engine/renderer/mesh.h" #include "engine/renderer/mesh.h"
namespace Engine {
Mesh::Mesh() { Mesh::Mesh() {
m_vao = 0; m_vao = 0;
m_vbo = 0; m_vbo = 0;
@ -58,3 +60,5 @@ void Mesh::Render(unsigned int count)
} }
Unbind(); Unbind();
} }
}

View File

@ -18,6 +18,8 @@
#include "engine/components/mesh.h" #include "engine/components/mesh.h"
#include "engine/components/batch.h" #include "engine/components/batch.h"
namespace Engine {
Renderer::Renderer(std::shared_ptr<Scene> scene) : m_scene(scene) Renderer::Renderer(std::shared_ptr<Scene> scene) : m_scene(scene)
{ {
m_proj = glm::perspective( m_proj = glm::perspective(
@ -246,3 +248,5 @@ void Renderer::Render() {
UpdateView(); UpdateView();
RenderScene(m_shader); RenderScene(m_shader);
} }
}

View File

@ -2,6 +2,8 @@
#include <GL/glew.h> #include <GL/glew.h>
#include "engine/renderer/shader.h" #include "engine/renderer/shader.h"
namespace Engine {
Shader::Shader() Shader::Shader()
{ {
} }
@ -133,3 +135,5 @@ void Shader::checkLinkingError()
<< std::endl; << std::endl;
} }
} }
}

View File

@ -7,6 +7,8 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
namespace Engine {
std::unique_ptr<Texture> Texture::LoadFile(const std::string& filename) { std::unique_ptr<Texture> Texture::LoadFile(const std::string& filename) {
auto texture = std::make_unique<Texture>(); auto texture = std::make_unique<Texture>();
@ -34,3 +36,5 @@ std::unique_ptr<Texture> Texture::LoadFile(const std::string& filename) {
return std::move(texture); return std::move(texture);
} }
}

View File

@ -12,6 +12,8 @@
#define DEFAULT_MATERIAL_NAME "default" #define DEFAULT_MATERIAL_NAME "default"
namespace Engine {
// ObjElement toElement(const std::string &s) { // ObjElement toElement(const std::string &s) {
// if (s == "#") return ObjElement::OHASH; // if (s == "#") return ObjElement::OHASH;
// if (s == "mtllib") return ObjElement::MTLLIB; // if (s == "mtllib") return ObjElement::MTLLIB;
@ -509,4 +511,4 @@ void Object::Render(Shader& shader, unsigned int count)
} }
} }
}

View File

@ -1,4 +1,9 @@
#include "engine/scene/scene.h" #include "engine/scene/scene.h"
Scene::Scene() : m_registry() { namespace Engine {
Entity Scene::CreateEntity() {
return { m_registry.create(), this };
}
} }

View File

@ -8,6 +8,8 @@
#include "engine/renderer/debug.h" #include "engine/renderer/debug.h"
namespace Engine {
std::shared_ptr<Window> Window::s_instance = nullptr; std::shared_ptr<Window> Window::s_instance = nullptr;
Window::Window(const char* title, int width, int height) { Window::Window(const char* title, int width, int height) {
@ -100,12 +102,10 @@ void Window::ProcessEvents() {
switch (event.type) { switch (event.type) {
case SDL_EVENT_WINDOW_CLOSE_REQUESTED: case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
case SDL_EVENT_QUIT: case SDL_EVENT_QUIT:
Dispatch(WindowCloseEvent());
EmitEvent(WindowCloseEvent{}); EmitEvent(WindowCloseEvent{});
break; break;
case SDL_EVENT_KEY_DOWN: case SDL_EVENT_KEY_DOWN:
if (event.key.scancode == SDL_SCANCODE_ESCAPE) { if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
Dispatch(WindowCloseEvent());
EmitEvent(WindowCloseEvent{}); EmitEvent(WindowCloseEvent{});
} }
if (event.key.scancode == SDL_SCANCODE_F11) { if (event.key.scancode == SDL_SCANCODE_F11) {
@ -125,7 +125,6 @@ void Window::ProcessEvents() {
width, width,
height); height);
auto event = WindowResizeEvent(static_cast<unsigned int>(m_width), static_cast<unsigned int>(m_height)); auto event = WindowResizeEvent(static_cast<unsigned int>(m_width), static_cast<unsigned int>(m_height));
Dispatch(event);
EmitEvent(event); EmitEvent(event);
SDL_SetWindowRelativeMouseMode(m_handle, true); SDL_SetWindowRelativeMouseMode(m_handle, true);
SDL_Rect boundaries = {0, 0, m_width, m_height}; SDL_Rect boundaries = {0, 0, m_width, m_height};
@ -152,3 +151,4 @@ void Window::Destroy() const {
SDL_DestroyWindow(m_handle); SDL_DestroyWindow(m_handle);
} }
}

View File

@ -19,8 +19,12 @@
#include "engine/components/rotate.h" #include "engine/components/rotate.h"
#include "engine/components/batch.h" #include "engine/components/batch.h"
#include "engine/scene/scene.h"
#include "engine/api.h" #include "engine/api.h"
using namespace Engine;
class Game : public IApplication { class Game : public IApplication {
public: public:
Game() {} Game() {}
@ -30,49 +34,49 @@ public:
m_scene = scene; m_scene = scene;
Object* lightObj = Object::LoadFile("./assets/common/sphere/sphere.obj"); Object* lightObj = Object::LoadFile("./assets/common/sphere/sphere.obj");
const auto lght = scene->m_registry.create(); lightEntity = scene->CreateEntity();
scene->m_registry.emplace<transform>(lght, glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f)); lightEntity.AddComponent<transform>(glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
scene->m_registry.emplace<light>(lght, light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f); lightEntity.AddComponent<light>(light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
scene->m_registry.emplace<mesh>(lght, std::shared_ptr<Object>(lightObj)); lightEntity.AddComponent<mesh>(std::shared_ptr<Object>(lightObj));
const auto cameraEntity = scene->m_registry.create(); cameraEntity = scene->CreateEntity();
scene->m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f)); cameraEntity.AddComponent<camera>();
scene->m_registry.emplace<camera>(cameraEntity); cameraEntity.AddComponent<transform>(glm::vec3(0.f, 2.f, 2.f));
Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj"); Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj");
const auto targetEntity = scene->m_registry.create(); modelEntity = scene->CreateEntity();
scene->m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.0f, 0.f)); modelEntity.AddComponent<transform>(glm::vec3(0.f, 0.0f, 0.f));
scene->m_registry.emplace<mesh>(targetEntity, std::shared_ptr<Object>(targetObj)); modelEntity.AddComponent<mesh>(std::shared_ptr<Object>(targetObj));
scene->m_registry.emplace<rotate>(targetEntity); modelEntity.AddComponent<rotate>();
Object* grass = Object::LoadFile("./assets/common/cube/cube.obj"); // Object* grass = Object::LoadFile("./assets/common/cube/cube.obj");
const auto cubeEntity = scene->m_registry.create(); // const auto cubeEntity = scene->m_registry.create();
scene->m_registry.emplace<transform>(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f)); // scene->m_registry.emplace<transform>(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f));
scene->m_registry.emplace<mesh>(cubeEntity, std::shared_ptr<Object>(grass)); // scene->m_registry.emplace<mesh>(cubeEntity, std::shared_ptr<Object>(grass));
// Cube template (use shared object to avoid reloading 1000 times) // 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")); std::shared_ptr<Object> cubeObj = std::shared_ptr<Object>(Object::LoadFile("./assets/grass_block/grass_block.obj"));
const auto batchEntt = scene->m_registry.create(); auto batchEntt = scene->CreateEntity();
scene->m_registry.emplace<batch>(batchEntt); auto cubeBatch = batchEntt.AddComponent<batch>();
scene->m_registry.emplace<mesh>(batchEntt, cubeObj); batchEntt.AddComponent<mesh>(cubeObj);
auto cubeBatch = scene->m_registry.get<batch>(batchEntt); // auto cubeBatch = scene->m_registry.get<batch>(batchEntt);
// Generate 1000 random cubes // Generate 1000 random cubes
for (int i = 0; i < 1000; ++i) { for (int i = 0; i < 1000; ++i) {
const auto cubeEntity = scene->m_registry.create(); auto cubeEntity = scene->CreateEntity();
float x = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100] float x = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]
float y = static_cast<float>(rand()) / RAND_MAX * 10.f; // range [0, 10] float y = static_cast<float>(rand()) / RAND_MAX * 10.f; // range [0, 10]
float z = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100] float z = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]
scene->m_registry.emplace<transform>(cubeEntity, glm::vec3(x, y, z)); cubeEntity.AddComponent<transform>(glm::vec3(x, y, z));
scene->m_registry.emplace<rotate>(cubeEntity); cubeEntity.AddComponent<rotate>();
scene->m_registry.emplace<batch::item>(cubeEntity, cubeBatch.id()); cubeEntity.AddComponent<batch::item>(cubeBatch.id());
} }
Object* floorObj = Object::LoadFile("./assets/common/plane/plane.obj"); Object* floorObj = Object::LoadFile("./assets/common/plane/plane.obj");
const auto floorEntt = scene->m_registry.create(); auto floorEntt = scene->CreateEntity();
scene->m_registry.emplace<transform>(floorEntt, glm::vec3(0.f)); floorEntt.AddComponent<transform>(glm::vec3(0.f));
scene->m_registry.emplace<mesh>(floorEntt, std::shared_ptr<Object>(floorObj)); floorEntt.AddComponent<mesh>(std::shared_ptr<Object>(floorObj));
std::cout << "Game initialized" << std::endl; std::cout << "Game initialized" << std::endl;
@ -130,11 +134,9 @@ public:
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f; if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f; if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
auto view = m_scene->m_registry.view<camera, transform>(); auto camTransform = cameraEntity.GetComponent<transform>();
for (auto [cam, camTransform] : view.each()) {
camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
camTransform.rotation = cameraViewDirection; camTransform.rotation = cameraViewDirection;
}
// update rotation // update rotation
if (!m_paused) { if (!m_paused) {
@ -167,8 +169,8 @@ public:
glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor); glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor);
// Update the directional light in the registry // Update the directional light in the registry
auto lightsView = m_scene->m_registry.view<light, transform>(); auto l = lightEntity.GetComponent<light>();
for (auto [entity, l, t] : lightsView.each()) { auto t = lightEntity.GetComponent<transform>();
if (l.type == light::LightType::DIRECTIONAL) { if (l.type == light::LightType::DIRECTIONAL) {
// "position" for directional light often stores direction vector // "position" for directional light often stores direction vector
// If your system instead uses transform.rotation, adjust accordingly // If your system instead uses transform.rotation, adjust accordingly
@ -176,15 +178,14 @@ public:
l.color = sunColor; l.color = sunColor;
l.intensity = intensity; l.intensity = intensity;
} }
}
auto rotateEntts = m_scene->m_registry.view<transform, rotate>(); // auto rotateEntts = m_scene->m_registry.view<transform, rotate>();
for (auto [entity, t] : rotateEntts.each()) { // for (auto [entity, t] : rotateEntts.each()) {
// auto targetTransform = rotateEntts.get<transform>(entity); // // auto targetTransform = rotateEntts.get<transform>(entity);
if (!m_scene->m_registry.all_of<light>(entity)) { // if (!m_scene->m_registry.all_of<light>(entity)) {
t.rotation.y = m_angle; // t.rotation.y = m_angle;
} // }
} // }
m_frameCount++; m_frameCount++;
m_currentTicks = SDL_GetTicks(); m_currentTicks = SDL_GetTicks();
@ -210,6 +211,10 @@ public:
private: private:
std::shared_ptr<Scene> m_scene; std::shared_ptr<Scene> m_scene;
Entity lightEntity;
Entity cameraEntity;
Entity modelEntity;
float m_angle; float m_angle;
Uint64 m_lastTicks; Uint64 m_lastTicks;