Reorganize & Fix a memory issue.
This commit is contained in:
1169
cmake/CPM.cmake
1169
cmake/CPM.cmake
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Engine/types/scene/Scene.h>
|
#include <Engine/types/Scene.h>
|
||||||
#include <Engine/GameWindow.h>
|
#include <Engine/types/GameWindow.h>
|
||||||
|
|
||||||
namespace Globals {
|
namespace Globals {
|
||||||
using namespace Engine;
|
using namespace Engine;
|
||||||
|
@@ -3,7 +3,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <Engine/types/InstancedTexture.h>
|
#include <Engine/types/InstancedTexture.h>
|
||||||
#include <Engine/types/entity/Hud.h>
|
#include <Engine/types/entity/Hud.h>
|
||||||
#include <Engine/types/scene/Fixed.h>
|
|
||||||
#include <JGL/types/RenderTarget.h>
|
#include <JGL/types/RenderTarget.h>
|
||||||
|
|
||||||
namespace Engine {
|
namespace Engine {
|
||||||
@@ -23,14 +22,11 @@ protected:
|
|||||||
std::vector<InstancedTexture*> instanced_alpha_masks{};
|
std::vector<InstancedTexture*> instanced_alpha_masks{};
|
||||||
|
|
||||||
std::vector<RenderTarget*> layers{};
|
std::vector<RenderTarget*> layers{};
|
||||||
std::vector<Fixed*> fixed_list{};
|
|
||||||
std::vector<Entity*> entity_list{};
|
std::vector<Entity*> entity_list{};
|
||||||
protected:
|
protected:
|
||||||
[[nodiscard]] std::vector<Entity*> GetFlatEntityList(const std::vector<Entity*>& entity_list) const;
|
[[nodiscard]] std::vector<Entity*> GetFlatEntityList(const std::vector<Entity*>& entity_list) const;
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] bool EntityListContains(const Entity* entity) const;
|
[[nodiscard]] bool EntityListContains(const Entity* entity) const;
|
||||||
[[nodiscard]] bool FixedListContains(const Fixed* fixed) const;
|
|
||||||
[[nodiscard]] size_t FixedCount() const;
|
|
||||||
[[nodiscard]] size_t EntityCount() const;
|
[[nodiscard]] size_t EntityCount() const;
|
||||||
[[nodiscard]] std::string GetName() const;
|
[[nodiscard]] std::string GetName() const;
|
||||||
[[nodiscard]] Camera* GetActiveCamera() const;
|
[[nodiscard]] Camera* GetActiveCamera() const;
|
||||||
@@ -41,15 +37,12 @@ public:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
void AppendEntity(Entity* entity);
|
void AppendEntity(Entity* entity);
|
||||||
void AppendFixed(Fixed* fixed);
|
|
||||||
|
|
||||||
// Removes and deallocates.
|
// Removes and deallocates.
|
||||||
void DestroyEntity(Entity* entity);
|
void DestroyEntity(Entity* entity);
|
||||||
void DestroyFixed(Fixed* fixed);
|
|
||||||
|
|
||||||
// Only removes from the list.
|
// Only removes from the list.
|
||||||
void RemoveEntity(Entity* entity);
|
void RemoveEntity(Entity* entity);
|
||||||
void RemoveFixed(Fixed* fixed);
|
|
||||||
|
|
||||||
virtual void Unload();
|
virtual void Unload();
|
||||||
virtual void Init() {}
|
virtual void Init() {}
|
@@ -8,4 +8,5 @@ namespace Engine {
|
|||||||
class Engine::Hud : public Engine::Renderable {
|
class Engine::Hud : public Engine::Renderable {
|
||||||
public:
|
public:
|
||||||
Hud() : Renderable(0) {};
|
Hud() : Renderable(0) {};
|
||||||
|
~Hud() override = default;
|
||||||
};
|
};
|
||||||
|
@@ -17,4 +17,5 @@ public:
|
|||||||
virtual void Render() = 0;
|
virtual void Render() = 0;
|
||||||
public:
|
public:
|
||||||
explicit Renderable(unsigned int layer) : layer(layer) {}
|
explicit Renderable(unsigned int layer) : layer(layer) {}
|
||||||
|
virtual ~Renderable() = default;
|
||||||
};
|
};
|
@@ -1,40 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <J3ML/LinearAlgebra/Vector2i.hpp>
|
|
||||||
#include <J3ML/Geometry/AABB2D.hpp>
|
|
||||||
#include <JGL/types/Texture.h>
|
|
||||||
|
|
||||||
using J3ML::LinearAlgebra::Vector2i;
|
|
||||||
using JGL::Texture;
|
|
||||||
|
|
||||||
namespace Engine {
|
|
||||||
class Fixed;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Things that are considered non-movable parts of the level.
|
|
||||||
// TODO instanced textures.
|
|
||||||
class Engine::Fixed {
|
|
||||||
private:
|
|
||||||
void GenerateCollision();
|
|
||||||
protected:
|
|
||||||
std::vector<Vector2i> collision{};
|
|
||||||
bool enabled = false;
|
|
||||||
bool collidable = false;
|
|
||||||
Vector2i position = {0, 0};
|
|
||||||
const Texture* texture = nullptr;
|
|
||||||
public:
|
|
||||||
[[nodiscard]] bool Collidable() const;
|
|
||||||
[[nodiscard]] Vector2i GetPosition() const;
|
|
||||||
[[nodiscard]] AABB2D GetBounds() const;
|
|
||||||
[[nodiscard]] bool Enabled() const;
|
|
||||||
[[nodiscard]] std::vector<Vector2i> GetCollision();
|
|
||||||
[[nodiscard]] const Texture* GetTexture();
|
|
||||||
public:
|
|
||||||
void Enable();
|
|
||||||
void Disable();
|
|
||||||
virtual void Render() {};
|
|
||||||
public:
|
|
||||||
Fixed(const Vector2i& position, bool enabled, bool collidable, const Texture* texture) : position(position), enabled(enabled), collidable(collidable), texture(texture)
|
|
||||||
{ if (collidable) GenerateCollision(); }
|
|
||||||
virtual ~Fixed();
|
|
||||||
};
|
|
@@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <Engine/types/scene/Scene.h>
|
#include <Engine/types/Scene.h>
|
||||||
|
|
||||||
class ControllableBox final : public Engine::Scene {
|
class ControllableBox final : public Engine::Scene {
|
||||||
public:
|
public:
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <Engine/types/scene/Scene.h>
|
#include <Engine/types/Scene.h>
|
||||||
#include <JGL/types/RenderTarget.h>
|
#include <JGL/types/RenderTarget.h>
|
||||||
|
|
||||||
class LoadingScreen final : public Engine::Scene {
|
class LoadingScreen final : public Engine::Scene {
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
#include <Engine/GameWindow.h>
|
#include <Engine/types/GameWindow.h>
|
||||||
#include <Engine/Globals.h>
|
#include <Engine/Globals.h>
|
||||||
#include <JGL/JGL.h>
|
#include <JGL/JGL.h>
|
||||||
|
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
#include <Engine/types/scene/Scene.h>
|
#include <Engine/types/Scene.h>
|
||||||
#include <Engine/types/entity/Camera.h>
|
#include <Engine/types/entity/Camera.h>
|
||||||
#include <Engine/types/entity/InstancedSprite.h>
|
#include <Engine/types/entity/InstancedSprite.h>
|
||||||
#include <JGL/JGL.h>
|
#include <JGL/JGL.h>
|
||||||
@@ -14,17 +14,6 @@ bool Scene::EntityListContains(const Entity* entity) const {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Scene::FixedListContains(const Fixed* fixed) const {
|
|
||||||
for (auto* f : fixed_list)
|
|
||||||
if (f == fixed)
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Scene::FixedCount() const {
|
|
||||||
return fixed_list.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Scene::EntityCount() const {
|
size_t Scene::EntityCount() const {
|
||||||
return GetFlatEntityList(entity_list).size();
|
return GetFlatEntityList(entity_list).size();
|
||||||
}
|
}
|
||||||
@@ -56,10 +45,6 @@ void Scene::Render(RenderTarget* render_target) {
|
|||||||
if (active_camera)
|
if (active_camera)
|
||||||
active_camera->Render();
|
active_camera->Render();
|
||||||
|
|
||||||
for (auto& f : fixed_list)
|
|
||||||
if (f->Enabled())
|
|
||||||
f->Render();
|
|
||||||
|
|
||||||
for (auto& r : display_list)
|
for (auto& r : display_list)
|
||||||
r->Render();
|
r->Render();
|
||||||
|
|
||||||
@@ -70,9 +55,6 @@ void Scene::Render(RenderTarget* render_target) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Scene::~Scene() {
|
Scene::~Scene() {
|
||||||
for (auto* f : fixed_list)
|
|
||||||
delete f;
|
|
||||||
|
|
||||||
for (auto* e : entity_list)
|
for (auto* e : entity_list)
|
||||||
delete e;
|
delete e;
|
||||||
|
|
||||||
@@ -87,9 +69,6 @@ Scene::~Scene() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Scene::Unload() {
|
void Scene::Unload() {
|
||||||
for (auto* f : fixed_list)
|
|
||||||
delete f;
|
|
||||||
|
|
||||||
for (auto* e : entity_list)
|
for (auto* e : entity_list)
|
||||||
delete e;
|
delete e;
|
||||||
|
|
||||||
@@ -101,36 +80,18 @@ void Scene::AppendEntity(Entity* entity) {
|
|||||||
entity_list.push_back(entity);
|
entity_list.push_back(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::AppendFixed(Fixed* fixed) {
|
|
||||||
if (!FixedListContains(fixed))
|
|
||||||
fixed_list.push_back(fixed);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Scene::DestroyEntity(Entity *entity) {
|
void Scene::DestroyEntity(Entity *entity) {
|
||||||
auto it = std::find(entity_list.begin(), entity_list.end(), entity);
|
auto it = std::find(entity_list.begin(), entity_list.end(), entity);
|
||||||
if (it != entity_list.end())
|
if (it != entity_list.end())
|
||||||
delete *it, entity_list.erase(it);
|
delete *it, entity_list.erase(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::DestroyFixed(Fixed* fixed) {
|
|
||||||
auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed);
|
|
||||||
if (it != fixed_list.end())
|
|
||||||
delete *it, fixed_list.erase(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Scene::RemoveEntity(Entity* entity) {
|
void Scene::RemoveEntity(Entity* entity) {
|
||||||
auto it = std::find(entity_list.begin(), entity_list.end(), entity);
|
auto it = std::find(entity_list.begin(), entity_list.end(), entity);
|
||||||
if (it != entity_list.end())
|
if (it != entity_list.end())
|
||||||
entity_list.erase(it);
|
entity_list.erase(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Scene::RemoveFixed(Fixed* fixed) {
|
|
||||||
auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed);
|
|
||||||
if (it != fixed_list.end())
|
|
||||||
fixed_list.erase(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string Scene::GetName() const {
|
std::string Scene::GetName() const {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
@@ -1,5 +1,5 @@
|
|||||||
#include "Engine/types/entity/mixins/Movable.h"
|
#include <Engine/types/entity/mixins/Movable.h>
|
||||||
#include "Engine/Globals.h"
|
#include <Engine/Globals.h>
|
||||||
|
|
||||||
using namespace J3ML;
|
using namespace J3ML;
|
||||||
void Engine::Movable::MoveX(float speed) {
|
void Engine::Movable::MoveX(float speed) {
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
#include "Engine/types/entity/mixins/BoxCollider.h"
|
#include <Engine/types/entity/mixins/BoxCollider.h>
|
||||||
|
|
||||||
using namespace Engine;
|
using namespace Engine;
|
||||||
bool BoxCollider::BoxCollides(BoxCollider* rhs) {
|
bool BoxCollider::BoxCollides(BoxCollider* rhs) {
|
||||||
|
@@ -1,52 +0,0 @@
|
|||||||
#include "Engine/types/scene/Fixed.h"
|
|
||||||
|
|
||||||
bool Engine::Fixed::Collidable() const {
|
|
||||||
return collidable;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector2i Engine::Fixed::GetPosition() const {
|
|
||||||
return position;
|
|
||||||
}
|
|
||||||
|
|
||||||
AABB2D Engine::Fixed::GetBounds() const {
|
|
||||||
auto maximum = Vector2(position.x + texture->GetDimensions().x, position.y + texture->GetDimensions().y);
|
|
||||||
return { Vector2(position), maximum };
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Engine::Fixed::Enabled() const {
|
|
||||||
return enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::Fixed::Enable() {
|
|
||||||
enabled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::Fixed::Disable() {
|
|
||||||
enabled = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Engine::Fixed::~Fixed() {
|
|
||||||
delete texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::Fixed::GenerateCollision() {
|
|
||||||
if (!Collidable() || !Enabled() || !texture)
|
|
||||||
return;
|
|
||||||
|
|
||||||
std::vector<Vector2i> result{};
|
|
||||||
auto pixel_data = texture->GetPixelData();
|
|
||||||
|
|
||||||
for (int y = 0; y < texture->GetDimensions().y; y++)
|
|
||||||
for (int x = 0; x < texture->GetDimensions().x; x++)
|
|
||||||
if (pixel_data[y * texture->GetDimensions().x + x].A() != 0)
|
|
||||||
result.emplace_back((int) x + position.x, (int) y + position.y);
|
|
||||||
collision = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<Vector2i> Engine::Fixed::GetCollision() {
|
|
||||||
return collision;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Texture* Engine::Fixed::GetTexture() {
|
|
||||||
return texture;
|
|
||||||
}
|
|
Reference in New Issue
Block a user