Skip to content

Commit 455001f

Browse files
authored
Merge pull request #53 from Im-Rises/develop
Updated code
2 parents 92d55a0 + 8a6d0c2 commit 455001f

7 files changed

Lines changed: 231 additions & 245 deletions

File tree

PhysicalEngine/PhysicalEngineLauncher.cpp

Lines changed: 122 additions & 113 deletions
Large diffs are not rendered by default.

PhysicalEngine/Scene/Components/Component.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ class GameObject;
1616

1717
class Component {
1818
public:
19-
static const char* componentsNamesList[6];
19+
static const char *componentsNamesList[6];
2020

2121
private:
22-
static constexpr const char* COMPONENT_TYPE = "Component";
22+
static constexpr const char *COMPONENT_TYPE = "Component";
2323

2424
protected:
25-
GameObject* m_gameObject;
25+
GameObject *m_gameObject;
2626

2727
public:
28-
explicit Component(GameObject* gameObject);
28+
explicit Component(GameObject *gameObject);
2929

3030
virtual ~Component();
3131

3232
virtual void update(float time) = 0;
3333

3434
virtual void drawGui() = 0;
3535

36-
virtual std::string getName() const = 0;
36+
[[nodiscard]] virtual auto getName() const -> std::string = 0;
3737

38-
GameObject* getGameObject();
38+
auto getGameObject() -> GameObject *;
3939

40-
static Component* createComponent(const std::string& name, GameObject* gameObject);
40+
static auto createComponent(const std::string &name, GameObject *gameObject) -> Component *;
4141
};
4242

4343

PhysicalEngine/Scene/Components/DefaultComponent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class DefaultComponent {
77
public:
88
virtual void drawGui() = 0;
99

10-
virtual std::string getName() const = 0;
10+
[[nodiscard]] virtual auto getName() const -> std::string = 0;
1111

1212
};
1313

