Heres some code to setup a ResourceManagement System for any type object that uses Integer IDs. One day I will rewrite it using templates - hehe. The system manages integer IDs using a LIFO stack, which reuses IDs more efficiently for resources created/destroyed often (ie: particles) during run-time. Need a new ID? Pop it off. Finished with an old one? Push it on. It also use STL Map to map keys (string labels) to the integer IDs. This is useful when you want to use string keys (ie: imagenames, filenames, etc) to reference IDs.
ResourceManagement.h
#pragma once
#ifndef RESOURCEMGMT_H
#define RESOURCEMGMT_H
#include <stack>
#include <list>
#include <map>
#include <string>
using namespace std;
class indexmanager{
/*!
* Description: Integer ID manager; Creates and manages Integer IDs with a LIFO Stack and List.
* Manage Integer IDs for Arrays Elements, DGDK Objects Reference, much more...
* Plug-in: Common Library
*/
private:
int indices;
std::stack<int> notusedindexstack; //not used
std::list<int> usedindexlist; //used
std::list<int>::iterator usedindexlistiterator;
public:
indexmanager(void){
}
indexmanager(int size){
/*!
* Description: [Constructor] Creates a Stack of IDs
*
* Plug-in: Common Library
* -# Input:
* -# int size - start number of IDs pushed to the stack from highest to lowest LIFO order.
*/
indices = size;
for(size+=1;size>0;size--){
notusedindexstack.push(size);
}
}
~indexmanager(void){}
int pop(void){
/*!
* Description: Pops ID from stack. If stack is empty it will increase the index return its value as the ID
*
* Plug-in: Common Library
* -# Input:
* -# none
*
* -# Output:
* -# none
*/
int ID;
if(notusedindexstack.size() == 1){
ID = ++indices;
} else {
ID = notusedindexstack.top();
notusedindexstack.pop();
}
usedindexlist.push_back(ID);
return(ID);
}
void push(int ID){
/*!
* Description: Pushes ID to stack for reuse.
*
* Plug-in: Common Library
* -# Input:
* -# int ID - pushed on to the stack.
*
* -# Output:
* -# none
*/
notusedindexstack.push(ID);
usedindexlist.remove(ID);
}
int total(void){
/*!
* Description: Returns index total aka number from IDs 1 to n
*
* Plug-in: Common Library
*
* -# Input:
* -# none
*
* -# Output:
* -# int count - value equal total number of indices
*/
int count = indices;
return(count);
}
int notused(void){
/*!
* Description: Returns count of IDs not used
*
* Plug-in: Common Library
*
* -# Input:
* -# none
*
* -# Output:
* -# int count - value equal number of indices in not used
*/
int count = notusedindexstack.size();
return(count);
}
int used(void){
/*!
* Description: Returns count of IDs used
*
* Plug-in: Common Library
*
* -# Input:
* -# none
*
* -# Output:
* -# int count - value equal number of indices used
*/
int count = usedindexlist.size();
return(count);
}
};
class resourcemanager{
/*!
* Description: Resource Manager; Manage Resource by Name Key, Index as Value
* Plug-in: Common Library
*/
private:
indexmanager *indexer;
std::map<std::string,int> namemap;
std::map<std::string,int>::iterator namemapiterator;
public:
resourcemanager(void){
}
resourcemanager(int size){
/*!
* Description: [Constructor] Creates a Stack of IDs
*
* Plug-in: Common Library
* -# Input:
* -# int size - start number of IDs pushed to the stack from highest to lowest LIFO order.
*/
indexer = new indexmanager(size);
}
~resourcemanager(void){
delete indexer;
}
int pop(void){
return(indexer->pop());
}
void push(int ID){
indexer->push(ID);
}
int add(std::string name){
/*!
* Description: Adds object name to namemap by ID
*
* Plug-in: Common Library
* -# Input:
* -# none
*
* -# Output:
* -# none
*/
if(!namemap[name]){
namemap[name] = indexer->pop();
}
return(namemap[name]);
}
void remove(std::string name){
/*!
* Description: Removes object name from namemap
*
* Plug-in: Common Library
* -# Input:
* -# none
*
* -# Output:
* -# none
*/
if(namemap[name]){
indexer->push(namemap[name]);
namemap.erase(name);
}
}
int get(std::string name){
/*!
* Description: Returns ID if exist, otherwise ERROR
*
* Plug-in: Common Library
* -# Input:
* -# none
*
* -# Output:
* -# none
*/
if(!namemap[name]){return(-1);}
return(namemap[name]);
}
void set(std::string name, int ID){
/*!
* Description: map name to ID manually, if none exist
*
* Plug-in: Common Library
* -# Input:
* -# none
*
* -# Output:
* -# none
*/
if(!namemap[name]){
namemap[name] = ID;
}
}
};
#endif
Using ResourceManager in your code.
//initialize resourcemanager
resourcemanager *dbImageResource = new resourcemanager(256);
resourcemanager *dbSpriteResource = new resourcemanager(256);
//new resource
int dbImageID = dbImageResource->pop();
dbLoadImage("image.bmp",dbImageID);
int dbSpriteID = dbSpriteResource->pop();
dbSprite(dbSpriteID,x,y,dbImageID);
//delete resource
dbDeleteSprite(dbSpriteID);
dbSpriteResource->push(dbSpriteID);
dbDeleteImage(dbImageID);
dbImageResource->push(dbImageID);
//free resourcemanager
delete dbSpriteResource;
delete dbImageResource;
You will find yourself using ResourceManagers for DGDKs Memblocks, Bitmaps, Cameras, Lights, Objects(3D), Sounds, Music, Vectors.