Advertisement
szymski

Tileset Loading

Apr 4th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace D2Project.Formats
  8. {
  9.     //DT1 - tile format with graphics
  10.     public class DT1
  11.     {
  12.         struct Header
  13.         {
  14.             public long x1;
  15.             public long x2;
  16.             public long zeros;
  17.             public long nb_block;
  18.             public long bh_ptr;
  19.         }
  20.  
  21.         class Tile
  22.         {
  23.             public struct Header
  24.             {
  25.                 public long direction;
  26.                 public long roof_y;
  27.                 public byte sound;
  28.                 public byte animated;
  29.                 public long size_y;
  30.                 public long size_x;
  31.                 public long zeros1;
  32.                 public long orientation;
  33.                 public long main_index;
  34.                 public long sub_index;
  35.                 public long frame;
  36.                 public long unknown_a;
  37.                 public long unknown_b;
  38.                 public long unknown_c;
  39.                 public long unknown_d;
  40.                 public long floor_flags;
  41.                 public long zeros2;
  42.                 public long data_ptr;
  43.                 public long length;
  44.                 public long sub_block;
  45.                 public long zeros3;
  46.             }
  47.             public Header header;
  48.             public class Block
  49.             {
  50.                 public struct Header
  51.                 {
  52.                     public long x;      //2
  53.                     public long y;      //2
  54.                     public long zeros1; //2
  55.                     public long grid_x; //1
  56.                     public long grid_y; //1
  57.                     public long format; //2
  58.                     public long length; //4
  59.                     public long zeros2; //2
  60.                     public long file_offset; //4
  61.                 }
  62.                 public Header header;
  63.             }
  64.             public Block[] blocks;
  65.         }
  66.  
  67.         Header header;
  68.         Tile[] tiles;
  69.  
  70.         byte[] file;
  71.  
  72.         public DT1(byte[] f)
  73.         {
  74.             file = f;
  75.             Console.WriteLine("Reading DT1");
  76.             header.x1 = BitConverter.ToInt32(f.Take(4).ToArray(), 0);
  77.             header.x2 = BitConverter.ToInt32(f.Skip(4).Take(4).ToArray(), 0);
  78.             header.nb_block = BitConverter.ToInt32(f.Skip(268).Take(4).ToArray(), 0);
  79.             header.bh_ptr = BitConverter.ToInt32(f.Skip(272).Take(4).ToArray(), 0);
  80.             Console.WriteLine("{0} {1} {2} {3}", header.x1, header.x2, header.nb_block, header.bh_ptr);
  81.             tiles = new Tile[header.nb_block];
  82.             int offset = (int)header.bh_ptr;
  83.             for (int i = 0; i < header.nb_block; i++)
  84.             {
  85.                 tiles[i] = new Tile();
  86.                 tiles[i].header.direction = BitConverter.ToInt32(f.Skip(offset).Take(4).ToArray(), 0);
  87.                 tiles[i].header.roof_y = BitConverter.ToInt16(f.Skip(offset + 4).Take(2).ToArray(), 0);
  88.                 tiles[i].header.sound = f.Skip(offset + 6).Take(1).ToArray()[0];
  89.                 tiles[i].header.animated = f.Skip(offset + 7).Take(1).ToArray()[0];
  90.                 tiles[i].header.size_y = BitConverter.ToInt32(f.Skip(offset + 8).Take(4).ToArray(), 0);
  91.                 tiles[i].header.size_x = BitConverter.ToInt32(f.Skip(offset + 12).Take(4).ToArray(), 0);
  92.                 tiles[i].header.orientation = BitConverter.ToInt32(f.Skip(offset + 20).Take(4).ToArray(), 0);
  93.                 tiles[i].header.main_index = BitConverter.ToInt32(f.Skip(offset + 24).Take(4).ToArray(), 0);
  94.                 tiles[i].header.sub_index = BitConverter.ToInt32(f.Skip(offset + 28).Take(4).ToArray(), 0);
  95.                 tiles[i].header.frame = BitConverter.ToInt32(f.Skip(offset + 32).Take(4).ToArray(), 0);
  96.                 tiles[i].header.floor_flags = BitConverter.ToInt32(f.Skip(offset + 40).Take(25).ToArray(), 0);
  97.                 tiles[i].header.data_ptr = BitConverter.ToInt32(f.Skip(offset + 72).Take(4).ToArray(), 0);
  98.                 tiles[i].header.length = BitConverter.ToInt32(f.Skip(offset + 76).Take(4).ToArray(), 0);
  99.                 tiles[i].header.sub_block = BitConverter.ToInt32(f.Skip(offset + 80).Take(4).ToArray(), 0);
  100.                 tiles[i].blocks = new Tile.Block[tiles[i].header.sub_block];
  101.                 int offset2 = (int)tiles[i].header.data_ptr;
  102.                 for (int j = 0; j < tiles[i].header.sub_block; j++)
  103.                 {
  104.                     tiles[i].blocks[j] = new Tile.Block();
  105.                     tiles[i].blocks[j].header.x = BitConverter.ToInt16(f.Skip(offset2).Take(2).ToArray(), 0);
  106.                     tiles[i].blocks[j].header.y = BitConverter.ToInt16(f.Skip(offset2 + 2).Take(2).ToArray(), 0);
  107.                     tiles[i].blocks[j].header.grid_x = f.Skip(offset2 + 6).Take(1).ToArray()[0];
  108.                     tiles[i].blocks[j].header.grid_y = f.Skip(offset2 + 7).Take(1).ToArray()[0];
  109.                     tiles[i].blocks[j].header.format = BitConverter.ToInt16(f.Skip(offset2 + 8).Take(2).ToArray(), 0);
  110.                     tiles[i].blocks[j].header.length = BitConverter.ToInt32(f.Skip(offset2 + 10).Take(4).ToArray(), 0);
  111.                     tiles[i].blocks[j].header.file_offset = BitConverter.ToInt32(f.Skip(offset2 + 16).Take(4).ToArray(), 0);
  112.                     offset2 += 20;
  113.                 }
  114.                 offset += 96;
  115.             }
  116.         }
  117.  
  118.         public SFML.Graphics.Image GetImage()
  119.         {
  120.            // SFML.Graphics.Image img = new SFML.Graphics.Image((uint)32, (uint)32, SFML.Graphics.Color.Red);
  121.            // return img;
  122.  
  123.             SFML.Graphics.Image img = new SFML.Graphics.Image((uint)256, (uint)256, SFML.Graphics.Color.Transparent);
  124.             for (int i = 0; i < 80; i++)
  125.             {
  126.                 for (int j = 0; j < 160; j++)
  127.                 {
  128.                     img.SetPixel((uint)i, (uint)j, Engine.Instance.graphics.palettes[Palette.Names.Act1].GetColor(file[14100+j+i*80]));
  129.                 }  
  130.             }
  131.  
  132.             return img;
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement