Skip to content

Commit 6065868

Browse files
committed
Starting new ECS model
1 parent 742ac94 commit 6065868

23 files changed

+309
-218
lines changed

src/Game.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "GL.hpp"
33
#include "Window.hpp"
44
#include "World/World.hpp"
5-
#include "Constructor.hpp"
65

76
#include <memory>
87
#include <unordered_set>
@@ -46,14 +45,18 @@ class Game::GameImpl {
4645

4746
// Update worlds
4847
for (World* world : m_worlds) {
49-
World::current = world;
48+
world->updateEntities();
5049
world->update();
5150
}
5251
}
5352
}
5453

5554
void addWorld(World* world) {
5655
m_worlds.insert(world);
56+
World* w = World::current;
57+
World::current = world;
58+
world->init();
59+
World::current = w;
5760
}
5861

5962
void delWorld(World* world) {

src/Main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include "World/ExampleWorld.hpp"
55
#include <Embed/Embed.hpp>
66

7-
#include "World/World.hpp"
8-
97
int main() {
108
Game game;
119
game.addWorld<ExampleWorld>();

src/StdAfx.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
#include <glm/mat3x3.hpp>
2121
#include <type_traits>
2222
#include <functional>
23-
#include "Integers.hpp"
23+
#include <bitset>
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
#include "Camera.hpp"
2-
#include "../GL.hpp"
2+
3+
#include <GL.hpp>
34
#include <glm/gtc/type_ptr.hpp>
45
#include <glm/gtx/string_cast.hpp>
56

6-
#include "World.hpp"
7+
#include "../Entity.hpp"
78
#include "Window.hpp"
89

9-
Camera::Camera(bool control)
10-
: m_fov(90.f),
11-
m_yaw(-90.f),
12-
m_pitch(-0.f),
13-
m_control(control) {
14-
transform.setPosition(0.0, 0.0, -1.0);
10+
void Camera::init() {
11+
TRANSFORM.setPosition(0.0, 0.0, -1.0);
1512
}
1613

1714
void Camera::update() {
15+
auto& t = TRANSFORM;
1816
m_pitch = glm::clamp(m_pitch, -89.f, 89.f);
1917
m_direction.x = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
2018
m_direction.y = std::sin(glm::radians(m_pitch));
2119
m_direction.z = std::sin(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
2220
m_direction = glm::normalize(m_direction);
23-
transform.model = glm::lookAt(transform.position, transform.position - m_direction, {0.0, 1.0, 0.0});
21+
t.model = glm::lookAt(t.position, t.position - m_direction, {0.0, 1.0, 0.0});
2422

2523
// set projection
2624
m_proj = glm::perspective(glm::radians(m_fov), (float)WINDOW.getResolutionRatio(), 0.01f, 10000.0f);
@@ -39,10 +37,10 @@ void Camera::update() {
3937
float mov_x = std::cos(glm::radians(m_yaw))*WTIME.getDelta()*2.0;
4038
float mov_z = std::sin(glm::radians(m_yaw))*WTIME.getDelta()*2.0;
4139

42-
if (WINDOW.getKey(KEY_W)) transform.position += glm::vec3(mov_x, 0.f, mov_z);
43-
if (WINDOW.getKey(KEY_S)) transform.position += glm::vec3(-mov_x, 0.f, -mov_z);
44-
if (WINDOW.getKey(KEY_A)) transform.position += glm::vec3(mov_z, 0.f, -mov_x);
45-
if (WINDOW.getKey(KEY_D)) transform.position += glm::vec3(-mov_z, 0.f, mov_x);
40+
if (WINDOW.getKey(KEY_W)) t.position += glm::vec3(mov_x, 0.f, mov_z);
41+
if (WINDOW.getKey(KEY_S)) t.position += glm::vec3(-mov_x, 0.f, -mov_z);
42+
if (WINDOW.getKey(KEY_A)) t.position += glm::vec3(mov_z, 0.f, -mov_x);
43+
if (WINDOW.getKey(KEY_D)) t.position += glm::vec3(-mov_z, 0.f, mov_x);
4644
}
4745

4846
void Camera::addShader(GL::Shader& s) {
@@ -54,11 +52,9 @@ void Camera::delShader(GL::Shader& s) {
5452
}
5553

5654
void Camera::updateUniforms(GL::Shader& s) {
55+
auto& t = TRANSFORM;
5756
glUseProgram(s);
58-
glUniformMatrix4fv(s.getUniform("uview"), 1, GL_FALSE, glm::value_ptr(transform.model));
57+
glUniformMatrix4fv(s.getUniform("uview"), 1, GL_FALSE, glm::value_ptr(t.model));
5958
glUniformMatrix4fv(s.getUniform("uproj"), 1, GL_FALSE, glm::value_ptr(m_proj));
6059
glUseProgram(0);
6160
}
62-
63-
Camera::~Camera() {
64-
}

src/World/Camera.hpp renamed to src/World/Component/Camera.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@
22
#include <glm/mat4x4.hpp>
33
#include <glm/vec3.hpp>
44
#include <unordered_map>
5+
#include "Component.hpp"
56

6-
#include "Entity/Entity.hpp"
7-
#include "Component/Transform.hpp"
7+
#include "Transform.hpp"
88

9-
class Camera : Entity {
10-
Transform transform;
11-
float m_fov, m_yaw, m_pitch;
12-
bool m_control;
9+
class Camera : public Component {
10+
float m_fov {90.f}, m_yaw {-90.f}, m_pitch {0.f};
11+
bool m_control { false };
1312
glm::vec3 m_direction;
1413
glm::mat4 m_proj;
1514
std::unordered_set<GL::Shader*> m_shaders;
1615

1716
public:
18-
Camera(bool control=false);
19-
~Camera();
2017

18+
void init();
2119
void update();
2220
//void updateUniforms(GL::Shader&);
2321
void addShader(GL::Shader&);
2422
void delShader(GL::Shader&);
2523
void updateUniforms(GL::Shader& s);
24+
void setControl(bool v) { m_control = v; }
2625
};

src/World/Component/Component.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "Component.hpp"
2+
#include "Transform.hpp"
3+
#include "Transform2D.hpp"
4+
#include "Physics.hpp"
5+
#include "Shader.hpp"
6+
#include "Texture.hpp"
7+
#include "Triangle.hpp"
8+
9+
10+
ComponentManager::ComponentManager() {
11+
12+
}
13+
14+
void ComponentManager::update() {
15+
TupleForwardFn<ComponentList>([&] <typename... Ts> () {
16+
([&] <typename T> () {
17+
std::vector<T>& pool = *std::get<Pool<T>>(m_componentPool);
18+
for (T& component : pool) {
19+
component.update();
20+
}
21+
}.template operator()<Ts>(), ...);
22+
});
23+
}
24+
25+
ComponentManager::~ComponentManager() {
26+
27+
}

src/World/Component/Component.hpp

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,84 @@
11
#pragma once
22
#include <Integers.hpp>
3+
#include <Meta.hpp>
4+
5+
#include "../World.hpp"
6+
7+
using ComponentList = std::tuple<
8+
class Transform,
9+
class Transform2D,
10+
class Physics,
11+
class Shader,
12+
class Texture,
13+
class Triangle
14+
>;
15+
16+
constexpr u32 NComponents = std::tuple_size_v<ComponentList>;
317

418
class Entity;
519
struct Component {
6-
Component(Entity& e) {}
7-
Component() {}
8-
virtual void update(Entity& e) {}
9-
virtual ~Component() {}
20+
u32 id;
21+
22+
Component(u32 eId) : id(eId) {}
23+
24+
Entity& getEntity() {
25+
return WORLD.getEntity(id);
26+
}
1027
};
1128

12-
template<typename... Ts>
13-
struct ComponentRefs {
14-
std::tuple<Ts&...> refs;
29+
30+
class ComponentManager {
31+
std::bitset<NComponents> m_loadedMask;
32+
33+
template<typename T>
34+
using Pool = std::unique_ptr<std::vector<T>>;
35+
36+
template<typename... Ts>
37+
using MultiPool = std::tuple<Pool<Ts>...>;
38+
39+
TupleForward<ComponentList, MultiPool> m_componentPool;
40+
public:
41+
42+
ComponentManager();
43+
~ComponentManager();
44+
45+
template<typename T>
46+
static consteval u32 getComponentId() {
47+
return TupleGetIndex<T, ComponentList>;
48+
}
49+
50+
template<typename T>
51+
void loadComponent() {
52+
m_loadedMask.set(getComponentId<T>());
53+
54+
auto& uniq = std::get<Pool<T>>(m_componentPool);
55+
uniq = std::make_unique<std::vector<T>>();
56+
uniq->reserve(32);
57+
}
58+
59+
void update();
60+
61+
template<typename T, typename... Ts>
62+
u32 addComponent(Ts&&... args) {
63+
std::vector<T>& pool = *std::get<Pool<T>>(m_componentPool);
64+
pool.emplace_back(std::forward<Ts>(args)...);
65+
return pool.size();
66+
}
1567

1668
template<typename T>
17-
ComponentRefs(T& t) {
18-
([&] () {
19-
std::get<Ts>(refs) = static_cast<Ts&>(t);
20-
}(), ...);
69+
T& getComponent(u32 id) {
70+
std::vector<T>& pool = *std::get<Pool<T>>(m_componentPool);
71+
return pool[id];
2172
}
2273

2374
template<typename T>
24-
void get() {
25-
return std::get<T>(refs);
75+
void delComponent(u32 id) {
76+
std::vector<T>& pool = *std::get<Pool<T>>(m_componentPool);
77+
T& last = pool[pool.size()-1];
78+
T& replaced = pool[id];
79+
replaced = std::move(last);
80+
replaced.id = id;
81+
pool.pop_back();
2682
}
2783
};
2884

29-
#include "../Entity/Entity.hpp"

src/World/Component/ComponentList.hpp

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/World/Component/Physics.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
2-
#include "Component.hpp"
32

4-
class Physics : public Component {
3+
class Physics {
54

5+
public:
6+
void update() {}
67
};

src/World/Component/Shader.hpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
#pragma once
22
#include <GL.hpp>
3-
#include "Component.hpp"
43

5-
class Shader : public Component {
6-
GL::Shader* m_shader;
4+
class Shader {
5+
76
public:
8-
void setShader(GL::Shader& s) { m_shader = &s; }
9-
GL::Shader& getShader() { return *m_shader; }
7+
void update() {}
8+
// GL::Shader* m_shader;
9+
// public:
10+
// void setShader(GL::Shader& s) { m_shader = &s; }
11+
// GL::Shader& getShader() { return *m_shader; }
1012

11-
Shader(Entity& e, GL::Shader& s) : m_shader(&s) {}
12-
Shader() {}
13+
// Shader(Entity& e, GL::Shader& s) : m_shader(&s) {}
14+
// Shader() {}
1315

14-
void use() {
15-
glUseProgram(*m_shader);
16-
}
16+
// void use() {
17+
// glUseProgram(*m_shader);
18+
// }
1719

18-
void use(const auto& fn) {
19-
glUseProgram(*m_shader);
20-
fn();
21-
glUseProgram(0);
22-
}
20+
// void use(const auto& fn) {
21+
// glUseProgram(*m_shader);
22+
// fn();
23+
// glUseProgram(0);
24+
// }
2325

24-
auto getAttrib(const char* a) {
25-
return m_shader->getAttrib(a);
26-
}
26+
// auto getAttrib(const char* a) {
27+
// return m_shader->getAttrib(a);
28+
// }
2729

28-
auto getUniform(const char* u) {
29-
return m_shader->getUniform(u);
30-
}
30+
// auto getUniform(const char* u) {
31+
// return m_shader->getUniform(u);
32+
// }
3133

32-
void unuse() {
33-
glUseProgram(0);
34-
}
34+
// void unuse() {
35+
// glUseProgram(0);
36+
// }
3537
};

0 commit comments

Comments
 (0)