Get rid of having to have Globals::CurrentScene everywhere.
This commit is contained in:
@@ -22,7 +22,7 @@ CPMAddPackage(
|
||||
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-26.zip
|
||||
)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "-O3 -Wall -Wextra")
|
||||
#set(CMAKE_CXX_FLAGS "-O3 -Wall -Wextra")
|
||||
|
||||
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
|
||||
file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp")
|
||||
|
@@ -55,6 +55,6 @@ public:
|
||||
void Render(JGL::RenderTarget* render_target);
|
||||
void Render(ReWindow::RWindow* window);
|
||||
public:
|
||||
explicit Scene(const std::string& name, const u8 tick_rate = 40) : tick_rate(tick_rate), name(name), entity_list(new Entity()) {}
|
||||
explicit Scene(const std::string& name, const u8 tick_rate = 40) : tick_rate(tick_rate), name(name), entity_list(new Entity(this)) {}
|
||||
virtual ~Scene();
|
||||
};
|
||||
|
@@ -4,13 +4,15 @@
|
||||
#include <chrono>
|
||||
|
||||
namespace Engine {
|
||||
class Scene;
|
||||
class Entity;
|
||||
}
|
||||
|
||||
class Engine::Entity {
|
||||
protected:
|
||||
private:
|
||||
// nullptr means no parent.
|
||||
Entity* parent = nullptr;
|
||||
Scene* scene = nullptr;
|
||||
|
||||
// Epoch micro-seconds.
|
||||
long creation_time = 0.0f;
|
||||
@@ -27,6 +29,8 @@ public:
|
||||
[[nodiscard]] bool HasChildren() const { return !children.empty(); }
|
||||
[[nodiscard]] bool HasParent() const { return parent; }
|
||||
[[nodiscard]] Entity* GetParent() const;
|
||||
[[nodiscard]] Scene* GetScene() const;
|
||||
[[nodiscard]] Scene* GetScene();
|
||||
|
||||
// Microseconds.
|
||||
[[nodiscard]] long GetCreationTime() const;
|
||||
@@ -35,5 +39,6 @@ public:
|
||||
virtual void Update() {}
|
||||
public:
|
||||
virtual ~Entity();
|
||||
explicit Entity(Scene* scene) : scene(scene), creation_time(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()) {}
|
||||
explicit Entity() : creation_time(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()) {}
|
||||
};
|
@@ -23,7 +23,7 @@ public:
|
||||
void SetSize(const Vector2& new_size) { size = new_size; }
|
||||
public:
|
||||
explicit Trigger(const Vector2& position, const Vector2& size, bool enabled = true) :
|
||||
size(size), Entity(), Living(enabled), BoxCollider(enabled), Movable(position), Renderable(0, false) {}
|
||||
size(size), Living(enabled), BoxCollider(enabled), Movable(position), Renderable(0, false) {}
|
||||
|
||||
~Trigger() override = default;
|
||||
};
|
||||
|
@@ -24,6 +24,10 @@ void Scene::Update() {
|
||||
|
||||
if (elapsed < target_tick_time)
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(target_tick_time - elapsed));
|
||||
|
||||
// TODO Research how we can fix desync issues that would come about by a tick occasionally taking longer than we expected.
|
||||
if (elapsed > target_tick_time)
|
||||
Logger::Info("This tick took " + std::to_string((elapsed - target_tick_time).count()) + "μs longer than the target tick time.");
|
||||
}
|
||||
|
||||
void Scene::UpdateLoop() {
|
||||
@@ -57,12 +61,17 @@ void Scene::Render() {
|
||||
}
|
||||
|
||||
void Scene::Render(JGL::RenderTarget* render_target) {
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
if (!render_target)
|
||||
Logger::Fatal("Rendering the current scene to a RenderTarget that is nullptr?");
|
||||
|
||||
J2D::Begin(render_target, true);
|
||||
Render();
|
||||
J2D::End();
|
||||
auto stop = std::chrono::steady_clock::now();
|
||||
|
||||
auto frame_time = std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
|
||||
render_target_frame_delta = frame_time / (1000.f * 1000.f);
|
||||
last_rendered_to_window = false;
|
||||
}
|
||||
|
||||
|
@@ -3,9 +3,9 @@
|
||||
|
||||
void Engine::Camera::ShouldRender(bool state) {
|
||||
if (state)
|
||||
Globals::CurrentScene->SetActiveCamera(this);
|
||||
GetScene()->SetActiveCamera(this);
|
||||
}
|
||||
|
||||
bool Engine::Camera::ShouldRender() const {
|
||||
return Globals::CurrentScene->GetActiveCamera() == this;
|
||||
return GetScene()->GetActiveCamera() == this;
|
||||
}
|
||||
|
@@ -1,7 +1,10 @@
|
||||
#include <J3ML/J3ML.hpp>
|
||||
#include <Engine/types/entity/Entity.h>
|
||||
#include <Engine/Logger.h>
|
||||
|
||||
void Engine::Entity::AppendChild(Entity* entity) {
|
||||
using namespace Engine;
|
||||
|
||||
void Entity::AppendChild(Entity* entity) {
|
||||
if (entity->parent)
|
||||
entity->parent->RemoveChild(entity);
|
||||
|
||||
@@ -9,12 +12,12 @@ void Engine::Entity::AppendChild(Entity* entity) {
|
||||
children.push_back(entity);
|
||||
}
|
||||
|
||||
Engine::Entity::~Entity() {
|
||||
Entity::~Entity() {
|
||||
for (auto* e : children)
|
||||
delete e;
|
||||
}
|
||||
|
||||
void Engine::Entity::UpdateChildren() {
|
||||
void Entity::UpdateChildren() {
|
||||
for (auto& e : children) {
|
||||
if (!e)
|
||||
continue;
|
||||
@@ -26,37 +29,37 @@ void Engine::Entity::UpdateChildren() {
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::Entity::DestroyChild(Entity* entity) {
|
||||
void 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) {
|
||||
void Entity::RemoveChild(Entity* entity) {
|
||||
auto it = std::find(children.begin(), children.end(), entity);
|
||||
if (it != children.end())
|
||||
children.erase(it);
|
||||
entity->parent = nullptr;
|
||||
}
|
||||
|
||||
long Engine::Entity::GetCreationTime() const {
|
||||
long Entity::GetCreationTime() const {
|
||||
return creation_time;
|
||||
}
|
||||
|
||||
long Engine::Entity::GetAge() const {
|
||||
long 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 {
|
||||
Entity* Entity::GetParent() const {
|
||||
return parent;
|
||||
}
|
||||
|
||||
std::vector<Engine::Entity*> Engine::Entity::GetChildren() const {
|
||||
std::vector<Entity*> Entity::GetChildren() const {
|
||||
return children;
|
||||
}
|
||||
|
||||
std::vector<Engine::Entity*> Engine::Entity::GetAllDescendents(const Engine::Entity* entity) {
|
||||
std::vector<Engine::Entity*> result = entity->GetChildren();
|
||||
std::vector<Entity*> Entity::GetAllDescendents(const Entity* entity) {
|
||||
std::vector<Entity*> result = entity->GetChildren();
|
||||
|
||||
if (result.empty())
|
||||
return result;
|
||||
@@ -67,3 +70,23 @@ std::vector<Engine::Entity*> Engine::Entity::GetAllDescendents(const Engine::Ent
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Scene* Entity::GetScene() const {
|
||||
if (scene)
|
||||
return scene;
|
||||
|
||||
const Entity* top = GetParent();
|
||||
while (top && top->GetParent())
|
||||
top = top->GetParent();
|
||||
|
||||
if (top->scene == nullptr)
|
||||
Logger::Fatal("Getting the pointer to the current scene but we've lost it somehow?");
|
||||
|
||||
return top->scene;
|
||||
}
|
||||
|
||||
Scene* Entity::GetScene() {
|
||||
const Entity* entity = this;
|
||||
scene = entity->GetScene();
|
||||
return scene;
|
||||
}
|
||||
|
@@ -3,31 +3,31 @@
|
||||
|
||||
using namespace Engine;
|
||||
Texture* InstancedSprite::GetTexture() {
|
||||
auto* itx = Globals::CurrentScene->GetInstancedTexture(this);
|
||||
auto* itx = GetScene()->GetInstancedTexture(this);
|
||||
if (itx)
|
||||
return itx->GetTexture();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Texture* InstancedSprite::GetAlphaMask() {
|
||||
auto* ita = Globals::CurrentScene->GetInstancedAlphaMask(this);
|
||||
auto* ita = GetScene()->GetInstancedAlphaMask(this);
|
||||
if (ita)
|
||||
return ita->GetTexture();
|
||||
return Sprite::GetAlphaMask();
|
||||
}
|
||||
|
||||
InstancedSprite::~InstancedSprite() {
|
||||
auto* itx = Globals::CurrentScene->GetInstancedTexture(this);
|
||||
auto* itx = GetScene()->GetInstancedTexture(this);
|
||||
if (itx) {
|
||||
itx->RemoveUser(this);
|
||||
if (itx->ReferenceCount() == 0)
|
||||
Globals::CurrentScene->DestroyInstancedTexture(itx);
|
||||
GetScene()->DestroyInstancedTexture(itx);
|
||||
}
|
||||
|
||||
auto* ita = Globals::CurrentScene->GetInstancedAlphaMask(this);
|
||||
auto* ita = GetScene()->GetInstancedAlphaMask(this);
|
||||
if (ita) {
|
||||
ita->RemoveUser(this);
|
||||
if (ita->ReferenceCount() == 0)
|
||||
Globals::CurrentScene->DestroyInstancedAlphaMask(ita);
|
||||
GetScene()->DestroyInstancedAlphaMask(ita);
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
#include <Engine/types/entity/mixins/Movable.h>
|
||||
#include <Engine/Globals.h>
|
||||
|
||||
// TODO Improve the design such that in mixins we wouldn't have to get Globals::CurrentScene
|
||||
// Maybe pass in the delta time as a parameter or something idk.
|
||||
using namespace J3ML;
|
||||
void Engine::Movable::MoveX(float speed) {
|
||||
position.x = position.x + (speed * Globals::CurrentScene->TickDeltaTime());
|
||||
|
@@ -7,8 +7,8 @@ void LoadingScreen::Init() {
|
||||
}
|
||||
|
||||
void LoadingScreen::Render() {
|
||||
angle += (elapsed * 4) * Globals::CurrentScene->FrameDeltaTime();
|
||||
elapsed += Globals::CurrentScene->FrameDeltaTime();
|
||||
angle += (elapsed * 4) * FrameDeltaTime();
|
||||
elapsed += FrameDeltaTime();
|
||||
|
||||
// Pretend we're actually loading something idk.
|
||||
if (elapsed >= 2.75)
|
||||
|
Reference in New Issue
Block a user