예제 #1
0
        private static FileFormats CheckSprFormats(Stream str)
        {
            FileFormats format;
            long        oldPos = str.Position;

            ColorPalette  pal  = new Bitmap(50, 50, PixelFormat.Format8bppIndexed).Palette;
            IndexedBitmap ibmp = new IndexedBitmap(pal);


            if (ibmp.SetBitmapFromRleStream(str, FileFormats.SpriteSpr) != null)
            {
                format = FileFormats.SpriteSpr;
            }
            else
            {
                str.Position = oldPos;
                ibmp         = new IndexedBitmap(pal);
                if (ibmp.SetBitmapFromRleStream(str, FileFormats.ResIdxFramesSpr) != null)
                {
                    format = FileFormats.ResIdxFramesSpr;
                }
                else
                {
                    format = FileFormats._Unknown;
                }
            }

            str.Position = oldPos;
            return(format);
        }
예제 #2
0
 /// <summary>
 /// Creates a new <see cref="SkaaSprite"/> from a stream of SPR-formatted data
 /// </summary>
 /// <param name="str">The stream to read the SPR data from</param>
 /// <param name="pal">The <see cref="ColorPalette"/> to apply to the sprite's images</param>
 /// <returns>A new <see cref="SkaaSprite"/></returns>
 /// <remarks>
 /// The original game code for reading SPR files can be found <c>ResourceDb::init_imported()</c> 
 /// in src/ORESDB.cpp around line 72. The <c>resName</c> will be "sprite\\NAME.SPR". SPR files are 
 /// are considered <c>FLAT</c> by 7KAA. 
 /// </remarks>
 public static SkaaSprite FromSprStream(Stream str, ColorPalette pal)
 {
     SkaaSprite spr = new SkaaSprite();
     if (str is FileStream)
         spr.SpriteId = Path.GetFileNameWithoutExtension(((FileStream)str).Name);
     try
     {
         while (str.Position < str.Length)
         {
             IndexedBitmap iBmp = new IndexedBitmap(pal);
             SkaaSpriteFrame sf = new SkaaSpriteFrame(spr);
             sf.IndexedBitmap = iBmp;
             sf.BitmapOffset = str.Position;
             iBmp.SetBitmapFromRleStream(str, FileFormats.SpriteSpr);
             spr.Frames.Add(sf);
         }
     }
     catch (Exception e)
     {
         Logger.TraceEvent(TraceEventType.Error, 0, $"Failed to load sprite: {spr.SpriteId} (Exception: {e.Message})");
         Debugger.Break();
     }
     return spr;
 }
예제 #3
0
        private static FileFormats CheckSprFormats(Stream str)
        {
            FileFormats format;
            long oldPos = str.Position;

            ColorPalette pal = new Bitmap(50, 50, PixelFormat.Format8bppIndexed).Palette;
            IndexedBitmap ibmp = new IndexedBitmap(pal);

            if (ibmp.SetBitmapFromRleStream(str, FileFormats.SpriteSpr) != null)
                format = FileFormats.SpriteSpr;
            else
            {
                str.Position = oldPos;
                ibmp = new IndexedBitmap(pal);
                if (ibmp.SetBitmapFromRleStream(str, FileFormats.ResIdxFramesSpr) != null)
                    format = FileFormats.ResIdxFramesSpr;
                else
                    format = FileFormats._Unknown;
            }

            str.Position = oldPos;
            return format;
        }
예제 #4
0
        /// <summary>
        /// Reads all the frame data from the specified file, of type <see cref="FileFormats.ResIdxMultiBmp"/>
        /// </summary>
        /// <param name="filepath">The path to the file</param>
        /// <param name="pal">The <see cref="ColorPalette"/> to use in building the frame's image</param>
        /// <returns>
        /// A <see cref="Tuple{T1, T2}"/> where <c>T1</c> is the new <see cref="SkaaSprite"/> and <c>T2</c> is
        /// the <see cref="DataTable"/> read from the ResIdx header.
        /// </returns>
        /// <remarks>
        /// This differs from loading an SPR file in that the file contains the <see cref="SkaaFrame"/> data
        /// directly in its header rather than in the standard game set (see <see cref="GameSetFile"/>. The 
        /// <see cref="DataTable"/> only has fields for FrameName and FrameOffset; any other data is stored 
        /// elsewhere, generally in DBF (dBaseIII) files that may or may not be in the standard game set.
        /// </remarks>
        private static Tuple<SkaaSprite, DataTable> ReadFrames(string filepath, ColorPalette pal, bool offsetsOnly)
        {
            SkaaSprite spr = new SkaaSprite();
            DataTable dt = new DataTable();
            dt.AddDataSource(Path.GetFileName(filepath));
            dt.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = SkaaGameDataLib.Util.DataRowExtensions.ResIdxFrameNameColumn });
            dt.Columns.Add(new DataColumn() { DataType = typeof(uint), ColumnName = SkaaGameDataLib.Util.DataRowExtensions.ResIdxFrameOffsetColumn });

            using (FileStream fs = new FileStream(filepath, FileMode.Open))
            {
                //Read the file definitions from the ResIdx header.
                Dictionary<string, uint> dic = ResourceDefinitionReader.ReadDefinitions(fs, offsetsOnly);
                spr.SpriteId = Path.GetFileNameWithoutExtension(filepath);
                dt.TableName = spr.SpriteId;

                foreach (string key in dic.Keys)
                {
                    SkaaSpriteFrame sf = new SkaaSpriteFrame(null);
                    sf.BitmapOffset = dic[key];
                    fs.Position = sf.BitmapOffset;
                    sf.Name = key;
                    IndexedBitmap iBmp = new IndexedBitmap(pal);
                    sf.IndexedBitmap = iBmp;
                    iBmp.SetBitmapFromRleStream(fs, FileFormats.ResIdxFramesSpr);

                    spr.Frames.Add(sf);

                    DataRow row = dt.NewRow();
                    dt.Rows.Add(row);
                    row.BeginEdit();
                    row[SkaaGameDataLib.Util.DataRowExtensions.ResIdxFrameNameColumn] = key;
                    row[SkaaGameDataLib.Util.DataRowExtensions.ResIdxFrameOffsetColumn] = dic[key];
                    row.AcceptChanges();
                }
            }

            return new Tuple<SkaaSprite, DataTable>(spr, dt);
        }