Thread: Generic Resource_Manager WIP with lots TODO

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Generic Resource_Manager WIP with lots TODO

    Just posting this for a peer review. Largely incomplete..

    Resource_Manager.h
    Code:
    #pragma warning(disable: 4786)
    
    #include <vector>
    #include "MS3D.H"
    #include <map>
    
    class Resource_Cache // The "containers" and their data structures
    {
    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:
    
    	bool Add_MS3DModel_To_Cache(std::string Filename); // loads a milkshape model to the cache
    
    };
    
    template < typename resource_t >
    class Manager					// container managers
    {
    
        std::map< std::string , resource_t* > Resources;
    
    public:
    
        resource_t & Load( const std::string & filename );
    
    
    	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	// Global Manager
    {
    	Resource_Cache GlobalResources;
        Manager<MS3DModel> Models;
    
    	std::string get_extension(std::string Filename);
    
    public:
    	void Add_Resource(std::string Filename)
    	{
    		
    		if (get_extension (Filename ) == "ms3d")
    		{
    			GlobalResources.Add_MS3DModel_To_Cache(Filename);
    		}
    		
    	}
    
    };
    Resource_Manager.cpp
    Code:
    #include "Resource_Manager.h"
    #include <windows.h>
    
    ////////////////////////////////////
    // Resource Cache //////////////////
    ////////////////////////////////////
    bool 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 );
    		return 0;									// If Model Didn't Load, Quit
    	}
    
    	 MS3DModels.push_back(Model); 
    
    	return 1;
    }
    ///////////////////////////////////////////////
    ///////////////////////////////////////////////
    ///////////////////////////////////////////////
    
    ///////////////////////////////////////////////
    // Manager Template ///////////////////////////
    ///////////////////////////////////////////////
    template < typename resource_t >
    resource_t & Manager<resource_t>::Load( 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;
        }
    }
    ///////////////////////////////////////////////
    ///////////////////////////////////////////////
    ///////////////////////////////////////////////
    
    
    ///////////////////////////////////////////////
    // 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;
    }
    ///////////////////////////////////////////////
    ///////////////////////////////////////////////
    ///////////////////////////////////////////////
    Thats about it so far, pretty simple...

    TODO list includes:

    Make it so the Add_Resource function uses the managers to compose a list of already loaded resources and their references.

    Add more functionality to the Load functions in Resource_Cache to return a reference to a resource, so that you can properly pair up a filename with its cooresponding resource


    Hmm, that is all I can think of...

    EXPLANATION:

    Well, Resource_Cache holds containers of information, right now currently only for MS3D Models. The Manager template manages the I/O of the Resource_Cache, the Resource_Manager class decides which manager template gets utilized (one for each type of resource reference)...
    Last edited by Shamino; 01-24-2006 at 04:02 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templated Generic Resource Manager, WIP..
    By Shamino in forum C++ Programming
    Replies: 13
    Last Post: 02-19-2006, 06:29 PM