Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.IO;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.Runtime.InteropServices;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void Form1_Load(object sender, EventArgs e)
  22.         {
  23.             int[] palette = new int[]{Color.Black.ToArgb(), Color.Red.ToArgb(), Color.Blue.ToArgb(), Color.Yellow.ToArgb()};
  24.             int[] pixels = new int[128 * 128];
  25.             var chrData = File.ReadAllBytes(@"C:\gangstreet\ascii.chr");
  26.  
  27.  
  28.             var nesTileDrawing = new NesTileDrawing(chrData, palette, pixels, 128);
  29.  
  30.             for (int y = 0; y < 16; y++)
  31.             {
  32.                 for (int x = 0; x < 16; x++)
  33.                 {
  34.                     nesTileDrawing.DrawTile(x * 8, y * 8, x + y * 16);
  35.                 }
  36.             }
  37.             Bitmap bmp = new Bitmap(128,128,  System.Drawing.Imaging.PixelFormat.Format32bppRgb);
  38.             var bits = bmp.LockBits(new Rectangle(0, 0, 128, 128), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
  39.             Marshal.Copy(pixels, 0, bits.Scan0, pixels.Length);
  40.             bmp.UnlockBits(bits);
  41.             this.BackgroundImage = bmp;
  42.             this.BackgroundImageLayout = ImageLayout.None;
  43.         }
  44.  
  45.         class NesTileDrawing
  46.         {
  47.             byte[] chrData;
  48.             int[] palette;
  49.             int[] pixels;
  50.             int imageWidth;
  51.  
  52.             public NesTileDrawing(byte[] chrData, int[] palette, int[] pixels, int imageWidth)
  53.             {
  54.                 this.chrData = chrData;
  55.                 this.palette = palette;
  56.                 this.pixels = pixels;
  57.                 this.imageWidth = imageWidth;
  58.             }
  59.  
  60.             public void DrawTile(int x0, int y0, int tileNumber)
  61.             {
  62.                 for (int y = 0; y < 8; y++)
  63.                 {
  64.                     int imageY = y0 + y;
  65.                     int mask = 0x80;
  66.                     byte b = chrData[tileNumber * 16 + y];
  67.                     byte b2 = chrData[tileNumber * 16 + 8 + y];
  68.                     int imageOffset = imageY * imageWidth;
  69.  
  70.                     for (int x = 0; x < 8; x++)
  71.                     {
  72.                         int imageX = x0 + x;
  73.                         int c = 0;
  74.                         if (0 != (mask & b)) c += 1;
  75.                         if (0 != (mask & b2)) c += 2;
  76.                         c = palette[c];
  77.                         pixels[imageOffset + imageX] = c;
  78.                         mask >>= 1;
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement