Change demo to showcase instancing.

This commit is contained in:
2025-01-03 19:36:42 -05:00
parent 6664925621
commit 283678e257
7 changed files with 55 additions and 24 deletions

View File

@@ -22,7 +22,7 @@ CPMAddPackage(
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-26.zip 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 HEADERS "include/*.h" "include/*.hpp")
file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp") file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp")

View File

@@ -21,8 +21,9 @@ public:
* If there is no instanced alpha mask and unique alpha mask is set * If there is no instanced alpha mask and unique alpha mask is set
* to nullptr, Then there is no alpha mask. - Redacted. * to nullptr, Then there is no alpha mask. - Redacted.
*/ */
~InstancedSprite() override;
InstancedSprite(const Vector2& position, unsigned int layer, const std::string& instanced_texture_filesystem_path, 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 = "") 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), texture_path(instanced_texture_filesystem_path),
Sprite(position, layer, face_angle, base_color, nullptr, unique_alpha_mask) {} alpha_mask_path(instanced_alpha_mask_filesystem_path) {}
}; };

View File

@@ -34,8 +34,11 @@ 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]] InstancedTexture* GetInstancedTexture(const InstancedSprite* user);
[[nodiscard]] Texture* GetInstancedAlphaMask(const InstancedSprite* user); [[nodiscard]] InstancedTexture* GetInstancedAlphaMask(const InstancedSprite* user);
void DestroyInstancedTexture(const InstancedTexture* itx);
void DestroyInstancedAlphaMask(const InstancedTexture* alpha_mask);
public: public:
void AppendEntity(Entity* entity); void AppendEntity(Entity* entity);
void AppendFixed(Fixed* fixed); void AppendFixed(Fixed* fixed);

View File

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

View File

@@ -3,11 +3,27 @@
using namespace Engine; using namespace Engine;
Texture* InstancedSprite::GetTexture() { Texture* InstancedSprite::GetTexture() {
return Globals::CurrentScene->GetInstancedTexture(this); return Globals::CurrentScene->GetInstancedTexture(this)->GetTexture();
} }
Texture* InstancedSprite::GetAlphaMask() { Texture* InstancedSprite::GetAlphaMask() {
if (Sprite::GetAlphaMask()) if (Sprite::GetAlphaMask())
return Sprite::GetAlphaMask(); return Sprite::GetAlphaMask();
return Globals::CurrentScene->GetInstancedAlphaMask(this); return Globals::CurrentScene->GetInstancedAlphaMask(this)->GetTexture();
} }
InstancedSprite::~InstancedSprite() {
auto* itx = Globals::CurrentScene->GetInstancedTexture(this);
if (itx) {
itx->RemoveUser(this);
if (itx->ReferenceCount() == 0)
Globals::CurrentScene->DestroyInstancedTexture(itx);
}
auto* ita = Globals::CurrentScene->GetInstancedAlphaMask(this);
if (ita) {
ita->RemoveUser(this);
if (ita->ReferenceCount() == 0)
Globals::CurrentScene->DestroyInstancedAlphaMask(ita);
}
}

View File

@@ -76,6 +76,12 @@ Scene::~Scene() {
for (auto* e : entity_list) for (auto* e : entity_list)
delete e; delete e;
for (auto* itx : instanced_textures)
delete itx;
for (auto* a : instanced_alpha_masks)
delete a;
delete heads_up_display; delete heads_up_display;
delete active_camera; delete active_camera;
} }
@@ -145,28 +151,40 @@ std::vector<Entity*> Scene::GetFlatEntityList(const std::vector<Entity*>& ent_li
return result; return result;
} }
Texture* Scene::GetInstancedTexture(const InstancedSprite* user) { InstancedTexture* Scene::GetInstancedTexture(const InstancedSprite* user) {
for (auto* itx : instanced_textures) for (auto* itx : instanced_textures)
if (itx->InUseBy(user)) if (itx->InUseBy(user))
return itx->GetTexture(); return itx;
auto* t = new Texture(user->GetTextureFilesystemPath()); auto* t = new Texture(user->GetTextureFilesystemPath());
auto* itx = new InstancedTexture(t, user); auto* itx = new InstancedTexture(t, user);
instanced_textures.push_back(itx); instanced_textures.push_back(itx);
return t; return itx;
} }
Texture* Scene::GetInstancedAlphaMask(const InstancedSprite* user) { InstancedTexture* Scene::GetInstancedAlphaMask(const InstancedSprite* user) {
for (auto* itx : instanced_alpha_masks) for (auto* itx : instanced_alpha_masks)
if (itx->InUseBy(user)) if (itx->InUseBy(user))
return itx->GetTexture(); return itx;
if (!user->GetAlphaMaskFilesystemPath().empty()) { if (!user->GetAlphaMaskFilesystemPath().empty()) {
auto *t = new Texture(user->GetTextureFilesystemPath()); auto *t = new Texture(user->GetTextureFilesystemPath());
auto *itx = new InstancedTexture(t, user); auto *itx = new InstancedTexture(t, user);
instanced_textures.push_back(itx); instanced_textures.push_back(itx);
return t; return itx;
} }
return nullptr; return nullptr;
} }
void Scene::DestroyInstancedTexture(const InstancedTexture* itx) {
auto it = std::find(instanced_textures.begin(), instanced_textures.end(), itx);
if (it != instanced_textures.end())
delete *it, instanced_textures.erase(it);
}
void Scene::DestroyInstancedAlphaMask(const Engine::InstancedTexture* alpha_mask) {
auto it = std::find(instanced_alpha_masks.begin(), instanced_alpha_masks.end(), alpha_mask);
if (it != instanced_alpha_masks.end())
delete *it, instanced_alpha_masks.erase(it);
}

View File

@@ -2,10 +2,6 @@
#include <Engine/Globals.h> #include <Engine/Globals.h>
#include <JGL/JGL.h> #include <JGL/JGL.h>
void Game::Box::Render() {
J2D::FillRect(Colors::Red, Vector2(position), {20, 20});
}
void Game::Box::Update() { void Game::Box::Update() {
if (Globals::Window->IsKeyDown(Keys::W)) if (Globals::Window->IsKeyDown(Keys::W))
MoveY(-500); MoveY(-500);