using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework; using System.IO; namespace GLEED2D { //Copied from http://forums.create.msdn.com/forums/t/72423.aspx public static class ExtensionMethods { public static Texture2D ConvertToPreMultipliedAlpha(this Texture2D texture) { Color[] data = new Color[texture.Width * texture.Height]; texture.GetData(data, 0, data.Length); for (int i = 0; i < data.Length; i++) { data[i] = new Color(new Vector4(data[i].ToVector3() * (data[i].A / 255f), (data[i].A / 255f))); } texture.SetData(data, 0, data.Length); return texture; } } class TextureLoader { private static TextureLoader instance; public static TextureLoader Instance { get { if (instance == null) instance = new TextureLoader(); return instance; } } Dictionary textures = new Dictionary(); public Texture2D FromFile(GraphicsDevice gd, string filename) { if (!textures.ContainsKey(filename)) { //TextureCreationParameters tcp = TextureCreationParameters.Default; //tcp.Format = SurfaceFormat.Color; //tcp.ColorKey = Constants.Instance.ColorTextureTransparent; //textures[filename] = Texture2D.FromFile(gd, filename, tcp); //using (Stream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None)) //{ StreamReader stream = new StreamReader(filename); textures[filename] = Texture2D.FromStream(gd, stream.BaseStream).ConvertToPreMultipliedAlpha(); stream.Close(); //} } return textures[filename]; } public void Clear() { textures.Clear(); } } }