little cleanup of templates

This commit is contained in:
aap 2020-12-18 13:58:59 +01:00
parent e9a5670348
commit a080fbfbd4
3 changed files with 51 additions and 52 deletions

View File

@ -1065,7 +1065,7 @@ CFileLoader::LoadMLOInstance(int id, const char *line)
&rot.x, &rot.y, &rot.z, &rot.x, &rot.y, &rot.z,
&angle); &angle);
float rad = Acos(angle) * 2.0f; float rad = Acos(angle) * 2.0f;
CInstance *inst = CModelInfo::GetMloInstanceStore().alloc(); CInstance *inst = CModelInfo::GetMloInstanceStore().Alloc();
minfo->lastInstance++; minfo->lastInstance++;
RwMatrix *matrix = RwMatrixCreate(); RwMatrix *matrix = RwMatrixCreate();
@ -1305,7 +1305,7 @@ CFileLoader::Load2dEffect(const char *line)
CTxdStore::SetCurrentTxd(CTxdStore::FindTxdSlot("particle")); CTxdStore::SetCurrentTxd(CTxdStore::FindTxdSlot("particle"));
mi = CModelInfo::GetModelInfo(id); mi = CModelInfo::GetModelInfo(id);
effect = CModelInfo::Get2dEffectStore().alloc(); effect = CModelInfo::Get2dEffectStore().Alloc();
mi->Add2dEffect(effect); mi->Add2dEffect(effect);
effect->pos = CVector(x, y, z); effect->pos = CVector(x, y, z);
effect->col = CRGBA(r, g, b, a); effect->col = CRGBA(r, g, b, a);

View File

