Exemplo n.º 1
0
        public void ProcessSPR2(string Path)
        {
            List <IffChunk> SPR2s = new List <IffChunk>();

            foreach (IffChunk Chunk in m_Chunks)
            {
                if (Chunk.Type == "SPR2")
                {
                    SPR2s.Add(Chunk);
                }
            }

            foreach (IffChunk Chunk in SPR2s)
            {
                BinaryReader ChunkReader = new BinaryReader(new MemoryStream(Chunk.Data));

                SPR2 Spr = new SPR2();
                Spr.Version1 = ChunkReader.ReadInt16();

                if (Spr.Version1 == 0)
                {
                    Spr.Version2 = ReadBigShort(ChunkReader);
                }
                else
                {
                    Spr.Version2 = ChunkReader.ReadInt16();
                }

                Spr.FrameCount = ChunkReader.ReadInt32();
                MessageBox.Show("Framecount: " + Spr.FrameCount);
                Spr.PaletteID   = ChunkReader.ReadInt32();
                Spr.OffsetTable = new int[Spr.FrameCount];

                for (int i = 0; i < Spr.FrameCount; i++)
                {
                    Spr.OffsetTable[i] = ChunkReader.ReadInt32();
                }

                for (int i = 0; i < Spr.FrameCount; i++)
                {
                    SpriteFrame Frame = new SpriteFrame();
                    Frame.Width            = ChunkReader.ReadInt16();
                    Frame.Height           = ChunkReader.ReadInt16();
                    Frame.Flags            = ChunkReader.ReadInt16();
                    Frame.Unknown          = ChunkReader.ReadInt16();
                    Frame.PaletteID        = ChunkReader.ReadInt16();
                    Frame.TransparentPixel = ChunkReader.ReadInt16();
                    Frame.XPos             = ChunkReader.ReadInt16();
                    Frame.YPos             = ChunkReader.ReadInt16();

                    BinaryWriter SpriteWriter = new BinaryWriter(Frame.SpriteData);

                    for (int l = 0; l <= Frame.Height; l++)
                    {
                        SpriteWriter.Write(ChunkReader.ReadBytes(2));
                        SpriteWriter.Write(ChunkReader.ReadBytes(Frame.Width));
                    }

                    SpriteWriter.Flush();
                    //SpriteWriter.Close();

                    Spr.AddFrame(Frame);

                    //Each SPR2 resource contains a reference to a PALT chunk/resource.
                    Spr.Pal = new Palette();
                    IffChunk TmpChunk = new IffChunk();

                    foreach (IffChunk C in m_Chunks)
                    {
                        if (C.TypeNum == Spr.PaletteID)
                        {
                            //Guess what? The typenumber of each chunk is
                            //NOT unique, so you have to check on type as
                            //well!
                            if (C.Type == "PALT")
                            {
                                TmpChunk = C;
                                break;
                            }
                        }
                    }

                    BinaryReader PaltReader = new BinaryReader(new MemoryStream(TmpChunk.Data));
                    Spr.Pal.AlwaysOne = PaltReader.ReadInt32();
                    Spr.Pal.Always256 = PaltReader.ReadInt32();
                    PaltReader.ReadBytes(8); //The PALT header has 8 additional bytes of 0.

                    Spr.Pal.RGBTable = new Palette.RGB[Spr.Pal.Always256];

                    for (int l = 0; l < Spr.Pal.Always256; l++)
                    {
                        Spr.Pal.RGBTable[l]       = new Palette.RGB();
                        Spr.Pal.RGBTable[l].Red   = PaltReader.ReadByte();
                        Spr.Pal.RGBTable[l].Green = PaltReader.ReadByte();
                        Spr.Pal.RGBTable[l].Blue  = PaltReader.ReadByte();
                    }

                    PaltReader.Close();
                }

                for (int i = 0; i < Spr.FrameCount; i++)
                {
                    SpriteFrame Frame = Spr.GetFrame(i);

                    BinaryReader SpriteReader = new BinaryReader(new MemoryStream(Frame.SpriteData.ToArray()));
                    int          X = 0, Y = 0;
                    bool         Stop = false;

                    Bitmap BM          = new Bitmap(Frame.Width, Frame.Height);
                    Color  Transparent = Color.FromArgb(Spr.Pal.RGBTable[Frame.TransparentPixel].Red,
                                                        Spr.Pal.RGBTable[Frame.TransparentPixel].Green,
                                                        Spr.Pal.RGBTable[Frame.TransparentPixel].Blue);

                    Graphics Gfx = Graphics.FromImage(BM);

                    while (SpriteReader.BaseStream.Position < (SpriteReader.BaseStream.Length - 1) && !Stop)
                    {
                        byte Opcode = SpriteReader.ReadByte();
                        byte Data   = SpriteReader.ReadByte();

                        switch (Opcode)
                        {
                        case 1:     //Transparent pixels.
                            for (int Dat = 0; Dat < Data; Dat++)
                            {
                                if (X < Frame.Width)
                                {
                                    BM.SetPixel(X, Y, Transparent);
                                }

                                X++;
                            }

                            break;

                        case 2:
                            byte Col = SpriteReader.ReadByte();

                            for (int Dat = 0; Dat < Data; Dat++)
                            {
                                if (X < Frame.Width)
                                {
                                    BM.SetPixel(X, Y, GetColorFromPalette(Col, Spr));
                                }

                                X++;
                            }
                            break;

                        case 3:     //Pixels.
                            byte Pixel = SpriteReader.ReadByte();

                            for (int Dat = 0; Dat < Data; Dat++)
                            {
                                if (X < Frame.Width && Y < Frame.Height)
                                {
                                    BM.SetPixel(X, Y, GetColorFromPalette(Pixel, Spr));
                                }

                                X++;
                            }

                            break;

                        case 4:     //New line.
                            for (; X < BM.Width; X++)
                            {
                                BM.SetPixel(X, Y, Transparent);
                            }

                            Y++;     //Next line.
                            X = 0;
                            break;

                        case 5:     //End of sprite.
                            Stop = true;
                            break;

                        case 9:     //Transparent rows.
                            Gfx.FillRectangle(Brushes.Transparent,
                                              new Rectangle(0, Y, BM.Width, (Y + (Data - 1))));
                            X = 0;
                            Y = Y + (Data - 1);
                            break;
                        }
                    }

                    Random Rnd = new Random(DateTime.Now.Millisecond);

                    BM.Save(Path + "\\" + i.ToString() + Rnd.Next() + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }
Exemplo n.º 2
0
        public void ProcessSPR2(string Path)
        {
            List<IffChunk> SPR2s = new List<IffChunk>();

            foreach (IffChunk Chunk in m_Chunks)
            {
                if (Chunk.Type == "SPR2")
                    SPR2s.Add(Chunk);
            }

            foreach (IffChunk Chunk in SPR2s)
            {
                BinaryReader ChunkReader = new BinaryReader(new MemoryStream(Chunk.Data));

                SPR2 Spr = new SPR2();
                Spr.Version1 = ChunkReader.ReadInt16();

                if (Spr.Version1 == 0)
                    Spr.Version2 = ReadBigShort(ChunkReader);
                else
                    Spr.Version2 = ChunkReader.ReadInt16();

                Spr.FrameCount = ChunkReader.ReadInt32();
                MessageBox.Show("Framecount: " + Spr.FrameCount);
                Spr.PaletteID = ChunkReader.ReadInt32();
                Spr.OffsetTable = new int[Spr.FrameCount];

                for (int i = 0; i < Spr.FrameCount; i++ )
                    Spr.OffsetTable[i] = ChunkReader.ReadInt32();

                for (int i = 0; i < Spr.FrameCount; i++)
                {
                    SpriteFrame Frame = new SpriteFrame();
                    Frame.Width = ChunkReader.ReadInt16();
                    Frame.Height = ChunkReader.ReadInt16();
                    Frame.Flags = ChunkReader.ReadInt16();
                    Frame.Unknown = ChunkReader.ReadInt16();
                    Frame.PaletteID = ChunkReader.ReadInt16();
                    Frame.TransparentPixel = ChunkReader.ReadInt16();
                    Frame.XPos = ChunkReader.ReadInt16();
                    Frame.YPos = ChunkReader.ReadInt16();

                    BinaryWriter SpriteWriter = new BinaryWriter(Frame.SpriteData);

                    for (int l = 0; l <= Frame.Height; l++)
                    {
                        SpriteWriter.Write(ChunkReader.ReadBytes(2));
                        SpriteWriter.Write(ChunkReader.ReadBytes(Frame.Width));
                    }

                    SpriteWriter.Flush();
                    //SpriteWriter.Close();

                    Spr.AddFrame(Frame);

                    //Each SPR2 resource contains a reference to a PALT chunk/resource.
                    Spr.Pal = new Palette();
                    IffChunk TmpChunk = new IffChunk();

                    foreach (IffChunk C in m_Chunks)
                    {
                        if (C.TypeNum == Spr.PaletteID)
                        {
                            //Guess what? The typenumber of each chunk is
                            //NOT unique, so you have to check on type as
                            //well!
                            if (C.Type == "PALT")
                            {
                                TmpChunk = C;
                                break;
                            }
                        }
                    }

                    BinaryReader PaltReader = new BinaryReader(new MemoryStream(TmpChunk.Data));
                    Spr.Pal.AlwaysOne = PaltReader.ReadInt32();
                    Spr.Pal.Always256 = PaltReader.ReadInt32();
                    PaltReader.ReadBytes(8); //The PALT header has 8 additional bytes of 0.

                    Spr.Pal.RGBTable = new Palette.RGB[Spr.Pal.Always256];

                    for (int l = 0; l < Spr.Pal.Always256; l++)
                    {
                        Spr.Pal.RGBTable[l] = new Palette.RGB();
                        Spr.Pal.RGBTable[l].Red = PaltReader.ReadByte();
                        Spr.Pal.RGBTable[l].Green = PaltReader.ReadByte();
                        Spr.Pal.RGBTable[l].Blue = PaltReader.ReadByte();
                    }

                    PaltReader.Close();
                }

                for (int i = 0; i < Spr.FrameCount; i++)
                {
                    SpriteFrame Frame = Spr.GetFrame(i);

                    BinaryReader SpriteReader = new BinaryReader(new MemoryStream(Frame.SpriteData.ToArray()));
                    int X = 0, Y = 0;
                    bool Stop = false;

                    Bitmap BM = new Bitmap(Frame.Width, Frame.Height);
                    Color Transparent = Color.FromArgb(Spr.Pal.RGBTable[Frame.TransparentPixel].Red,
                        Spr.Pal.RGBTable[Frame.TransparentPixel].Green,
                        Spr.Pal.RGBTable[Frame.TransparentPixel].Blue);

                    Graphics Gfx = Graphics.FromImage(BM);

                    while (SpriteReader.BaseStream.Position < (SpriteReader.BaseStream.Length - 1) && !Stop)
                    {
                        byte Opcode = SpriteReader.ReadByte();
                        byte Data = SpriteReader.ReadByte();

                        switch (Opcode)
                        {
                            case 1: //Transparent pixels.
                                for (int Dat = 0; Dat < Data; Dat++)
                                {
                                    if(X < Frame.Width)
                                        BM.SetPixel(X, Y, Transparent);
                                    
                                    X++;
                                }

                                break;
                            case 2:
                                byte Col = SpriteReader.ReadByte();

                                for (int Dat = 0; Dat < Data; Dat++)
                                {
                                    if (X < Frame.Width)
                                        BM.SetPixel(X, Y, GetColorFromPalette(Col, Spr));
                                    
                                    X++;
                                }
                                break;
                            case 3: //Pixels.
                                byte Pixel = SpriteReader.ReadByte();

                                for (int Dat = 0; Dat < Data; Dat++)
                                {
                                    if (X < Frame.Width && Y < Frame.Height)
                                        BM.SetPixel(X, Y, GetColorFromPalette(Pixel, Spr));
                                    
                                    X++;
                                }

                                break;
                            case 4: //New line.
                                for (; X < BM.Width; X++)
                                    BM.SetPixel(X, Y, Transparent);

                                Y++; //Next line.
                                X = 0;
                                break;
                            case 5: //End of sprite.
                                Stop = true;
                                break;
                            case 9: //Transparent rows.
                                Gfx.FillRectangle(Brushes.Transparent, 
                                    new Rectangle(0, Y, BM.Width, (Y + (Data - 1))));
                                X = 0;
                                Y = Y + (Data - 1);
                                break;
                        }
                    }

                    Random Rnd = new Random(DateTime.Now.Millisecond);

                    BM.Save(Path + "\\" + i.ToString() + Rnd.Next() + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }
Exemplo n.º 3
0
 public void AddSprite(SPR2 Sprite)
 {
     m_Sprite = Sprite;
     m_Frame  = (SPR2Frame)m_Sprite.GetFrame(0); //Floors only have 1 frame.
 }