private void newToolStripMenuItem_Click(object sender, EventArgs e) { SpriteForm newMDI = new SpriteForm(); newMDI.MdiParent = this; newMDI.Show(); }
private void importRomToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Rom File (*.sfc)|*.sfc|All Files (*.*)|*.*"; ofd.Title = "Select a Rom File"; var result = ofd.ShowDialog(); if (result == DialogResult.OK) { var rom = new SpriteLibrary.Rom(ofd.FileName); var sprite = rom.GetSprite(); SpriteForm newMDI = new SpriteForm(ofd.FileName, sprite); newMDI.MdiParent = this; newMDI.Show(); UpdateCurrentSprite(newMDI.loadedSprite); } }
public void LoadFile(string fileName) { try { var spriteFile = File.ReadAllBytes(fileName); var loadedSprite = new SpriteLibrary.PlayerSprite(spriteFile); if (loadedSprite.Version == 0) { loadedSprite.DisplayText = Path.GetFileNameWithoutExtension(fileName); } SpriteForm newMDI = new SpriteForm(fileName, loadedSprite); newMDI.MdiParent = this; newMDI.Show(); UpdateCurrentSprite(newMDI.loadedSprite); } catch (Exception ex) { logger.Error(ex); MessageBox.Show($"Failed to load file {fileName}. Make sure it's a sprite file.", "Error"); } }
private void importPNGToolStripMenuItem_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Filter = "PNG File (*.png)|*.png|All Files (*.*)|*.*"; ofd.Title = "Select a PNG File"; var invalidPixelsFound = false; var result = ofd.ShowDialog(); if (result == DialogResult.OK) { Bitmap tempBitmap = (Bitmap)Image.FromFile(ofd.FileName); if (tempBitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb) { var clone = new Bitmap(tempBitmap.Width, tempBitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics gr = Graphics.FromImage(clone)) { gr.DrawImage(tempBitmap, new Rectangle(0, 0, tempBitmap.Width, tempBitmap.Height)); } tempBitmap = clone; } if (tempBitmap.Width != 128 || tempBitmap.Height != 448) { MessageBox.Show("Invalid PNG image. Must be 128 x 448 pixels", "Error"); return; } var sprite = new SpriteLibrary.PlayerSprite(); sprite.DisplayText = Path.GetFileNameWithoutExtension(ofd.FileName); var bitmapData = tempBitmap.LockBits(new Rectangle(0, 0, tempBitmap.Width, tempBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, tempBitmap.PixelFormat); var totalBytes = bitmapData.Stride * bitmapData.Height; var pixels = new byte[totalBytes]; Marshal.Copy(bitmapData.Scan0, pixels, 0, totalBytes); tempBitmap.UnlockBits(bitmapData); // load palette from png int xoffset = 120; int yoffset = 440; var colors = new Color[8 * 8]; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { int pixelPosition = (y + yoffset) * bitmapData.Stride + ((x + xoffset) * 4); int b = pixels[pixelPosition + 0]; int g = pixels[pixelPosition + 1]; int r = pixels[pixelPosition + 2]; int a = pixels[pixelPosition + 3]; colors[x + y * 8] = Color.FromArgb(a, r, g, b); } } var palette = new Color[62]; for (int i = 0; i < 15; i++) { palette[i] = colors[i + 1]; } for (int i = 15; i < 30; i++) { palette[i] = colors[i + 2]; } for (int i = 30; i < 45; i++) { palette[i] = colors[i + 3]; } for (int i = 45; i < 60; i++) { palette[i] = colors[i + 4]; } palette[60] = colors[16]; palette[61] = colors[32]; sprite.SetPalette(palette); // load pixel data var pixelBytes = new byte[128 * 448]; var greenPalette = new Dictionary <int, Color>(); for (int i = 0; i < 16; i++) { greenPalette[i] = colors[i]; } for (int y = 0; y < 448; y++) { for (int x = 0; x < 128; x++) { int pixelPosition = y * bitmapData.Stride + (x * 4); int b = pixels[pixelPosition + 0]; int g = pixels[pixelPosition + 1]; int r = pixels[pixelPosition + 2]; int a = pixels[pixelPosition + 3]; var pixel = Color.FromArgb(a, r, g, b); if (y >= 440 && x >= 120) { // palette data, skip it pixelBytes[x + y * 128] = 0; continue; } if (a == 0 || greenPalette[0] == pixel) { pixelBytes[x + y * 128] = 0; } else { var pixelValue = greenPalette.FirstOrDefault(c => c.Value == pixel); if (pixelValue.Key > 0) { pixelBytes[x + y * 128] = (byte)pixelValue.Key; } else { invalidPixelsFound = true; //logger.Debug($"Import PNG: Bad pixel value found. File: {ofd.FileName}, Pixel: [{x + y * 128}] {pixel.ToString()}"); } } } } List <byte[]> tiles = new List <byte[]>(); for (int yTile = 0; yTile < 56; yTile++) { for (int xTile = 0; xTile < 16; xTile++) { var currentTile = new byte[8 * 8]; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { currentTile[x + y * 8] = pixelBytes[(xTile * 8 + x) + (yTile * 8 + y) * 128]; } } tiles.Add(currentTile); } } byte[] pixels4bpp = new byte[0x7000]; int offset = 0; foreach (var tile in tiles) { var four = SpriteLibrary.Utilities.Tile8x8To4Bpp(tile); Array.Copy(four, 0, pixels4bpp, offset, 32); offset += 32; } sprite.Set4bppPixelData(pixels4bpp); if (invalidPixelsFound) { MessageBox.Show("PNG contains pixels that do not use the first 16 colors of the embedded palette. Pixels have been made transparent. Please verify your source file.", "Error"); } SpriteForm newMDI = new SpriteForm(ofd.FileName, sprite); newMDI.MdiParent = this; newMDI.Show(); UpdateCurrentSprite(newMDI.loadedSprite); } } }