PDA

View Full Version : A "monster" c struct to convert



Legolas
27-10-2007, 01:56 AM
Ok, so... As usual, every new libnds release I get some problems converting it to pascal. This time a sprite related struct is changed and, when I saw the c source, my face looked like this one: :shock:
Ladies and Gentlemen, the scary "monster":


typedef union {
struct {
struct {
u16 posY: 8;
union {
struct {
u8: 1;
bool isHidden: 1;
u8: 6;
};
struct {
bool isRotoscale: 1;
bool rsDouble: 1;
tObjMode objMode: 2;
bool isMosaic: 1;
tObjColMode colMode: 1;
tObjShape objShape: 2;
};
};
};

union {
struct {
u16 posX: 9;
u8: 7;
};
struct {
u8: 8;
union {
struct {
u8: 4;
bool hFlip: 1;
bool vFlip: 1;
u8: 2;
};
struct {
u8: 1;
u8 rsMatrixIdx: 5;
tObjSize objSize: 2;
};
};
};
};

struct {
u16 tileIdx: 10;
tObjPriority objPriority: 2;
u8 objPal: 4;
};

u16 attribute3;
};

struct {
uint16 attribute[3];
uint16 filler;
};

} SpriteEntry, * pSpriteEntry;


The main thing that drives me nuts is the fact that neither structs nor unions have a name, as some bit fields too. I could try to break this big nested struct in something smaller (and more readable), but I don't know how this change will be internally "digested" by the library.

arthurprs
27-10-2007, 02:55 AM
they are anonymous unions, but no idea how to convert them to pascal :?

JernejL
27-10-2007, 08:02 AM
dump the data to a file, look with a hex editor and figure out what is what, then write a pascal one.

Brainer
27-10-2007, 09:55 AM
Indeed, it's a very difficult structure to convert, yet I tried to do it. I'm very unsure about the speed and correctness of this code, but I thought you should take a look.

type
//: Only for tests!
U16 = Integer;
U8 = Integer;
tObjMode = Boolean;
tObjColMode = Boolean;
tObjColShape = Double;
tObjShape = Boolean;
tObjSize = Single;
tObjPriority = Byte;
uint16 = LongInt;

{ .: StructA :. }
StructA = record
case Integer of
0: (A: u8);
1: (isHidden: Boolean);
2: (B: u8);
end;

{ .: StructB :. }
StructB = record
case Integer of
0: (isRotoscale: Boolean);
1: (rsDouble: Boolean);
2: (objMode: tObjMode);
3: (isMosaic: Boolean);
4: (colMode: tObjColMode);
5: (objShape: tObjColShape);
end;

{ .: StructC :. }
StructC = record
posX: u16;
A: u8;
end;

{ .: StructD :. }
StructD = record
A: u8;
hFlip: Boolean;
vFlip: Boolean;
B: u8;
end;

{ .: StructE :. }
StructE = record
A: u8;
rsMatrixIdx: u8;
objSize: tObjSize;
end;

{ .: StructF :. }
StructF = record
tileIdx: u16;
objPriority: tObjPriority;
objPal: u8;
end;

{ .: StructG :. }
StructG = record
attribute: array[0..2] of uint16;
filler: uint16;
end;

{ .: UnionA :. }
UnionA = record
posY: u16;
case Integer of
0: (A: StructA);
1: (B: StructB);
end;

{ .: UnionB :. }
UnionB = record
A: u8;
case Integer of
0: (B: StructD);
1: (C: StructE);
end;

{ .: UnionC :. }
UnionC = record
A: StructC;
B: UnionB;
end;

{ .: SpriteEntry :. }
pSpriteEntry = ^SpriteEntry;
SpriteEntry = record
attribute3: u16;
U1: UnionA;
U2: UnionB;
A: StructF;
B: StructG;
end;


Hope that helps.

JSoftware
27-10-2007, 10:06 AM
You can' convert it without placing labels on the unions. Using bitpacking you can get a quite good match

arthurprs
27-10-2007, 01:33 PM
Post the result, i will like see how the struct is on pascal