@ -1,31 +1,31 @@
#pragma once #pragma once
template<typename T, int n> template<typename T, int32 n>
class CStore class CStore
{ {
public: public:
int allocPtr; int32 allocPtr;
T store[n]; T store[n];
T *alloc(void){ T *Alloc(void){
if(this->allocPtr >= n){ if(allocPtr >= n){
printf("Size of this thing:%d needs increasing\n", n); printf("Size of this thing:%d needs increasing\n", n);
assert(0); assert(0);
} }
return &this->store[this->allocPtr++]; return &store[allocPtr++];
} }
void clear(void){ void Clear(void){
this->allocPtr = 0; allocPtr = 0;
} }
int getIndex(T *item){ int32 GetIndex(T *item){
assert(item >= &this->store[0]); assert(item >= &store[0]);
assert(item < &this->store[n]); assert(item < &store[n]);
return item - this->store; return item - store;
} }
T *getItem(int index){ T *GetItem(int32 index){
assert(index >= 0); assert(index >= 0);
assert(index < n); assert(index < n);
return &this->store[index]; return &store[index];
} }
}; };
@ -40,12 +40,11 @@ class CPool
}; };
uint8 u; uint8 u;
} *m_flags; } *m_flags;
int m_size; int32 m_size;
int m_allocPtr; int32 m_allocPtr;
public: public:
CPool(int size){ CPool(int32 size){
// TODO: use new here
m_entries = (U*)new uint8[sizeof(U)*size]; m_entries = (U*)new uint8[sizeof(U)*size];
m_flags = (Flags*)new uint8[sizeof(Flags)*size]; m_flags = (Flags*)new uint8[sizeof(Flags)*size];
m_size = size; m_size = size;
@ -69,7 +68,7 @@ public:
m_allocPtr = 0; m_allocPtr = 0;
} }
} }
int GetSize(void) const { return m_size; } int32 GetSize(void) const { return m_size; }
T *New(void){ T *New(void){
bool wrapped = false; bool wrapped = false;
do do
@ -93,12 +92,12 @@ public:
m_flags[m_allocPtr].id++; m_flags[m_allocPtr].id++;
return (T*)&m_entries[m_allocPtr]; return (T*)&m_entries[m_allocPtr];
} }
T *New(int handle){ T *New(int32 handle){
T *entry = (T*)&m_entries[handle>>8]; T *entry = (T*)&m_entries[handle>>8];
SetNotFreeAt(handle); SetNotFreeAt(handle);
return entry; return entry;
} }
void SetNotFreeAt(int handle){ void SetNotFreeAt(int32 handle){
int idx = handle>>8; int idx = handle>>8;
m_flags[idx].free = 0; m_flags[idx].free = 0;
m_flags[idx].id = handle & 0x7F; m_flags[idx].id = handle & 0x7F;
@ -123,21 +122,21 @@ public:
return m_flags[handle>>8].u == (handle & 0xFF) ? return m_flags[handle>>8].u == (handle & 0xFF) ?
(T*)&m_entries[handle >> 8] : nil; (T*)&m_entries[handle >> 8] : nil;
} }
int GetIndex(T *entry){ int32 GetIndex(T *entry){
int i = GetJustIndex_NoFreeAssert(entry); int i = GetJustIndex_NoFreeAssert(entry);
return m_flags[i].u + (i<<8); return m_flags[i].u + (i<<8);
} }
int GetJustIndex(T *entry){ int32 GetJustIndex(T *entry){
int index = GetJustIndex_NoFreeAssert(entry); int index = GetJustIndex_NoFreeAssert(entry);
assert(!IsFreeSlot(index)); assert(!IsFreeSlot(index));
return index; return index;
} }
int GetJustIndex_NoFreeAssert(T* entry){ int32 GetJustIndex_NoFreeAssert(T* entry){
int index = ((U*)entry - m_entries); int index = ((U*)entry - m_entries);
assert((U*)entry == (U*)&m_entries[index]); // cast is unsafe - check required assert((U*)entry == (U*)&m_entries[index]); // cast is unsafe - check required
return index; return index;
} }
int GetNoOfUsedSpaces(void) const{ int32 GetNoOfUsedSpaces(void) const{
int i; int i;
int n = 0; int n = 0;
for(i = 0; i < m_size; i++) for(i = 0; i < m_size; i++)
@ -241,7 +240,7 @@ public:
link->Remove(); // remove from list link->Remove(); // remove from list
freeHead.Insert(link); // insert into free list freeHead.Insert(link); // insert into free list
} }
int Count(void){ int32 Count(void){
int n = 0; int n = 0;
CLink<T> *lnk; CLink<T> *lnk;
for(lnk = head.next; lnk != &tail; lnk = lnk->next) for(lnk = head.next; lnk != &tail; lnk = lnk->next)

View File

@ -26,15 +26,15 @@ CModelInfo::Initialise(void)
for(i = 0; i < MODELINFOSIZE; i++) for(i = 0; i < MODELINFOSIZE; i++)
ms_modelInfoPtrs[i] = nil; ms_modelInfoPtrs[i] = nil;
ms_2dEffectStore.clear(); ms_2dEffectStore.Clear();
ms_mloInstanceStore.clear(); ms_mloInstanceStore.Clear();
ms_xtraCompsModelStore.clear(); ms_xtraCompsModelStore.Clear();
ms_simpleModelStore.clear(); ms_simpleModelStore.Clear();
ms_timeModelStore.clear(); ms_timeModelStore.Clear();
ms_mloModelStore.clear(); ms_mloModelStore.Clear();
ms_clumpModelStore.clear(); ms_clumpModelStore.Clear();
ms_pedModelStore.clear(); ms_pedModelStore.Clear();
ms_vehicleModelStore.clear(); ms_vehicleModelStore.Clear();
m = AddSimpleModel(MI_CAR_DOOR); m = AddSimpleModel(MI_CAR_DOOR);
m->SetColModel(&CTempColModels::ms_colModelDoor1); m->SetColModel(&CTempColModels::ms_colModelDoor1);
@ -108,22 +108,22 @@ CModelInfo::ShutDown(void)
for(i = 0; i < ms_2dEffectStore.allocPtr; i++) for(i = 0; i < ms_2dEffectStore.allocPtr; i++)
ms_2dEffectStore.store[i].Shutdown(); ms_2dEffectStore.store[i].Shutdown();
ms_2dEffectStore.clear(); ms_2dEffectStore.Clear();
ms_simpleModelStore.clear(); ms_simpleModelStore.Clear();
ms_mloInstanceStore.clear(); ms_mloInstanceStore.Clear();
ms_mloModelStore.clear(); ms_mloModelStore.Clear();
ms_xtraCompsModelStore.clear(); ms_xtraCompsModelStore.Clear();
ms_timeModelStore.clear(); ms_timeModelStore.Clear();
ms_pedModelStore.clear(); ms_pedModelStore.Clear();
ms_clumpModelStore.clear(); ms_clumpModelStore.Clear();
ms_vehicleModelStore.clear(); ms_vehicleModelStore.Clear();
} }
CSimpleModelInfo* CSimpleModelInfo*
CModelInfo::AddSimpleModel(int id) CModelInfo::AddSimpleModel(int id)
{ {
CSimpleModelInfo *modelinfo; CSimpleModelInfo *modelinfo;
modelinfo = CModelInfo::ms_simpleModelStore.alloc(); modelinfo = CModelInfo::ms_simpleModelStore.Alloc();
CModelInfo::ms_modelInfoPtrs[id] = modelinfo; CModelInfo::ms_modelInfoPtrs[id] = modelinfo;
modelinfo->Init(); modelinfo->Init();
return modelinfo; return modelinfo;
@ -133,7 +133,7 @@ CMloModelInfo *
CModelInfo::AddMloModel(int id) CModelInfo::AddMloModel(int id)
{ {
CMloModelInfo *modelinfo; CMloModelInfo *modelinfo;
modelinfo = CModelInfo::ms_mloModelStore.alloc(); modelinfo = CModelInfo::ms_mloModelStore.Alloc();
CModelInfo::ms_modelInfoPtrs[id] = modelinfo; CModelInfo::ms_modelInfoPtrs[id] = modelinfo;
modelinfo->m_clump = nil; modelinfo->m_clump = nil;
modelinfo->firstInstance = 0; modelinfo->firstInstance = 0;
@ -145,7 +145,7 @@ CTimeModelInfo*
CModelInfo::AddTimeModel(int id) CModelInfo::AddTimeModel(int id)
{ {
CTimeModelInfo *modelinfo; CTimeModelInfo *modelinfo;
modelinfo = CModelInfo::ms_timeModelStore.alloc(); modelinfo = CModelInfo::ms_timeModelStore.Alloc();
CModelInfo::ms_modelInfoPtrs[id] = modelinfo; CModelInfo::ms_modelInfoPtrs[id] = modelinfo;
modelinfo->Init(); modelinfo->Init();
return modelinfo; return modelinfo;
@ -155,7 +155,7 @@ CClumpModelInfo*
CModelInfo::AddClumpModel(int id) CModelInfo::AddClumpModel(int id)
{ {
CClumpModelInfo *modelinfo; CClumpModelInfo *modelinfo;
modelinfo = CModelInfo::ms_clumpModelStore.alloc(); modelinfo = CModelInfo::ms_clumpModelStore.Alloc();
CModelInfo::ms_modelInfoPtrs[id] = modelinfo; CModelInfo::ms_modelInfoPtrs[id] = modelinfo;
modelinfo->m_clump = nil; modelinfo->m_clump = nil;
return modelinfo; return modelinfo;
@ -165,7 +165,7 @@ CPedModelInfo*
CModelInfo::AddPedModel(int id) CModelInfo::AddPedModel(int id)
{ {
CPedModelInfo *modelinfo; CPedModelInfo *modelinfo;
modelinfo = CModelInfo::ms_pedModelStore.alloc(); modelinfo = CModelInfo::ms_pedModelStore.Alloc();
CModelInfo::ms_modelInfoPtrs[id] = modelinfo; CModelInfo::ms_modelInfoPtrs[id] = modelinfo;
modelinfo->m_clump = nil; modelinfo->m_clump = nil;
return modelinfo; return modelinfo;
@ -175,7 +175,7 @@ CVehicleModelInfo*
CModelInfo::AddVehicleModel(int id) CModelInfo::AddVehicleModel(int id)
{ {
CVehicleModelInfo *modelinfo; CVehicleModelInfo *modelinfo;
modelinfo = CModelInfo::ms_vehicleModelStore.alloc(); modelinfo = CModelInfo::ms_vehicleModelStore.Alloc();
CModelInfo::ms_modelInfoPtrs[id] = modelinfo; CModelInfo::ms_modelInfoPtrs[id] = modelinfo;
modelinfo->m_clump = nil; modelinfo->m_clump = nil;
modelinfo->m_vehicleType = -1; modelinfo->m_vehicleType = -1;
@ -245,7 +245,7 @@ CModelInfo::ConstructMloClumps()
void void
CModelInfo::ReInit2dEffects() CModelInfo::ReInit2dEffects()
{ {
ms_2dEffectStore.clear(); ms_2dEffectStore.Clear();
for (int i = 0; i < MODELINFOSIZE; i++) { for (int i = 0; i < MODELINFOSIZE; i++) {
if (ms_modelInfoPtrs[i]) if (ms_modelInfoPtrs[i])