Code: Show/Hide typedef struct tagBITMAPFILEHEADER {
WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } BITMAPFILEHEADER, *PBITMAPFILEHEADER; Members bfType Specifies the file type, must be BM. bfSize Specifies the size, in bytes, of the bitmap file. bfReserved1 Reserved; must be zero. bfReserved2 Reserved; must be zero. bfOffBits Specifies the offset, in bytes, from the beginning of the BITMAPFILEHEADER structure to the bitmap bits. |
Anonymous wrote: |
it assumes data comes in 4 byte chunks (I think Asss has this problem too) |
Code: Show/Hide //Check if there is a tileset. byte[] header= new byte[6]; //Open the System.IO.FileStream lvl = System.IO.File.Open(tileSetPath,FileMode.Open,FileAccess.ReadWrite); lvl.Read(header,0,6); //If first two bytes of header != "BM", assume theres no tileset. if(header[0] != 0x42 || header[1] != 0x4D) return; //Reset the FileStream so you can... lvl.Flush(); //Load the tileset to a bitmap. tileSetBMP = new Bitmap(lvl); lvl.Close(); |
Mine GO BOOM wrote: | |
Then every 4bytes (or int) you read in, just type it to a struct tilerecord. Example (quick hack, didn't debug/compile):
|
Mr Ekted wrote: |
WARNING: DO NOT WRITE CODE LIKE MGB |
Code: Show/Hide byte[] rawInput = new byte[4];
while (fs.read(rawInput) != -1) { int row = rawInput[0] << 4 + rawInput[1] >> 4; int col = ((rawInput[1]) << 8) + ((rawInput[2]) & (0xFFF)); System.out.println("Read tile: (" + row + "," + col + ") value: " + rawInput[3]); } |
Quote: |
Read tile: (0,0) value: 1
Read tile: (65536,4096) value: 1 Read tile: (2,8192) value: 1 Read tile: (196608,12288) value: 1 |
Code: Show/Hide unsigned long data;
x = data & 0x03ff; y = (data >> 12) & 0x03ff; tile = data >> 24; |
Code: Show/Hide BYTE a;
int b; a = 0xff; b = a << 8; |
Mr Ekted wrote: |
Doesn't java have an unsigned 32-bit type? |
Code: Show/Hide int r = dis.readInt();
System.out.println("Raw SInt32 = \t" + Integer.toBinaryString(r)); int tile = (r >>> 24); int y = (r >>> 12) & 0x03FF; int x = r & 0x03FF; System.out.println("x = \t" + Integer.toBinaryString(x)); System.out.println("y = \t" + Integer.toBinaryString(y)); //int t = r & 0x000000ff; // this reads the tile fine System.out.println("(" + x + "," + y + ") = " + tile); |
Code: Show/Hide Raw SInt32 = 10101001
x = 10101001 y = 0 (169,0) = 0 Raw SInt32 = 1000100000000000010101001 x = 10101001 y = 100000000 (169,256) = 1 Raw SInt32 = 10001000000000000010101001 x = 10101001 y = 1000000000 (169,512) = 2 Raw SInt32 = 11001100000000000010101001 x = 10101001 y = 1100000000 (169,768) = 3 |
Code: Show/Hide int tile = r & 0x000000ff; |
Quote: |
xxxx xxxx xx00 yyyy yyyy yy00 tttt tttt = 32 bits
where xs are x value bits, and so on where 0s are illegal (>1023) |
Code: Show/Hide package MapEditor.Data; import java.awt.Image; /** * represents a single tile at a location on a map */ public class Tile { private int value; private int x; private int y; public Tile(int value, int x, int y) throws IllegalArgumentException { try { setValue(value); setX(x); setY(y); } catch (IllegalArgumentException e) { throw e; } } public int getValue() { return value; } public void setValue(int value) throws IllegalArgumentException { if (!testTileValue(value)) throw getStandardTileValueException(value); this.value = value; } public int getX() { return x; } public void setX(int x) throws IllegalArgumentException { if (!testTileLocationValue(x)) throw getStandardTileLocationException(x); this.x = x; } public int getY() { return y; } public void setY(int y) throws IllegalArgumentException { if (!testTileLocationValue(y)) throw getStandardTileLocationException(y); this.y = y; } /** * @see TileSet.getTileImage() */ public Image getTileImage(TileSet set) { return set.getTileImage(this.getValue()); } public String toString() { return "(" + getX() + "," + getY() + ") = " + getValue(); } /** * @param v the value to test * @return whether v is a valid VIE tile value */ public static boolean testTileValue(int v) { if (v == 216 || v == 217 || v == 219 || v == 220) return true; if ((v >= 1 && v <= 190)) return true; return false; } /** * @param n the location value to test * @return whether 0 <=n <=1023 */ public static boolean testTileLocationValue(int n) { if (n >= 0 && n <= 1023) return true; return false; } public static IllegalArgumentException getStandardTileLocationException(int v) { return new IllegalArgumentException("Tile location " + v + " is invalid (must be 0 to 1023"); } public static IllegalArgumentException getStandardTileValueException(int v) { return new IllegalArgumentException("Tile value " + v + " is invalid (must be 1-190, 216,217,219, or 220"); } } |
Code: Show/Hide /** * NOTE: write(int) or read(int) do not demand all 32 bits, use * DataInputStream and DataOutputStream to encode LVLS * * @param t the tile to encode * @return a 32 bit java integer that represents the tile in LVL format */ public static int buildRawData(Tile t) { int ret = ((t.getX() & 0x000000ff) << 24) | ((t.getY() & 0x0000000f) << 20) | ((t.getX() & 0x0000000f) << 8) | ((t.getY() & 0x00000ff0) << 4) | (t.getValue() & 0x000000ff); return ret; } |
Code: Show/Hide /** * NOTE: write(int) or read(int) do not demand all 32 bits, use * DataInputStream and DataOutputStream to encode LVLS * * @param r the int to be parsed * @return a tile representing the raw tile's encoded information */ public static Tile parseRawTileData(int r) { int tile = r & 0x000000ff; int x = ((r & 0x000f0000) >>> 8) | ((r & 0xff000000) >>> 24); int y = ((r & 0x0000ff00) >>> 4) | ((r & 0x00f00000) >>> 20); return new Tile(tile, x, y); } |