PhysicalEngine/Scene/Components/Transform/Transform.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class Transform : private DefaultComponent {
1313
private:
14-
static constexpr const char* COMPONENT_TYPE = "Transform";
14+
static constexpr const char *COMPONENT_TYPE = "Transform";
1515

1616
public:
1717
float positionX, positionY, positionZ;
@@ -28,17 +28,17 @@ class Transform : private DefaultComponent {
2828

2929
void setPosition(float x, float y, float z);
3030

31-
void setPosition(const Vector3d& position);
31+
void setPosition(const Vector3d &position);
3232

33-
void setRotation(const Quaternion& rotation);
33+
void setRotation(const Quaternion &rotation);
3434

35-
Quaternion getRotation() const;
35+
[[nodiscard]] auto getRotation() const -> Quaternion;
3636

37-
Vector3d getPosition() const;
37+
[[nodiscard]] auto getPosition() const -> Vector3d;
3838

39-
Matrix34 getMatrix() const;
39+
[[nodiscard]] auto getMatrix() const -> Matrix34;
4040

41-
std::string getName() const override;
41+
[[nodiscard]] auto getName() const -> std::string override;
4242

4343
// virtual Vector3d getForward() const =0;
4444
};

PhysicalEngine/Scene/GameObject.cpp

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
unsigned int GameObject::idCounter = 0;
1010

11-
Shader* GameObject::defaultShader = nullptr;
11+
Shader *GameObject::defaultShader = nullptr;
1212

13-
GameObject::GameObject(Scene* scene) {
14-
if (defaultShader == nullptr)
15-
{
13+
GameObject::GameObject(Scene *scene) {
14+
if (defaultShader == nullptr) {
1615
defaultShader = new Shader();
1716
}
1817
// defaultShader = new Shader();
@@ -23,7 +22,7 @@ GameObject::GameObject(Scene* scene) {
2322
}
2423

2524

26-
GameObject::GameObject(Scene* scene, Mesh* mesh) : GameObject(scene) {
25+
GameObject::GameObject(Scene *scene, Mesh *mesh) : GameObject(scene) {
2726
mesh->getVerticesUseIndices();
2827
this->mesh = mesh;
2928
create();
@@ -42,37 +41,34 @@ void GameObject::create() {
4241

4342
glBindBuffer(GL_ARRAY_BUFFER, VBO);
4443
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * mesh->getVertices().size(), mesh->getVertices().data(),
45-
GL_STATIC_DRAW);
44+
GL_STATIC_DRAW);
4645

47-
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
46+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
4847
glEnableVertexAttribArray(0);
4948

5049
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *) 0);
5150
// glEnableVertexAttribArray(0);
5251
// glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *) (3 * sizeof(float)));
5352
// glEnableVertexAttribArray(1);
5453

55-
if (mesh->getVerticesUseIndices())
56-
{
54+
if (mesh->getVerticesUseIndices()) {
5755
glGenBuffers(1, &EBO);
5856
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
5957
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * mesh->getIndices().size(),
60-
mesh->getIndices().data(),
61-
GL_STATIC_DRAW);
58+
mesh->getIndices().data(),
59+
GL_STATIC_DRAW);
6260
}
6361

6462
defaultShader->use();
6563
}
6664

6765
GameObject::~GameObject() {
68-
if (defaultShader != nullptr)
69-
{
66+
if (defaultShader != nullptr) {
7067
delete defaultShader;
7168
defaultShader = nullptr;
7269
}
7370
delete mesh;
74-
for (auto& component : components)
75-
{
71+
for (auto &component: components) {
7672
delete component;
7773
}
7874
destroy();
@@ -90,8 +86,7 @@ void GameObject::destroy() {
9086

9187

9288
void GameObject::update(float deltaTime) {
93-
for (auto& component : components)
94-
{
89+
for (auto &component: components) {
9590
component->update(deltaTime);
9691
}
9792
}
@@ -107,7 +102,7 @@ void GameObject::draw(int display_w, int display_h, glm::mat4 view, float fov) {
107102
glm::mat4 model = convertToGlmMat4(matrix);
108103
glm::mat4 projection = glm::mat4(1.0f);
109104
projection = glm::perspective(glm::radians(fov / 2), static_cast<float>(display_w) / static_cast<float>(display_h),
110-
0.1f, 100.0f);
105+
0.1f, 100.0f);
111106

112107
// Shader use
113108
defaultShader->use();
@@ -119,36 +114,28 @@ void GameObject::draw(int display_w, int display_h, glm::mat4 view, float fov) {
119114

120115
// Handle the VAO, VBO and EBO depending on the mesh type (with or without indices)
121116
glBindVertexArray(VAO);
122-
if (mesh->getVerticesUseIndices())
123-
{
124-
glDrawElements(GL_TRIANGLES, (GLsizei)mesh->getIndices().size(), GL_UNSIGNED_INT, 0);
125-
}
126-
else
127-
{
128-
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)mesh->getVertices().size());
117+
if (mesh->getVerticesUseIndices()) {
118+
glDrawElements(GL_TRIANGLES, (GLsizei) mesh->getIndices().size(), GL_UNSIGNED_INT, 0);
119+
} else {
120+
glDrawArrays(GL_TRIANGLES, 0, (GLsizei) mesh->getVertices().size());
129121
}
130122
}
131123

132-
const std::vector<Component*>& GameObject::getComponents() const {
124+
const std::vector<Component *> &GameObject::getComponents() const {
133125
return components;
134126
}
135127

136-
void GameObject::addComponent(Component* component) {
128+
void GameObject::addComponent(Component *component) {
137129
components.push_back(component);
138130
}
139131

140-
void GameObject::addComponentByName(const std::string& name) {
141-
for (auto& componentName : Component::componentsNamesList)
142-
{
143-
if (componentName == name)
144-
{
145-
Component* component = Component::createComponent(name, this);
146-
if (component != nullptr && getComponentByName(name) == nullptr)
147-
{
132+
void GameObject::addComponentByName(const std::string &name) {
133+
for (auto &componentName: Component::componentsNamesList) {
134+
if (componentName == name) {
135+
Component *component = Component::createComponent(name, this);
136+
if (component != nullptr && getComponentByName(name) == nullptr) {
148137
components.push_back(component);
149-
}
150-
else if (component != nullptr)
151-
{
138+
} else if (component != nullptr) {
152139
delete component;
153140
}
154141
}
@@ -171,47 +158,44 @@ std::string GameObject::getName() const {
171158
return gameObjectName + " " + std::to_string(id);
172159
}
173160

174-
Scene* GameObject::getScenePtr() const {
161+
Scene *GameObject::getScenePtr() const {
175162
return parentScene;
176163
}
177164

178-
glm::mat4 GameObject::convertToGlmMat4(Matrix34& matrix) const {
165+
glm::mat4 GameObject::convertToGlmMat4(Matrix34 &matrix) const {
179166
// remplire colonne par colonne
180167
return glm::mat4(matrix(0, 0), matrix(1, 0), matrix(2, 0), 0,
181-
matrix(0, 1), matrix(1, 1), matrix(2, 1), 0,
182-
matrix(0, 2), matrix(1, 2), matrix(2, 2), 0,
183-
matrix(0, 3), matrix(1, 3), matrix(2, 3), 1);
168+
matrix(0, 1), matrix(1, 1), matrix(2, 1), 0,
169+
matrix(0, 2), matrix(1, 2), matrix(2, 2), 0,
170+
matrix(0, 3), matrix(1, 3), matrix(2, 3), 1);
184171
}
185172

186173

187-
Component* GameObject::getComponentByName(const std::string& name) const {
188-
for (auto& component : components)
189-
{
190-
if (component->getName() == name)
191-
{
174+
Component *GameObject::getComponentByName(const std::string &name) const {
175+
for (auto &component: components) {
176+
if (component->getName() == name) {
192177
return component;
193178
}
194179
}
195180
return nullptr;
196181
}
197182

198-
bool GameObject::hasComponentByName(const std::string& name) const {
199-
return std::any_of(components.begin(), components.end(), [&name](Component* component) {
183+
bool GameObject::hasComponentByName(const std::string &name) const {
184+
return std::any_of(components.begin(), components.end(), [&name](Component *component) {
200185
return component->getName() == name;
201186
});
202187
}
203188

204-
void GameObject::deleteComponentByName(const std::string& name) {
205-
for (auto it = components.begin(); it != components.end(); ++it)
206-
{
207-
if ((*it)->getName() == name)
208-
{
189+
void GameObject::deleteComponentByName(const std::string &name) {
190+
for (auto it = components.begin(); it != components.end(); ++it) {
191+
if ((*it)->getName() == name) {
192+
delete *it;//TODO: check if it's ok
209193
components.erase(it);
210-
// delete *it;
211194
return;
212195
}
213196
}
214197
}
215-
Mesh* GameObject::getMesh() const {
198+
199+
Mesh *GameObject::getMesh() const {
216200
return mesh;
217201
}

0 commit comments

Comments
 (0)