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 #pragma once
#include <rewindow/types/window.h>
#include <rewindow/logger/logger.h>
namespace Engine { namespace Engine {
class DemoGameWindow; class DemoGameWindow;
@@ -11,7 +12,16 @@ public:
public: public:
void OnRefresh(float elapsed) override; void OnRefresh(float elapsed) override;
public: 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 #pragma once
#include <Engine/Level/Scene.h> #include <Engine/types/scene/Scene.h>
#include <Engine/GameWindow.h> #include <Engine/GameWindow.h>
namespace Globals { 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 #pragma once
#include <Engine/Entity/Renderable.h> #include <Engine/types/entity/mixins/Renderable.h>
namespace Engine { namespace Engine {
class Hud; class Hud;
@@ -7,5 +7,5 @@ namespace Engine {
class Engine::Hud : public Engine::Renderable { class Engine::Hud : public Engine::Renderable {
public: 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 #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 <JGL/types/Texture.h>
#include <J3ML/Geometry/AABB2D.hpp> #include <J3ML/Geometry/AABB2D.hpp>
@@ -8,23 +11,24 @@ namespace Engine {
class Sprite; class Sprite;
} }
class Engine::Sprite : public Renderable { using JGL::Texture;
protected: class Engine::Sprite : public Entity, public Renderable, public Movable {
private:
Texture* texture = nullptr; Texture* texture = nullptr;
// Positive alpha mask.
Texture* alpha_mask = nullptr; Texture* alpha_mask = nullptr;
protected:
Vector2 scale = {1, 1}; Vector2 scale = {1, 1};
// Local space, Where the face_angle rotation should be preformed about. // Local space, Where the face_angle rotation should be preformed about.
Vector2 origin = {0, 0}; Vector2 origin = {0, 0};
Color4 base_color = Colors::White; Color4 base_color = Colors::White;
public: protected:
[[nodiscard]] Texture* GetTexture(); [[nodiscard]] virtual Texture* GetTexture();
// World space. [[nodiscard]] virtual Texture* GetAlphaMask();
[[nodiscard]] virtual AABB2D GetBounds(); void SetAlphaMask(Texture* new_alpha_mask);
public: public:
void Render() override; void Render() override;
public: public:
~Sprite() override; ~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, 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(pos, depth,face_angle), texture(texture), alpha_mask(alpha_mask), base_color(base_color) {} 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 #pragma once
#include "J3ML/LinearAlgebra/Vector2i.hpp" #include <J3ML/LinearAlgebra/Vector2i.hpp>
#include "J3ML/Geometry/AABB2D.hpp" #include <J3ML/Geometry/AABB2D.hpp>
#include "JGL/types/Texture.h" #include <JGL/types/Texture.h>
using J3ML::LinearAlgebra::Vector2i; using J3ML::LinearAlgebra::Vector2i;
using JGL::Texture; using JGL::Texture;

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,6 @@
#include <Engine/Globals.h> #include <Engine/Globals.h>
#include <rewindow/logger/logger.h> #include <Game/scene/Loading.h>
#include <Game/Scene/ControllableBox.h> #include <Game/scene/ControllableBox.h>
#include <Game/Scene/Loading.h>
using namespace JGL; using namespace JGL;
@@ -14,16 +13,7 @@ void CreateScenes() {
}; };
int main() { int main() {
ReWindow::Logger::Error.EnableConsole(false);
ReWindow::Logger::Warning.EnableConsole(false);
ReWindow::Logger::Debug.EnableConsole(false);
Globals::Window = new Engine::DemoGameWindow("Demo Game", 1024, 896); Globals::Window = new Engine::DemoGameWindow("Demo Game", 1024, 896);
Globals::Window->SetRenderer(RenderingAPI::OPENGL);
Globals::Window->Open();
Globals::Window->InitGL();
Globals::Window->SetResizable(false);
Globals::Window->SetVsyncEnabled(false);
CreateScenes(); CreateScenes();
Globals::ChangeScene("LoadingScreen"); Globals::ChangeScene("LoadingScreen");

View File

@@ -1,5 +1,5 @@
#include "Engine/Entity/Animation.h" #include <Engine/types/entity/Animation.h>
#include "jlog/Logger.hpp" #include <jlog/Logger.hpp>
float Animation::GetMsBetweenFrames() const { float Animation::GetMsBetweenFrames() const {
return ms_between_frames; return ms_between_frames;

View File

@@ -1,109 +0,0 @@
#include <J3ML/J3ML.hpp>
#include <Engine/Entity/Entity.h>
#include <Engine/Globals.h>
using namespace J3ML;
void Engine::Entity::MoveX(float speed) {
position.x = position.x + (speed * Globals::Window->GetDeltaTime());
}
void Engine::Entity::MoveY(float speed) {
position.y = position.y + (speed * Globals::DeltaTime());
}
void Engine::Entity::Move(float angle_rad, float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(angle_rad);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(angle_rad);
}
void Engine::Entity::MoveForward(float speed) {
Move(face_angle, speed);
}
void Engine::Entity::MoveBackward(float speed) {
speed = -speed;
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
}
void Engine::Entity::MoveLeft(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * -Math::Sin(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
}
void Engine::Entity::MoveRight(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * -Math::Cos(face_angle);
}
void Engine::Entity::SetRotation(float new_face_angle) {
face_angle = new_face_angle;
if (face_angle < 0)
face_angle = fmod(face_angle + Math::Pi * 2, Math::Pi * 2);
else if (face_angle >= 2 * M_PI)
face_angle = fmod(face_angle, Math::Pi * 2);
}
void Engine::Entity::Rotate(float speed) {
SetRotation(face_angle + speed * Globals::DeltaTime());
}
float Engine::Entity::GetRotation() const {
return face_angle;
}
Vector2 Engine::Entity::GetPosition() const {
return position;
}
bool Engine::Entity::AppendChild(Entity* entity) {
bool success = false;
if (!Globals::CurrentScene->EntityListContains(entity)) {
entity->parent = this;
children.push_back(entity);
success = true;
}
return success;
}
Engine::Entity::~Entity() {
for (auto* e : children)
delete e;
}
void Engine::Entity::UpdateChildren() {
for (auto& e : children) {
e->Update();
if (!e->children.empty())
e->UpdateChildren();
}
}
void Engine::Entity::DestroyChild(Entity* entity) {
auto it = std::find(children.begin(), children.end(), entity);
if (it != children.end())
delete *it, children.erase(it);
}
void Engine::Entity::RemoveChild(Entity* entity) {
auto it = std::find(children.begin(), children.end(), entity);
if (it != children.end())
children.erase(it);
}
long Engine::Entity::GetCreationTime() const {
return creation_time;
}
long Engine::Entity::GetAge() const {
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() - creation_time;
}
Engine::Entity* Engine::Entity::GetParent() const {
return parent;
}
std::vector<Engine::Entity*> Engine::Entity::GetChildren() const {
return children;
}

View File

@@ -1,21 +0,0 @@
#include <Engine/Entity/Sprite.h>
Engine::Sprite::~Sprite() {
delete texture;
delete alpha_mask;
}
void Engine::Sprite::Render() {
if (texture && !alpha_mask)
J2D::DrawSprite(texture, position, face_angle, origin, scale, base_color);
if (texture && alpha_mask)
J2D::DrawSprite(texture, alpha_mask, position, face_angle, origin, scale, base_color);
}
AABB2D Engine::Sprite::GetBounds() {
return { position, texture->GetDimensions() + position };
}
Texture* Engine::Sprite::GetTexture() {
return texture;
}

View File

@@ -1,6 +1,5 @@
#include <Engine/GameWindow.h> #include <Engine/GameWindow.h>
#include <Engine/Globals.h> #include <Engine/Globals.h>
#include <Engine/Entity/Camera.h>
#include <JGL/JGL.h> #include <JGL/JGL.h>

View File

@@ -0,0 +1,28 @@
#include <Engine/types/InstancedTexture.h>
#include <Engine/types/entity/InstancedSprite.h>
using namespace Engine;
void InstancedTexture::AddUser(const InstancedSprite* user) {
// Prevent double references.
if (!InUseBy(user))
users.push_back(user);
}
void InstancedTexture::RemoveUser(const InstancedSprite* user) {
auto it = std::find(users.begin(), users.end(), user);
if (it != users.end())
users.erase(it);
}
bool InstancedTexture::InUseBy(const InstancedSprite* rhs) {
for (const auto* u : users)
if (u == rhs)
return true;
return false;
}
InstancedTexture::~InstancedTexture() {
delete texture;
for (const auto* is: users)
delete is;
}

View File

@@ -0,0 +1,53 @@
#include <J3ML/J3ML.hpp>
#include <Engine/types/entity/Entity.h>
#include <Engine/Globals.h>
bool Engine::Entity::AppendChild(Entity* entity) {
if (!Globals::CurrentScene->EntityListContains(entity)) {
entity->parent = this;
children.push_back(entity);
return true;
}
return false;
}
Engine::Entity::~Entity() {
for (auto* e : children)
delete e;
}
void Engine::Entity::UpdateChildren() {
for (auto& e : children) {
e->Update();
if (!e->children.empty())
e->UpdateChildren();
}
}
void Engine::Entity::DestroyChild(Entity* entity) {
auto it = std::find(children.begin(), children.end(), entity);
if (it != children.end())
delete *it, children.erase(it);
}
void Engine::Entity::RemoveChild(Entity* entity) {
auto it = std::find(children.begin(), children.end(), entity);
if (it != children.end())
children.erase(it);
}
long Engine::Entity::GetCreationTime() const {
return creation_time;
}
long Engine::Entity::GetAge() const {
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() - creation_time;
}
Engine::Entity* Engine::Entity::GetParent() const {
return parent;
}
std::vector<Engine::Entity*> Engine::Entity::GetChildren() const {
return children;
}

View File

@@ -0,0 +1,13 @@
#include <Engine/types/entity/InstancedSprite.h>
#include <Engine/Globals.h>
using namespace Engine;
Texture* InstancedSprite::GetTexture() {
return Globals::CurrentScene->GetInstancedTexture(this);
}
Texture* InstancedSprite::GetAlphaMask() {
if (Sprite::GetAlphaMask())
return Sprite::GetAlphaMask();
return Globals::CurrentScene->GetInstancedAlphaMask(this);
}

View File

@@ -0,0 +1,57 @@
#include "Engine/types/entity/mixins/Movable.h"
#include "Engine/Globals.h"
using namespace J3ML;
void Engine::Movable::MoveX(float speed) {
position.x = position.x + (speed * Globals::Window->GetDeltaTime());
}
void Engine::Movable::MoveY(float speed) {
position.y = position.y + (speed * Globals::DeltaTime());
}
void Engine::Movable::Move(float angle_rad, float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(angle_rad);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(angle_rad);
}
void Engine::Movable::MoveForward(float speed) {
Move(face_angle, speed);
}
void Engine::Movable::MoveBackward(float speed) {
speed = -speed;
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
}
void Engine::Movable::MoveLeft(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * -Math::Sin(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
}
void Engine::Movable::MoveRight(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * -Math::Cos(face_angle);
}
void Engine::Movable::SetRotation(float new_face_angle) {
face_angle = new_face_angle;
if (face_angle < 0)
face_angle = fmod(face_angle + Math::Pi * 2, Math::Pi * 2);
else if (face_angle >= 2 * M_PI)
face_angle = fmod(face_angle, Math::Pi * 2);
}
void Engine::Movable::Rotate(float speed) {
SetRotation(face_angle + speed * Globals::DeltaTime());
}
float Engine::Movable::GetRotation() const {
return face_angle;
}
Vector2 Engine::Movable::GetPosition() const {
return position;
}

View File

@@ -0,0 +1,29 @@
#include <Engine/types/entity/Sprite.h>
#include <JGL/JGL.h>
Engine::Sprite::~Sprite() {
delete texture;
delete alpha_mask;
}
void Engine::Sprite::Render() {
auto* t = GetTexture();
auto* a = GetAlphaMask();
if (t && !a)
return J2D::DrawSprite(t, position, face_angle, origin, scale, base_color);
if (t && a)
return J2D::DrawSprite(t, a, position, face_angle, origin, scale, base_color);
}
Texture* Engine::Sprite::GetTexture() {
return texture;
}
Texture* Engine::Sprite::GetAlphaMask() {
return alpha_mask;
}
void Engine::Sprite::SetAlphaMask(Texture* new_alpha_mask) {
alpha_mask = new_alpha_mask;
}

View File

@@ -0,0 +1,14 @@
#include "Engine/types/entity/mixins/BoxCollider.h"
using namespace Engine;
bool BoxCollider::BoxCollides(BoxCollider* rhs) {
if (GetBounds().Intersects(rhs->GetBounds()))
return true;
if (GetBounds().Contains(rhs->GetBounds()))
return true;
return false;
}
bool BoxCollider::BoxCollides(const Vector2& rhs) {
return GetBounds().Contains(rhs);
}

View File

@@ -1,4 +1,4 @@
#include <Engine/Level/Fixed.h> #include "Engine/types/scene/Fixed.h"
bool Engine::Fixed::Collidable() const { bool Engine::Fixed::Collidable() const {
return collidable; return collidable;

View File

@@ -1,32 +1,35 @@
#include <Engine/Level/Scene.h> #include <Engine/types/scene/Scene.h>
#include <Engine/Entity/Camera.h> #include <Engine/types/entity/Camera.h>
#include <Engine/types/entity/InstancedSprite.h>
#include <JGL/JGL.h>
#include <algorithm> #include <algorithm>
bool Engine::Scene::EntityListContains(const Entity* entity) const { using namespace Engine;
bool Scene::EntityListContains(const Entity* entity) const {
auto flat = GetFlatEntityList(entity_list); auto flat = GetFlatEntityList(entity_list);
for (auto* e : flat) for (auto* e : flat)
if (e == entity) if (e == entity)
return true; return true;
return false; return false;
} }
bool Engine::Scene::FixedListContains(const Fixed* fixed) const { bool Scene::FixedListContains(const Fixed* fixed) const {
for (auto* f : fixed_list) for (auto* f : fixed_list)
if (f == fixed) if (f == fixed)
return true; return true;
return false; return false;
} }
size_t Engine::Scene::FixedCount() const { size_t Scene::FixedCount() const {
return fixed_list.size(); return fixed_list.size();
} }
size_t Engine::Scene::EntityCount() const { size_t Scene::EntityCount() const {
return GetFlatEntityList(entity_list).size(); return GetFlatEntityList(entity_list).size();
} }
void Engine::Scene::Update() { void Scene::Update() {
if (active_camera) if (active_camera)
active_camera->Update(); active_camera->Update();
@@ -35,7 +38,7 @@ void Engine::Scene::Update() {
e->Update(); e->Update();
} }
void Engine::Scene::Render(RenderTarget* render_target) { void Scene::Render(RenderTarget* render_target) {
// The Camera and the HUD are ignored here because they're special cases. // The Camera and the HUD are ignored here because they're special cases.
std::vector<Renderable*> display_list{}; std::vector<Renderable*> display_list{};
for (auto* e : GetFlatEntityList(entity_list)) for (auto* e : GetFlatEntityList(entity_list))
@@ -45,7 +48,7 @@ void Engine::Scene::Render(RenderTarget* render_target) {
display_list.push_back(r); display_list.push_back(r);
std::sort(display_list.begin(), display_list.end(), [](Renderable* a, Renderable* b) { std::sort(display_list.begin(), display_list.end(), [](Renderable* a, Renderable* b) {
return a->GetDepth() > b->GetDepth(); return a->GetLayer() > b->GetLayer();
}); });
J2D::Begin( render_target, true); J2D::Begin( render_target, true);
@@ -66,7 +69,7 @@ void Engine::Scene::Render(RenderTarget* render_target) {
J2D::End(); J2D::End();
} }
Engine::Scene::~Scene() { Scene::~Scene() {
for (auto* f : fixed_list) for (auto* f : fixed_list)
delete f; delete f;
@@ -77,7 +80,7 @@ Engine::Scene::~Scene() {
delete active_camera; delete active_camera;
} }
void Engine::Scene::Unload() { void Scene::Unload() {
for (auto* f : fixed_list) for (auto* f : fixed_list)
delete f; delete f;
@@ -87,50 +90,50 @@ void Engine::Scene::Unload() {
delete heads_up_display; delete heads_up_display;
} }
void Engine::Scene::AppendEntity(Entity* entity) { void Scene::AppendEntity(Entity* entity) {
if (!EntityListContains(entity)) if (!EntityListContains(entity))
entity_list.push_back(entity); entity_list.push_back(entity);
} }
void Engine::Scene::AppendFixed(Fixed* fixed) { void Scene::AppendFixed(Fixed* fixed) {
if (!FixedListContains(fixed)) if (!FixedListContains(fixed))
fixed_list.push_back(fixed); fixed_list.push_back(fixed);
} }
void Engine::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 Engine::Scene::DestroyFixed(Fixed* fixed) { void Scene::DestroyFixed(Fixed* fixed) {
auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed); auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed);
if (it != fixed_list.end()) if (it != fixed_list.end())
delete *it, fixed_list.erase(it); delete *it, fixed_list.erase(it);
} }
void Engine::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 Engine::Scene::RemoveFixed(Fixed* fixed) { void Scene::RemoveFixed(Fixed* fixed) {
auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed); auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed);
if (it != fixed_list.end()) if (it != fixed_list.end())
fixed_list.erase(it); fixed_list.erase(it);
} }
std::string Engine::Scene::GetName() const { std::string Scene::GetName() const {
return name; return name;
} }
Engine::Camera* Engine::Scene::GetActiveCamera() const { Camera* Scene::GetActiveCamera() const {
return active_camera; return active_camera;
} }
std::vector<Engine::Entity*> Engine::Scene::GetFlatEntityList(const std::vector<Entity*>& ent_list) const { std::vector<Entity*> Scene::GetFlatEntityList(const std::vector<Entity*>& ent_list) const {
std::vector<Entity*> result{}; std::vector<Entity*> result{};
for (auto* e : ent_list) { for (auto* e : ent_list) {
@@ -142,3 +145,28 @@ std::vector<Engine::Entity*> Engine::Scene::GetFlatEntityList(const std::vector<
return result; return result;
} }
Texture* Scene::GetInstancedTexture(const InstancedSprite* user) {
for (auto* itx : instanced_textures)
if (itx->InUseBy(user))
return itx->GetTexture();
auto* t = new Texture(user->GetTextureFilesystemPath());
auto* itx = new InstancedTexture(t, user);
instanced_textures.push_back(itx);
return t;
}
Texture* Scene::GetInstancedAlphaMask(const InstancedSprite* user) {
for (auto* itx : instanced_alpha_masks)
if (itx->InUseBy(user))
return itx->GetTexture();
if (!user->GetAlphaMaskFilesystemPath().empty()) {
auto *t = new Texture(user->GetTextureFilesystemPath());
auto *itx = new InstancedTexture(t, user);
instanced_textures.push_back(itx);
return t;
}
return nullptr;
}

View File

@@ -1,4 +1,6 @@
#include <Game/Entity/Box.h> #include <Game/entity/Box.h>
#include <Engine/Globals.h>
#include <JGL/JGL.h>
void Game::Box::Render() { void Game::Box::Render() {
J2D::FillRect(Colors::Red, Vector2(position), {20, 20}); J2D::FillRect(Colors::Red, Vector2(position), {20, 20});
@@ -13,4 +15,4 @@ void Game::Box::Update() {
MoveX(-500); MoveX(-500);
if (Globals::Window->IsKeyDown(Keys::D)) if (Globals::Window->IsKeyDown(Keys::D))
MoveX(500); MoveX(500);
} }

View File

@@ -1,5 +1,6 @@
#include <Game/Entity/DemoGameHud.h> #include <Game/entity/DemoGameHud.h>
#include <Engine/Globals.h> #include <Engine/Globals.h>
#include <JGL/JGL.h>
void Game::DemoGameHud::Render() { void Game::DemoGameHud::Render() {
J2D::DrawString(Colors::Whites::Ivory, "Framerate: " + std::to_string((int) Globals::Window->GetRefreshRate()), 0, 0, 1, 16); J2D::DrawString(Colors::Whites::Ivory, "Framerate: " + std::to_string((int) Globals::Window->GetRefreshRate()), 0, 0, 1, 16);

View File

@@ -1,6 +1,6 @@
#include <Game/Scene/ControllableBox.h> #include <Game/scene/ControllableBox.h>
#include <Game/Entity/DemoGameHud.h> #include <Game/entity/DemoGameHud.h>
#include <Game/Entity/Box.h> #include <Game/entity/Box.h>
void ControllableBox::Init() { void ControllableBox::Init() {
auto* hud = new Game::DemoGameHud(); auto* hud = new Game::DemoGameHud();

View File

@@ -1,5 +1,6 @@
#include <Game/Scene/Loading.h> #include <Game/scene/Loading.h>
#include <Engine/Globals.h> #include <Engine/Globals.h>
#include <JGL/JGL.h>
void LoadingScreen::Init() { void LoadingScreen::Init() {
RedactedSoftware = new JGL::Texture("assets/sprites/Re3D.png"); RedactedSoftware = new JGL::Texture("assets/sprites/Re3D.png");