Skip to content

Commit

Permalink
You Smart, You Loyal, I appreciate that
Browse files Browse the repository at this point in the history
  • Loading branch information
Shervanator committed Jan 29, 2017
1 parent f1604a0 commit e6950a7
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 94 deletions.
2 changes: 2 additions & 0 deletions src/engine/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef ENTITY_COMPONENT_H
#define ENTITY_COMPONENT_H

#include <memory>

#include "Input.h"
#include "Shader.h"
#include "Entity.h"
Expand Down
4 changes: 2 additions & 2 deletions src/engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void Engine::tick(void)

Entity *pickedEntity = m_physicsManager->pick(&ray);

if (pickedEntity != NULL)
if (pickedEntity != nullptr)
gl_manager->drawEntity(pickedEntity);

gl_manager->drawLine(ray.getLine(100.0f));
Expand All @@ -113,7 +113,7 @@ void Engine::tick(void)
f1Pressed = false;
}

window->getGuiManager()->render(game->getRootScene());
window->getGuiManager()->render(game->getRootScene().get());

window->swapBuffer();
}
Expand Down
21 changes: 8 additions & 13 deletions src/engine/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Entity::Entity(const std::string& tag)
Entity::setTag(this, tag);

m_tag = tag;
parentEntity = NULL;
parentEntity = nullptr;
}

Entity::Entity(void)
{
parentEntity = NULL;
parentEntity = nullptr;
}

Entity::~Entity(void)
Expand All @@ -34,11 +34,6 @@ Entity::~Entity(void)
{
delete component;
}

for (auto child : children)
{
delete child;
}
}

void Entity::setTag(Entity *entity, const std::string& tag)
Expand All @@ -51,7 +46,7 @@ std::vector<Entity*> Entity::findByTag(const std::string& tag)
return Entity::taggedEntities[tag];
}

