feat: refactoring
This commit is contained in:
@ -3,6 +3,8 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class FileManager
|
||||
{
|
||||
public:
|
||||
@ -12,4 +14,6 @@ public:
|
||||
static std::string read(const std::string &filename);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // FILE_MANAGER_H
|
||||
@ -1,6 +1,8 @@
|
||||
#ifndef PARSER_H_
|
||||
#define PARSER_H_
|
||||
|
||||
namespace Engine {
|
||||
|
||||
// Very fast OBJ/MTL line parser
|
||||
class Parser {
|
||||
private:
|
||||
@ -17,4 +19,6 @@ public:
|
||||
int TakeIndex(int baseCount);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // PARSER_H_
|
||||
@ -9,10 +9,10 @@
|
||||
#include "engine/app/app.h"
|
||||
#include "engine/renderer/core.h"
|
||||
|
||||
extern IApplication* CreateApplication();
|
||||
extern Engine::IApplication* CreateApplication();
|
||||
|
||||
int main() {
|
||||
auto engine = Engine::GetInstance();
|
||||
engine->Run(std::unique_ptr<IApplication>(CreateApplication()));
|
||||
auto engine = Engine::Engine::GetInstance();
|
||||
engine->Run(std::unique_ptr<Engine::IApplication>(CreateApplication()));
|
||||
return 0;
|
||||
}
|
||||
@ -5,15 +5,17 @@
|
||||
#include "engine/window/event.hpp"
|
||||
#include "engine/export.h"
|
||||
|
||||
class ENGINE_API IApplication {
|
||||
public:
|
||||
virtual ~IApplication() = default;
|
||||
namespace Engine {
|
||||
class ENGINE_API IApplication {
|
||||
public:
|
||||
virtual ~IApplication() = default;
|
||||
|
||||
virtual void OnInit(std::shared_ptr<Scene> scene) {};
|
||||
virtual void OnUpdate() {};
|
||||
virtual void OnShutdown() {};
|
||||
|
||||
virtual void OnEvent(const Event& event) {};
|
||||
};
|
||||
virtual void OnInit(std::shared_ptr<Scene> scene) {};
|
||||
virtual void OnUpdate() {};
|
||||
virtual void OnShutdown() {};
|
||||
|
||||
virtual void OnEvent(const Event& event) {};
|
||||
};
|
||||
}
|
||||
|
||||
#endif // APPLICATION_H_
|
||||
@ -4,6 +4,7 @@
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include "engine/export.h"
|
||||
|
||||
namespace Engine {
|
||||
// requires mesh component
|
||||
struct ENGINE_API batch {
|
||||
friend class Renderer;
|
||||
@ -25,5 +26,6 @@ private:
|
||||
private:
|
||||
void prepare(glm::mat4 *instances, unsigned int count);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // COMPONENT_BATCH_H_
|
||||
@ -3,6 +3,8 @@
|
||||
|
||||
#include "engine/export.h"
|
||||
|
||||
namespace Engine {
|
||||
struct ENGINE_API camera {};
|
||||
}
|
||||
|
||||
#endif // COMPONENTS_PLAYER_H_
|
||||
@ -6,6 +6,7 @@
|
||||
#include "engine/renderer/renderer.h"
|
||||
#include "engine/export.h"
|
||||
|
||||
namespace Engine {
|
||||
struct ENGINE_API light {
|
||||
friend class Renderer;
|
||||
public:
|
||||
@ -25,5 +26,6 @@ private:
|
||||
glm::mat4 lightSpace;
|
||||
int shadowRes{1024};
|
||||
};
|
||||
}
|
||||
|
||||
#endif // COMPONENTS_LIGHT_H_
|
||||
@ -6,8 +6,10 @@
|
||||
#include "engine/renderer/wavefront.h"
|
||||
#include "engine/export.h"
|
||||
|
||||
namespace Engine {
|
||||
struct ENGINE_API mesh {
|
||||
std::shared_ptr<Object> object;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // COMPONENTS_MESH_H_
|
||||
@ -3,6 +3,8 @@
|
||||
|
||||
#include "engine/export.h"
|
||||
|
||||
namespace Engine {
|
||||
struct ENGINE_API rotate {};
|
||||
}
|
||||
|
||||
#endif // COMPONENT_ROTATE_H_
|
||||
@ -4,10 +4,12 @@
|
||||
#include <glm/glm.hpp>
|
||||
#include "engine/export.h"
|
||||
|
||||
namespace Engine {
|
||||
struct ENGINE_API transform {
|
||||
glm::vec3 position;
|
||||
glm::vec3 rotation;
|
||||
glm::vec3 scale;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // COMPONENTS_TRANSFORM_H_
|
||||
@ -3,6 +3,8 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class Vertex {
|
||||
friend class Mesh;
|
||||
private:
|
||||
@ -14,4 +16,6 @@ public:
|
||||
: m_position(position), m_normal(normal), m_texCoord(texCoord) {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // RENDERER_BASICS_H
|
||||
@ -13,6 +13,8 @@
|
||||
#include "engine/app/app.h"
|
||||
#include "engine/export.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class ENGINE_API Engine : public EventHandler {
|
||||
public:
|
||||
static Engine* GetInstance();
|
||||
@ -31,5 +33,6 @@ private:
|
||||
bool m_running;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // ENGINE_H_
|
||||
@ -3,6 +3,8 @@
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
namespace Engine {
|
||||
|
||||
void MessageCallback(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
@ -11,4 +13,6 @@ void MessageCallback(GLenum source,
|
||||
const GLchar* message,
|
||||
const void* userParam);
|
||||
|
||||
}
|
||||
|
||||
#endif // RENDERER_DEBUG_
|
||||
@ -6,6 +6,8 @@
|
||||
|
||||
#include "engine/renderer/texture.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class Material {
|
||||
private:
|
||||
glm::vec3 m_ambient { 0.2f, 0.2f, 0.2f };
|
||||
@ -39,4 +41,6 @@ public:
|
||||
inline void SetIllumination(float illum) { m_illum = illum; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // MATERIAL_H_
|
||||
@ -7,6 +7,8 @@
|
||||
|
||||
#include "engine/renderer/basics.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class Mesh {
|
||||
public: // TODO: abstract away
|
||||
unsigned int m_vao, m_vbo, m_ebo;
|
||||
@ -24,4 +26,6 @@ public:
|
||||
void Render(unsigned int count);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // MESH_H_
|
||||
@ -8,6 +8,8 @@
|
||||
#include "engine/export.h"
|
||||
#include "engine/components/light.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
// TODO: make static or singleton
|
||||
class ENGINE_API Renderer {
|
||||
public:
|
||||
@ -37,4 +39,6 @@ private:
|
||||
glm::mat4 m_view;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // RENDERER_H_
|
||||
@ -7,6 +7,8 @@
|
||||
|
||||
#include "engine/export.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class ENGINE_API Shader
|
||||
{
|
||||
public:
|
||||
@ -45,4 +47,6 @@ private:
|
||||
void checkLinkingError();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // SHADER_H
|
||||
@ -3,6 +3,8 @@
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
Texture() : m_id(0) {}
|
||||
@ -13,4 +15,6 @@ private:
|
||||
unsigned int m_id;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // TEXTURE_H_
|
||||
|
||||
@ -14,6 +14,8 @@
|
||||
|
||||
#include "engine/export.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
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 };
|
||||
|
||||
@ -53,4 +55,6 @@ private:
|
||||
std::unordered_map<std::string, std::shared_ptr<Material>> m_materials;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // MODEL_H_
|
||||
@ -2,14 +2,45 @@
|
||||
#define ENGINE_SCENE_H_
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class Scene;
|
||||
|
||||
class Entity {
|
||||
friend class Scene;
|
||||
private:
|
||||
Entity(entt::entity entity, Scene* scene) : m_entity(entity), m_scene(scene) {}
|
||||
public:
|
||||
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;
|
||||
Scene *m_scene;
|
||||
};
|
||||
|
||||
class Scene {
|
||||
private:
|
||||
friend class Entity;
|
||||
public:
|
||||
Scene();
|
||||
|
||||
std::unique_ptr<Entity> CreateEntity();
|
||||
private:
|
||||
entt::registry m_registry;
|
||||
friend class Renderer;
|
||||
friend class Game;
|
||||
};
|
||||
|
||||
} // namespace Engine
|
||||
|
||||
#endif // ENGINE_SCENE_H_
|
||||
@ -7,6 +7,8 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Engine {
|
||||
|
||||
enum class EventType {
|
||||
WINDOW_RESIZE,
|
||||
WINDOW_CLOSE,
|
||||
@ -71,4 +73,6 @@ private:
|
||||
std::size_t m_next_id = 1;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // EVENT_H_
|
||||
@ -3,6 +3,8 @@
|
||||
|
||||
#include "engine/window/event.hpp"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class WindowEvent : public Event {
|
||||
public:
|
||||
WindowEvent() : Event(Event::EventCategory::WINDOW) {}
|
||||
@ -27,4 +29,6 @@ public:
|
||||
WindowCloseEvent() {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // WINDOW_EVENTS_H_
|
||||
@ -13,6 +13,8 @@
|
||||
#define DEFAULT_WIDTH 1024
|
||||
#define DEFAULT_HEIGHT 768
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class Window : public EventEmitter {
|
||||
friend class Engine;
|
||||
private:
|
||||
@ -48,4 +50,6 @@ private:
|
||||
int m_height;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //WINDOW_H_
|
||||
@ -4,6 +4,8 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace Engine {
|
||||
|
||||
FileManager::FileManager()
|
||||
{
|
||||
}
|
||||
@ -30,4 +32,6 @@ std::string FileManager::read(const std::string &filename)
|
||||
}
|
||||
|
||||
return fileStream.str();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
|
||||
#include "engine/IO/parser.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
// Skip whitespace
|
||||
void Parser::SkipSpaces() {
|
||||
while (*m_sv == ' ' || *m_sv == '\t') ++m_sv;
|
||||
@ -120,4 +122,6 @@ bool Parser::TakeFaceIndices(int &vi, int &ti, int &ni) {
|
||||
// At this point m_sv is either at whitespace, end, or next token char.
|
||||
// Do NOT mutate indices (leave them raw). Let NormalizeIndex handle conversion.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
#include "engine/components/batch.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
unsigned int batch::LastID = 0;
|
||||
|
||||
batch::batch() {
|
||||
@ -27,4 +29,6 @@ void batch::prepare(glm::mat4 *instances, unsigned int count) {
|
||||
// Just update the data region — much cheaper
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::mat4) * count, instances);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -5,6 +5,8 @@
|
||||
#include "engine/window/event.hpp"
|
||||
#include "engine/renderer/wavefront.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
Engine* Engine::s_instance = nullptr;
|
||||
|
||||
void Engine::Run(std::unique_ptr<IApplication> app) {
|
||||
@ -66,3 +68,4 @@ Engine* Engine::GetInstance() {
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace Engine {
|
||||
|
||||
void MessageCallback(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
@ -59,3 +61,5 @@ void MessageCallback(GLenum source,
|
||||
// (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
|
||||
// type, severity, message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
#include "engine/renderer/mesh.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
Mesh::Mesh() {
|
||||
m_vao = 0;
|
||||
m_vbo = 0;
|
||||
@ -57,4 +59,6 @@ void Mesh::Render(unsigned int count)
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indexBuffer.size()), GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
Unbind();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -18,6 +18,8 @@
|
||||
#include "engine/components/mesh.h"
|
||||
#include "engine/components/batch.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
Renderer::Renderer(std::shared_ptr<Scene> scene) : m_scene(scene)
|
||||
{
|
||||
m_proj = glm::perspective(
|
||||
@ -245,4 +247,6 @@ void Renderer::Render() {
|
||||
ApplyLights(m_shader);
|
||||
UpdateView();
|
||||
RenderScene(m_shader);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
#include <GL/glew.h>
|
||||
#include "engine/renderer/shader.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
Shader::Shader()
|
||||
{
|
||||
}
|
||||
@ -132,4 +134,6 @@ void Shader::checkLinkingError()
|
||||
<< infoLog
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
std::unique_ptr<Texture> Texture::LoadFile(const std::string& filename) {
|
||||
auto texture = std::make_unique<Texture>();
|
||||
|
||||
@ -34,3 +36,5 @@ std::unique_ptr<Texture> Texture::LoadFile(const std::string& filename) {
|
||||
|
||||
return std::move(texture);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -12,6 +12,8 @@
|
||||
|
||||
#define DEFAULT_MATERIAL_NAME "default"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
// ObjElement toElement(const std::string &s) {
|
||||
// if (s == "#") return ObjElement::OHASH;
|
||||
// if (s == "mtllib") return ObjElement::MTLLIB;
|
||||
@ -509,4 +511,4 @@ void Object::Render(Shader& shader, unsigned int count)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,4 +1,11 @@
|
||||
#include "engine/scene/scene.h"
|
||||
|
||||
Scene::Scene() : m_registry() {
|
||||
namespace Engine {
|
||||
|
||||
Scene::Scene() = default;
|
||||
|
||||
std::unique_ptr<Entity> Scene::CreateEntity() {
|
||||
return std::unique_ptr<Entity>(new Entity(m_registry.create(), this));
|
||||
}
|
||||
|
||||
} // namespace Engine
|
||||
|
||||
@ -8,6 +8,8 @@
|
||||
|
||||
#include "engine/renderer/debug.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
std::shared_ptr<Window> Window::s_instance = nullptr;
|
||||
|
||||
Window::Window(const char* title, int width, int height) {
|
||||
@ -100,12 +102,10 @@ void Window::ProcessEvents() {
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||
case SDL_EVENT_QUIT:
|
||||
Dispatch(WindowCloseEvent());
|
||||
EmitEvent(WindowCloseEvent{});
|
||||
break;
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
||||
Dispatch(WindowCloseEvent());
|
||||
EmitEvent(WindowCloseEvent{});
|
||||
}
|
||||
if (event.key.scancode == SDL_SCANCODE_F11) {
|
||||
@ -125,7 +125,6 @@ void Window::ProcessEvents() {
|
||||
width,
|
||||
height);
|
||||
auto event = WindowResizeEvent(static_cast<unsigned int>(m_width), static_cast<unsigned int>(m_height));
|
||||
Dispatch(event);
|
||||
EmitEvent(event);
|
||||
SDL_SetWindowRelativeMouseMode(m_handle, true);
|
||||
SDL_Rect boundaries = {0, 0, m_width, m_height};
|
||||
@ -152,3 +151,4 @@ void Window::Destroy() const {
|
||||
SDL_DestroyWindow(m_handle);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -19,8 +19,12 @@
|
||||
#include "engine/components/rotate.h"
|
||||
#include "engine/components/batch.h"
|
||||
|
||||
#include "engine/scene/scene.h"
|
||||
|
||||
#include "engine/api.h"
|
||||
|
||||
using namespace Engine;
|
||||
|
||||
class Game : public IApplication {
|
||||
public:
|
||||
Game() {}
|
||||
@ -30,49 +34,49 @@ public:
|
||||
m_scene = scene;
|
||||
|
||||
Object* lightObj = Object::LoadFile("./assets/common/sphere/sphere.obj");
|
||||
const auto lght = scene->m_registry.create();
|
||||
scene->m_registry.emplace<transform>(lght, 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);
|
||||
scene->m_registry.emplace<mesh>(lght, std::shared_ptr<Object>(lightObj));
|
||||
lightEntity = scene->CreateEntity();
|
||||
lightEntity->AddComponent<transform>(glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
|
||||
lightEntity->AddComponent<light>(light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
|
||||
lightEntity->AddComponent<mesh>(std::shared_ptr<Object>(lightObj));
|
||||
|
||||
const auto cameraEntity = scene->m_registry.create();
|
||||
scene->m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f));
|
||||
scene->m_registry.emplace<camera>(cameraEntity);
|
||||
cameraEntity = scene->CreateEntity();
|
||||
cameraEntity->AddComponent<camera>();
|
||||
cameraEntity->AddComponent<transform>(glm::vec3(0.f, 2.f, 2.f));
|
||||
|
||||
Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj");
|
||||
const auto targetEntity = scene->m_registry.create();
|
||||
scene->m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.0f, 0.f));
|
||||
scene->m_registry.emplace<mesh>(targetEntity, std::shared_ptr<Object>(targetObj));
|
||||
scene->m_registry.emplace<rotate>(targetEntity);
|
||||
modelEntity = scene->CreateEntity();
|
||||
modelEntity->AddComponent<transform>(glm::vec3(0.f, 0.0f, 0.f));
|
||||
modelEntity->AddComponent<mesh>(std::shared_ptr<Object>(targetObj));
|
||||
modelEntity->AddComponent<rotate>();
|
||||
|
||||
Object* grass = Object::LoadFile("./assets/common/cube/cube.obj");
|
||||
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<mesh>(cubeEntity, std::shared_ptr<Object>(grass));
|
||||
// Object* grass = Object::LoadFile("./assets/common/cube/cube.obj");
|
||||
// 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<mesh>(cubeEntity, std::shared_ptr<Object>(grass));
|
||||
|
||||
// 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"));
|
||||
const auto batchEntt = scene->m_registry.create();
|
||||
scene->m_registry.emplace<batch>(batchEntt);
|
||||
scene->m_registry.emplace<mesh>(batchEntt, cubeObj);
|
||||
auto cubeBatch = scene->m_registry.get<batch>(batchEntt);
|
||||
const auto batchEntt = scene->CreateEntity();
|
||||
auto cubeBatch = batchEntt->AddComponent<batch>();
|
||||
batchEntt->AddComponent<mesh>(cubeObj);
|
||||
// auto cubeBatch = scene->m_registry.get<batch>(batchEntt);
|
||||
// Generate 1000 random cubes
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
const auto cubeEntity = scene->m_registry.create();
|
||||
const auto cubeEntity = scene->CreateEntity();
|
||||
|
||||
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 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));
|
||||
scene->m_registry.emplace<rotate>(cubeEntity);
|
||||
scene->m_registry.emplace<batch::item>(cubeEntity, cubeBatch.id());
|
||||
cubeEntity->AddComponent<transform>(glm::vec3(x, y, z));
|
||||
cubeEntity->AddComponent<rotate>();
|
||||
cubeEntity->AddComponent<batch::item>(cubeBatch.id());
|
||||
}
|
||||
|
||||
Object* floorObj = Object::LoadFile("./assets/common/plane/plane.obj");
|
||||
const auto floorEntt = scene->m_registry.create();
|
||||
scene->m_registry.emplace<transform>(floorEntt, glm::vec3(0.f));
|
||||
scene->m_registry.emplace<mesh>(floorEntt, std::shared_ptr<Object>(floorObj));
|
||||
const auto floorEntt = scene->CreateEntity();
|
||||
floorEntt->AddComponent<transform>(glm::vec3(0.f));
|
||||
floorEntt->AddComponent<mesh>(std::shared_ptr<Object>(floorObj));
|
||||
|
||||
std::cout << "Game initialized" << std::endl;
|
||||
|
||||
@ -130,11 +134,9 @@ public:
|
||||
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
|
||||
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
|
||||
|
||||
auto view = m_scene->m_registry.view<camera, transform>();
|
||||
for (auto [cam, camTransform] : view.each()) {
|
||||
camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
||||
camTransform.rotation = cameraViewDirection;
|
||||
}
|
||||
auto camTransform = cameraEntity->GetComponent<transform>();
|
||||
camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
||||
camTransform.rotation = cameraViewDirection;
|
||||
|
||||
// update rotation
|
||||
if (!m_paused) {
|
||||
@ -167,24 +169,23 @@ public:
|
||||
glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor);
|
||||
|
||||
// Update the directional light in the registry
|
||||
auto lightsView = m_scene->m_registry.view<light, transform>();
|
||||
for (auto [entity, l, t] : lightsView.each()) {
|
||||
if (l.type == light::LightType::DIRECTIONAL) {
|
||||
// "position" for directional light often stores direction vector
|
||||
// If your system instead uses transform.rotation, adjust accordingly
|
||||
t.position = sunDir * 15.f; // use this as light direction
|
||||
l.color = sunColor;
|
||||
l.intensity = intensity;
|
||||
}
|
||||
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
|
||||
t.position = sunDir * 15.f; // use this as light direction
|
||||
l.color = sunColor;
|
||||
l.intensity = intensity;
|
||||
}
|
||||
|
||||
auto rotateEntts = m_scene->m_registry.view<transform, rotate>();
|
||||
for (auto [entity, t] : rotateEntts.each()) {
|
||||
// auto targetTransform = rotateEntts.get<transform>(entity);
|
||||
if (!m_scene->m_registry.all_of<light>(entity)) {
|
||||
t.rotation.y = m_angle;
|
||||
}
|
||||
}
|
||||
// auto rotateEntts = m_scene->m_registry.view<transform, rotate>();
|
||||
// for (auto [entity, t] : rotateEntts.each()) {
|
||||
// // auto targetTransform = rotateEntts.get<transform>(entity);
|
||||
// if (!m_scene->m_registry.all_of<light>(entity)) {
|
||||
// t.rotation.y = m_angle;
|
||||
// }
|
||||
// }
|
||||
|
||||
m_frameCount++;
|
||||
m_currentTicks = SDL_GetTicks();
|
||||
@ -210,6 +211,10 @@ public:
|
||||
private:
|
||||
std::shared_ptr<Scene> m_scene;
|
||||
|
||||
std::unique_ptr<Entity> lightEntity;
|
||||
std::unique_ptr<Entity> cameraEntity;
|
||||
std::unique_ptr<Entity> modelEntity;
|
||||
|
||||
float m_angle;
|
||||
Uint64 m_lastTicks;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user