diff options
Diffstat (limited to 'src/Texture.cpp')
-rw-r--r-- | src/Texture.cpp | 76 |
1 files changed, 53 insertions, 23 deletions
diff --git a/src/Texture.cpp b/src/Texture.cpp index 01a19aa..b17d204 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -1,34 +1,64 @@ #include <Texture.h> +#include <string.h> + +struct texture_t { + char *name; + GLuint tex; +} __attribute__ ((packed)); + +struct texture_t *LoadedTexture[256]; +unsigned int LoadedTextureCounter = 0; namespace Texture{ GLuint loadTexture(const char *fileName){ - SDL_Surface *image = IMG_Load(fileName); + SDL_Surface *image; + GLuint object = 0; + unsigned int i; - if(!image)return 0; - DEBUG_printf("Loaded image file: %s\n", fileName); - unsigned object = 0; //creates a new unsigned variable for the texture + for(unsigned int i=0;i<LoadedTextureCounter;i++){ + if(!strcmp(LoadedTexture[i]->name,fileName)){ +#ifdef DEBUG + DEBUG_printf("Reusing loaded texture for %s\n",fileName); +#endif // DEBUG + return LoadedTexture[i]->tex; + } + } - glGenTextures(1, &object); //turns "object" into a texture - glBindTexture(GL_TEXTURE_2D, object); //binds "object" to the top of the stack - glPixelStoref(GL_UNPACK_ALIGNMENT,1 ); + if(!(image = IMG_Load(fileName))) + return 0; +#ifdef DEBUG + DEBUG_printf("Loaded image file: %s\n", fileName); +#endif // DEBUG + + glGenTextures(1,&object); // Turns "object" into a texture + glBindTexture(GL_TEXTURE_2D,object); // Binds "object" to the top of the stack + glPixelStoref(GL_UNPACK_ALIGNMENT,1); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //sets the "min" filter - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //the the "max" filter of the stack + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Sets the "min" filter + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The the "max" filter of the stack - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //Wrap the texture to the matrix - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //Wrap the texutre to the matrix + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // Wrap the texture to the matrix + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // - glTexImage2D( GL_TEXTURE_2D, - 0, - GL_RGBA, - image->w, - image->h, - 0, - GL_RGBA, - GL_UNSIGNED_BYTE, - image->pixels); //sets the texture to the image file loaded above + glTexImage2D(GL_TEXTURE_2D, // Sets the texture to the image file loaded above + 0, + GL_RGBA, + image->w, + image->h, + 0, + GL_RGBA, + GL_UNSIGNED_BYTE, + image->pixels + ); - SDL_FreeSurface(image); //Free surface + SDL_FreeSurface(image); // Free the surface + + LoadedTexture[LoadedTextureCounter] = (struct texture_t *)malloc(sizeof(struct texture_t)); + LoadedTexture[LoadedTextureCounter]->name = (char *)malloc(strlen(fileName)); + LoadedTexture[LoadedTextureCounter]->tex = object; + strcpy(LoadedTexture[LoadedTextureCounter]->name,fileName); + LoadedTextureCounter++; + return object; } } @@ -58,5 +88,5 @@ void Texturec::bindPrev(){ } void Texturec::walk(){ - -}
\ No newline at end of file + // hey +} |