///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Share the image with another sprite /// </summary> /// /// <param name="spriteToCopy">The original sprite</param> /// <param name="newSprite">The sprite that will get the same image as the sprite that is being copied</param> /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public bool CopyTexture(Impl.Sprite spriteToCopy, Impl.Sprite newSprite) { // Ignore null pointers if (spriteToCopy.texture == null) { newSprite.texture = null; return(true); } // Loop all our textures to check if we already have this one foreach (var pair in m_ImageMap) { ImageMapData data = pair.Value; foreach (Impl.Texture texture in data.data) { // Check if the pointer points to our texture if (texture == spriteToCopy.texture) { // The texture is now used at multiple places ++(texture.users); newSprite.texture = spriteToCopy.texture; newSprite.sprite = new Sprite(spriteToCopy.sprite); return(true); } } } Internal.Output("TGUI warning: Can't copy texture that wasn't loaded by TextureManager."); return(false); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Removes the sprite /// </summary> /// /// <param name="sprite">The sprite that should be removed</param> /// /// When no other sprite is using the same image then the image will be removed from memory. /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void RemoveTexture(Impl.Sprite sprite) { // Ignore already removed sprites if (sprite.texture == null) { return; } // Loop all our textures to check which one it is foreach (var pair in m_ImageMap) { ImageMapData data = pair.Value; foreach (Impl.Texture texture in data.data) { // Check if the pointer points to our texture if (texture == sprite.texture) { // If this was the only place where the texture is used then delete it if (--(texture.users) == 0) { int usage = 0; foreach (Impl.Texture t in data.data) { if (t.image == data.image) { usage++; } } // Remove the texture from the list, or even the whole image if it isn't used anywhere else if (usage == 1) { m_ImageMap.Remove(pair.Key); } else { data.data.Remove(texture); } } // The pointer is now useless sprite.texture = null; return; } } } // Internal.Output("TGUI warning: Can't remove texture that wasn't loaded by TextureManager."); return; }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Load another image/frame from a file /// </summary> /// /// <param name="filename">The filename of the image that you want to use as next frame.</param> /// <param name="frameDuration">The amount of time (in milliseconds) that the frame will be displayed on the screen. /// When the duration is 0 then the animation will be blocked at that frame.</param> /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void AddFrame(string filename, uint frameDuration) { Impl.Sprite tempTexture = new Impl.Sprite(); // Load the texture from the file Global.TextureManager.GetTexture(Global.ResourcePath + filename, tempTexture); // If this is the first frame then set it as the current displayed frame if (m_Textures.Count == 0) { m_CurrentFrame = 0; // Remember the size of this image m_Size = new Vector2f(tempTexture.Size.X, tempTexture.Size.Y); } // Add the texture tempTexture.sprite.Color = new Color(255, 255, 255, m_Opacity); m_Textures.Add(tempTexture); // Store the frame duration m_FrameDuration.Add((int)frameDuration); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Load the texture that is described by the value /// </summary> /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void ReadTexture(int i, string folder, Impl.Sprite sprite) { // The shortest string needs three character if (Values [i].Length < 2) { throw new Exception("The value of property " + Properties[i] + " in section " + m_Section + " doesn't contain a filename between quotes."); } // The first character has to be a quote if (Values [i] [0] != '"') { throw new Exception("The value of property " + Properties[i] + " in section " + m_Section + " didn't begin with a double quote."); } // There has to be another quote int index = Values [i].IndexOf('"', 1); if (index == -1) { throw new Exception("The value of property " + Properties[i] + " in section " + m_Section + " didn't contain an ending quote."); } // There can't be more than two quotes int unexistingIndex = Values [i].IndexOf('"', index + 1); if (unexistingIndex != -1) { throw new Exception("The value of property " + Properties[i] + " in section " + m_Section + " contains more than two quotes."); } // Check if there is still something behind the quotes IntRect rect = new IntRect(); if (Values [i].Length > index + 1) { // Drop the brackets if (Values [i] [index + 1] == '(' && Values [i] [Values [i].Length - 1] == ')') { string rectStr = Values [i].Substring(index + 2, Values [i].Length - index - 3); // Extract the rect string[] rectComponents = rectStr.Split(','); // A rectangle has 4 components (left, top, right, height) if (rectComponents.Length == 4) { rect = new IntRect(Convert.ToInt32(rectComponents [0]), Convert.ToInt32(rectComponents [1]), Convert.ToInt32(rectComponents [2]), Convert.ToInt32(rectComponents [3])); } else { throw new Exception("The value of property " + Properties[i] + " in section " + m_Section + " contains brackets after the filename, but without four components split by commas."); } } else // The string doesn't begin and end with a bracket { throw new Exception("The value of property " + Properties[i] + " in section " + m_Section + " contains contains characters after the filename of which the first and last are not brackets."); } } // Try to load the strings between the quotes Global.TextureManager.GetTexture(folder + Values[i].Substring(1, index - 1), sprite, rect); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <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; }