Thread: Switch from .ms3d to the ASCII format?

  1. #16
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Here is what I've got so far...

    So I have my MS3D Model class setup with the data structures pertaining to an MS3D Model in it...

    In my Resource_Cache I have another data structure set up to contain a pointer to an MS3D Model, a position, and an orientation.. Also in the Resource_Cache class is a function that creates a new ModelInfo struct, fills it out, and pushes it back into a container of MS3D Models.

    Resource_Manager.h
    Code:
    #include <vector>
    #include "MS3D.H"
    
    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:
    
    	bool Add_MS3DModel_To_Cache(const char *filename); // loads a milkshape model to the cache
    };
    Resource_Manager.cpp
    Code:
    #include "Resource_Manager.h"
    #include <windows.h>
    
    bool Resource_Cache::Add_MS3DModel_To_Cache(const char *filename)
    {
    
    	/* 
    	Add conditional to check if filename is loaded already
    	If filename is loaded pushback another model info
    	with the same model, perhaps keep a list of loaded
    	filenames and their corresponding model pointers
    	*/
    
    	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;
    }
    As you can see, it is just the basis of what I need.

    I need to add conditionals and check if I've already loaded the model filename given..

    I suppose I can store the filenames loaded and their corresponding pointers in a linked list or another vector, and check if filenames are identical, then return that filenames saved pointer (also in the linked list of MS3D Models)...

    Thats the only way I can think of to keep from loading the same filename twice.. And the only thing I can think of to do if we do happen to come across that case.

    I noticed that your AddMS3DModeltoRenderer function (you gave me the source a while ago) Doesn't load the files, it just takes a pointer to an already loaded model..

    Now, do you A: Load all the models regardless of what is going down in your program?

    Or B: have a resource manager somewhere managing what gets loaded and what doesn't?

    Also remember, we'll dealing with a resource manager here, not a renderer.. It is understandable that your GLRenderer class does not have file repeat protection...
    Last edited by Shamino; 01-23-2006 at 12:43 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #17

    Join Date
    May 2005
    Posts
    1,042
    That is just it shamino, I do not rely on an external resource manager...it may be a good idea to do so, but I keep mine even simpler than that. It probably would be a good idea to have some sort of a 'resource manager' that handles all of the ms3d files, I do something similar with my texture manager, but I don't do it for ms3d models.

    Here's how, in my engine, a ms3d model comes to exist:
    I develop a class, something like a 'monster.' The monster will hold a pointer to a ms3d model, and when the monster is created it will load its appropriate model. One of the reasons that each monster holds a ms3d model is that when each model has a copy, instead of there just being one instance of the model, is that it allows me to do things such as deform the model (alter its triangle vertices based on damage it receives) without destroying the model that the other monsters may use.

    But the way you just outlined things is fine.
    I'm not immature, I'm refined in the opposite direction.

  3. #18
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I've decided to use a map to keep a list of Resource filenames that I've loaded, also a reference accompanying that resource..


    I'll post some sample code..

    Code:
    template < typename resource_t >
    class manager {
        std::map< std::string , boost::shared_ptr< resource_t > > resources;
    public:
        resource_t & load( const std::string & filename ) {
            std::map< std::string , boost::shared_ptr< resource_t > >::iterator entry = resources.find( filename );
            if ( entry != resources.end() ) { //found
                return * entry->second;
            } else {
                boost::shared_ptr< resource_t > resource( new resource_t( filename ) );
                resources.insert( std::make_pair( filename , resource ) );
                return * resource;
            }
        }
    };
    
    class texture {
    public:
        textue( const std::string & filename ) { ... }
        ...
    };
    
    class model {
    public:
        model( const std::string & filename ) { ... }
        ...
    };
    
    class sound {
    public:
        sound( const std::string & filename ) { ... }
        ...
    };
    
    int main( void ) {
        manager< texture > textures;
        manager< model > models;
        manager< sound > sounds;
    
        texture & logo = textures.load( "logo.jpeg" );
        model & cat = textures.load( "cat.3ds" );
        sound & batman_theme = textures.load( "sounds/batman.mp3" );
    
        render( logo );
        render( cat );
        loop( sound );
    }
    We can use a template to avoid alot of repeating code, because a map will only handle one type of reference at a time, so therefore we can use templates to make it easy to create a map that will keep track of a certain resource-reference..

    You could consider it a good thing, not having a single container of references to look for, because if you know you're loading a sound you don't want to search through a container that has textures, models, and sounds, it would be slow!

    I'll get to implementing this soon.. Will let you know how it goes .
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #19
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    More updatage

    Resource_Manager.h
    Code:
    #include <vector>
    #include "MS3D.H"
    #include <map>
    class Resource_Cache
    {
    
    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(const char *filename); // loads a milkshape model to the cache
    
    };
    
    template < typename resource_t >
    class Resource_Manager 
    {
    
        std::map< std::string , resource_t* > Resources;
    
    public:
    
        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;
            }
        }
    
    	virtual ~Resource_Manager( void )
    	{
    		std::map< std::string ,  resource_t * >::iterator destroyer, end;
    		for ( destroyer = resources.begin() , end = resources.end() ; destroyer != end ; ++destroyer ) 
    		{
    			delete destroyer->first;
    			delete destroyer->second;
    		}
    	}	
    
    };
    Resource_Manager.cpp
    Code:
    #include "Resource_Manager.h"
    #include <windows.h>
    
    bool Resource_Cache::Add_MS3DModel_To_Cache(const char *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;
    }
    TODO:

    Add file extension recognition to choose right loader function.

    Make it so the actual load functions return a reference to the manager so that he can make a pair out of the resource filename and the reference.

    Make it so no matter what another reference gets pushed back into the list of things to draw if a load request is sent.. We can't just do nothing if there is a file already loaded!

    That is all I can think of right now! Working!
    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. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. switch() inside while()
    By Bleech in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 02:34 AM
  4. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  5. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM