88
99unsigned 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
6765GameObject::~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
9288void 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