Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Drawing;
- namespace MyFirstConsole
- {
- class Program
- {
- public static void Main(string[] args)
- {
- // Sets where to start reading byte array
- int offset = 0;
- // Variables used for write to file settings
- int rowCount = 0;
- // Printout string
- string printout = "";
- // Load lvl file
- byte[] fileBytes = File.ReadAllBytes("D:/Projects/CSharp Workspace/Windows app/MyFirstConsole/oldtrench4.lvl");
- // Read first 2 bytes to see if Tile bmp is included
- if ((Encoding.ASCII.GetString(new byte[] { fileBytes[0], fileBytes[1] })).Equals("BM"))
- {
- // Setting the tile reader to start after bmp file info
- offset = BitConverter.ToInt32(new byte[] { fileBytes[2], fileBytes[3], fileBytes[4], fileBytes[5] }, 0);
- Console.WriteLine("Contains BMP! Offset:" + offset.ToString());
- }
- // Storing the bit sets so we know how many times to loop
- int remainingBytes = fileBytes.Length - offset;
- // Begin loading tiles
- // Make sure to do it in sets of 4 - tiledID=1byte x and y combined is 3byte
- while (remainingBytes > 3)
- {
- // Gathering the next 4 bytes and converting to Int32
- byte[] nextFour = new byte[4] { fileBytes[offset], fileBytes[offset + 1], fileBytes[offset + 2], fileBytes[offset + 3] };
- int i = BitConverter.ToInt32(nextFour, 0);
- // Shifting and extracting info from a number using hokus pokus magic - i dont understand this
- int tile = i >> 24 & 0x00ff;
- int y = (i >> 12) & 0x03FF;
- int x = i & 0x03FF;
- // Add the tile info to the next line of our printout
- printout += (" [tile:" + tile.ToString().PadLeft(3) + "|x:" + x.ToString().PadLeft(5) + "|y:" + y.ToString().PadLeft(5) + "]");
- // incrementing to our rowcount - how many tiles to print per line
- rowCount++;
- // updating remaining bytes
- remainingBytes -= 4;
- offset += 4;
- // Checking if its time to print next row
- if (rowCount >= 8 || remainingBytes < 4)
- {
- rowCount = 0;
- using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:/Projects/CSharp Workspace/Windows app/MyFirstConsole/test.txt", true))
- {
- file.WriteLine(printout);
- printout = "";
- }
- }
- }
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment