Refactor & More.

Changed the entity system to utilize mix-ins.

Added instanced sprites.

Texture support.
This commit is contained in:
2025-01-03 19:28:58 -05:00
parent c9daae8d25
commit 6664925621
39 changed files with 503 additions and 310 deletions

View File

@@ -1,13 +0,0 @@
#pragma once
#include <Engine/Entity/Renderable.h>
namespace Engine {
class Camera;
}
class Engine::Camera : public Renderable {
public:
void Render() override {};
public:
explicit Camera(const Vector2& position) : Renderable(position, 0) {}
};

View File

@@ -1,59 +0,0 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <vector>
#include <chrono>
namespace Engine {
class Entity;
}
class Engine::Entity {
protected:
// nullptr means no parent.
Entity* parent = nullptr;
// Epoch micro-seconds.
long creation_time = 0.0f;
std::vector<Entity*> children{};
Vector2 position = {0, 0};
// Assuming 0 radians is up.
float face_angle = 0.0f;
void UpdateChildren();
public:
// Movements independent of the face_angle.
void MoveX(float speed);
void MoveY(float speed);
void Move(float angle_rad, float speed);
// Movements dependent on face angle.
void MoveForward(float speed);
void MoveBackward(float speed);
void MoveLeft(float speed);
void MoveRight(float speed);
void Rotate(float speed);
void SetRotation(float new_rotation);
// Parent child structure.
[[nodiscard]] bool AppendChild(Entity* entity);
void DestroyChild(Entity* entity);
void RemoveChild(Entity* entity);
public:
[[nodiscard]] std::vector<Entity*> GetChildren() const;
[[nodiscard]] Entity* GetParent() const;
// Microseconds.
[[nodiscard]] long GetCreationTime() const;
[[nodiscard]] long GetAge() const;
[[nodiscard]] float GetRotation() const;
[[nodiscard]] Vector2 GetPosition() const;
public:
virtual void Update() {}
public:
virtual ~Entity();
explicit Entity(const Vector2& position, float rotation = 0.0f) :
creation_time(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()),
position(position), face_angle(rotation) {}
};

View File

@@ -1,22 +0,0 @@
#pragma once
#include <Engine/Entity/Entity.h>
#include <JGL/JGL.h>
namespace Engine {
class Renderable;
}
class Engine::Renderable : public Entity {
protected:
// Higher numbers are farther to the back.
unsigned int depth = 0;
public:
[[nodiscard]] unsigned int GetDepth() const { return depth; }
void SetDepth(unsigned int new_depth) { depth = new_depth; }
public:
// *must* be overridden.
virtual void Render() = 0;
public:
explicit Renderable(const Vector2& position, unsigned int depth = 0, float rotation = 0.0f) : Entity(position, rotation), depth(depth) {}
};

View File

@@ -1,5 +1,6 @@
#include <rewindow/types/window.h>
#pragma once
#include <rewindow/types/window.h>
#include <rewindow/logger/logger.h>
namespace Engine {
class DemoGameWindow;
@@ -11,7 +12,16 @@ public:
public:
void OnRefresh(float elapsed) override;
public:
DemoGameWindow() : ReWindow::RWindow() {}
DemoGameWindow(const std::string& title, int width, int height) : ReWindow::RWindow(title, width, height) {}
DemoGameWindow(const std::string& title, int width, int height) : ReWindow::RWindow(title, width, height)
{
ReWindow::Logger::Error.EnableConsole(false);
ReWindow::Logger::Warning.EnableConsole(false);
ReWindow::Logger::Debug.EnableConsole(false);
SetRenderer(RenderingAPI::OPENGL);
Open();
InitGL();
SetResizable(false);
SetVsyncEnabled(false);
}
};

View File

