Hi, I''m doing a ModelManager class which should make sure I''m only loading my models once etc. Right now I have a STL list which traverses each model, looks if it has been loaded before, and takes appropriate action. But I''m kinda lost in the STL woods and I''m unsure how to go on about deleting objects from the list etc. My loading/insertion function is probably wrong too. I have searched the forums and Google but haven''t been able to find anything that can help me (or that I could understand), so no I''m turning to you.
This is my CModelManager and CModelHolder classes, in a .h and a .cpp file:
#ifndef CMODELMANAGER_H_
#define CMODELMANAGER_H_
#include <list>
#include "CModel.h"
using namespace std;
class CModelHolder
{
public:
CModelHolder(void);
CModelHolder(const CModelHolder ©);
~CModelHolder(void);
char *m_strFileName;
CMD2Model m_Model;
int count; // Holds the count the model has been assigned
};
class CModelManager
{
public:
CModelManager(void);
~CModelManager(void);
CMD2Model *GetModel(char *modelFilename, char *modelTexture);
bool ReleaseModel(char *modelFilename);
list<CModelHolder> m_ModelList;
list<CModelHolder>::iterator m_Iterator;
};
extern CModelManager g_ModelManager;
#endif
#include "CModelManager.h"
// Global CModelManager
CModelManager g_ModelManager;
CModelHolder::CModelHolder(void)
{
m_strFileName = NULL;
count = 0;
}
CModelHolder::CModelHolder(const CModelHolder ©)
{
m_strFileName = copy.m_strFileName;
count = copy.count;
m_Model = copy.m_Model;
}
CModelHolder::~CModelHolder(void)
{
}
CModelManager::CModelManager(void)
{
m_ModelList.clear();
}
CModelManager::~CModelManager(void)
{
}
// Compares 2 model filenames and if they match it returns the model, else it loads in a new one
CMD2Model *CModelManager::GetModel(char *modelFilename, char *modelTexture)
{
for(m_Iterator = m_ModelList.begin(); m_Iterator != m_ModelList.end(); m_Iterator++)
{
if(!strcmp(m_Iterator->m_strFileName, modelFilename))
{
m_Iterator->count++;
return &(m_Iterator->m_Model);
}
}
CModelHolder *newModel = new CModelHolder;
if(newModel->m_Model.LoadModelAndSkin(modelFilename, modelTexture))
{
newModel->count = 1;
newModel->m_strFileName = modelFilename;
m_ModelList.push_back(*newModel);
return &(newModel->m_Model);
}
else
return NULL;
}
bool CModelManager::ReleaseModel(char *modelFilename)
{
for(m_Iterator = m_ModelList.begin(); m_Iterator != m_ModelList.end(); m_Iterator++)
{
if(!strcmp(m_Iterator->m_strFileName, modelFilename))
{
m_Iterator->count--;
if(m_Iterator->count <= 0)
{
m_Iterator->m_Model.UnloadModel();
delete &(m_Iterator);
m_ModelList.erase(m_Iterator++);
}
return true;
}
}
return false;
}
What I want this to do is:
Store a list of objects which consists of a filename (the path to the model), the count of how many times the model has been assigned, and the model itself. Then in the GetModel() function I search the list to see if I have already loaded a model with the filename given as the function parameter. If I have, I return a pointer to that model. If I haven''t, I load in the model, assign the filename etc, and pushes the object on the list. The ReleaseModel() function should work in the same way but opposite. When the count is 0 (i.e when no object is using it anymore) it unloads the model, and deletes the memory held by the modelHolder, and removes the object from the list.
The problem is that I''m getting a "Debug Assertion Failed" error when running the program in Debug mode, but not in Release mode. The error comes up when I''m closing the application (i.e when I''m unloading the model). The model loads and renders fine.
Thanks alot for helping, and remember that I''m a total newbie about STL so be kind