Skip to content

Commit

Permalink
Patch 9:
Browse files Browse the repository at this point in the history
Adding gameobjects to a scene
  • Loading branch information
Dyronix committed Sep 3, 2016
1 parent 593dae1 commit 75d6cdf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions DyroEngine/include/SceneGraph/Scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ class Scene : public Object, public IDrawable

virtual void destroy();

void addGameObject(GameObject* object);
void removeGameObject(GameObject* object);

void setRenderer(Renderer* renderer);
Renderer* getRenderer() const;

private:
Renderer* renderer;
std::vector<GameObject*> vec_objects;
};

#endif //_SCENE_H
Expand Down
51 changes: 51 additions & 0 deletions DyroEngine/src/SceneGraph/Scene/Scene.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "SceneGraph/Scene/Scene.h"
#include "SceneGraph/Object/GameObjects/GameObject.h"

#ifndef _ALGORITHM
#include <algorithm>
Expand All @@ -8,6 +9,7 @@

#include "Helpers/Singleton.h"

#include "Interfaces/IDrawable.h"

Scene::Scene(const std::tstring& name)
:Object(name)
Expand All @@ -20,16 +22,46 @@ Scene::~Scene()

bool Scene::initialize()
{
for (GameObject* obj : this->vec_objects)
{
if (obj->getInitialized())
continue;
if (!obj->initialize())
return false;
}

return true;
}
void Scene::update()
{
for (GameObject* obj : this->vec_objects)
{
if (obj->isActive())
obj->update();
}
}
void Scene::draw()
{
for (GameObject* obj : this->vec_objects)
{
IDrawable* drawable_obj = dynamic_cast<IDrawable*>(obj);
if(drawable_obj == nullptr)
continue;

if(drawable_obj->getCanDraw())
drawable_obj->draw();
}
}
bool Scene::shutdown()
{
for (GameObject* obj : this->vec_objects)
{
if (!obj->shutdown())
return false;
SafeDelete(obj);
}
this->vec_objects.clear();

return true;
}

Expand All @@ -47,6 +79,25 @@ void Scene::destroy()
Object::destroy();
}

void Scene::addGameObject(GameObject* object)
{
std::vector<GameObject*>::iterator it = std::find(this->vec_objects.begin(), this->vec_objects.end(), object);
if (it == this->vec_objects.end())
{
this->vec_objects.push_back(object);
object->setScene(this);
}
}
void Scene::removeGameObject(GameObject* object)
{
std::vector<GameObject*>::iterator it = std::find(this->vec_objects.begin(), this->vec_objects.end(), object);
if (it != this->vec_objects.end())
{
this->vec_objects.erase(it);
SafeDelete((*it));
}
}

void Scene::setRenderer(Renderer* renderer)
{
this->renderer = renderer;
Expand Down

0 comments on commit 75d6cdf

Please sign in to comment.