public Level(Reader reader) { Title = reader.ReadRSDKString(); Console.WriteLine(Title); byte[] buffer = new byte[5]; reader.Read(displayBytes, 0, 5); //Waste 5 bytes, I don't care about them right now. //The first 4 bytes are loaded into StageSystem.ActiveTileLayers. 5th byte is tLayerMidPoint. //If you want to know the values then look at the values for "DisplayBytes" reader.Read(buffer, 0, 2); //Read size width = 0; height = 0; // Map width in 128 pixel units // In RSDKv2, it's one byte long width = buffer[0]; height = buffer[1]; MapLayout = new ushort[height][]; for (int i = 0; i < height; i++) { MapLayout[i] = new ushort[width]; } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // 128x128 Block number is 16-bit // Big-Endian in RSDKv2 and RSDKv3 reader.Read(buffer, 0, 2); //Read size MapLayout[y][x] = (ushort)(buffer[1] + (buffer[0] << 8)); } } // Read number of object types, Only RSDKv2 and RSDKv3 support this feature int ObjTypeCount = reader.ReadByte(); for (int n = 0; n < ObjTypeCount; n++) { string name = reader.ReadRSDKString(); objectTypeNames.Add(name); Console.WriteLine(name); } // Read object data int ObjCount = 0; // 2 bytes, big-endian, unsigned ObjCount = reader.ReadByte() << 8; ObjCount |= reader.ReadByte(); int obj_type = 0; int obj_subtype = 0; int obj_xPos = 0; int obj_yPos = 0; for (int n = 0; n < ObjCount; n++) { // Object type, 1 byte, unsigned obj_type = reader.ReadByte(); // Object subtype, 1 byte, unsigned obj_subtype = reader.ReadByte(); // X Position, 2 bytes, big-endian, signed obj_xPos = reader.ReadSByte() << 8; obj_xPos |= reader.ReadByte(); // Y Position, 2 bytes, big-endian, signed obj_yPos = reader.ReadSByte() << 8; obj_yPos |= reader.ReadByte(); // Add object objects.Add(new Object(obj_type, obj_subtype, obj_xPos, obj_yPos)); } reader.Close(); }
public GraphicsImage(Reader reader, bool dcGFX = false) { if (dcGFX) { reader.ReadByte(); } width = (ushort)(reader.ReadByte() << 8); width |= reader.ReadByte(); height = (ushort)(reader.ReadByte() << 8); height |= reader.ReadByte(); // Create image gfxImage = new Bitmap(width, height, PixelFormat.Format8bppIndexed); ColorPalette cp = gfxImage.Palette; // Read & Process palette for (int i = 0; i < 255; i++) { GFXpal[i].R = reader.ReadByte(); GFXpal[i].G = reader.ReadByte(); GFXpal[i].B = reader.ReadByte(); cp.Entries[i] = Color.FromArgb(255, GFXpal[i].R, GFXpal[i].G, GFXpal[i].B); } gfxImage.Palette = cp; //Read Image Data byte[] buf = new byte[3]; bool finished = false; int cnt = 0; int loop = 0; data = new byte[(width * height) + 1]; while (!finished) { buf[0] = reader.ReadByte(); if (buf[0] == 255) { buf[1] = reader.ReadByte(); if (buf[1] == 255) { finished = true; break; } else { buf[2] = reader.ReadByte(); loop = 0; // Repeat value needs to decreased by one to decode // the graphics from the Dreamcast demo if (dcGFX) { buf[2]--; } while (loop < buf[2] && !reader.IsEof) { data[cnt++] = buf[1]; loop++; } } } else { data[cnt++] = buf[0]; } } //Console.Write("file Length = " + reader.BaseStream.Length + " file pos = " + reader.Pos + " data remaining = " + (reader.BaseStream.Length - reader.Pos)); // Write data to image int pixel = 0; for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { BitmapData ImgData = gfxImage.LockBits(new Rectangle(new Point(w, h), new Size(1, 1)), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); byte b = System.Runtime.InteropServices.Marshal.ReadByte(ImgData.Scan0); System.Runtime.InteropServices.Marshal.WriteByte(ImgData.Scan0, (data[pixel])); gfxImage.UnlockBits(ImgData); pixel++; } } reader.Close(); }
public void read(Reader reader, bool skipHeader = false, int clrCnt = 0x80) { #region Header if (!skipHeader) { reader.seek(6, System.IO.SeekOrigin.Begin); // GIF89a width = reader.ReadByte(); width |= (ushort)(reader.ReadByte() << 8); height = reader.ReadByte(); height |= (ushort)(reader.ReadByte() << 8); byte info = reader.ReadByte(); // Palette Size clrCnt = (info & 0x7) + 1; if (clrCnt > 0) { clrCnt = 1 << clrCnt; } reader.ReadByte(); // background colour index reader.ReadByte(); // unused if (clrCnt != 0x100) { throw new Exception("RSDK-Formatted Gif files must use 256 colours!"); } } for (int c = 0; c < clrCnt; ++c) { palette[c].R = reader.ReadByte(); palette[c].G = reader.ReadByte(); palette[c].B = reader.ReadByte(); } #endregion #region Blocks byte blockType = reader.ReadByte(); while (blockType != 0 && blockType != ';') { switch (blockType) { default: // Unknown Console.WriteLine($"Unknown Block Type ({blockType})"); break; case (byte)'!': // Extension { byte extensionType = reader.ReadByte(); switch (extensionType) { case 0xF9: // Graphics Control Extension { int blockSize = reader.ReadByte(); byte disposalFlag = reader.ReadByte(); ushort frameDelay = reader.ReadUInt16(); byte transparentIndex = reader.ReadByte(); reader.ReadByte(); // terminator } break; case 0x01: // Plain Text Extension case 0xFE: // Comment Extension case 0xFF: // Application Extension { int blockSize = reader.ReadByte(); while (blockSize != 0) { // Read block reader.BaseStream.Position += blockSize; blockSize = reader.ReadByte(); // next block Size, if its 0 we know its the end of block } } break; default: // Unknown Console.WriteLine($"Unknown Extension Type ({extensionType})"); return; } } break; case (byte)',': // Image descriptor { int left = reader.ReadUInt16(); int top = reader.ReadUInt16(); int right = reader.ReadUInt16(); int bottom = reader.ReadUInt16(); byte info2 = reader.ReadByte(); bool interlaced = (info2 & 0x40) != 0; if (info2 >> 7 == 1) { for (int c = 0x80; c < 0x100; ++c) { palette[c].R = reader.ReadByte(); palette[c].G = reader.ReadByte(); palette[c].B = reader.ReadByte(); } } readPictureData(width, height, interlaced, reader); } break; } blockType = reader.ReadByte(); } #endregion if (!skipHeader) { reader.Close(); } }
public Scene(Reader reader) { Title = reader.ReadRSDKString(); //Console.WriteLine(Title); byte[] buffer = new byte[5]; ActiveLayer0 = reader.ReadByte(); ActiveLayer1 = reader.ReadByte(); ActiveLayer2 = reader.ReadByte(); ActiveLayer3 = reader.ReadByte(); Midpoint = reader.ReadByte(); reader.Read(buffer, 0, 2); //Read size width = 0; height = 0; // Map width in 128 pixel units // In RSDKv2, it's one byte long width = buffer[0]; height = buffer[1]; MapLayout = new ushort[height][]; for (int i = 0; i < height; i++) { MapLayout[i] = new ushort[width]; } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // 128x128 Block number is 16-bit // Big-Endian in RSDKv2 and RSDKv3 reader.Read(buffer, 0, 2); //Read size MapLayout[y][x] = (ushort)(buffer[1] + (buffer[0] << 8)); } } // Read number of object types, Only RSDKv1 and RSDKv2 support this feature int ObjTypeCount = reader.ReadByte(); for (int n = 0; n < ObjTypeCount; n++) { string name = reader.ReadRSDKString(); objectTypeNames.Add(name); } // Read object data int ObjCount = 0; // 2 bytes, big-endian, unsigned ObjCount = reader.ReadByte() << 8; ObjCount |= reader.ReadByte(); Object.cur_id = 0; for (int n = 0; n < ObjCount; n++) { // Add object objects.Add(new Object(reader)); } reader.Close(); }