|
Server Help Community forums for Subgame, ASSS, and bots
|
Author |
Message |
grazzhoppa Novice
Joined: Jan 03 2007 Posts: 29 Offline
|
Posted: Sat Jan 06, 2007 11:47 pm Post subject: A good way to reading bits? (reading the lvz format) |
|
|
|
|
I made something that reads a .lvz file and it works for all the .lvz's I tested it on, but I have feeling there is an easier way to read "# bits" than the way I tried.
Here is a snip of what it does for each item that "lvzformat.txt" describes:
It puts an uncompressed section into a char array called "uncompressedDATA". And it uses an int called "dataPointer" to keep track of where it is in the uncompressed data array.
// next 2 bytes:
// i12 Display Time - How long will display for, in 1/10th of a second.
unsigned short displayTime = 0; // 16 bit int, will hold a 12 bit number
memcpy(&displayTime, &uncompressedDATA[dataPointer], 2); // read 16 bits/2 bytes. Only interested in the first 12 bits though.
dataPointer += 1; // move the dataPointer only one byte because the next item shares some of the 2nd byte
displayTime <<= 4; // push the last 4 bits off of the number
displayTime >>= 4; // move the significant 12 bits back to the beginning of the number
printf(" object loop: displayTime is %i 1/100ths of a second\n", displayTime);
// i4 Display Mode - Which display mode this object uses. Values later.
unsigned char displayMode = 0; // 8 bit int, unsigned so bit shift is predictable
memcpy(&displayMode, &uncompressedDATA[dataPointer], 1); // read 8 bits/1 byte, only interested last 4 bits.
dataPointer += 1;
displayMode >>= 4; // push the rightmost 4 bits of the 8bit number off, so only the last 4 bits represent the number. |
Is there a better way other than bit shifting like that for each "item" in the lvz format that is not a multiple of 1byte/8bits?
Also, I don't know bitshift will work when the item in the .lvz is supposed to represent a negative number (like the x coord/y coord of a screen object). I don't have a Windows computer, so I can't make a new .lvz to test a .lvz that uses negative numbers for those items.
Can someone please supply a .lvz that uses negative numbers for the x and y coord of a screen object?
Here is how it reads the x/y coord of a screen object:
// next 2 bytes after mapOrScreenObject and objectID bytes:
// u4 X Type - Which part of the screen the X coord is offset from (see OffsetTypes lower down).
unsigned char xScreenOffsetType = 0; // will hold 4 significant bits only.
memcpy(&xScreenOffsetType, &uncompressedDATA[dataPointer], 1); // read 1 byte
// ++dataPointer; // do not move the pointer, still need to read part of the 1st byte.
xScreenOffsetType <<= 4; // push 4 leftmost bits off the 8 bit number
xScreenOffsetType >>= 4; // move the significant 4 bits back to the significant side of the number.
printf(" object loop: xScreenOffsetType is %i\n", xScreenOffsetType);
// i12 X Coord - The X coord value, in pixels, this object will be displayed on. More info later.
/*unsigned */ short xCoord = 0; // 16 bit int, only 12 significant bits will be put into it. This value can be negative.
memcpy(&xCoord, &uncompressedDATA[dataPointer], 2); // read 16 bits/2 bytes from the uncompressedDATA.
dataPointer += 2;
xCoord >>= 4; // push the 4bits from the previous variable off of this 16bit number, to get the significant 12 bits of this variable. Bitshifting a signed int!
printf(" object loop: xCoord at %i pixel\n", xCoord);
|
If you're going to run the program - warning: it'll dump all the files from the .lvz into the same directory as the program. It doesn't create a nice .ini like deBuildLevel from the lvzToolKit, but it prints out all the .lvz information to the console. It compiles with g++, and not tested on other compilers.
Give the path to the .lvz file as the argument to the program:
./lvzreader "./test lvz information/akoth5.lvz" |
it is in c/c++ code
LVZReader.cpp - 31.77 KB
File downloaded or viewed 24 time(s)
LVZstructures.h - 2.79 KB
File downloaded or viewed 27 time(s)
|
|
Back to top |
|
|
Mine GO BOOM Hunch Hunch What What
Age:40 Gender: Joined: Aug 01 2002 Posts: 3614 Location: Las Vegas Offline
|
Posted: Sun Jan 07, 2007 12:49 am Post subject: |
|
|
|
|
Try looking into using struct. But this depends on your processor's byte order, but assuming you are using a normal x86 processor (ie: windows, the Intel macs), this isn't a problem.
#pragma pack(1)
struct PacketObjectInfo2Map {
unsigned short mapobject : 1;
unsigned short id : 15;
short x;
short y;
unsigned char imagenumber;
unsigned char layer;
unsigned short displaytime : 12;
unsigned short timermode : 4;
};
#pragma pack() | The above is a sample cut from my DeBuildLVZ program. At least in Visual Studios, the #pragma pack(1) is a signal to the compiler that the following structures need to be packed to one byte boundaries. This is required, since most compilers will add in spaces so variables align on 4 byte intervals (read up on 32bit processors and how they read/write memory to understand why). The #pragma pack() at the end tells the compiler that it can go back to whatever the default is for your compiler. (For others, it is better to use push and pull so your headers can be reused in other projects that may adjust the bit packing boundaries.)
How a struct works, look at the Wikipedia article I linked to above. What do those :# things mean? They are bit fields. They allow you to pack data into space much easier than using normal bit shifting operations.
How to use them? Say you have a char* array that you know contains object data. The array should be 10 bytes long.
struct PacketObjectInfo2Map *objinfo;
objinfo = (struct PacketObjectInfo2Map*)myCharArray;
printf("Object ID: %d Location: (%d,%d)\n", objinfo->id, objinfo->x, objinfo->y); | These are horrible examples, but searching about bit fields and struct will lead you to better examples of how they work.
|
|
Back to top |
|
|
grazzhoppa Novice
Joined: Jan 03 2007 Posts: 29 Offline
|
Posted: Sun Jan 07, 2007 1:39 am Post subject: |
|
|
|
|
Thank you
|
|
Back to top |
|
|
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Sun Jan 07, 2007 2:29 pm Post subject: Re: A good way to reading bits? (reading the lvz format) |
|
|
|
|
grazzhoppa wrote: | I don't have a Windows computer, so I can't make a new .lvz to test a .lvz that uses negative numbers for those items. |
You don't need windows if you use X-LvzToolkit. _________________ SubSpace Discretion: A Third Generation SubSpace Client
|
|
Back to top |
|
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
Software by php BB © php BB Group Server Load: 1193 page(s) served in previous 5 minutes.
|