///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Loads a texture /// </summary> /// /// <param name="filename">Filename of the image to load</param> /// <param name="sprite">The sprite object to store the loaded image</param> /// <param name="rect">Load only this part of the image.</param> /// /// The second time you call this function with the same filename, the previously loaded image will be reused. /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void GetTexture(string filename, Impl.Sprite sprite, SFML.Graphics.IntRect rect = new SFML.Graphics.IntRect()) { // Look if we already had this image ImageMapData data; if (m_ImageMap.TryGetValue(filename, out data)) { // Loop all our textures to find the one containing the image foreach (Impl.Texture tex in data.data) { // Only reuse the texture when the exact same part of the image is used if ((tex.rect.Left == rect.Left) && (tex.rect.Top == rect.Top) && (tex.rect.Width == rect.Width) && (tex.rect.Height == rect.Height)) { // The texture is now used at multiple places ++(tex.users); // We already have the texture, so pass the data sprite.texture = tex; // Set the texture in the sprite sprite.sprite.Texture = tex.texture; return; } } } else // The image doesn't exist yet { data = new ImageMapData (); data.data = new List<Impl.Texture> (); m_ImageMap.Add (filename, data); } // Add new data to the list Impl.Texture texture = new Impl.Texture(); sprite.texture = texture; sprite.texture.image = data.image; sprite.texture.rect = rect; data.data.Add(texture); // Load the image if (Global.ResourceManager == null) { sprite.texture.image = new SFML.Graphics.Image (filename); } else { if (Global.ResourceManager.GetObject(filename) is byte[]) { byte[] raw = Global.ResourceManager.GetObject(filename) as byte[]; MemoryStream mem = new MemoryStream(raw); sprite.texture.image = new SFML.Graphics.Image(mem); } else if (Global.ResourceManager.GetObject(filename) is System.Drawing.Image) { System.Drawing.Image raw = Global.ResourceManager.GetObject(filename) as System.Drawing.Image; MemoryStream mem = new MemoryStream(); // Copy the image to the Stream raw.Save(mem, raw.RawFormat); // Copy stream into new memory stream - prevents an AccessViolationException mem = new MemoryStream(mem.ToArray()); sprite.texture.image = new SFML.Graphics.Image(mem); } } // Create a texture from the image if ((rect.Left == 0) && (rect.Top == 0) && (rect.Width == 0) && (rect.Height == 0)) sprite.texture.texture = new SFML.Graphics.Texture (sprite.texture.image); else sprite.texture.texture = new SFML.Graphics.Texture (sprite.texture.image, rect); // Set the texture in the sprite sprite.sprite.Texture = sprite.texture.texture; // Set the other members of the data sprite.texture.filename = filename; sprite.texture.users = 1; }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Loads a texture /// </summary> /// /// <param name="filename">Filename of the image to load</param> /// <param name="sprite">The sprite object to store the loaded image</param> /// <param name="rect">Load only this part of the image.</param> /// /// The second time you call this function with the same filename, the previously loaded image will be reused. /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void GetTexture(string filename, Impl.Sprite sprite, SFML.Graphics.IntRect rect = new SFML.Graphics.IntRect()) { // Look if we already had this image ImageMapData data; if (m_ImageMap.TryGetValue(filename, out data)) { // Loop all our textures to find the one containing the image foreach (Impl.Texture tex in data.data) { // Only reuse the texture when the exact same part of the image is used if ((tex.rect.Left == rect.Left) && (tex.rect.Top == rect.Top) && (tex.rect.Width == rect.Width) && (tex.rect.Height == rect.Height)) { // The texture is now used at multiple places ++(tex.users); // We already have the texture, so pass the data sprite.texture = tex; // Set the texture in the sprite sprite.sprite.Texture = tex.texture; return; } } } else // The image doesn't exist yet { data = new ImageMapData(); data.data = new List <Impl.Texture> (); m_ImageMap.Add(filename, data); } // Add new data to the list Impl.Texture texture = new Impl.Texture(); sprite.texture = texture; sprite.texture.image = data.image; sprite.texture.rect = rect; data.data.Add(texture); // Load the image if (Global.ResourceManager == null) { sprite.texture.image = new SFML.Graphics.Image(filename); } else { if (Global.ResourceManager.GetObject(filename) is byte[]) { byte[] raw = Global.ResourceManager.GetObject(filename) as byte[]; MemoryStream mem = new MemoryStream(raw); sprite.texture.image = new SFML.Graphics.Image(mem); } else if (Global.ResourceManager.GetObject(filename) is System.Drawing.Image) { System.Drawing.Image raw = Global.ResourceManager.GetObject(filename) as System.Drawing.Image; MemoryStream mem = new MemoryStream(); // Copy the image to the Stream raw.Save(mem, raw.RawFormat); // Copy stream into new memory stream - prevents an AccessViolationException mem = new MemoryStream(mem.ToArray()); sprite.texture.image = new SFML.Graphics.Image(mem); } } // Create a texture from the image if ((rect.Left == 0) && (rect.Top == 0) && (rect.Width == 0) && (rect.Height == 0)) { sprite.texture.texture = new SFML.Graphics.Texture(sprite.texture.image); } else { sprite.texture.texture = new SFML.Graphics.Texture(sprite.texture.image, rect); } // Set the texture in the sprite sprite.sprite.Texture = sprite.texture.texture; // Set the other members of the data sprite.texture.filename = filename; sprite.texture.users = 1; }