void WriteFromWaveStream(string inputPath, string outputPath, WaveStream reader) { var waveFormat = reader.WaveFormat; int sampleRate = Math.Min(16000, waveFormat.SampleRate); using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader)) { WaveFormat newFormat = new WaveFormat(sampleRate, 8, 1); using (WaveStream eightBitPcmStream = new WaveFormatConversionStream(newFormat, pcmStream)) { CppWriter cppWriter = new CppWriter(Path.GetFileName(Path.GetFileNameWithoutExtension(inputPath)), outputPath); cppWriter.Write(sampleRate); List <byte> totalBytes = new List <byte>(); int b = eightBitPcmStream.ReadByte(); while (b != -1) { totalBytes.Add((byte)(b + 128)); // Make it signed 8-bit PCM, as this comes through as unsigned b = eightBitPcmStream.ReadByte(); } cppWriter.Write(totalBytes.Count); foreach (byte data in totalBytes) { cppWriter.Write(data); } cppWriter.Finalise(); Console.WriteLine("Audio file \"" + Path.GetFullPath(outputPath) + "\" successfully converted"); } } }
void WriteHeader(CppWriter writer, UVs[] sliceCoordinates, Color[] palette, int dataLength, Compression.CompressionType compressionType, uint bpp) { UInt32 compressionTypeSize = Compression.ToGBACompressionHeader(compressionType, bpp); writer.Write((UInt32)sliceCoordinates.Length); writer.Write((byte)palette.Length); writer.Write((UInt32)dataLength); writer.Write((UInt32)compressionTypeSize); }
void WritePalette(CppWriter writer, Color[] palette) { for (int i = 0; i < palette.Length; ++i) { Color colour = palette[i]; UInt16 rbgColor = PaletteHelper.ToRgb16(colour); writer.Write(rbgColor); } }
public void Convert(string inputPath, string outputPath, IList <Event> events) { CppWriter cppWriter = new CppWriter(Path.GetFileName(Path.GetFileNameWithoutExtension(inputPath)), outputPath); cppWriter.Write((uint)events.Count); foreach (var e in events) { cppWriter.Write(e.m_deltaTick); cppWriter.Write((ushort)((uint)e.m_properties.id | ((uint)e.m_properties.dataIndex << 2))); foreach (var eventData in e.m_properties.data) { cppWriter.Write(eventData.data); } } Console.WriteLine("Dmg Midi \"" + System.IO.Path.GetFullPath(outputPath) + "\" successfully converted"); cppWriter.Finalise(); }
/// <param name="inputPath">Input file path, namespace name is derived from this</param> /// <param name="outputPath">Output cpp file</param> /// <param name="bitmaps">List of image files to map tilemaps from</param> /// <param name="paletteBankIndexOffset">Allows us to force things like UI palettes into the back of the palette bank</param> public void Convert(string inputPath, string outputPath, IList <Bitmap> bitmaps, byte paletteBankIndexOffset = 0) { CppWriter cppWriter = new CppWriter(Path.GetFileName(Path.GetFileNameWithoutExtension(inputPath)), outputPath); Console.WriteLine("Processing master palette"); Color[] masterPalette; Tile[] masterTileSet; TileMap[] tileMapList; GenerateTileMaps(bitmaps, paletteBankIndexOffset, out masterPalette, out masterTileSet, out tileMapList); GBAMapData gbaMapData = GenerateMapData(tileMapList, paletteBankIndexOffset); const int maxMaps = sizeof(byte) * 8; if (gbaMapData.mapIsDynamic.Count >= maxMaps) { // Can't store this in the current bitmask size throw new Exception(string.Format("Too many tilemaps found. Maps = {0}, max is {1}. Consider increasing the bitmask of \"mapIsDynamicBitMask\".", gbaMapData.mapIsDynamic.Count, maxMaps)); } Console.WriteLine("Total unique tiles = " + masterTileSet.Length); Console.WriteLine("Processing tilemaps"); Compression.CompressionType compressionType = Compression.CompressionType.BitPacked; uint destBpp = CalculateDestBitsPerPixel(masterTileSet); List <UInt32> tileSetData = GenerateTileSetData(masterTileSet, compressionType, destBpp); // Write palette { cppWriter.Write(paletteBankIndexOffset); cppWriter.Write((byte)masterPalette.Length); for (int i = 0; i < masterPalette.Length; ++i) { Color colour = masterPalette[i]; UInt16 rbgColor = PaletteHelper.ToRgb16(colour); cppWriter.Write(rbgColor); } } // Write tileset { // Compression flags UInt32 compressionTypeSize = Compression.ToGBACompressionHeader(compressionType, destBpp); cppWriter.Write(compressionTypeSize); // Actual data cppWriter.Write((UInt32)tileSetData.Count); for (int i = 0; i < tileSetData.Count; ++i) { cppWriter.Write(tileSetData[i]); } } // Write tile maps { cppWriter.Write((byte)tileMapList.Length); cppWriter.Write((UInt32)gbaMapData.screenEntries.Length); byte mapIsDynamicBitMask = BoolListToBitmaskU8(gbaMapData.mapIsDynamic); cppWriter.Write(mapIsDynamicBitMask); // Width and height arrays foreach (int width in gbaMapData.widthLists) { cppWriter.Write((byte)width); } foreach (int height in gbaMapData.heightLists) { cppWriter.Write((byte)height); } foreach (var mapEntry in gbaMapData.screenEntries) { cppWriter.Write(mapEntry.m_data); } } Console.WriteLine("Tilemap \"" + outputPath + "\" successfully converted"); cppWriter.Finalise(); }
public void Convert(string inputPath, string outputPath, Bitmap bitmap, UVs[] sliceCoordinates) { CppWriter cppWriter = new CppWriter(Path.GetFileName(Path.GetFileNameWithoutExtension(inputPath)), outputPath); Size size = bitmap.Size; if (size.Width % TileConfig.TileWidth != 0 || size.Height % TileConfig.TileHeight != 0) { throw new Exception("Size not compatible with GBA tiles. Width and height of pixels must be multiples of 8."); } Console.WriteLine("Processing colour palette"); int xStart = 0; int yStart = 0; int width = bitmap.Width; int height = bitmap.Height; Color[] preProcessedPalette = PaletteHelper.GeneratePaletteFromImage(bitmap, xStart, yStart, width, height); // Validate pallet length { if (preProcessedPalette.Length > 256) { throw new Exception("Palette length out of range for the GBA"); } // Todo, currently not supported if (preProcessedPalette.Length > 16) { throw new Exception("Palette length out of range for 4bbp format"); } } Console.WriteLine(String.Format("Palette length: {0}", preProcessedPalette.Length)); Compression.CompressionType compressionType = Compression.CompressionType.BitPacked; uint maxColours = MathX.IsPowerOf2((uint)preProcessedPalette.Length) ? (uint)preProcessedPalette.Length : MathX.NextPowerOf2((uint)preProcessedPalette.Length); uint destBpp = (uint)MathX.IndexOfHighestSetBit(maxColours); if (!MathX.IsPowerOf2(destBpp)) { destBpp = MathX.NextPowerOf2(destBpp); } Console.WriteLine(string.Format("Compression type = {0}", compressionType.ToString())); Console.WriteLine(string.Format("Destination Bit Depth = {0}", destBpp)); List <int> dataOffsets = new List <int>(); List <List <UInt32> > spriteData = new List <List <UInt32> >(); dataOffsets.Add(0); int totalData = 0; // Collect data and add offsets { Console.WriteLine("Processing sprite data"); for (int i = 0; i < sliceCoordinates.Length; ++i) { StringBuilder dataSb = new StringBuilder(); UVs slice = sliceCoordinates[i]; int spriteWidth = slice.width, spriteHeight = slice.height; List <UInt32> data = WriteSpriteData(bitmap, preProcessedPalette, spriteWidth, spriteHeight, slice.x, slice.y, compressionType, destBpp); spriteData.Add(data); // Add offsets if (i < sliceCoordinates.Length - 1) { dataOffsets.Add(dataOffsets[i] + data.Count); } totalData += data.Count; } } WriteHeader(cppWriter, sliceCoordinates, preProcessedPalette, totalData, compressionType, destBpp); WritePalette(cppWriter, preProcessedPalette); // Write width { for (int i = 0; i < sliceCoordinates.Length; ++i) { int spriteWidth = sliceCoordinates[i].width; cppWriter.Write((byte)spriteWidth); } } // Write height { for (int i = 0; i < sliceCoordinates.Length; ++i) { int spriteHeight = sliceCoordinates[i].height; cppWriter.Write((byte)spriteHeight); } } // Write offsets { for (int i = 0; i < dataOffsets.Count; i++) { cppWriter.Write((UInt32)dataOffsets[i]); } } // Write data { foreach (var dataList in spriteData) { foreach (UInt32 data in dataList) { cppWriter.Write((UInt32)data); } } } Console.WriteLine("Sprite \"" + outputPath + "\" successfully converted"); cppWriter.Finalise(); }