Advertisement
Guest User

ImageManager.d

a guest
Oct 15th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.90 KB | None | 0 0
  1. module playground.ImageManager;
  2.  
  3. import std.stdio;
  4. import std.string;
  5. import derelict.sdl2.sdl;
  6.  
  7. struct ImageInfo
  8. {
  9.     SDL_Surface *image;
  10.     string friendlyName;
  11.     string fileName;
  12. }
  13.  
  14.  
  15. /**
  16.  * Manages loading and caching of image resources.
  17.  *
  18.  */
  19. class ImageManager
  20. {
  21.     ImageInfo[string] images;
  22.    
  23.     this()
  24.     {
  25.        
  26.     }
  27.    
  28.     ~this()
  29.     {
  30.         foreach(string name, ImageInfo iInfo; images)
  31.         {
  32.             debug writeln("Freeing image ", name);
  33.             SDL_FreeSurface(iInfo.image);
  34.         }
  35.     }
  36.    
  37.     /**
  38.      * Determines if a specific image has been loaded
  39.      */
  40.     private bool HasImage(string friendlyName)
  41.     {
  42.         ImageInfo tmp;
  43.         return images.get(friendlyName, tmp) != tmp;
  44.     }
  45.    
  46.     /**
  47.      * Frees the underlying image, as well as removes the ImageInfo from the cache.
  48.      * @param friendlyName The 'friendly' name of the image (i.e. 'HelloWorld')
  49.      */
  50.     private void FreeImage(string friendlyName)
  51.     {
  52.         if(!HasImage(friendlyName))
  53.             return;
  54.         debug writeln("Freeing image ", friendlyName);
  55.         SDL_FreeSurface(images[friendlyName].image);
  56.         debug writeln("Removing ", friendlyName);
  57.         images.remove(friendlyName);
  58.     }
  59.    
  60.     /**
  61.      * Loads and caches the specified image.
  62.      * @param friendlyName The 'friendly' name of the image (i.e. 'HelloWorld')
  63.      * @param fileName The filename of the image to load.
  64.      * @return True if successful, otherwise false.
  65.      */
  66.     public bool CacheImage(string friendlyName, string fileName)
  67.     {
  68.         if(HasImage(friendlyName))
  69.         {
  70.             if(images[friendlyName].fileName == fileName)
  71.             {              
  72.                 debug writeln("Ignoring cache request for ",friendlyName);
  73.                 return true;
  74.             } else {
  75.                 debug writefln("CacheImage: Filepaths differ for [%s]. Reloading.", friendlyName);
  76.                 FreeImage(friendlyName);
  77.             }
  78.            
  79.         }
  80.        
  81.         SDL_Surface *loadedImage;
  82.         loadedImage = SDL_LoadBMP(toStringz(fileName));
  83.         if(loadedImage != null)
  84.         {
  85.             debug writeln("Caching image ", friendlyName);
  86.             ImageInfo iInfo;
  87.             iInfo.image = loadedImage;
  88.             iInfo.fileName = fileName;
  89.             iInfo.friendlyName = friendlyName;
  90.             images[friendlyName] = iInfo;
  91.             return true;
  92.         }
  93.        
  94.         debug writefln("Failed to cache image [%s] @ [%s] with error: %s.", friendlyName, fileName, SDL_GetError());
  95.         return false;
  96.     }
  97.    
  98.     public SDL_Surface* LoadImage(string friendlyName, string filename)
  99.     {
  100.         SDL_Surface *loadedImage;
  101.         loadedImage = SDL_LoadBMP(toStringz(filename));
  102.         if(loadedImage != null)
  103.         {
  104.             debug writeln("Loading image ", friendlyName);
  105.             ImageInfo iInfo;
  106.             iInfo.image = loadedImage;
  107.             iInfo.fileName = filename;
  108.             iInfo.friendlyName = friendlyName;
  109.             images[friendlyName] = iInfo;
  110.             return loadedImage;
  111.         }
  112.         return null;
  113.     }
  114.  
  115.    
  116.     public SDL_Surface* GetImage(string friendlyName)
  117.     {
  118.         if(HasImage(friendlyName))
  119.         {
  120.             images[friendlyName].image.refcount++;
  121.             return images[friendlyName].image;
  122.         }
  123.        
  124.         debug writefln("Error: Failed to retrieve image [%s].", friendlyName);
  125.         return null;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement