And finally, Proof of concept, completed version... currently only works with .ms3d files..
Resource_Manager.h
Code:
#pragma warning(disable: 4786)
#include <vector>
#include "MS3D.H"
#include <map>
class Resource_Cache
{
friend class Resource_Manager;
public:
struct MS3DModelRenderData
{
float Position[3]; // will be changed to vectors eventually
float Orientation[3];
MS3DModel *pModel;
};
std::vector<MS3DModelRenderData*> MS3DModels; // holds pointers to all MS3D Models
private:
MS3DModel * Add_MS3DModel_To_Cache(std::string Filename); // loads a milkshape model to the cache
};
template < typename resource_t >
class Manager
{
std::map< std::string , resource_t* > Resources;
public:
bool Check_Existance( const std::string & filename );
resource_t * Return_Resource_Reference( const std::string & filename );
void Add_To_List( const std::string & Filename, resource_t* );
virtual ~Manager( void )
{
std::map< std::string , resource_t * >::iterator destroyer, end;
for ( destroyer = Resources.begin() , end = Resources.end() ; destroyer != end ; ++destroyer )
{
delete destroyer->second;
}
}
};
class Resource_Manager
{
Resource_Cache GlobalResources;
Manager<MS3DModel> Models;
std::string get_extension(std::string Filename);
public:
void Add_Resource(std::string Filename);
};
Resource_Manager.cpp
Code:
#include "Resource_Manager.h"
#include <windows.h>
#include <iostream>
#include "assert.h"
////////////////////////////////////
// Resource Cache //////////////////
////////////////////////////////////
MS3DModel * Resource_Cache::Add_MS3DModel_To_Cache(std::string Filename)
{
MS3DModelRenderData *Model;
Model = new MS3DModelRenderData;
Model->pModel = new MS3DModel;
if ( Model->pModel->Load_MS3D_Model( Filename ) == false )
{
MessageBox( NULL, "Couldn't load the model data.", "Error", MB_OK | MB_ICONERROR );
assert(Model->pModel != NULL);
return 0;
}
MS3DModels.push_back(Model);
return Model->pModel;
}
///////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
// Manager Template ///////////////////////////
///////////////////////////////////////////////
template < typename resource_t >
resource_t * Manager<resource_t>::Return_Resource_Reference(const std::string & filename )
{
std::map< std::string , resource_t* >::iterator entry = Resources.find( filename );
if ( entry != Resources.end() ) // if the entry is found
{
return entry->second; // return the reference to the resource
}
/*
else
{
resource_t* resource( new resource_t( filename ) );
// first we gotta load the resource, then we can make a pair
// and return the reference
Resources.insert( std::make_pair( filename , resource ) );
return * resource;
}
*/
else return false;
}
template < typename resource_t >
bool Manager<resource_t>::Check_Existance( const std::string & filename )
{
std::map< std::string , resource_t* >::iterator entry = Resources.find( filename );
if ( entry != Resources.end() ) // if the entry is found
{
return true;
}
else return false;
}
template < typename resource_t >
void Manager<resource_t>::Add_To_List(const std::string & Filename, resource_t * Resource)
{
Resources.insert(std::make_pair( Filename , Resource) );
}
///////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
// Resource Manager ///////////////////////////
///////////////////////////////////////////////
std::string Resource_Manager::get_extension(std::string Filename)
{
std::string::size_type last_dot_index = Filename.rfind( '.' );
std::string extension = Filename.substr( last_dot_index + 1 ); //+1 to not include the dot...
return extension;
}
void Resource_Manager::Add_Resource(std::string Filename)
{
if (get_extension (Filename ) == "ms3d")
{
if ( Models.Check_Existance (Filename) == true )
{
std::cout << "Resource is on the list!" << std::endl;
Resource_Cache::MS3DModelRenderData * RenderObject = new Resource_Cache::MS3DModelRenderData;
RenderObject->pModel = Models.Return_Resource_Reference ( Filename );
GlobalResources.MS3DModels.push_back(RenderObject);
}
else
{
std::cout << "Resource not on list!" << std::endl;
Models.Add_To_List(Filename, GlobalResources.Add_MS3DModel_To_Cache(Filename));
}
}
}
///////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
And the test.cpp
Code:
#include "Resource_Manager.h"
Resource_Manager Global_Manager;
int main()
{
Global_Manager.Add_Resource("AttackShip.ms3d");
Global_Manager.Add_Resource("AttackShip.ms3d");
return 0;
}
And finally, the output.. Very simple, telling us it wasn't in the list before, therefore it loads the file, then the second line of output is telling us the file is now on the list with a proper resource, and then just returns the cooresponding resource pointer...
Code:
Resource not on list!
Resource is on the list!
Press any key to continue