diff --git a/OpenRa.Game/Graphics/MappedImage.cs b/OpenRa.Game/Graphics/MappedImage.cs new file mode 100644 index 0000000000..1d9365eae8 --- /dev/null +++ b/OpenRa.Game/Graphics/MappedImage.cs @@ -0,0 +1,31 @@ +using System.Xml; +using System.Drawing; +using Ijw.DirectX; +using System.IO; +namespace OpenRa.Game.Graphics +{ + class MappedImage + { + readonly Rectangle rect; + public readonly string Src; + public readonly string Name; + + public MappedImage(string defaultSrc, XmlElement e) + { + Src = (e.HasAttribute("src")) ? e.GetAttribute("src") : defaultSrc; + Name = e.GetAttribute("name"); + if (Src == null) + throw new InvalidDataException("Image src missing"); + + rect = new Rectangle(int.Parse(e.GetAttribute("x")), + int.Parse(e.GetAttribute("y")), + int.Parse(e.GetAttribute("width")), + int.Parse(e.GetAttribute("height"))); + } + + public Sprite GetImage(Renderer r, Sheet s) + { + return new Sprite(s, rect, TextureChannel.Alpha); + } + } +} diff --git a/OpenRa.Game/Graphics/SequenceProvider.cs b/OpenRa.Game/Graphics/SequenceProvider.cs index 756f523a8b..eddc27204b 100644 --- a/OpenRa.Game/Graphics/SequenceProvider.cs +++ b/OpenRa.Game/Graphics/SequenceProvider.cs @@ -10,15 +10,25 @@ namespace OpenRa.Game.Graphics { static Dictionary> units; static Dictionary cursors; - + + static Dictionary> collections; + static Dictionary cachedSheets; + static Dictionary> cachedSprites; + public static void Initialize( bool useAftermath ) { units = new Dictionary>(); cursors = new Dictionary(); + collections = new Dictionary>(); + cachedSheets = new Dictionary(); + cachedSprites = new Dictionary>(); + LoadSequenceSource("sequences.xml"); if (useAftermath) LoadSequenceSource("sequences-aftermath.xml"); + + LoadChromeSource("chrome.xml"); } static void LoadSequenceSource(string filename) @@ -32,7 +42,27 @@ namespace OpenRa.Game.Graphics foreach (XmlElement eCursor in document.SelectNodes("/sequences/cursor")) LoadSequencesForCursor(eCursor); } + + static void LoadChromeSource(string filename) + { + XmlDocument document = new XmlDocument(); + document.Load(FileSystem.Open(filename)); + foreach (XmlElement eCollection in document.SelectNodes("/chrome/collection")) + LoadChromeForCollection(eCollection); + } + static void LoadChromeForCollection(XmlElement eCollection) + { + string elementName = eCollection.GetAttribute("name"); + string defaultSrc = (eCollection.HasAttribute("src") ? eCollection.GetAttribute("src") : null); + + var images = eCollection.SelectNodes("./image").OfType() + .Select(e => new MappedImage(defaultSrc, e)) + .ToDictionary(s => s.Name); + + collections.Add(elementName, images); + } + static void LoadSequencesForCursor(XmlElement eCursor) { string cursorSrc = eCursor.GetAttribute("src"); @@ -73,5 +103,32 @@ namespace OpenRa.Game.Graphics { return cursors[cursor]; } + + + public static Sprite GetImageFromCollection(Renderer renderer,string collection, string image) + { + // Cached sprite + if (cachedSprites.ContainsKey(collection) && cachedSprites[collection].ContainsKey(image)) + return cachedSprites[collection][image]; + + var mi = collections[collection][image]; + + // Cached sheet + Sheet sheet; + if (cachedSheets.ContainsKey(mi.Src)) + sheet = cachedSheets[mi.Src]; + else + { + sheet = new Sheet(renderer, mi.Src); + cachedSheets.Add(mi.Src, sheet); + } + + // Cache the sprite + if (!cachedSprites.ContainsKey(collection)) + cachedSprites.Add(collection, new Dictionary()); + cachedSprites[collection].Add(image, mi.GetImage(renderer, sheet)); + + return cachedSprites[collection][image]; + } } } diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 088f626e4e..aac3bc8b21 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -100,6 +100,7 @@ + diff --git a/chrome.xml b/chrome.xml new file mode 100644 index 0000000000..9be118172e --- /dev/null +++ b/chrome.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +