Back to index

supertuxkart  0.5+dfsg1
Public Member Functions | Private Member Functions | Private Attributes
MaterialManager Class Reference

#include <material_manager.hpp>

Collaboration diagram for MaterialManager:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 MaterialManager ()
void loadMaterial ()
void reInit ()
int addEntity (Material *m)
MaterialgetMaterial (ssgLeaf *lf)
MaterialgetMaterial (const std::string &t, bool is_full_path=false)
void addSharedMaterial (const std::string &filename)
bool pushTempMaterial (const std::string &filename)
void popTempMaterial ()

Private Member Functions

char * parseFileName (char **str)
int parseMaterial (FILE *fd)
void parseMaterialFile (const std::string &filename)

Private Attributes

int m_shared_material_index
std::vector< Material * > m_materials

Detailed Description

Definition at line 29 of file material_manager.hpp.


Constructor & Destructor Documentation

Definition at line 36 of file material_manager.cpp.

{
    /* Create list - and default material zero */

    m_materials.reserve(256);
    // We can't call init/loadMaterial here, since the global variable
    // material_manager has not yet been initialised, and
    // material_manager is used in the Material constructor.
    // Therefore, the code for loading the material had to
    // be moved into a separate function.
}

Member Function Documentation

Definition at line 49 of file material_manager.cpp.

{
    m_materials.push_back(m);
    return (int)m_materials.size()-1;
}
void MaterialManager::addSharedMaterial ( const std::string &  filename)

Definition at line 86 of file material_manager.cpp.

{
    // Use temp material for reading, but then set the shared
    // material index later, so that these materials are not popped
    if(filename=="")
    {
        char msg[MAX_ERROR_MESSAGE_LENGTH];
        snprintf(msg, sizeof(msg), "FATAL: File '%s' not found\n", filename.c_str());
        throw std::runtime_error(msg);
    }
    if(!pushTempMaterial(filename))
    {
        char msg[MAX_ERROR_MESSAGE_LENGTH];
        snprintf(msg, sizeof(msg), "FATAL: Parsing error in '%s'\n", filename.c_str());
        throw std::runtime_error(msg);
    }
    m_shared_material_index = (int)m_materials.size();
}   // addSharedMaterial

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 192 of file material_manager.cpp.

{
    return m_materials[l -> getExternalPropertyIndex ()] ;
}   // getMaterial

Here is the caller graph for this function:

Material * MaterialManager::getMaterial ( const std::string &  t,
bool  is_full_path = false 
)

Definition at line 198 of file material_manager.cpp.

{
    if(fname=="")
    {
        // This happens while reading the stk_config file, which contains
        // kart_properties information (but no icon file): since at this 
        // stage loadMaterial() hasn't been called, an exception can be
        // triggered here (as it happened with visual c++), when
        // m_materials[0] is accessed.
        if(m_materials.size()>=1) return m_materials[0];
        return NULL;
    }

    std::string basename=StringUtils::basename(fname);

    // Search backward so that temporary (track) textures are found first
    for(int i = (int)m_materials.size()-1; i>=0; i-- )
    {
        if(m_materials[i]->getTexFname()==basename) return m_materials[i];
    }

    // Add the new material
    Material* m=new Material(fname,"", (int)m_materials.size(), is_full_path);
    m_materials.push_back(m);

    return m ;
}   // getMaterial

Here is the call graph for this function:

Definition at line 68 of file material_manager.cpp.

{
    // Create the default/empty material.
    m_materials.push_back(new Material((int)m_materials.size()));

    // Use temp material for reading, but then set the shared
    // material index later, so that these materials are not popped
    const std::string fname     = "materials.dat";
    std::string       full_name = file_manager->getTextureFile(fname);
    addSharedMaterial(full_name);

    ssgSetAppStateCallback(getAppState);
    fuzzy_gst        = getMaterial("fuzzy.rgb")->getState();
    // Save index of shared textures
    m_shared_material_index = (int)m_materials.size();
}   // MaterialManager

Here is the call graph for this function:

Here is the caller graph for this function:

char * MaterialManager::parseFileName ( char **  str) [private]

Definition at line 130 of file material_manager.cpp.

{
    char *p = *str ;

    /* Skip leading spaces */
    while ( *p <= ' ' && *p != '\0' ) p++ ;

    /* Skip blank lines and comments */
    if ( *p == '#' || *p == '\0' )
        return NULL ;

    if ( *p != '"' )
    {
        fprintf(stderr, "ERROR: Material file entries must start with '\"'\n"
                "ERROR: Offending line is '%s'\n", *str);
        return NULL ;
    }

    /* Filename? */
    char *f = ++p ;
    while ( *p != '"' && *p != '\0' ) p++ ;

    if ( *p != '"' )
    {
        fprintf(stderr,
                "ERROR: Unterminated string constant '%s' in materials file.\n", *str ) ;
        return NULL ;
    }

    *p = '\0' ;
    *str = ++p ;

    return f ;
}   // parseFilename

Here is the caller graph for this function:

int MaterialManager::parseMaterial ( FILE *  fd) [private]

Definition at line 166 of file material_manager.cpp.

{
    char str [ 1024 ] ;

    while ( ! feof ( fd ) )
    {
        char *s = str ;

        if ( fgets ( s, 1024, fd ) == NULL )
            return false ;

        s [ strlen(s) - 1 ] = '\0' ;

        char *f = parseFileName ( & s ) ;

        if ( f != NULL )
        {
            m_materials.push_back(new Material (f, s, (int)m_materials.size() ));
            return true ;
        }
    }

    return false ;
}   // parseMaterial

Here is the call graph for this function:

Here is the caller graph for this function:

void MaterialManager::parseMaterialFile ( const std::string &  filename) [private]

Definition at line 120 of file material_manager.cpp.

{
    for(int i=(int)m_materials.size()-1; i>=this->m_shared_material_index; i--)
    {
        delete m_materials[i];
        m_materials.pop_back();
    }   // for i
}   // popTempMaterial

Here is the caller graph for this function:

bool MaterialManager::pushTempMaterial ( const std::string &  filename)

Definition at line 106 of file material_manager.cpp.

{
    FILE *fd = fopen(filename.c_str(), "r" );

    if ( fd == NULL ) return false;

    while ( parseMaterial ( fd ) )
        /* Read file */ ;

    fclose ( fd ) ;
    return true;
}   // pushTempMaterial

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 56 of file material_manager.cpp.

{
    for(std::vector<Material*>::const_iterator i=m_materials.begin();
        i!=m_materials.end(); i++)
    {
        delete *i;
    }
    m_materials.clear();
    loadMaterial();
}   // reInit

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

std::vector<Material*> MaterialManager::m_materials [private]

Definition at line 38 of file material_manager.hpp.

Definition at line 36 of file material_manager.hpp.


The documentation for this class was generated from the following files: