Performance optimization
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
#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(); }
|
||||
};
|
@@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <JGL/types/Texture.h>
|
||||
|
||||
namespace Engine {
|
||||
class InstancedTexture;
|
||||
class InstancedSprite;
|
||||
}
|
||||
|
||||
class Engine::InstancedTexture {
|
||||
protected:
|
||||
JGL::Texture* texture = nullptr;
|
||||
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();
|
||||
};
|
@@ -1,13 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <Engine/types/InstancedTexture.h>
|
||||
#include <Engine/types/entity/Entity.h>
|
||||
#include <Engine/types/entity/Hud.h>
|
||||
#include <JGL/types/RenderTarget.h>
|
||||
#include <Engine/types/ConcurrentWorker.h>
|
||||
#include <MultiThreading/Thread.h>
|
||||
#include <rewindow/types/window.h>
|
||||
#include <unordered_map>
|
||||
#include <thread>
|
||||
#include "Engine/types/entity/InstancedSprite.h"
|
||||
|
||||
namespace Engine {
|
||||
class Camera;
|
||||
@@ -24,12 +25,11 @@ protected:
|
||||
// 25ms.
|
||||
u8 tick_rate = 40;
|
||||
bool should_tick = true;
|
||||
ConcurrentWorker* update_worker;
|
||||
MultiThreading::Thread* update_worker;
|
||||
std::string name;
|
||||
Camera* active_camera = nullptr;
|
||||
Entity* entity_list = nullptr;
|
||||
std::vector<InstancedTexture*> instanced_textures{};
|
||||
std::vector<InstancedTexture*> instanced_alpha_masks{};
|
||||
std::unordered_map<size_t, JGL::Texture*> instanced_textures{};
|
||||
protected:
|
||||
virtual void Update();
|
||||
virtual void Render();
|
||||
@@ -38,8 +38,7 @@ public:
|
||||
[[nodiscard]] size_t EntityCount() const;
|
||||
[[nodiscard]] std::string GetName() const;
|
||||
[[nodiscard]] Camera* GetActiveCamera() const;
|
||||
[[nodiscard]] InstancedTexture* GetInstancedTexture(const InstancedSprite* user);
|
||||
[[nodiscard]] InstancedTexture* GetInstancedAlphaMask(const InstancedSprite* user);
|
||||
[[nodiscard]] JGL::Texture* GetInstancedTexture(const InstancedSprite* user);
|
||||
[[nodiscard]] bool ShouldTick() const;
|
||||
[[nodiscard]] float TickDeltaTime() const;
|
||||
[[nodiscard]] float FrameDeltaTime() const;
|
||||
@@ -49,8 +48,7 @@ public:
|
||||
public:
|
||||
void ShouldTick(bool state);
|
||||
void SetActiveCamera(Camera* camera) { active_camera = camera; };
|
||||
void DestroyInstancedTexture(const InstancedTexture* itx);
|
||||
void DestroyInstancedAlphaMask(const InstancedTexture* alpha_mask);
|
||||
void DestroyInstancedTexture(const InstancedSprite* itx);
|
||||
virtual void Unload();
|
||||
virtual void Init() {};
|
||||
void Run();
|
||||
|
@@ -8,12 +8,9 @@ namespace Engine {
|
||||
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() const override;
|
||||
[[nodiscard]] Texture* GetAlphaMask() const override;
|
||||
public:
|
||||
/** The Texture *must* be instanced. The alpha-mask *can* be instanced.
|
||||
* You can use SetAlphaMask() to change to a unique one.
|
||||
@@ -24,6 +21,5 @@ public:
|
||||
~InstancedSprite() override;
|
||||
InstancedSprite(const Vector2& position, unsigned int layer, const std::string& instanced_texture_filesystem_path,
|
||||
const Vector2& origin = {0, 0}, float face_angle = 0.0f, const Color4& base_color = Colors::White, Texture* unique_alpha_mask = nullptr, const std::string& instanced_alpha_mask_filesystem_path = "")
|
||||
: Sprite(position, origin, layer, face_angle, base_color, nullptr, unique_alpha_mask), texture_path(instanced_texture_filesystem_path),
|
||||
alpha_mask_path(instanced_alpha_mask_filesystem_path) {}
|
||||
: Sprite(position, origin, layer, face_angle, base_color, nullptr, unique_alpha_mask), texture_path(instanced_texture_filesystem_path) {}
|
||||
};
|
||||
|
Reference in New Issue
Block a user