Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.IO;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- int[] palette = new int[]{Color.Black.ToArgb(), Color.Red.ToArgb(), Color.Blue.ToArgb(), Color.Yellow.ToArgb()};
- int[] pixels = new int[128 * 128];
- var chrData = File.ReadAllBytes(@"C:\gangstreet\ascii.chr");
- var nesTileDrawing = new NesTileDrawing(chrData, palette, pixels, 128);
- for (int y = 0; y < 16; y++)
- {
- for (int x = 0; x < 16; x++)
- {
- nesTileDrawing.DrawTile(x * 8, y * 8, x + y * 16);
- }
- }
- Bitmap bmp = new Bitmap(128,128, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
- var bits = bmp.LockBits(new Rectangle(0, 0, 128, 128), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
- Marshal.Copy(pixels, 0, bits.Scan0, pixels.Length);
- bmp.UnlockBits(bits);
- this.BackgroundImage = bmp;
- this.BackgroundImageLayout = ImageLayout.None;
- }
- class NesTileDrawing
- {
- byte[] chrData;
- int[] palette;
- int[] pixels;
- int imageWidth;
- public NesTileDrawing(byte[] chrData, int[] palette, int[] pixels, int imageWidth)
- {
- this.chrData = chrData;
- this.palette = palette;
- this.pixels = pixels;
- this.imageWidth = imageWidth;
- }
- public void DrawTile(int x0, int y0, int tileNumber)
- {
- for (int y = 0; y < 8; y++)
- {
- int imageY = y0 + y;
- int mask = 0x80;
- byte b = chrData[tileNumber * 16 + y];
- byte b2 = chrData[tileNumber * 16 + 8 + y];
- int imageOffset = imageY * imageWidth;
- for (int x = 0; x < 8; x++)
- {
- int imageX = x0 + x;
- int c = 0;
- if (0 != (mask & b)) c += 1;
- if (0 != (mask & b2)) c += 2;
- c = palette[c];
- pixels[imageOffset + imageX] = c;
- mask >>= 1;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement