/// <summary> Create a copy of a given <see cref="PalettedSprite"/> with the given <paramref name="palette"/> </summary> /// <param name="orig"> Orignal <see cref="PalettedSprite"/> to share palette indexes with </param> /// <param name="palette"> <see cref="Pixel"/>[] to use as this sprite's Palette. </param> public PalettedSprite(PalettedSprite orig, Pixel[] palette) { Width = orig.Width; Height = orig.Height; colors = orig.colors; this.palette = palette; }
/// <summary> Attempts to load a <see cref="PalettedSprite"/> from a `.pspr` format file </summary> /// <param name="path"> Path to file to load </param> /// <returns> Loaded sprite or null if load was unsuccessful. </returns> public static PalettedSprite LoadFromPSPR(string path) { PalettedSprite spr = null; try { using (Stream stream = File.OpenRead(path)) { using (BinaryReader reader = new BinaryReader(stream)) { if (((char)reader.ReadByte()) != 'P') { throw new InvalidDataException("Data file does not have PSPR header."); } if (((char)reader.ReadByte()) != 'S') { throw new InvalidDataException("Data file does not have PSPR header."); } if (((char)reader.ReadByte()) != 'P') { throw new InvalidDataException("Data file does not have PSPR header."); } if (((char)reader.ReadByte()) != 'R') { throw new InvalidDataException("Data file does not have PSPR header."); } int w = reader.ReadInt32(); int h = reader.ReadInt32(); int ncol = Mathf.Min(255, reader.ReadInt32()); Pixel[] placeholderPalette = new Pixel[ncol + 1]; spr = new PalettedSprite(w, h, placeholderPalette); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { spr.SetIndex(x, y, reader.ReadByte()); } } } } } catch (Exception e) { Console.WriteLine($"Error Loading PSPR {e}"); spr = null; } return(spr); }