@@ -1,6 +1,6 @@
#pragma once
#include <Engine/Level/Scene.h>
#include <Engine/types/scene/Scene.h>
#include <Engine/GameWindow.h>
namespace Globals {

View File

@@ -0,0 +1,24 @@
#pragma once
#include <vector>
#include <JGL/types/Texture.h>
namespace Engine {
class InstancedTexture;
class InstancedSprite;
}
class Engine::InstancedTexture {
protected:
JGL::Texture* texture;
std::vector<const InstancedSprite*> users{};
public:
[[nodiscard]] size_t ReferenceCount() { return users.size(); }
[[nodiscard]] bool InUseBy(const InstancedSprite* rhs);
[[nodiscard]] JGL::Texture* GetTexture() const { return texture; }
public:
void AddUser(const InstancedSprite* user);
void RemoveUser(const InstancedSprite* user);
public:
InstancedTexture(JGL::Texture* texture, const InstancedSprite* creator) : texture(texture) { users.push_back(creator); }
~InstancedTexture();
};

View File

@@ -0,0 +1,15 @@
#pragma once
#include <Engine/types/entity/Entity.h>
#include <Engine/types/entity/mixins/Movable.h>
#include <Engine/types/entity/mixins/Renderable.h>
namespace Engine {
class Camera;
}
class Engine::Camera : public Entity, public Movable, public Renderable {
public:
void Render() override {};
public:
explicit Camera(const Vector2& position) : Renderable(0), Movable(position) {}
};

View File

@@ -0,0 +1,36 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <vector>
#include <chrono>
namespace Engine {
class Entity;
}
class Engine::Entity {
protected:
// nullptr means no parent.
Entity* parent = nullptr;
// Epoch micro-seconds.
long creation_time = 0.0f;
std::vector<Entity*> children{};
void UpdateChildren();
public:
// Parent child structure.
[[nodiscard]] bool AppendChild(Entity* entity);
void DestroyChild(Entity* entity);
void RemoveChild(Entity* entity);
public:
[[nodiscard]] std::vector<Entity*> GetChildren() const;
[[nodiscard]] Entity* GetParent() const;
// Microseconds.
[[nodiscard]] long GetCreationTime() const;
[[nodiscard]] long GetAge() const;
public:
virtual void Update() {}
public:
virtual ~Entity();
explicit Entity() : creation_time(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()) {}
};

View File

@@ -1,5 +1,5 @@
#pragma once
#include <Engine/Entity/Renderable.h>
#include <Engine/types/entity/mixins/Renderable.h>
namespace Engine {
class Hud;
@@ -7,5 +7,5 @@ namespace Engine {
class Engine::Hud : public Engine::Renderable {
public:
Hud() : Renderable({0, 0}, 0, 0) {};
Hud() : Renderable(0) {};
};

View File

@@ -0,0 +1,28 @@
#pragma once
#include <Engine/types/entity/Sprite.h>
namespace Engine {
class InstancedSprite;
}
class Engine::InstancedSprite : public Sprite {
private:
std::string texture_path;
std::string alpha_mask_path;
public:
[[nodiscard]] std::string GetTextureFilesystemPath() const { return texture_path; }
[[nodiscard]] std::string GetAlphaMaskFilesystemPath() const { return alpha_mask_path; }
[[nodiscard]] Texture* GetTexture() override;
[[nodiscard]] Texture* GetAlphaMask() override;
public:
/** The Texture *must* be instanced. The alpha-mask *can* be instanced.
* You can use SetAlphaMask() to change to a unique one.
* To change back to the instanced one if there is one, You can SetAlphaMask(nullptr);
* If there is no instanced alpha mask and unique alpha mask is set
* to nullptr, Then there is no alpha mask. - Redacted.
*/
InstancedSprite(const Vector2& position, unsigned int layer, const std::string& instanced_texture_filesystem_path,
float face_angle = 0.0f, const Color4& base_color = Colors::White, Texture* unique_alpha_mask = nullptr, const std::string& instanced_alpha_mask_filesystem_path = "")
: texture_path(instanced_texture_filesystem_path), alpha_mask_path(instanced_alpha_mask_filesystem_path),
Sprite(position, layer, face_angle, base_color, nullptr, unique_alpha_mask) {}
};

View File

@@ -1,6 +1,9 @@
#pragma once
#include <Engine/Entity/Renderable.h>
#include <Colors.hpp>
#include <Engine/types/entity/Entity.h>
#include <Engine/types/entity/mixins/Movable.h>
#include <Engine/types/entity/mixins/Renderable.h>
#include <JGL/types/Texture.h>
#include <J3ML/Geometry/AABB2D.hpp>
@@ -8,23 +11,24 @@ namespace Engine {
class Sprite;
}
class Engine::Sprite : public Renderable {
protected:
using JGL::Texture;
class Engine::Sprite : public Entity, public Renderable, public Movable {
private:
Texture* texture = nullptr;
// Positive alpha mask.
Texture* alpha_mask = nullptr;
protected:
Vector2 scale = {1, 1};
// Local space, Where the face_angle rotation should be preformed about.
Vector2 origin = {0, 0};
Color4 base_color = Colors::White;
public:
[[nodiscard]] Texture* GetTexture();
// World space.
[[nodiscard]] virtual AABB2D GetBounds();
protected:
[[nodiscard]] virtual Texture* GetTexture();
[[nodiscard]] virtual Texture* GetAlphaMask();
void SetAlphaMask(Texture* new_alpha_mask);
public:
void Render() override;
public:
~Sprite() override;
explicit Sprite(const Vector2& pos, unsigned int depth = 0, const float face_angle = 0.0f, const Color4& base_color = Colors::White, Texture* texture = nullptr,
Texture* alpha_mask = nullptr) : Renderable(pos, depth,face_angle), texture(texture), alpha_mask(alpha_mask), base_color(base_color) {}
explicit Sprite(const Vector2& pos, unsigned int layer = 0, const float face_angle = 0.0f, const Color4& base_color = Colors::White, Texture* texture = nullptr,
Texture* alpha_mask = nullptr) : Renderable(layer), Movable(pos, face_angle), texture(texture), alpha_mask(alpha_mask), base_color(base_color) {}
};

View File

@@ -0,0 +1,20 @@
#pragma once
#include <J3ML/Geometry/AABB2D.hpp>
namespace Engine {
class BoxCollider;
}
class Engine::BoxCollider {
private:
bool enabled = false;
public:
[[nodiscard]] virtual AABB2D GetBounds() = 0;
[[nodiscard]] bool BoxCollides(BoxCollider* rhs);
[[nodiscard]] bool BoxCollides(const Vector2& rhs);
public:
void EnableBoxCollision() { enabled = true; }
void DisableBoxCollision() { enabled = false; }
public:
explicit BoxCollider(bool enabled = true) : enabled(enabled) {}
};

View File

@@ -0,0 +1,34 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector2.hpp>
namespace Engine {
class Movable;
}
// Movable Object.
class Engine::Movable {
protected:
// Assuming 0 radians is up.
float face_angle = 0.0f;
Vector2 position = {0, 0};
public:
// Movements independent of the face_angle.
void MoveX(float speed);
void MoveY(float speed);
void Move(float angle_rad, float speed);
// Movements dependent on face angle.
void MoveForward(float speed);
void MoveBackward(float speed);
void MoveLeft(float speed);
void MoveRight(float speed);
void Rotate(float speed);
void SetRotation(float new_rotation);
public:
[[nodiscard]] float GetRotation() const;
[[nodiscard]] Vector2 GetPosition() const;
public:
explicit Movable(const Vector2& position, float rad_rotation = 0.0f) : position(position), face_angle(rad_rotation) {}
};

View File

@@ -0,0 +1,20 @@
#pragma once
#include <Engine/types/entity/Entity.h>
namespace Engine {
class Renderable;
}
class Engine::Renderable {
protected:
// Higher numbers are farther to the back.
unsigned int layer = 0;
public:
[[nodiscard]] unsigned int GetLayer() const { return layer; }
void SetLayer(unsigned int new_layer) { layer = new_layer; }
public:
// *must* be overridden.
virtual void Render() = 0;
public:
explicit Renderable(unsigned int layer) : layer(layer) {}
};

View File

@@ -1,8 +1,8 @@
#pragma once
#include "J3ML/LinearAlgebra/Vector2i.hpp"
#include "J3ML/Geometry/AABB2D.hpp"
#include "JGL/types/Texture.h"
#include <J3ML/LinearAlgebra/Vector2i.hpp>
#include <J3ML/Geometry/AABB2D.hpp>
#include <JGL/types/Texture.h>
using J3ML::LinearAlgebra::Vector2i;
using JGL::Texture;

View File

@@ -1,9 +1,9 @@
#pragma once
#include <vector>
#include <Engine/Entity/Renderable.h>
#include <Engine/Entity/Hud.h>
#include <Engine/Level/Fixed.h>
#include <Engine/types/InstancedTexture.h>
#include <Engine/types/entity/Hud.h>
#include <Engine/types/scene/Fixed.h>
#include <JGL/types/RenderTarget.h>
namespace Engine {
@@ -11,11 +11,18 @@ namespace Engine {
class Scene;
}
using namespace JGL;
class Engine::Scene {
protected:
std::string name;
Hud* heads_up_display = nullptr;
Camera* active_camera = nullptr;
std::vector<InstancedTexture*> instanced_textures{};
std::vector<InstancedTexture*> instanced_alpha_masks{};
std::vector<RenderTarget*> layers{};
std::vector<Fixed*> fixed_list{};
std::vector<Entity*> entity_list{};
protected:
@@ -27,6 +34,8 @@ public:
[[nodiscard]] size_t EntityCount() const;
[[nodiscard]] std::string GetName() const;
[[nodiscard]] Camera* GetActiveCamera() const;
[[nodiscard]] Texture* GetInstancedTexture(const InstancedSprite* user);
[[nodiscard]] Texture* GetInstancedAlphaMask(const InstancedSprite* user);
public:
void AppendEntity(Entity* entity);
void AppendFixed(Fixed* fixed);

View File

@@ -1,16 +0,0 @@
#pragma once
#include <Engine/Entity/Renderable.h>
#include <Engine/Globals.h>
namespace Game {
class Box;
}
class Game::Box final : public Engine::Renderable {
public:
void Render() final;
void Update() final;
public:
explicit Box(const Vector2& position, unsigned int depth = 0, float rotation = 0.0f) : Renderable(position, depth, rotation) {}
};

17
include/Game/entity/Box.h Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include <Engine/types/entity/Entity.h>
#include <Engine/types/entity/mixins/Movable.h>
#include <Engine/types/entity/mixins/Renderable.h>
namespace Game {
class Box;
}
class Game::Box final : public Engine::Entity, public Engine::Renderable, public Engine::Movable {
public:
void Render() final;
void Update() final;
public:
explicit Box(const Vector2& position, unsigned int depth = 0, float rotation = 0.0f) : Renderable(depth), Movable(position, rotation) {}
};

View File

@@ -1,4 +1,4 @@
#include <Engine/Entity/Hud.h>
#include <Engine/types/entity/Hud.h>
namespace Game {
class DemoGameHud;

View File

@@ -1,5 +1,5 @@
#pragma once
#include <Engine/Level/Scene.h>
#include <Engine/types/scene/Scene.h>
class ControllableBox final : public Engine::Scene {
public:

View File

@@ -1,5 +1,6 @@
#pragma once
#include <Engine/Level/Scene.h>
#include <Engine/types/scene/Scene.h>
#include <JGL/types/RenderTarget.h>
class LoadingScreen final : public Engine::Scene {
protected:
@@ -11,6 +12,6 @@ public:
void Init() final;
void Update() final;
void Render(RenderTarget* render_target = nullptr) final;
void Render(JGL::RenderTarget* render_target = nullptr) final;
~LoadingScreen() final;
};