Rework some stuff for memory

This commit is contained in:
SeanOMik 2020-07-03 15:26:44 -05:00
parent 7ced4f85cf
commit b24f4feb4a
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
9 changed files with 88 additions and 35 deletions

View File

@ -14,7 +14,7 @@
class SnakeMovementComponent : public simpleengine::Component { class SnakeMovementComponent : public simpleengine::Component {
public: public:
explicit SnakeMovementComponent(simpleengine::Entity* owning_entity, float movement_speed) : simpleengine::Component(owning_entity), movement_speed(movement_speed) { explicit SnakeMovementComponent(simpleengine::Entity& owning_entity, float movement_speed) : simpleengine::Component(owning_entity), movement_speed(movement_speed) {
} }
@ -39,7 +39,7 @@ public:
movement_direction.y = 1; movement_direction.y = 1;
} }
owning_entity->Move(delta_time, movement_direction.x, movement_direction.y); owning_entity.Move(delta_time, movement_direction.x, movement_direction.y);
} }
private: private:
float movement_speed; float movement_speed;
@ -57,7 +57,7 @@ public:
shape = sf::RectangleShape(sf::Vector2f(15, 15)); shape = sf::RectangleShape(sf::Vector2f(15, 15));
shape.setFillColor(sf::Color::White); shape.setFillColor(sf::Color::White);
this->AddComponent(new SnakeMovementComponent(this, movement_speed)); this->AddComponent(std::make_unique<SnakeMovementComponent>(*this, movement_speed));
loc = sf::Vector2f(0, 0); loc = sf::Vector2f(0, 0);
} }
@ -69,11 +69,11 @@ public:
UpdateComponents(delta_time); UpdateComponents(delta_time);
if (loc.x >= window_size.x) { if (loc.x >= window_size.x) {
DestroyEntity(); DestroyLater();
} }
if (loc.y >= window_size.y) { if (loc.y >= window_size.y) {
DestroyEntity(); DestroyLater();
} }
} }
@ -84,7 +84,7 @@ public:
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
simpleengine::Game game(500, 500, "First Example"); simpleengine::Game game(500, 500, "First Example");
game.AddEvent(new simpleengine::EntityEvent(game.GetWindow(), new SnakePlayerEntity(game.GetWindow()->getSize()))); game.AddEvent(new simpleengine::EntityEvent(game.GetWindow(), std::make_unique<SnakePlayerEntity>(game.GetWindow()->getSize())));
return game.Run(); return game.Run();
} }

View File

@ -9,17 +9,24 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <memory>
namespace simpleengine { namespace simpleengine {
class Entity; class Entity;
class Component { class Component {
public: public:
explicit Component(Entity* owning_entity = nullptr) : owning_entity(owning_entity) {} explicit Component(Entity& owning_entity);
virtual ~Component() = default; //virtual ~Component() = default;
//virtual void Destroying(); // Called when the Component is about to be destroyed.
void DestroyLater(); // In most cases, this will be ran next Entity::Update.
const bool& IsGettingDestroyed() const;
virtual void Update(const float& delta_time) = 0; virtual void Update(const float& delta_time) = 0;
virtual void Render(sf::RenderTarget* target) {}; // Most components won't need to be rendered. virtual void Render(sf::RenderTarget* target) {}; // Most components won't need to be rendered.
protected: protected:
Entity* owning_entity; Entity& owning_entity;
bool destroying = false;
}; };
} }

View File

