Threaded ticks

TODO make movable objects use velocity so rendering still looks smooth when things move
This commit is contained in:
2025-01-08 00:58:27 -05:00
parent 45ccc88794
commit fd98c308f5
14 changed files with 187 additions and 55 deletions

View File

@@ -10,8 +10,18 @@ namespace Globals {
inline Scene* CurrentScene = nullptr;
inline DemoGameWindow* Window = nullptr;
inline float DeltaTime() { return Window->GetDeltaTime(); }
inline void ChangeScene(Scene* scene) { if (CurrentScene != nullptr) CurrentScene->Unload(); CurrentScene = scene; if (CurrentScene != nullptr) CurrentScene->Init(); }
inline void ChangeScene(Scene* scene)
{
if (CurrentScene != nullptr)
CurrentScene->Unload();
CurrentScene = scene;
if (CurrentScene != nullptr) {
CurrentScene->Init();
CurrentScene->Run();
}
}
inline bool ChangeScene (const std::string& scene_name)
{
Scene* scene = nullptr;

9
include/Engine/Logger.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include <jlog/Logger.hpp>
namespace Engine::Logger {
inline jlog::GenericLogger Info {"Engine::Info", jlog::GlobalLogFile, Colors::Gray, Colors::Gray, Colors::Gray, Colors::Gray};
inline jlog::GenericLogger Fatal {"Engine::Fatal", jlog::GlobalLogFile, Colors::Reds::Crimson, Colors::Gray, Colors::Gray, Colors::Reds::Crimson, Colors::White};
inline jlog::GenericLogger Debug {"Engine::Debug", jlog::GlobalLogFile, Colors::Purples::Purple, Colors::Gray, Colors::Gray, Colors::Purples::Purple, Colors::White};
}

View File

@@ -0,0 +1,24 @@
#pragma once
#include <thread>
#include <utility>
#include <atomic>
#include <sstream>
namespace Engine {
class ConcurrentWorker;
}
class Engine::ConcurrentWorker {
private:
std::atomic<bool> work_complete{ false };
std::thread worker_thread;
public:
[[nodiscard]] bool Done();
public:
~ConcurrentWorker();
template <typename Function, typename... Args>
explicit ConcurrentWorker(Function&& func, Args&&... args) : worker_thread([this, func = std::forward<Function>(func),
...args = std::forward<Args>(args)]() mutable { func(std::forward<Args>(args) ...); work_complete = true; })
{ worker_thread.detach(); }
};

View File

@@ -5,6 +5,9 @@
#include <Engine/types/entity/Entity.h>
#include <Engine/types/entity/Hud.h>
#include <JGL/types/RenderTarget.h>
#include <Engine/types/ConcurrentWorker.h>
#include <rewindow/types/window.h>
#include <thread>
namespace Engine {
class Camera;
@@ -12,13 +15,23 @@ namespace Engine {
}
class Engine::Scene {
private:
bool destroying = false;
bool last_rendered_to_window = false;
float render_target_frame_delta = 0;
protected:
// 25ms.
u8 tick_rate = 40;
bool should_tick = true;
ConcurrentWorker* update_worker;
std::string name;
Camera* active_camera = nullptr;
Entity* entity_list = nullptr;
std::vector<InstancedTexture*> instanced_textures{};
std::vector<InstancedTexture*> instanced_alpha_masks{};
protected:
virtual void Update();
virtual void Render();
public:
[[nodiscard]] Entity* GetEntityList() { return entity_list; }
[[nodiscard]] size_t EntityCount() const;
@@ -26,16 +39,22 @@ public:
[[nodiscard]] Camera* GetActiveCamera() const;
[[nodiscard]] InstancedTexture* GetInstancedTexture(const InstancedSprite* user);
[[nodiscard]] InstancedTexture* GetInstancedAlphaMask(const InstancedSprite* user);
[[nodiscard]] bool ShouldTick() const;
[[nodiscard]] float TickDeltaTime() const;
[[nodiscard]] float FrameDeltaTime() const;
public:
void ShouldTick(bool state);
void SetActiveCamera(Camera* camera) { active_camera = camera; };
void DestroyInstancedTexture(const InstancedTexture* itx);
void DestroyInstancedAlphaMask(const InstancedTexture* alpha_mask);
virtual void Unload();
virtual void Init() {}
virtual void Update();
virtual void Render(JGL::RenderTarget* render_target = nullptr);
virtual void Init() {};
void Run();
void UpdateLoop();
void Render(JGL::RenderTarget* render_target);
void Render(ReWindow::RWindow* window);
public:
explicit Scene(const std::string& name) : 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()) {}
virtual ~Scene();
};

View File

@@ -11,7 +11,6 @@ public:
explicit LoadingScreen(const std::string& name) : Scene(name) {}
void Init() final;
void Update() final;
void Render(JGL::RenderTarget* render_target = nullptr) final;
void Render() final;
~LoadingScreen() final;
};