Back to index

extremetuxracer  0.5beta
Classes | Defines | Functions
textures.h File Reference
#include "etracer.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  texture_node_t
class  Texture

Defines

#define TEX_SCALE   6

Functions

void init_textures ()
bool load_and_bind_texture (const char *binding, const char *filename)
bool load_texture (const char *texname, const char *filename, int repeatable)
texture_node_tget_texture (const char *texname)
bool bind_texture (const char *binding, const char *texname)
bool get_texture_binding (const char *binding, GLuint *texid)
bool unbind_texture (const char *binding)
void get_current_texture_dimensions (int *width, int *height)
bool flush_textures (void)
void register_texture_callbacks (Tcl_Interp *ip)

Class Documentation

struct texture_node_t

Definition at line 29 of file textures.h.

Class Members
int ref_count
int repeatable
GLuint texture_id
class Texture

Definition at line 35 of file textures.h.


Define Documentation

#define TEX_SCALE   6

Definition at line 27 of file textures.h.


Function Documentation

bool bind_texture ( const char *  binding,
const char *  texname 
)

Definition at line 194 of file textures.cpp.

{
    
       texture_node_t *tex;
       
       print_debug(DEBUG_TEXTURE, "Binding %s to texture name: %s", 
              binding, texname);
       
       tex = get_texture( texname );
       
       if (tex == NULL){
              print_warning( IMPORTANT_WARNING, 
                     "Attempt to bind to Texture unloaded texture: `%s'\n", texname );
              return false;
       }

       std::map<std::string,texture_node_t>::iterator oldtex;
    if ( (oldtex = bindingTable.find(binding))!=bindingTable.end() ){
              oldtex->second.ref_count--;
              bindingTable.erase(oldtex);
    }

       bindingTable[binding] = *tex;
       tex->ref_count++;

    return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool flush_textures ( void  )

Definition at line 239 of file textures.cpp.

{
       bool result;
       std::map<std::string,texture_node_t>::iterator mapit;
       for(mapit=textureTable.begin();mapit!=textureTable.end(); mapit++){
              if (mapit->second.ref_count == 0) {
                     result = del_texture( (*mapit).first.c_str() );
                     check_assertion(result, "Attempt to flush non-existant texture");     
                  // RK: reset pointer to start of table since mapit got deleted
            mapit = textureTable.begin();
              }
       }
       return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void get_current_texture_dimensions ( int *  width,
int *  height 
)

Definition at line 233 of file textures.cpp.

{
    glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (GLint*) width );
    glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (GLint*) height );
}
texture_node_t* get_texture ( const char *  texname)

Definition at line 163 of file textures.cpp.

{
       std::map<std::string,texture_node_t>::iterator it;
       
       if ( (it = textureTable.find(texname))!=textureTable.end() ){
              return &(it->second);
       }else{
              return NULL;
       }
}

Here is the caller graph for this function:

bool get_texture_binding ( const char *  binding,
GLuint *  texid 
)

Definition at line 35 of file textures.cpp.

{
    std::map<std::string,texture_node_t>::iterator texnode;
    if ( (texnode = bindingTable.find(binding))!=bindingTable.end() ){
              *texid = texnode->second.texture_id;
              return true;
    }
    return false;  
}

Here is the caller graph for this function:

void init_textures ( )

Definition at line 51 of file textures.cpp.

{
} 

Here is the caller graph for this function:

bool load_and_bind_texture ( const char *  binding,
const char *  filename 
)

Definition at line 45 of file textures.cpp.

{
    return (bool) ( load_texture( binding, filename, 1 ) &&
                    bind_texture( binding, binding ) );
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool load_texture ( const char *  texname,
const char *  filename,
int  repeatable 
)

Definition at line 75 of file textures.cpp.

{
       pp::Image *texImage = pp::Image::readFile(filename);
       
    texture_node_t *tex;
    int max_texture_size;


    print_debug(DEBUG_TEXTURE, "Loading texture %s from file: %s", 
              texname, filename);

    if ( texImage == NULL ) {
       print_warning( IMPORTANT_WARNING, 
                     "couldn't load image %s", filename );
       return false;
    }

       std::map<std::string,texture_node_t>::iterator it;
    if ( (it = textureTable.find(texname))!=textureTable.end() ){
              tex = &it->second;
              print_debug(DEBUG_TEXTURE, "Found texture %s with id: %d", 
                  texname, it->second.texture_id);
        glDeleteTextures( 1, &(tex->texture_id) );
       }else{
              tex = &textureTable[texname];
              tex->ref_count = 0;
       }
    
    tex->repeatable = repeatable;
    glGenTextures( 1, &(tex->texture_id) );
    glBindTexture( GL_TEXTURE_2D, tex->texture_id );

    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);


    if ( repeatable ) {
              glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
              glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    } else {
              glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
              glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
    }
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
                     get_min_filter() );
              
    /* Check if we need to scale image */
    glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*) &max_texture_size );
    if ( texImage->width > max_texture_size ||
        texImage->height > max_texture_size ) 
    {
       char *newdata = (char*)malloc( texImage->depth *
                                   max_texture_size *
                                   max_texture_size );

       check_assertion( newdata != NULL, "out of memory" );

       print_debug( DEBUG_TEXTURE, "Texture `%s' too large -- scaling to "
                   "maximum allowed size",
                   filename );

       /* In the case of large- or small-aspect ratio textures, this
           could end up using *more* space... oh well. */
       gluScaleImage( texImage->depth == 3 ? GL_RGB : GL_RGBA,
                     texImage->width, texImage->height, 
                     GL_UNSIGNED_BYTE,
                     texImage->data,
                     max_texture_size, max_texture_size, 
                     GL_UNSIGNED_BYTE,
                     newdata );

       free( texImage->data );
       texImage->data = (unsigned char*) newdata;
       texImage->width = max_texture_size;
       texImage->height = max_texture_size;
    }

    gluBuild2DMipmaps( GL_TEXTURE_2D, texImage->depth, texImage->width,
                     texImage->height, texImage->depth == 3 ? GL_RGB : GL_RGBA, 
                     GL_UNSIGNED_BYTE, texImage->data );

       delete texImage;

    return true;
} 

Here is the call graph for this function:

Here is the caller graph for this function:

void register_texture_callbacks ( Tcl_Interp *  ip)

Definition at line 301 of file textures.cpp.

{
    Tcl_CreateCommand (ip, "tux_load_texture",   load_texture_cb,   0,0);
    Tcl_CreateCommand (ip, "tux_bind_texture",   bind_texture_cb,   0,0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool unbind_texture ( const char *  binding)

Definition at line 222 of file textures.cpp.

{
       std::map<std::string,texture_node_t>::iterator tex;
       if ( (tex = bindingTable.find(binding))!=bindingTable.end() ){
              tex->second.ref_count--;
              bindingTable.erase(tex);
              return true;
       }
       return false;
}

Here is the caller graph for this function: