Skip to content

Commit

Permalink
shared_ptr rework
Browse files Browse the repository at this point in the history
still not finished
  • Loading branch information
PottierLoic committed Mar 21, 2024
1 parent 61ff76a commit 45d673c
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 82 deletions.
13 changes: 8 additions & 5 deletions core/component/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SpaceEngine {

Camera::Camera(Entity* owner) : Component(owner) {
Camera::Camera(std::weak_ptr<Entity> owner) : Component(owner) {
front = glm::vec3(0.0f, 0.0f, -1.0f);
zoom = 45.0f;
worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
Expand All @@ -12,11 +12,14 @@ Camera::Camera(Entity* owner) : Component(owner) {
updateCameraVectors();
}

Camera::~Camera() {}

glm::mat4 Camera::getViewMatrix() {
Transform* tf = this->owner->getComponent<Transform>();
return glm::lookAt(glm::vec3(tf->position.x, tf->position.y, tf->position.z), position + front, up);
auto lockedOwner = owner.lock();
if (!lockedOwner) {
return glm::mat4(1.0f);
} else {
auto tf = lockedOwner->getComponent<Transform>();
return glm::lookAt(glm::vec3(tf->position.x, tf->position.y, tf->position.z), position + front, up);
}
}

void Camera::updateCameraVectors() {
Expand Down
9 changes: 2 additions & 7 deletions core/component/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@ class Camera : public Component {

/**
* @brief Default constructor: Initializes a new empty Camera component.
* @param owner (Entity*): A pointer to the entity that store the component.
* @param owner (std::weak_ptr<Entity>): A pointer to the entity that store the component.
*/
Camera(Entity* owner);

/**
* @brief Destructor: Destroys the Camera component. Note: May not have additional functionality in this case.
*/
~Camera();
Camera(std::weak_ptr<Entity> owner);

/**
* @brief Get the view matrix of the camera.
Expand Down
2 changes: 1 addition & 1 deletion core/component/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SpaceEngine {

Component::Component(Entity* owner) { this->owner = owner; }
Component::Component(std::weak_ptr<Entity> owner) { this->owner = owner; }
Component::~Component() {}

}
10 changes: 6 additions & 4 deletions core/component/component.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <memory>

namespace SpaceEngine {

class Entity; // Forward declaration of Entity class to avoid circular dependencies.
Expand All @@ -8,22 +10,22 @@ class Entity; // Forward declaration of Entity class to avoid circular dependenc
* The Component class serves as a base class for components in the entity-component system.
*
* Attributes
* - owner (Entity*): A pointer to the entity owning the component.
* - owner (std::weak_ptr<Entity>): A pointer to the entity owning the component.
*
* Methods:
* - Component(): Default constructor.
* - virtual ~Component(): Virtual destructor to allow proper cleanup in derived classes.
*/
class Component {
protected:
Entity* owner = nullptr; // Pointer to the entity owning the component.
std::weak_ptr<Entity> owner; // Pointer to the entity owning the component.

public:
/**
* @brief Default constructor.
* @param owner (Entity*): A pointer to the entity that store the component.
* @param owner (std::weak_ptr<Entity>): A pointer to the entity that store the component.
*/
Component(Entity* owner);
Component(std::weak_ptr<Entity> owner);

/**
* @brief Virtual destructor to allow proper cleanup in derived classes.
Expand Down
6 changes: 1 addition & 5 deletions core/component/light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

namespace SpaceEngine {

Light::Light(Entity* owner) : Component(owner) {

}

Light::~Light() {
Light::Light(std::weak_ptr<Entity> owner) : Component(owner) {

}

Expand Down
8 changes: 2 additions & 6 deletions core/component/light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ class Light : public Component {

/**
* @brief Default constructor: Initializes a new Light component with default values.
* @param owner (Entity*): A pointer to the entity that store the component.
* @param owner (weak_ptr<Entity>): A pointer to the entity that store the component.
*/
Light(Entity* owner);
Light(std::weak_ptr<Entity> owner);

/**
* @brief Destructor: Destroys the Light component. Note: May not have additional functionality in this case.
*/
~Light();
};

}
2 changes: 1 addition & 1 deletion core/component/model_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SpaceEngine {

ModelRenderer::ModelRenderer(Entity* owner) : Component(owner) {
ModelRenderer::ModelRenderer(std::weak_ptr<Entity> owner) : Component(owner) {
model = nullptr;
}

Expand Down
6 changes: 3 additions & 3 deletions core/component/model_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace SpaceEngine {
*/
class ModelRenderer : public Component {
public:
Model* model = nullptr; /* The 3D model to be rendered. */
std::shared_ptr<Model> model; /* The 3D model to be rendered. */

/**
* @brief Default constructor: Initializes a new empty ModelRenderer component.
* @param owner (Entity*): A pointer to the entity that store the component.
* @param owner (std::weak_ptr<Entity>): A pointer to the entity that store the component.
*/
ModelRenderer(Entity* owner);
ModelRenderer(std::weak_ptr<Entity> owner);

/**
* @brief Destructor: Destroys the ModelRenderer component. Note: May not have additional functionality in this case.
Expand Down
2 changes: 1 addition & 1 deletion core/component/physic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SpaceEngine {

Physic::Physic(Entity* owner) : Component(owner) {
Physic::Physic(std::weak_ptr<Entity> owner) : Component(owner) {
this->mass = 1.0;
this->drag = 0;
this->angularDrag = 0;
Expand Down
4 changes: 2 additions & 2 deletions core/component/physic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class Physic : public Component {

/**
* @brief Default constructor: Initializes a new Physic component with default values.
* @param owner (Entity*): A pointer to the entity that store the component.
* @param owner (std::weak_ptr<Entity>): A pointer to the entity that store the component.
*/
Physic(Entity* owner);
Physic(std::weak_ptr<Entity> owner);

/**
* @brief Destructor: Destroys the Physic component. Note: May not have additional functionality in this case.
Expand Down
2 changes: 1 addition & 1 deletion core/component/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SpaceEngine {

Transform::Transform(Entity* owner, std::string name) : Component(owner) {
Transform::Transform(std::weak_ptr<Entity> owner, std::string name) : Component(owner) {
this->name = name;
this->position = Vector3();
this->scale = Vector3(1.0f, 1.0f, 1.0f);
Expand Down
4 changes: 2 additions & 2 deletions core/component/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class Transform : public Component {

/**
* @brief Default constructor: Initializes a new transform component with default values.
* @param owner (Entity*): A pointer to the entity that store the component.
* @param owner (std::weak_ptr<Entity>): A pointer to the entity that store the component.
* @param name: The name of the object.
*/
Transform(Entity* owner, std::string name);
Transform(std::weak_ptr<Entity> owner, std::string name);

/**
* @brief Destructor: Destroys the transform component. Note: May not have additional functionality in this case.
Expand Down
12 changes: 8 additions & 4 deletions core/entity/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ Entity::Entity(std::string name) {
this->addComponent<Transform>(name);
}

void Entity::addChildren(Entity* child) {
childs.push_back(child);
void Entity::addChild(std::unique_ptr<Entity> child) {
children.push_back(std::move(child));
}

void Entity::removeChildren(Entity* child) {
childs.erase(std::remove(childs.begin(), childs.end(), child), childs.end());
void Entity::removeChild(int index) {
if (index >= 0 && static_cast<std::size_t>(index) < children.size()) {
children.erase(children.begin() + index);
} else {
std::cerr << "Attempted to remove a child with an invalid index: " << index << std::endl;
}
}

}
19 changes: 10 additions & 9 deletions core/entity/entity.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <memory>
#include <algorithm>
#include <iostream>
#include <map>
Expand All @@ -14,12 +15,12 @@

namespace SpaceEngine {

class Entity {
class Entity : public std::enable_shared_from_this<Entity> {
public:
/* Map of components an Entity has. */
std::map<std::type_index, Component*> components;
std::map<std::type_index, std::shared_ptr<Component>> components;
/* Vector listing all the children entities. */
std::vector<Entity*> childs;
std::vector<std::shared_ptr<Entity>> children;

/* Default constructor: Initializes a new Entity with default values. */
Entity(std::string name);
Expand All @@ -33,7 +34,7 @@ class Entity {
*/
template <typename T, typename... Args>
void addComponent(Args&&... args) {
T* component = new T(this, std::forward<Args>(args)...);
auto component = std::make_shared<T>(shared_from_this(), std::forward<Args>(args)...);
components[typeid(T)] = component;
}

Expand All @@ -44,10 +45,10 @@ class Entity {
* @note Declared in .hpp to avoid massive template declaration.
*/
template <typename T>
T* getComponent() const {
std::shared_ptr<T> getComponent() const {
auto it = components.find(typeid(T));
if (it != components.end()) {
return dynamic_cast<T*>(it->second);
return std::dynamic_pointer_cast<T>(it->second);
}
return nullptr;
}
Expand All @@ -74,13 +75,13 @@ class Entity {
* Add a child to children vector of the entity.
* @param child: A reference to an Entity.
*/
void addChildren(Entity* child);
void addChild(std::unique_ptr<Entity> child);

/*
/* TODO: UPDATE AND CHECK IF INDEX IS GOOD
* Remove a child from children vector of the entity.
* @param child: A reference to an Entity.
*/
void removeChildren(Entity* child);
void removeChild(int index);
};

}
12 changes: 8 additions & 4 deletions core/scene/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ namespace SpaceEngine {

Scene::Scene() {
this->name = "New Scene";
Entity* defaultCamera = new Entity("Default Camera");
auto defaultCamera = std::make_shared<Entity>("Default Camera");
defaultCamera->addComponent<Camera>();
this->addEntity(defaultCamera);
this->selectedCamera = defaultCamera->getComponent<Camera>();
}

void Scene::addEntity(Entity* ent) {
void Scene::addEntity(std::shared_ptr<Entity> ent) {
entities.push_back(ent);
}

void Scene::removeEntity(Entity* ent) {
entities.erase(std::remove(entities.begin(), entities.end(), ent), entities.end());
void Scene::removeEntity(int index) {
if (index >= 0 && static_cast<size_t>(index) < entities.size()) {
entities.erase(entities.begin() + index);
} else {
std::cerr << "Error: Attempted to remove entity with invalid index: " << index << std::endl;
}
}

}
19 changes: 10 additions & 9 deletions core/scene/scene.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include "string"
#include "vector"
#include "algorithm"
#include <memory>
#include <string>
#include <vector>
#include <algorithm>

#include "entity/entity.hpp"
#include "component/camera.hpp"
Expand All @@ -28,10 +29,10 @@ class Scene{
std::string name;

// Vector of pointers to Entity contained within the scene
std::vector<Entity*> entities;
std::vector<std::shared_ptr<Entity>> entities;

// Main camera component in the scene. Used to render game.
Camera* selectedCamera = nullptr;
std::weak_ptr<Camera> selectedCamera;

/**
* @brief Initialize a Scene with default name.
Expand All @@ -42,13 +43,13 @@ class Scene{
* @brief Add an Entity to the scene.
* @param ent: Reference to the entity.
*/
void addEntity(Entity* ent);
void addEntity(std::shared_ptr<Entity> ent);

/**
/** TODO CHECK IF INDEX IS GOOD AND REWORK IF NEEDED
* @brief Remove an Entity from the scene.
* @param ent: Reference to the entity.
* @param index: index of the entity.
*/
void removeEntity(Entity* ent);
void removeEntity(int index);
};

}
12 changes: 6 additions & 6 deletions editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ int main() {
//TODO : Remove too
// Scene creation
Scene scene = Scene();
scene.addEntity(new Entity("Backpack"));
scene.addEntity(std::make_shared<Entity>("Backpack"));
scene.entities[1]->addComponent<ModelRenderer>();
scene.entities[1]->getComponent<ModelRenderer>()->model = new Model("../../models/backpack/backpack.obj");
scene.entities[1]->getComponent<ModelRenderer>()->model = std::make_shared<Model>("../../models/backpack/backpack.obj");

// Menu creation
Menu menu = Menu(&scene);
Menu menu = Menu(std::make_shared<Scene>());
menu.selectedEntity = scene.entities[1];

// TODO: REMOVE OPENGL TESTS
Expand Down Expand Up @@ -198,9 +198,9 @@ int main() {
shader.setMat4("view", view);

for (auto& entity : scene.entities) {
ModelRenderer* modelRenderer = entity->getComponent<ModelRenderer>();
if (modelRenderer != nullptr) {
Transform* tf = entity->getComponent<Transform>();
auto modelRenderer = entity->getComponent<ModelRenderer>();
if (modelRenderer && modelRenderer->model) {
auto tf = entity->getComponent<Transform>();
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(tf->position.x, tf->position.y, tf->position.z));
model = glm::scale(model, glm::vec3(tf->scale.x, tf->scale.y, tf->scale.z));
Expand Down
Loading

0 comments on commit 45d673c

Please sign in to comment.