@ -16,7 +16,7 @@ namespace simpleengine {
class MovementComponent : public Component { class MovementComponent : public Component {
public: public:
explicit MovementComponent(Entity* owning_entity, float movement_speed); MovementComponent(Entity& owning_entity, float movement_speed);
void Update(const float &delta_time) override; void Update(const float &delta_time) override;
private: private:

View File

@ -9,43 +9,48 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <stack> #include <vector>
#include <memory>
namespace simpleengine { namespace simpleengine {
class Component; class Component;
class Game; class Game;
class Event; class Event;
class Entity {
class Entity : std::enable_shared_from_this<Entity> {
friend class Game; friend class Game;
friend class Event; friend class Event;
public: public:
Entity() = default; Entity() = default;
virtual ~Entity() = default; virtual ~Entity() = default;
Entity(const Entity& entity) = delete;
virtual void Move(const float& delta_time, const float& x, const float& y) {}; virtual void Move(const float& delta_time, const float& x, const float& y) {};
virtual void Update(const float& delta_time) = 0; virtual void Update(const float& delta_time) = 0;
virtual void Render(sf::RenderTarget* target) = 0; virtual void Render(sf::RenderTarget* target) = 0;
virtual void Destroying() {}; // Called when the entity is about to be destroyed.
void DestroyEntity() { // Called when the entity is about to be destroyed.
destroying = true; // Make sure to call this in your extending Entity.
} virtual void Destroying();
const bool& IsGettingDestroyed() const { void DestroyLater(); // In most cases, this will be ran next EntityEvent::Update()
return destroying; const bool& IsGettingDestroyed() const;
}
// If your event does not extend from EntityEvent, you will need to execute this yourself inside Event::Update. // If your event does not extend from EntityEvent, you will need to execute this yourself inside Event::Update.
void UpdateComponents(const float& delta_time); void UpdateComponents(const float& delta_time);
void AddComponent(Component* component); void AddComponent(std::unique_ptr<Component> component);
std::shared_ptr<Entity> GetShared() {
return shared_from_this();
}
private: private:
// This is ran from class `Game` and `Event`. It runs the `UpdateComponents` method and then the `Update` method. // This is ran from class `Game` and `Event`. It runs the `UpdateComponents` method and then the `Update` method.
void UpdateEntity(const float& delta_time) { void UpdateEntity(const float& delta_time) {
UpdateComponents(delta_time); UpdateComponents(delta_time);
} }
std::vector<Component*> components; std::vector<std::unique_ptr<Component>> components;
bool destroying = false; bool destroying = false;
}; };
} }

View File

@ -13,14 +13,10 @@
namespace simpleengine { namespace simpleengine {
class EntityEvent : public Event { class EntityEvent : public Event {
public: public:
explicit EntityEvent(sf::RenderWindow* window, Entity* entity) : simpleengine::Event(window), entity(entity) { explicit EntityEvent(sf::RenderWindow* window, std::unique_ptr<Entity> entity) : simpleengine::Event(window), entity(std::move(entity)) {
} }
~EntityEvent() override {
delete entity;
}
void CheckForQuit() override { void CheckForQuit() override {
/*if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { /*if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
quit = true; quit = true;
@ -29,13 +25,17 @@ namespace simpleengine {
void Update(const float& delta_time) override { void Update(const float& delta_time) override {
entity->Update(delta_time); entity->Update(delta_time);
if (entity->IsGettingDestroyed()) {
entity->DestroyLater();
}
} }
void Render(sf::RenderTarget* target) override { void Render(sf::RenderTarget* target) override {
entity->Render(target); entity->Render(target);
} }
private: private:
Entity* entity; std::unique_ptr<Entity> entity;
}; };
} }

View File

@ -12,6 +12,8 @@
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include <stack> #include <stack>
#include "entity.h"
namespace simpleengine { namespace simpleengine {
class Event; class Event;

19
src/component.cpp Normal file
View File

@ -0,0 +1,19 @@
//
// Created by SeanOMik on 7/3/2020.
// Github: https://github.com/SeanOMik
// Email: seanomik@gmail.com
//
#include "component.h"
simpleengine::Component::Component(Entity& owning_entity) : owning_entity(owning_entity) {
}
void simpleengine::Component::DestroyLater() {
destroying = true;
}
const bool &simpleengine::Component::IsGettingDestroyed() const {
return destroying;
}

View File

@ -5,27 +5,29 @@
// //
#include "components/movement/movement_component.h" #include "components/movement/movement_component.h"
#include <utility>
#include "entity.h" #include "entity.h"
simpleengine::MovementComponent::MovementComponent(simpleengine::Entity *owning_entity, float movement_speed) : simpleengine::MovementComponent::MovementComponent(Entity& owning_entity, float movement_speed) :
Component(owning_entity), movement_speed(movement_speed) { Component(owning_entity), movement_speed(movement_speed) {
} }
void simpleengine::MovementComponent::Update(const float &delta_time) { void simpleengine::MovementComponent::Update(const float &delta_time) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
owning_entity->Move(delta_time, -1, 0); owning_entity.Move(delta_time, -1, 0);
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
owning_entity->Move(delta_time, 0, -1); owning_entity.Move(delta_time, 0, -1);
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
owning_entity->Move(delta_time, 1, 0); owning_entity.Move(delta_time, 1, 0);
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
owning_entity->Move(delta_time, 0, 1); owning_entity.Move(delta_time, 0, 1);
} }
} }

View File

@ -8,11 +8,29 @@
#include "component.h" #include "component.h"
void simpleengine::Entity::UpdateComponents(const float& delta_time) { void simpleengine::Entity::UpdateComponents(const float& delta_time) {
for (Component* component : components) { for (std::unique_ptr<Component>& component : components) {
component->Update(delta_time); component->Update(delta_time);
if (component->IsGettingDestroyed()) {
components.erase(std::remove(components.begin(), components.end(), component));
}
} }
} }
void simpleengine::Entity::AddComponent(simpleengine::Component *component) { void simpleengine::Entity::AddComponent(std::unique_ptr<Component> component) {
components.emplace_back(component); components.push_back(std::move(component));
}
void simpleengine::Entity::Destroying() {
for (std::unique_ptr<Component>& component : components) {
component->DestroyLater();
}
}
void simpleengine::Entity::DestroyLater() {
destroying = true;
}
const bool &simpleengine::Entity::IsGettingDestroyed() const {
return destroying;
} }