例如,我有以下 C++ 结构...
struct dleaf_t
{
int contents; // OR of all brushes (not needed?)
short cluster; // cluster this leaf is in
short area : 9; // area this leaf is in
short flags : 7; // flags
short mins[ 3 ]; // for frustum culling
short maxs[ 3 ];
unsigned short firstleafface; // index into leaffaces
unsigned short numleaffaces;
unsigned short firstleafbrush; // index into leafbrushes
unsigned short numleafbrushes;
short leafWaterDataID; // -1 for not in water
//!!! NOTE: for maps of version 19 or lower uncomment this block
/*
CompressedLightCube ambientLighting; // Precaculated light info for entities.
short padding; // padding to 4-byte boundary
*/
};
通常,我可以使用 JNA 轻松地在 Java 中表示结构,但此结构使用数据类型的自定义位数(如果我没记错的话)。
例如,area
是 9 位的short
,而不是像通常那样的 16 位。 flags
缺少 7 位...等等。
如何使用自定义位大小的数据类型定义 JNA 结构?
最佳答案
将两个位字段合并为一个 int
字段,然后编写成员方法来提取已组合的各个字段的值,例如
public int area_flags;
public int getArea() { return area_flags & 0x1FF; }
public int getFlags() { return (area_flags >> 9) & 0x3F; }
您可能需要稍微尝试一下,具体取决于您的编译器如何打包这些位。
关于java - JNA:如何定义具有自定义位大小字段的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38387638/