Tuesday, January 6, 2015

Texture Management

The texture management is fairly simplistic. The management simply stores the texture and it's unique id in a map to be referenced when creating sprites. Since the textures are stored on the video card, they should be managed by the application as to when textures need loaded or removed from the graphics memory. The unique identifiers are limited to 65535. 

Concern 1: Current management does not reallocate ids of removed textures. I must weigh the pros and cons of simply adding more identifiers or reallocating ids when one is removed.

Concern 2: Using integer ids means that the controlling application must remember the ids. Perhaps I should store them by name or path. 

Design decisions may change how this is done entirely so I'm not too worried about it at the start. My starting goal was simply to store the texture which I accomplished.

I did use a singleton class. Whether this is really needed is debatable.

 // System Includes  
 #include <map>     //map  
 #include <iostream>   //cout  
 // SFML Includes  
 #include <SFML/Graphics.hpp>  
 // Engine Includes  
 #include "TextureManager.hpp"  
 TextureManager::TextureManager(void)  
 {  
   m_NumTextures = 0;  
 };  
 // Add an Texture to the Texture manager from a sprite sheet  
 int TextureManager::addTexture(const char* iTexturePath)  
 {  
   sf::Texture texture;  
   int retVal = m_NumTextures;  
   if(!texture.loadFromFile(iTexturePath))  
   {  
     std::cout << "Unable to load texture " << iTexturePath << std::endl;  
     retVal = -1;  
   }  
   else  
   {  
     std::cout << "Loaded texture " << iTexturePath << std::endl;  
     // TODO: Possible Latency issue with copying the texture  
     m_Textures.insert(std::pair<int, sf::Texture>(m_NumTextures, texture));  
     m_NumTextures++;  
   }  
   return retVal;  
 };  
 // Returns a reference to the Texture at the index to be used by the game engine  
 sf::Texture& TextureManager::getTexture(int iIndex)  
 {  
   return m_Textures[iIndex];  
 };  
 // Removes all Textures and resets ids  
 void TextureManager::clear(void)  
 {  
   m_NumTextures = 0;  
   m_Textures.clear();  
 };  
 // Removes a specific Texture id  
 void TextureManager::remove(int iTextureId)  
 {  
   // Don't reset the number of textures as we need to keep providing unique identifiers  
   m_Textures.erase(iTextureId);  
 };  

The above code is the source for the singleton texture manager as of now.

No comments:

Post a Comment