/// <summary> /// Loads this BgfFile instance from XML/BMP /// </summary> /// <param name="Filename"></param> public void LoadXml(string Filename) { string path = Path.GetDirectoryName(Filename); // save raw filename without path or extensions Filename = Path.GetFileNameWithoutExtension(Filename); // init XML reader XmlReader reader = XmlReader.Create(Filename); // rootnode reader.ReadToFollowing("bgf"); uint version = Convert.ToUInt32(reader["version"]); if (version >= BgfFile.VERSION9) { ShrinkFactor = Convert.ToUInt32(reader["shrink"]); // skip maxindices, we use dynamic getter // MaxIndices = Convert.ToUInt32(reader["maxindices"]); // frames reader.ReadToFollowing("frames"); int FrameCount = Convert.ToInt32(reader["count"]); Frames.Capacity = FrameCount; for (int i = 0; i < FrameCount; i++) { reader.ReadToFollowing("frame"); uint width = Convert.ToUInt32(reader["width"]); uint height = Convert.ToUInt32(reader["height"]); int xoffset = Convert.ToInt32(reader["xoffset"]); int yoffset = Convert.ToInt32(reader["yoffset"]); string file = reader["file"]; // hotspots reader.ReadToFollowing("hotspots"); byte hotspotcount = Convert.ToByte(reader["count"]); List <BgfBitmapHotspot> hotspots = new List <BgfBitmapHotspot>(hotspotcount); for (int j = 0; j < hotspotcount; j++) { reader.ReadToFollowing("hotspot"); sbyte index = Convert.ToSByte(reader["index"]); int x = Convert.ToInt32(reader["x"]); int y = Convert.ToInt32(reader["y"]); BgfBitmapHotspot hotspot = new BgfBitmapHotspot(index, x, y); hotspots.Add(hotspot); } // load bitmap from file Bitmap bmp = (Bitmap)Image.FromFile(path + "/" + file); byte[] pixelData = BgfBitmap.BitmapToPixelData(bmp); BgfBitmap bgfBitmap = new BgfBitmap( (uint)i + 1, version, width, height, xoffset, yoffset, hotspots, false, 0, pixelData); // cleanp temporary bitmap bmp.Dispose(); Frames.Add(bgfBitmap); } // framesets reader.ReadToFollowing("framesets"); int FrameSetCount = Convert.ToInt32(reader["count"]); FrameSets.Capacity = FrameSetCount; for (int i = 0; i < FrameSetCount; i++) { reader.ReadToFollowing("frameset"); string[] indices = reader["indices"].Split(' '); List <int> intIndices = new List <int>(); foreach (string index in indices) { intIndices.Add(Convert.ToInt32(index)); } BgfFrameSet bgfFrameSet = new BgfFrameSet((uint)i + 1, intIndices); FrameSets.Add(bgfFrameSet); } } else { throw new Exception("Wrong format version: " + version + " (expected " + BgfFile.VERSION9 + ")."); } }
/// <summary> /// Import a storyboard given a full path to the storyboard bmp. The XML file for /// the storyboard must have a matching filename with the XML extension, and be in /// the same directory. /// </summary> /// <param name="Filename"></param> /// <returns></returns> public bool ImportStoryboard(string Filename) { string path = Path.GetDirectoryName(Filename); // raw filename without path or extensions string xmlFilename = Path.GetFileNameWithoutExtension(Filename); // index for reading bmp data, incremented as we split the giant bmp uint readoffset = 0; // open bmp into one giant bitmap Bitmap bmp = (Bitmap)Image.FromFile(Filename); uint totalWidth = (uint)bmp.Width; uint totalHeight = (uint)bmp.Height; byte[] pixelData = BgfBitmap.BitmapToPixelData(bmp); // cleanp temporary bitmap bmp.Dispose(); // init XML reader XmlReader reader = XmlReader.Create(Path.Combine(path, xmlFilename) + FileExtensions.XML); // read in xml file // rootnode reader.ReadToFollowing("bgf"); uint version = Convert.ToUInt32(reader["version"]); if (version >= BgfFile.VERSION9) { ShrinkFactor = Convert.ToUInt32(reader["shrink"]); // skip maxindices, we use dynamic getter // MaxIndices = Convert.ToUInt32(reader["maxindices"]); // frames reader.ReadToFollowing("frames"); int FrameCount = Convert.ToInt32(reader["count"]); uint savedTotalHeight = Convert.ToUInt32(reader["totalheight"]); uint savedTotalWidth = Convert.ToUInt32(reader["totalwidth"]); // Saved dimensions must match those loaded for the storyboard bmp. if (savedTotalWidth != totalWidth || savedTotalHeight != totalHeight) { return(false); } Frames.Capacity = FrameCount; for (int i = 0; i < FrameCount; i++) { reader.ReadToFollowing("frame"); uint width = Convert.ToUInt32(reader["width"]); uint height = Convert.ToUInt32(reader["height"]); int xoffset = Convert.ToInt32(reader["xoffset"]); int yoffset = Convert.ToInt32(reader["yoffset"]); // hotspots reader.ReadToFollowing("hotspots"); byte hotspotcount = Convert.ToByte(reader["count"]); List <BgfBitmapHotspot> hotspots = new List <BgfBitmapHotspot>(hotspotcount); for (int j = 0; j < hotspotcount; j++) { reader.ReadToFollowing("hotspot"); sbyte index = Convert.ToSByte(reader["index"]); int x = Convert.ToInt32(reader["x"]); int y = Convert.ToInt32(reader["y"]); BgfBitmapHotspot hotspot = new BgfBitmapHotspot(index, x, y); hotspots.Add(hotspot); } // get pixels for this frame from the single bmp byte[] pixels = new byte[height * width]; uint writeoffset = 0; for (int j = 0; j < height; ++j) { Wrapper.CopyMem(pixelData, (int)readoffset, pixels, (int)writeoffset, width); readoffset += totalWidth; writeoffset += width; } BgfBitmap bgfBitmap = new BgfBitmap( (uint)i + 1, version, width, height, xoffset, yoffset, hotspots, false, 0, pixels); Frames.Add(bgfBitmap); } // framesets reader.ReadToFollowing("framesets"); int FrameSetCount = Convert.ToInt32(reader["count"]); FrameSets.Capacity = FrameSetCount; for (int i = 0; i < FrameSetCount; i++) { reader.ReadToFollowing("frameset"); string[] indices = reader["indices"].Split(' '); List <int> intIndices = new List <int>(); foreach (string index in indices) { intIndices.Add(Convert.ToInt32(index)); } BgfFrameSet bgfFrameSet = new BgfFrameSet((uint)i + 1, intIndices); FrameSets.Add(bgfFrameSet); } } else { throw new Exception("Wrong format version: " + version + " (expected " + BgfFile.VERSION9 + ")."); } return(true); }