void Entity::addChild(Entity* child)
void Entity::addChild(std::shared_ptr<Entity> child)
{
child->parentEntity = this;
children.push_back(child);
Expand All @@ -64,7 +59,7 @@ void Entity::addChild(Entity* child)

void Entity::updateInputAll(Input *input, int delta)
{
if (parentEntity == NULL) {
if (parentEntity == nullptr) {
worldMatrix = transform.getTransformMatrix();
} else {
worldMatrix = parentEntity->worldMatrix * transform.getTransformMatrix();
Expand Down Expand Up @@ -141,9 +136,9 @@ Transform& Entity::getTransform(void)
return transform;
}

std::vector<Entity*> *Entity::getChildren(void)
std::vector<std::shared_ptr<Entity>> Entity::getChildren(void)
{
return &children;
return children;
}

std::vector<Component*> *Entity::getComponents(void)
Expand All @@ -158,7 +153,7 @@ glm::mat4& Entity::getWorldMatrix(void)

glm::vec4 Entity::getPosition(void)
{
if (parentEntity == NULL) {
if (parentEntity == nullptr) {
return transform.getPosition();
} else {
return parentEntity->worldMatrix * transform.getPosition();
Expand All @@ -167,7 +162,7 @@ glm::vec4 Entity::getPosition(void)

glm::vec4 Entity::getDirection(void)
{
if (parentEntity == NULL) {
if (parentEntity == nullptr) {
return transform.getDirection();
} else {
return glm::normalize(parentEntity->worldMatrix * transform.getDirection());
Expand Down
7 changes: 4 additions & 3 deletions src/engine/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <map>
#include <typeindex>
#include <algorithm>
#include <memory>

#include "Transform.h"
#include "Shader.h"
Expand All @@ -25,7 +26,7 @@ class Entity
Entity(void);
~Entity(void);

void addChild(Entity* child);
void addChild(std::shared_ptr<Entity> child);

template <class T>
inline void addComponent(T* component)
Expand All @@ -52,7 +53,7 @@ class Entity

Transform& getTransform(void);

std::vector<Entity*> *getChildren(void);
std::vector<std::shared_ptr<Entity>> getChildren(void);
std::vector<Component*> *getComponents(void);

glm::mat4& getWorldMatrix(void);
Expand Down Expand Up @@ -98,7 +99,7 @@ class Entity

Entity *parentEntity;

std::vector<Entity*> children;
std::vector<std::shared_ptr<Entity>> children;
std::vector<Component*> components;

glm::mat4 worldMatrix;
Expand Down
11 changes: 6 additions & 5 deletions src/engine/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

Game::Game(void)
{
this->rootScene = std::make_shared<Entity>();
}

Game::~Game(void)
Expand All @@ -26,22 +27,22 @@ void Game::init(GLManager *glManager)
{
}

void Game::addToScene(Entity *entity)
void Game::addToScene(std::shared_ptr<Entity> entity)
{
rootScene.addChild(entity);
rootScene->addChild(entity);
}

void Game::updateInput(Input *input, int delta)
{
rootScene.updateInputAll(input, delta);
rootScene->updateInputAll(input, delta);
}

void Game::update(int delta)
{
rootScene.updateAll(delta);
rootScene->updateAll(delta);
}

void Game::render(GLManager *glManager)
{
glManager->renderScene(&rootScene);
glManager->renderScene(rootScene.get());
}
6 changes: 3 additions & 3 deletions src/engine/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class Game
virtual void update(int delta);
virtual void render(GLManager *glManager);

inline Entity *getRootScene(void) { return &rootScene; };
inline std::shared_ptr<Entity> getRootScene(void) { return rootScene; };

protected:
void addToScene(Entity *entity);
void addToScene(std::shared_ptr<Entity> entity);
Engine *getEngine(void) const;

private:
Entity rootScene;
std::shared_ptr<Entity> rootScene;
Engine *engine;
};

Expand Down
4 changes: 2 additions & 2 deletions src/engine/GuiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ void renderSceneGraph(Entity *sceneGraph)
renderComponent(component);
}

for (auto entity : *sceneGraph->getChildren()) {
renderSceneGraph(entity);
for (auto entity : sceneGraph->getChildren()) {
renderSceneGraph(entity.get());
}

ImGui::TreePop();
Expand Down
5 changes: 1 addition & 4 deletions src/engine/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "Material.h"

Material::Material(Texture *diffuseMap, Texture *normalMap, Texture *specularMap)
Material::Material(std::shared_ptr<Texture> diffuseMap, std::shared_ptr<Texture> normalMap, std::shared_ptr<Texture> specularMap)
{
m_diffuseMap = diffuseMap;
m_normalMap = normalMap;
Expand All @@ -13,9 +13,6 @@ Material::Material(Texture *diffuseMap, Texture *normalMap, Texture *specularMap

Material::~Material(void)
{
delete m_diffuseMap;
delete m_normalMap;
delete m_specularMap;
}

void Material::bind(void) const
Expand Down
8 changes: 4 additions & 4 deletions src/engine/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
class Material
{
public:
Material(Texture *diffuseMap, Texture *normalMap = new Texture(Asset("default_normal.jpg")), Texture *specularMap = new Texture(Asset("default_specular.jpg")));
Material(std::shared_ptr<Texture> diffuseMap, std::shared_ptr<Texture> normalMap = std::make_shared<Texture>(Asset("default_normal.jpg")), std::shared_ptr<Texture> specularMap = std::make_shared<Texture>(Asset("default_specular.jpg")));
~Material(void);

void bind(void) const;

private:
Texture *m_diffuseMap;
Texture *m_specularMap;
Texture *m_normalMap;
std::shared_ptr<Texture> m_diffuseMap;
std::shared_ptr<Texture> m_specularMap;
std::shared_ptr<Texture> m_normalMap;
};

#endif
31 changes: 15 additions & 16 deletions src/engine/MeshLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ MeshLoader::~MeshLoader(void)
{
}

Entity *MeshLoader::getEntity(void) const
std::shared_ptr<Entity> MeshLoader::getEntity(void) const
{
return m_entity;
}

void MeshLoader::loadScene(const aiScene* scene)
{
m_entity = new Entity();
m_entity = std::make_shared<Entity>();

for (int i = 0; i < scene->mNumMeshes; i++) {
const aiMesh* model = scene->mMeshes[i];
Expand Down Expand Up @@ -85,37 +85,36 @@ void MeshLoader::loadScene(const aiScene* scene)
const aiMaterial* pMaterial = scene->mMaterials[model->mMaterialIndex];
log_info("tex num: %i", model->mMaterialIndex);

Texture *diffuseMap = NULL;
Texture *normalMap = NULL;
Texture *specularMap = NULL;
std::shared_ptr<Texture> diffuseMap;
std::shared_ptr<Texture> normalMap;
std::shared_ptr<Texture> specularMap;

aiString Path;

if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0
&& pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
diffuseMap = new Texture(Asset(Path.data));
diffuseMap = std::make_shared<Texture>(Asset(Path.data));
} else {
diffuseMap = new Texture(Asset("default_normal.jpg"));
diffuseMap = std::make_shared<Texture>(Asset("default_normal.jpg"));
}

if (pMaterial->GetTextureCount(aiTextureType_HEIGHT) > 0
&& pMaterial->GetTexture(aiTextureType_HEIGHT, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
normalMap = new Texture(Asset(Path.data));
normalMap = std::make_shared<Texture>(Asset(Path.data));
} else {
normalMap = new Texture(Asset("default_normal.jpg"));
normalMap = std::make_shared<Texture>(Asset("default_normal.jpg"));
}

if (pMaterial->GetTextureCount(aiTextureType_SPECULAR) > 0
&& pMaterial->GetTexture(aiTextureType_SPECULAR, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
specularMap = new Texture(Asset(Path.data));
specularMap = std::make_shared<Texture>(Asset(Path.data));
} else {
specularMap = new Texture(Asset("default_specular.jpg"));
specularMap = std::make_shared<Texture>(Asset("default_specular.jpg"));
}

m_entity->addComponent(
new MeshRenderer(
new Mesh(m_fileName + std::string(model->mName.C_Str()), &vertices[0], vertices.size(), &indices[0], indices.size()),
new Material(diffuseMap, normalMap, specularMap)
));
m_entity->addComponent<MeshRenderer>(
std::make_shared<Mesh>(m_fileName + std::string(model->mName.C_Str()), &vertices[0], vertices.size(), &indices[0], indices.size()),
std::make_shared<Material>(diffuseMap, normalMap, specularMap)
);
}
}
4 changes: 2 additions & 2 deletions src/engine/MeshLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class MeshLoader
MeshLoader(const std::string file);
~MeshLoader(void);

Entity *getEntity(void) const;
std::shared_ptr<Entity> getEntity(void) const;

private:
void loadScene(const aiScene* scene);

std::string m_fileName;

Entity *m_entity;
std::shared_ptr<Entity> m_entity;
};

#endif
2 changes: 1 addition & 1 deletion src/engine/PhysicsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Entity *PhysicsManager::pick(Ray *ray) const
{
glm::vec3 intersectionPosition;
float closest = std::numeric_limits<float>::max();
Entity *entity = NULL;
Entity *entity = nullptr;

for (unsigned int i = 0; i < m_colliders.size(); i++)
{
Expand Down
32 changes: 13 additions & 19 deletions src/engine/Plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@

#include "Plane.h"

Plane::Plane(void)
{
Vertex vertices[] = {
Vertex(glm::vec3(-0.5, 0, 0.5), glm::vec2(0, 0), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0)),
Vertex(glm::vec3(0.5, 0, 0.5), glm::vec2(1, 0), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0)),
Vertex(glm::vec3(0.5, 0, -0.5), glm::vec2(1, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0)),
Vertex(glm::vec3(-0.5, 0, -0.5), glm::vec2(0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0))
};
Vertex vertices[] = {
Vertex(glm::vec3(-0.5, 0, 0.5), glm::vec2(0, 0), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0)),
Vertex(glm::vec3(0.5, 0, 0.5), glm::vec2(1, 0), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0)),
Vertex(glm::vec3(0.5, 0, -0.5), glm::vec2(1, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0)),
Vertex(glm::vec3(-0.5, 0, -0.5), glm::vec2(0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0))
};

unsigned int indices[] = {
0, 1, 2,
0, 2, 3
};
unsigned int indices[] = {
0, 1, 2,
0, 2, 3
};

mesh = new Mesh("BUILTIN_plane", vertices, 4, indices, 6);
}

Plane::~Plane(void)
Plane::Plane(void)
{
delete mesh;
}

Mesh *Plane::getMesh(void) const
std::shared_ptr<Mesh> Plane::getMesh(void)
{
return mesh;
return std::make_shared<Mesh>("BUILTIN_plane", vertices, 4, indices, 6);
}
5 changes: 1 addition & 4 deletions src/engine/Plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ class Plane
Plane(void);
~Plane(void);

Mesh *getMesh(void) const;

private:
Mesh *mesh;
static std::shared_ptr<Mesh> getMesh(void);
};

#endif
Loading

0 comments on commit e6950a7

Please sign in to comment.