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.Drawing.Imaging;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- namespace WindowsFormsApplication1
- {
- struct MetaTile
- {
- public byte topLeft;
- public byte topRight;
- public byte bottomLeft;
- public byte bottomRight;
- }
- public partial class Form1 : Form
- {
- int CurrentTile;
- Bitmap entire = new Bitmap(127, 127);
- Bitmap map16 = new Bitmap(169,169);
- BinaryReader file = new BinaryReader(File.Open("test.bin", FileMode.Open));
- Color[] palDat = new Color[64];
- Bitmap chr = new Bitmap(8, 2048, PixelFormat.Format32bppRgb);
- List<MetaTile> metatiles = new List<MetaTile>();
- byte[] ROM;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- palDat[0] = Color.Black;
- palDat[1] = Color.Wheat;
- palDat[2] = Color.Orange;
- palDat[3] = Color.Black;
- ROM = file.ReadBytes((int)file.BaseStream.Length);
- // pictureBox1.Image = LoadWholeChrPage(0, ROM, palDat);
- MetaTile test= new MetaTile();
- test.topLeft = 0x53;
- test.topRight = 0x54;
- test.bottomLeft = 0x55;
- test.bottomRight = 0x56;
- metatiles.Add(test);
- for (int x = 0; x < 50; x += 16)
- {
- for (int y = 0; y < 50; y += 16)
- {
- DrawMetatile(metatiles[0], x, y, Graphics.FromImage(map16));
- }
- }
- pictureBox2.Image = map16;
- }
- Bitmap LoadWholeChrPage(int chrOffset, byte[] _theROM, Color[] _thePalette)
- {
- for(int i = 0; i < 256; i++) {
- int tileOffset = chrOffset + i * 16;
- Load_8x8(i, _theROM, tileOffset, chr, _thePalette);
- }
- return chr;
- }
- Bitmap Load_8x8(int tileNum, byte[] rom, int tileOffset, Bitmap canvas, Color[] palDat)
- {
- byte plane0;
- byte plane1;
- byte final;
- for (int y = 0; y < 8; y++)
- {
- for (int x = 0; x < 8; x++)
- {
- plane0 = Convert.ToByte(((rom[tileOffset + y]) & (0x80 >> x)) >> (7 - x));
- plane1 = Convert.ToByte(((rom[tileOffset + y + 8]) & (0x80 >> x)) >> (7 - x));
- final = Convert.ToByte((plane1 << 1) + plane0);
- canvas.SetPixel(x, y + tileNum * 8, palDat[final]);
- }
- }
- return canvas;
- }
- private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
- {
- // Note: eight tiles per row in the tileset.
- CurrentTile = ((e.Y / 8) * 1) + (e.X / 8);
- label1.Text = "CurrentTile: " + CurrentTile.ToString();
- }
- void DrawTile(Graphics target, byte tileIndex, int x, int y)
- {
- // Determine where we will grab the tile from, assuming tiles are stored in a vertical strip
- var sourceRect = new Rectangle(0, tileIndex * 8, 8, 8);
- // Determine where it will be drawn
- var destRect = new Rectangle(x, y, 8, 8);
- // Draw it
- target.DrawImage(LoadWholeChrPage(0, ROM, palDat), destRect, sourceRect, GraphicsUnit.Pixel);
- }
- void DrawMetatile(MetaTile mt, int x, int y, Graphics target)
- {
- DrawTile(target,mt.topLeft, x, y);
- DrawTile(target,mt.topRight, x+8, y);
- DrawTile(target,mt.bottomLeft, x, y+8);
- DrawTile(target,mt.bottomRight, x+8, y+8);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement