Advertisement
Sand25

IAssestHandler

Apr 8th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. using SFML.Audio;
  2. using SFML.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace BlockBreaker.App.Main
  10. {
  11.     public class IAssestHandler
  12.     {
  13.         private static IAssestHandler instance;
  14.  
  15.         public String audioDir = "Resources/audio/";
  16.         public String textureDir = "Resources/textures/";
  17.         public String fontDir = "Resources/fonts/";
  18.  
  19.         SortedDictionary<String, Texture> textures = new SortedDictionary<string, Texture> { };
  20.         SortedDictionary<String, Font> fonts = new SortedDictionary<string, Font> { };
  21.         SortedDictionary<String, SoundBuffer> audioBuffer = new SortedDictionary<string, SoundBuffer> { };
  22.  
  23.         private IAssestHandler()
  24.         {
  25.            
  26.         }
  27.  
  28.         public static IAssestHandler Instance
  29.         {
  30.             get
  31.             {
  32.                 if (instance == null)
  33.                 {
  34.                     instance = new IAssestHandler();
  35.                 }
  36.                 return instance;
  37.             }
  38.         }
  39.  
  40.         public Texture getTexture(String name)
  41.         {
  42.             Texture texture;
  43.             if (name == "")
  44.             {
  45.  
  46.              if (textures.TryGetValue("pixel.png", out texture))
  47.                 {
  48.                     return texture;
  49.                 }
  50.              else
  51.                 {
  52.                     Image image = new Image(1, 1);
  53.                     textures["pixel.png"] = new Texture(image);
  54.                     return textures["pixel.png"];
  55.                 }
  56.             }
  57.             else
  58.             {
  59.                 if (textures.TryGetValue(name, out texture))
  60.                 {
  61.                     return texture;
  62.                 }
  63.                 else
  64.                 {
  65.                     try
  66.                     {
  67.                         textures[name] = new Texture("../" + textureDir + name);
  68.                     }catch (Exception ex)
  69.                     {
  70.                         Console.Write(ex.StackTrace);
  71.                         return getTexture("");
  72.                     }
  73.                     return textures[name];
  74.                 }
  75.             }
  76.         }
  77.  
  78.         public Font getFont(String name)
  79.             {
  80.             Font font;
  81.  
  82.                 if (fonts.TryGetValue(name, out font))
  83.                 {
  84.                     return font;
  85.                 }
  86.                 else
  87.                 {
  88.                     try
  89.                     {
  90.                         fonts[name] = new Font("../" + fontDir + name);
  91.                     }
  92.                     catch (Exception ex)
  93.                     {
  94.                         Console.Write(ex.StackTrace);
  95.                     }
  96.                 }
  97.             return fonts[name];
  98.             }
  99.         public Sound getSound(String name)
  100.         {
  101.             SoundBuffer buffer;
  102.  
  103.             if (audioBuffer.TryGetValue(name, out buffer))
  104.             {
  105.                 return new Sound(buffer);
  106.             }
  107.             else
  108.             {
  109.                 try
  110.                 {
  111.                     audioBuffer[name] = new SoundBuffer("../" + fontDir + name);
  112.                    
  113.                 }
  114.                 catch (Exception ex)
  115.                 {
  116.                     Console.Write(ex.StackTrace);
  117.                 }
  118.                 return new Sound(audioBuffer[name]);
  119.             }
  120.  
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement