Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using OpenTK.Graphics.OpenGL;
  7. using OpenTK;
  8.  
  9. using System.Drawing;
  10.  
  11.  
  12. namespace Unicorn21.OpenTKRenderer
  13. {
  14.     public class ContentManager
  15.     {
  16.  
  17.         private static ContentManager _instance;
  18.         public static ContentManager Instance { get { if (_instance == null) _instance = new ContentManager(); return _instance; } }
  19.  
  20.         private ContentManager()
  21.         {
  22.             _collection = new Dictionary<string, GLTexInfo>();
  23.         }
  24.  
  25.         private Dictionary<string, GLTexInfo> _collection;
  26.  
  27.         public GLTexInfo LoadTexture(string s)
  28.         {
  29.             return this[s];
  30.         }
  31.  
  32.         public void ClearTextures()
  33.         {
  34.             foreach (var i in _collection.Values)
  35.             {
  36.                 GL.DeleteTexture(i.glID);
  37.             }
  38.  
  39.             this._collection = new Dictionary<string, GLTexInfo>();
  40.         }
  41.  
  42.         public GLTexInfo this[string s]
  43.         {
  44.             get
  45.             {
  46.                 if (!_collection.Keys.Contains(s))
  47.                 {
  48.                     if (!System.IO.File.Exists("Content/" + s + ".png")) throw new ApplicationException("Texture not Found <" + s + ".png>.");
  49.  
  50.                     Bitmap b = new Bitmap("Content/" + s + ".png");
  51.  
  52.                     System.Drawing.Imaging.BitmapData _textureData =
  53.                         b.LockBits(
  54.                         new System.Drawing.Rectangle(0, 0, b.Width, b.Height),
  55.                         System.Drawing.Imaging.ImageLockMode.ReadOnly,
  56.                         System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  57.  
  58.                     var t = new GLTexInfo();
  59.  
  60.                     int texture;
  61.  
  62.                     GL.GenTextures(1, out texture);
  63.  
  64.                     t.glID = texture;
  65.                     t.Width = b.Width;
  66.                     t.Height = b.Height;
  67.  
  68.                     GL.BindTexture(TextureTarget.Texture2D, t.glID);
  69.  
  70.  
  71.                     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
  72.                     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
  73.  
  74.  
  75.  
  76.                     GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, b.Width, b.Height, 0,
  77.         OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, _textureData.Scan0);
  78.  
  79.                     b.UnlockBits(_textureData);
  80.  
  81.                    
  82.  
  83.                     _collection[s] = t;
  84.  
  85.  
  86.                    
  87.                 }
  88.  
  89.                 return _collection[s];
  90.             }
  91.  
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement