/// <summary> /// Gets the sprite texture for an event /// </summary> /// <param name="context">The context</param> /// <param name="imgBuffer">The image buffer, if available</param> /// <param name="s">The image descriptor to use</param> /// <returns>The texture</returns> public override Texture2D GetSpriteTexture(Context context, byte[] imgBuffer, R1_ImageDescriptor s) { if (s.ImageType != 2 && s.ImageType != 3) { return(null); } // Ignore dummy sprites if (s.IsDummySprite()) { return(null); } // Get the image properties var width = s.Width; var height = s.Height; var offset = s.ImageBufferOffset; var pal4 = FileFactory.Read <ObjectArray <RGBA5551Color> >(GetPalettePath(context.Settings, 4), context, (y, x) => x.Length = 256); var pal8 = FileFactory.Read <ObjectArray <RGBA5551Color> >(GetPalettePath(context.Settings, 8), context, (y, x) => x.Length = 256); // Select correct palette var palette = s.ImageType == 3 ? pal8.Value : pal4.Value; var paletteOffset = 16 * s.Unknown1; // Create the texture Texture2D tex = TextureHelpers.CreateTexture2D(width, height); // Set every pixel if (s.ImageType == 3) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { var paletteIndex = imgBuffer[offset + width * y + x]; // Set the pixel tex.SetPixel(x, height - 1 - y, palette[paletteIndex].GetColor()); } } } else if (s.ImageType == 2) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int actualX = (s.ImageOffsetInPageX + x) / 2; var paletteIndex = imgBuffer[offset + (width * y + x) / 2]; if (x % 2 == 0) { paletteIndex = (byte)BitHelpers.ExtractBits(paletteIndex, 4, 0); } else { paletteIndex = (byte)BitHelpers.ExtractBits(paletteIndex, 4, 4); } // Set the pixel tex.SetPixel(x, height - 1 - y, palette[paletteOffset + paletteIndex].GetColor()); } } } // Apply the changes tex.Apply(); // Return the texture return(tex); }