PsyOps

TilePrinter

Oct 10th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Drawing;
  7.  
  8. namespace MyFirstConsole
  9. {
  10.     class Program
  11.     {
  12.         public static void Main(string[] args)
  13.         {
  14.             // Sets where to start reading byte array
  15.             int offset = 0;
  16.  
  17.             // Variables used for write to file settings
  18.             int rowCount = 0;
  19.             // Printout string
  20.             string printout = "";
  21.  
  22.             // Load lvl file
  23.             byte[] fileBytes = File.ReadAllBytes("D:/Projects/CSharp Workspace/Windows app/MyFirstConsole/oldtrench4.lvl");
  24.  
  25.             // Read first 2 bytes to see if Tile bmp is included
  26.             if ((Encoding.ASCII.GetString(new byte[] { fileBytes[0], fileBytes[1] })).Equals("BM"))
  27.             {
  28.                 // Setting the tile reader to start after bmp file info
  29.                 offset = BitConverter.ToInt32(new byte[] { fileBytes[2], fileBytes[3], fileBytes[4], fileBytes[5] }, 0);
  30.                 Console.WriteLine("Contains BMP! Offset:" + offset.ToString());
  31.             }
  32.  
  33.             // Storing the bit sets so we know how many times to loop
  34.             int remainingBytes = fileBytes.Length - offset;
  35.  
  36.             // Begin loading tiles
  37.             // Make sure to do it in sets of 4 - tiledID=1byte x and y combined is 3byte
  38.             while (remainingBytes > 3)
  39.             {
  40.                 // Gathering the next 4 bytes and converting to Int32
  41.                 byte[] nextFour = new byte[4] { fileBytes[offset], fileBytes[offset + 1], fileBytes[offset + 2], fileBytes[offset + 3] };
  42.                 int i = BitConverter.ToInt32(nextFour, 0);
  43.  
  44.                 // Shifting and extracting info from a number using hokus pokus magic - i dont understand this
  45.                 int tile = i >> 24 & 0x00ff;
  46.                 int y = (i >> 12) & 0x03FF;
  47.                 int x = i & 0x03FF;
  48.  
  49.                 // Add the tile info to the next line of our printout
  50.                 printout += (" [tile:" + tile.ToString().PadLeft(3) + "|x:" + x.ToString().PadLeft(5) + "|y:" + y.ToString().PadLeft(5) + "]");
  51.                
  52.                 // incrementing to our rowcount - how many tiles to print per line
  53.                 rowCount++;
  54.                
  55.                 // updating remaining bytes
  56.                 remainingBytes -= 4;
  57.                 offset += 4;
  58.  
  59.                 // Checking if its time to print next row
  60.                 if (rowCount >= 8 || remainingBytes < 4)
  61.                 {
  62.                     rowCount = 0;
  63.                     using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:/Projects/CSharp Workspace/Windows app/MyFirstConsole/test.txt", true))
  64.                     {
  65.                         file.WriteLine(printout);
  66.                         printout = "";
  67.                     }
  68.                 }
  69.             }
  70.             Console.ReadLine();
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment