diff options
Diffstat (limited to 'src/Texture.cpp')
-rw-r--r-- | src/Texture.cpp | 79 |
1 files changed, 45 insertions, 34 deletions
diff --git a/src/Texture.cpp b/src/Texture.cpp index 1487dcd..1aaadf6 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -1,45 +1,62 @@ #include <Texture.h> #include <string.h> +/** + * A structure for keeping track of loaded textures. + */ + struct texture_t { - char *name; - GLuint tex; - dim2 dim; -} __attribute__ ((packed)); + std::string name; /**< The file path of the texture. */ + GLuint tex; /**< The GLuint for the loaded texture. */ + dim2 dim; /**< The dimensions of the texture. */ +}; -struct index_t{ +struct index_t { Color color; int indexx; int indexy; }; -struct texture_t *LoadedTexture[256]; -unsigned int LoadedTextureCounter = 0; +/** + * A vector of all loaded textures. + * + * Should a texture be asked to be loaded twice, loadTexture() can reference + * this array and reuse GLuint's to save memory. + */ + +static std::vector<struct texture_t> LoadedTexture; namespace Texture{ Color pixels[8][4]; - GLuint loadTexture(const char *fileName){ + + GLuint loadTexture(std::string fileName){ SDL_Surface *image; GLuint object = 0; - for(unsigned int i=0;i<LoadedTextureCounter;i++){ - if(!strcmp(LoadedTexture[i]->name,fileName)){ + // check if texture is already loaded + for(auto &t : LoadedTexture){ + if(t.name == fileName){ + #ifdef DEBUG - DEBUG_printf("Reusing loaded texture for %s\n",fileName); + DEBUG_printf("Reusing loaded texture for %s\n", fileName.c_str()); #endif // DEBUG - return LoadedTexture[i]->tex; + + return t.tex; } } - if(!fileName) - return 0; - - if(!(image = IMG_Load(fileName))) + // load SDL_surface of texture + if(!(image = IMG_Load(fileName.c_str()))) return 0; + #ifdef DEBUG - DEBUG_printf("Loaded image file: %s\n", fileName); + DEBUG_printf("Loaded image file: %s\n", fileName.c_str()); #endif // DEBUG + /* + * Load texture through OpenGL. + */ + 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); @@ -61,33 +78,27 @@ namespace Texture{ image->pixels ); - LoadedTexture[LoadedTextureCounter] = new struct texture_t; //(struct texture_t *)malloc(sizeof(struct texture_t)); - LoadedTexture[LoadedTextureCounter]->name = new char[strlen(fileName)+1]; //(char *)malloc(safe_strlen(fileName)); - LoadedTexture[LoadedTextureCounter]->tex = object; - strcpy(LoadedTexture[LoadedTextureCounter]->name,fileName); - LoadedTexture[LoadedTextureCounter]->dim.x = image->w; - LoadedTexture[LoadedTextureCounter]->dim.y = image->h; - LoadedTextureCounter++; + // add texture to LoadedTexture + LoadedTexture.push_back((struct texture_t){fileName,object,{image->w,image->h}}); - SDL_FreeSurface(image); // Free the surface + // free the SDL_Surface + SDL_FreeSurface(image); return object; } - dim2 imageDim(const char *fileName){ - for(unsigned int i=0;i<LoadedTextureCounter;i++){ - if(!strcmp(LoadedTexture[i]->name,fileName)){ - return LoadedTexture[i]->dim; - } + dim2 imageDim(std::string fileName){ + for(auto &t : LoadedTexture){ + if(t.name == fileName) + return t.dim; } return {0,0}; } void freeTextures(void){ - for(unsigned int i=0;i<LoadedTextureCounter;i++){ - glDeleteTextures(1,&LoadedTexture[i]->tex); - delete[] LoadedTexture[i]->name; - delete LoadedTexture[i]; + while(!LoadedTexture.empty()){ + glDeleteTextures(1, &LoadedTexture.back().tex); + LoadedTexture.pop_back(); } } |