//, Stream stream) /// <summary> /// Initializes a new <see cref="SpriteFrame"/>. /// </summary> /// <param name="parentSprite">The <see cref="Sprite"/> containing this <see cref="SpriteFrame"/></param> /// <param name="stream"></param> public SpriteFrame(Sprite parentSprite) { this.ParentSprite = parentSprite; //this.SprBitmapOffset = (int) stream.Position; this.GameSetDataRows = new List<DataRow>(); //this.FrameRawData = GameDataHandlers.SprStreamToSpriteFrame(stream); //ReadSprData(stream); //BuildBitmapFromSpr(); }
//public SpriteFrame AddFrame(SpriteFrame sf) //{ // this._spriteObject.Frames.Add(sf); // //sf.FrameUpdated += SpriteFrameUpdated; // return sf; //} /// <summary> /// Iterates through all the rows in the <see cref="Sprite"/>'s <see cref="GameSetDataTable"/> and /// sets each of this sprite's <see cref="SpriteFrame"/>'s <see cref="SpriteFrame.GameSetDataRows"/> /// property to the DataRow with a BITMAPPTR matching <see cref="SpriteFrame.SprBitmapOffset"/>. /// </summary> /// <returns>False if any frame did not have a match in the DataView. True otherwise.</returns> internal bool MatchFrameOffsets(Sprite spr) { foreach (DataRowView drv in this.SpriteDataView) { int offset = Convert.ToInt32(drv.Row.ItemArray[9]); SpriteFrame sf = spr.Frames.Find(f => f.SprBitmapOffset == offset); if (sf == null) { Trace.WriteLine(string.Format("Unable to find matching offset in Sprite.Frames for {0} and offset: {1}.\n\nDid you forget to load the proper SET file for this sprite?", spr.SpriteId, offset.ToString())); return false; } if (sf != null) sf.GameSetDataRows.Add(drv.Row); } return true; }
public static Bitmap SpriteToBmp(Sprite spr) { int totalFrames = spr.Frames.Count; int spriteWidth = 0, spriteHeight = 0, high = 0, low = 0; double sqrt = Math.Sqrt((double) totalFrames); if (totalFrames % 1 != 0) //totalFrames is a perfect square { low = (int) sqrt; high = (int) sqrt; } else { low = (int) Math.Floor(sqrt) + 1; //adds an additional row high = (int) Math.Ceiling(sqrt); } //need the largest height and width to tile the export foreach (SpriteFrame sf in spr.Frames) { if (sf.Width > spriteWidth) spriteWidth = sf.Width; if (sf.Height > spriteHeight) spriteHeight = sf.Height; } //calculated height and width of the bitmap //based on tiles of the largest possible size int exportWidth = high * spriteWidth, exportHeight = low * spriteHeight; using (Bitmap bitmap = new Bitmap(exportWidth, exportHeight)) { using (Graphics g = Graphics.FromImage(bitmap)) { int frameIndex = 0; for (int y = 0; y < exportHeight; y += spriteHeight) { //once we hit the max frames, just break for (int x = 0; x < exportWidth && frameIndex < spr.Frames.Count; x += spriteWidth) { g.DrawImage(spr.Frames[frameIndex].ImageBmp, new Point(x, y)); frameIndex++; } } } return bitmap; } }
/// <summary> /// Opens an SPR file and creates a <see cref="SpriteResource"/> object for it /// </summary> /// <param name="filepath">The absolute path to the SPR file to open</param> /// <returns>The newly-created <see cref="Sprite"/></returns> /// <remarks> /// The original game code for reading SPR files can be found <code>ResourceDb::init_imported()</code> /// in src/ORESDB.cpp around line 72. The <code>resName</code> will be "sprite\\NAME.SPR". (There's /// no need to follow the call into <code>File::file_open()</code> in OFILE.cpp. Though the files are /// well-structured, they are considered FLAT by 7KAA. /// </remarks> public void LoadSprite(string filepath) { if (this.ActivePalette == null) throw new Exception("Cannot load a Sprite if the ActivePalette is null."); //cant use the property here or we'll fire the event before we've finished loading Sprite spr = new Sprite(this.ActivePalette); //this._activeSprite = new Sprite(this.ActivePalette); using (FileStream spritestream = File.OpenRead(filepath)) { while (spritestream.Position < spritestream.Length) { SpriteFrame sf = new SpriteFrame(spr); SprDataHandlers.SprStreamToSpriteFrame(sf, spritestream); sf.ImageBmp = SprDataHandlers.FrameSprToBmp(sf, this.ActivePalette); spr.Frames.Add(sf); } } spr.Resource.FileName = Path.GetFileName(filepath); spr.SpriteId = Path.GetFileNameWithoutExtension(filepath); DataView dv = this.ActiveGameSet.GetSpriteDataView(spr.SpriteId); spr.SetSpriteDataView(dv); this.ActiveSprite = spr; }