handles
This commit is contained in:
@@ -2,10 +2,17 @@
|
||||
#include "../engine/Shared.h"
|
||||
#include "Global.h"
|
||||
#include "Log.h"
|
||||
#include "Puzzle.h"
|
||||
#include "rendering/Rendering.h"
|
||||
#include <bgfx/bgfx.h>
|
||||
#include <cstdint>
|
||||
|
||||
#define ENTITY_HANDLE(X) \
|
||||
struct X \
|
||||
{ \
|
||||
uint16_t Idx = UINT16_MAX; \
|
||||
};
|
||||
|
||||
namespace Game
|
||||
{
|
||||
struct EntityRenderData
|
||||
@@ -18,6 +25,7 @@ namespace Game
|
||||
void Render(const Model* models, const Material* materials);
|
||||
};
|
||||
|
||||
ENTITY_HANDLE(CubeHandle);
|
||||
struct Cube
|
||||
{
|
||||
int32_t TestX = -1;
|
||||
@@ -28,15 +36,21 @@ namespace Game
|
||||
void Update();
|
||||
};
|
||||
|
||||
ENTITY_HANDLE(TestEntityHandle);
|
||||
struct TestEntity
|
||||
{
|
||||
EntityRenderData EData;
|
||||
|
||||
void Setup();
|
||||
void Update();
|
||||
};
|
||||
|
||||
template <typename T, uint32_t C> class EntityManager
|
||||
ENTITY_HANDLE(PuzzleTileEntityHandle);
|
||||
struct PuzzleTileEntity
|
||||
{
|
||||
EntityRenderData EData;
|
||||
};
|
||||
|
||||
template <typename T, typename HandleT, uint32_t C> class EntityManager
|
||||
{
|
||||
public:
|
||||
uint16_t Count = 0;
|
||||
@@ -59,48 +73,41 @@ namespace Game
|
||||
return changed;
|
||||
}
|
||||
|
||||
T* New()
|
||||
HandleT New()
|
||||
{
|
||||
if (Data == nullptr)
|
||||
{
|
||||
Log("Accessed EntityManager before setup!");
|
||||
return nullptr;
|
||||
return {};
|
||||
}
|
||||
if (Count >= C)
|
||||
{
|
||||
Log("Too many entities!");
|
||||
return nullptr;
|
||||
return {};
|
||||
}
|
||||
Data[Count] = {};
|
||||
Data[Count].Setup();
|
||||
T* result = &Data[Count];
|
||||
HandleT H;
|
||||
H.Idx = Count;
|
||||
++Count;
|
||||
return result;
|
||||
return H;
|
||||
}
|
||||
|
||||
T* Get(uint16_t idx)
|
||||
T* Get(HandleT handle)
|
||||
{
|
||||
if (idx > Count)
|
||||
if (handle.Idx > Count)
|
||||
{
|
||||
Log("OOB Access!");
|
||||
return nullptr;
|
||||
}
|
||||
return &Data[idx];
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
for (uint32_t i = 0; i < Count; ++i)
|
||||
{
|
||||
Data[i].Update();
|
||||
}
|
||||
return &Data[handle.Idx];
|
||||
}
|
||||
|
||||
void Render(const Model* models, const Material* materials)
|
||||
{
|
||||
for (int32_t i = 0; i < Count; ++i)
|
||||
for (uint16_t i = 0; i < Count; ++i)
|
||||
{
|
||||
T* c = Get(i);
|
||||
T* c = Get({i});
|
||||
if (c) c->EData.Render(models, materials);
|
||||
}
|
||||
}
|
||||
@@ -109,8 +116,9 @@ namespace Game
|
||||
class Level
|
||||
{
|
||||
public:
|
||||
EntityManager<Cube, 1024> Cubes;
|
||||
EntityManager<TestEntity, 32> Tests;
|
||||
EntityManager<Cube, CubeHandle, 1024> Cubes;
|
||||
EntityManager<TestEntity, TestEntityHandle, 32> Tests;
|
||||
EntityManager<PuzzleTileEntity, PuzzleTileEntityHandle, 1024> PuzzleTiles;
|
||||
|
||||
public:
|
||||
void Setup(GameData& data);
|
||||
|
||||
Reference in New Issue
Block a user