Advertisement
Guest User

woe is me

a guest
Nov 22nd, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.93 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.Drawing.Imaging;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     struct MetaTile
  15.     {
  16.         public byte topLeft;
  17.         public byte topRight;
  18.         public byte bottomLeft;
  19.         public byte bottomRight;
  20.     }
  21.  
  22.     public partial class Form1 : Form
  23.     {
  24.         int CurrentTile;
  25.         Bitmap entire = new Bitmap(127, 127);
  26.         Bitmap map16 = new Bitmap(169,169);
  27.         BinaryReader file = new BinaryReader(File.Open("test.bin", FileMode.Open));
  28.         Color[] palDat = new Color[64];
  29.         Bitmap chr = new Bitmap(8, 2048, PixelFormat.Format32bppRgb);
  30.         List<MetaTile> metatiles = new List<MetaTile>();
  31.         byte[] ROM;
  32.  
  33.         public Form1()
  34.         {
  35.             InitializeComponent();
  36.         }
  37.  
  38.  
  39.         private void Form1_Load(object sender, EventArgs e)
  40.         {
  41.             palDat[0] = Color.Black;
  42.             palDat[1] = Color.Wheat;
  43.             palDat[2] = Color.Orange;
  44.             palDat[3] = Color.Black;
  45.             ROM = file.ReadBytes((int)file.BaseStream.Length);
  46.            // pictureBox1.Image = LoadWholeChrPage(0, ROM, palDat);
  47.  
  48.  
  49.             MetaTile test= new MetaTile();
  50.             test.topLeft = 0x53;
  51.             test.topRight = 0x54;
  52.             test.bottomLeft = 0x55;
  53.             test.bottomRight = 0x56;
  54.  
  55.             metatiles.Add(test);
  56.  
  57.            for (int x = 0; x < 50; x += 16)
  58.             {
  59.                 for (int y = 0; y < 50; y += 16)
  60.                 {
  61.                     DrawMetatile(metatiles[0], x, y, Graphics.FromImage(map16));
  62.                }
  63.             }
  64.             pictureBox2.Image = map16;
  65.         }
  66.  
  67.         Bitmap LoadWholeChrPage(int chrOffset, byte[] _theROM, Color[] _thePalette)
  68.         {
  69.  
  70.     for(int i = 0; i < 256; i++) {
  71.         int tileOffset = chrOffset + i * 16;
  72.         Load_8x8(i, _theROM, tileOffset, chr, _thePalette);
  73.             }
  74.     return chr;
  75.          }
  76.  
  77.  
  78.         Bitmap Load_8x8(int tileNum, byte[] rom, int tileOffset, Bitmap canvas, Color[] palDat)
  79.         {
  80.             byte plane0;
  81.             byte plane1;
  82.             byte final;
  83.  
  84.             for (int y = 0; y < 8; y++)
  85.             {
  86.                 for (int x = 0; x < 8; x++)
  87.             {
  88.                     plane0 = Convert.ToByte(((rom[tileOffset + y]) & (0x80 >> x)) >> (7 - x));
  89.                     plane1 = Convert.ToByte(((rom[tileOffset + y + 8]) & (0x80 >> x)) >> (7 - x));
  90.                     final = Convert.ToByte((plane1 << 1) + plane0);
  91.                     canvas.SetPixel(x, y + tileNum * 8, palDat[final]);
  92.                 }
  93.             }
  94.             return canvas;
  95.         }
  96.  
  97.         private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
  98.         {      
  99.                     // Note: eight tiles per row in the tileset.           
  100.                     CurrentTile = ((e.Y / 8) * 1) + (e.X / 8);     
  101.                     label1.Text = "CurrentTile: " + CurrentTile.ToString();
  102.         }
  103.  
  104.  
  105.  
  106.         void DrawTile(Graphics target, byte tileIndex, int x, int y)
  107.         {
  108.             // Determine where we will grab the tile from, assuming tiles are stored in a vertical strip
  109.             var sourceRect = new Rectangle(0, tileIndex * 8, 8, 8);
  110.  
  111.             // Determine where it will be drawn
  112.             var destRect = new Rectangle(x, y, 8, 8);
  113.  
  114.             // Draw it
  115.             target.DrawImage(LoadWholeChrPage(0, ROM, palDat), destRect, sourceRect, GraphicsUnit.Pixel);
  116.         }
  117.  
  118.         void DrawMetatile(MetaTile mt, int x, int y, Graphics target)
  119.         {
  120.             DrawTile(target,mt.topLeft, x, y);
  121.             DrawTile(target,mt.topRight, x+8, y);
  122.             DrawTile(target,mt.bottomLeft, x, y+8);
  123.             DrawTile(target,mt.bottomRight, x+8, y+8);
  124.         }
  125.  
  126.  
  127.         }
  128.  
  129.  
  130.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement