public GameBoyPrinterEmulatorWindow(IControllerReader reader) { if (Properties.Settings.Default.UpgradeRequired) { Properties.Settings.Default.Upgrade(); Properties.Settings.Default.UpgradeRequired = false; Properties.Settings.Default.Save(); } InitializeComponent(); DataContext = this; _reader = reader ?? throw new ArgumentNullException(nameof(reader)); SelectedPalette = Properties.Settings.Default.SelectedPalette; PrintSize = Properties.Settings.Default.PrintSize; using (Bitmap bmp = new Bitmap(Properties.Resources.PrintImage)) { _imageBuffer = new BitmapPixelMaker(480, 432); _imageBuffer.SetColor(palettes[SelectedPalette][0][3], palettes[SelectedPalette][1][3], palettes[SelectedPalette][2][3]); for (int i = 0; i < bmp.Width; ++i) { for (int j = 0; j < bmp.Height; ++j) { System.Drawing.Color pixel = bmp.GetPixel(i, j); if (pixel.R == 255 && pixel.G == 255 && pixel.B == 255) { _imageBuffer.SetRed(i, j, palettes[SelectedPalette][0][0]); _imageBuffer.SetGreen(i, j, palettes[SelectedPalette][1][0]); _imageBuffer.SetBlue(i, j, palettes[SelectedPalette][2][0]); } } } WriteableBitmap wbitmap = _imageBuffer.MakeBitmap(96, 96); // Create an Image to display the bitmap. _image = new System.Windows.Controls.Image { Stretch = Stretch.None, Margin = new Thickness(0) }; _ = GameBoyPrinterEmulatorWindowGrid.Children.Add(_image); _image.Source = wbitmap; _reader.ControllerStateChanged += Reader_ControllerStateChanged; _reader.ControllerDisconnected += Reader_ControllerDisconnected; CheckPalette(SelectedPalette); CheckSize(PrintSize); } }
private void Palette_Click(object sender, EventArgs e) { int newPalette = 0; if (sender == Palette_DMG) { newPalette = 1; } else if (sender == Palette_GBPocket) { newPalette = 2; } else if (sender == Palette_GBCUS) { newPalette = 3; } else if (sender == Palette_GBCJP) { newPalette = 4; } else if (sender == Palette_BGB) { newPalette = 5; } else if (sender == Palette_GKGray) { newPalette = 6; } else if (sender == Palette_GKGreen) { newPalette = 7; } else if (sender == Palette_BZ) { newPalette = 8; } CheckPalette(newPalette); for (int i = 0; i < 4; ++i) { _imageBuffer.ReplaceColor(new Pixel(palettes[SelectedPalette][0][i], palettes[SelectedPalette][1][i], palettes[SelectedPalette][2][i], 255), new Pixel(palettes[newPalette][0][i], palettes[newPalette][1][i], palettes[newPalette][2][i], 255)); } WriteableBitmap wbitmap = _imageBuffer.MakeBitmap(96, 96); _image.Source = wbitmap; SelectedPalette = newPalette; Properties.Settings.Default.SelectedPalette = SelectedPalette; }
private void DisplayError() { SelectedPalette = Properties.Settings.Default.SelectedPalette; PrintSize = Properties.Settings.Default.PrintSize; using (Bitmap bmp = new Bitmap(Properties.Resources.ErrorImage)) { _imageBuffer = new BitmapPixelMaker(480, 432); _imageBuffer.SetColor(palettes[SelectedPalette][0][3], palettes[SelectedPalette][1][3], palettes[SelectedPalette][2][3]); for (int i = 0; i < bmp.Width; ++i) { for (int j = 0; j < bmp.Height; ++j) { System.Drawing.Color pixel = bmp.GetPixel(i, j); if (pixel.R == 255 && pixel.G == 255 && pixel.B == 255) { _imageBuffer.SetRed(i, j, palettes[SelectedPalette][0][0]); _imageBuffer.SetGreen(i, j, palettes[SelectedPalette][1][0]); _imageBuffer.SetBlue(i, j, palettes[SelectedPalette][2][0]); } } } // imageBuffer.SetColor(0, 0, 0); // Convert the pixel data into a WriteableBitmap. WriteableBitmap wbitmap = _imageBuffer.MakeBitmap(96, 96); // Set the Image source. _image.Source = wbitmap; GameBoyPrinterEmulatorWindowGrid.Height = 432; GameBoyPrinterEmulatorWindowGrid.Width = 480; Height = 432; Width = 480; } }
private void Reader_ControllerStateChanged(object reader, ControllerStateEventArgs e) { _imageBuffer.SetColor(0, 0, 0, 255); int square_width = PrintSize;// 480 / (TILE_PIXEL_WIDTH * TILES_PER_LINE); int square_height = square_width; string[] tiles_rawBytes_array = e.RawPrinterData.Split('\n'); int total_tile_count = 0; for (int tile_i = 0; tile_i < tiles_rawBytes_array.Length; tile_i++) { string tile_element = tiles_rawBytes_array[tile_i]; // Check for invalid raw lines if (tile_element.Length == 0) { // Skip lines with no bytes (can happen with .split() ) continue; } else if (tile_element.StartsWith("!", StringComparison.Ordinal)) { // Skip lines used for comments continue; } else if (tile_element.StartsWith("#", StringComparison.Ordinal)) { // Skip lines used for comments continue; } else if (tile_element.StartsWith("//", StringComparison.Ordinal)) { // Skip lines used for comments continue; } else if (tile_element.StartsWith("{", StringComparison.Ordinal)) { // Skip lines used for comments continue; } total_tile_count++; } int tile_height_count = total_tile_count / TILES_PER_LINE; if (tile_height_count == 0) { DisplayError(); return; } _imageBuffer = new BitmapPixelMaker(square_width * TILE_PIXEL_WIDTH * TILES_PER_LINE, square_height * TILE_PIXEL_HEIGHT * tile_height_count); _image.Height = square_height * TILE_PIXEL_HEIGHT * tile_height_count; _image.Width = square_width * TILE_PIXEL_WIDTH * TILES_PER_LINE; GameBoyPrinterEmulatorWindowGrid.Height = square_height * TILE_PIXEL_HEIGHT * tile_height_count;; GameBoyPrinterEmulatorWindowGrid.Width = square_width * TILE_PIXEL_WIDTH * TILES_PER_LINE; Height = square_height * TILE_PIXEL_HEIGHT * tile_height_count; Width = square_width * TILE_PIXEL_WIDTH * TILES_PER_LINE; int tile_count = 0; for (int tile_i = 0; tile_i < tiles_rawBytes_array.Length; tile_i++) { string tile_element = tiles_rawBytes_array[tile_i]; // Check for invalid raw lines if (tile_element.Length == 0) { // Skip lines with no bytes (can happen with .split() ) continue; } else if (tile_element.StartsWith("!", StringComparison.Ordinal)) { // Skip lines used for comments continue; } else if (tile_element.StartsWith("#", StringComparison.Ordinal)) { // Skip lines used for comments continue; } else if (tile_element.StartsWith("//", StringComparison.Ordinal)) { // Skip lines used for comments continue; } else if (tile_element.StartsWith("{", StringComparison.Ordinal)) { // Skip lines used for comments continue; } // Gameboy Tile Offset int tile_x_offset = tile_count % TILES_PER_LINE; int tile_y_offset = tile_count / TILES_PER_LINE; byte[] pixels = Decode(tile_element); if (pixels != null) { Paint(_imageBuffer, pixels, square_width, square_height, tile_x_offset, tile_y_offset); } else { //status = false; } // Increment Tile Count Tracker tile_count++; } //imageBuffer.SetColor(0, 0, 0); // Convert the pixel data into a WriteableBitmap. WriteableBitmap wbitmap = _imageBuffer.MakeBitmap(96, 96); // Set the Image source. _image.Source = wbitmap; }