Emulating Proper Structs

BlitzMax Forums/BlitzMax Programming/Emulating Proper Structs

Gabriel(Posted 2006) [#1]
I need to emulate proper structs, and I distinctly remember Michael Reitzenstein telling me that arrays and types in BMax do not conform to the standards that Structs in other languages do.

The C++ code looks like this :

/*
** HGE Vertex structure
*/
struct hgeVertex
{
	float			x, y;		// screen position    
	float			z;			// Z-buffer depth 0..1
	DWORD			col;		// color
	float			tx, ty;		// texture coordinates
};

struct hgeQuad
{
	hgeVertex		v[4];
	HTEXTURE		tex;
	int				blend;
};


My BMax conversion ( since I don't normally need direct access to hgeVertex objects is this :

Type hgeQuad
	
	Field Vertex1_X:Float
	Field Vertex1_Y:Float
	Field Vertex1_Z:Float
	Field Vertex1_Col:Int
	Field Vertex1_TX:Float
	Field Vertex1_TY:Float
	
	Field Vertex2_X:Float
	Field Vertex2_Y:Float
	Field Vertex2_Z:Float
	Field Vertex2_Col:Int
	Field Vertex2_TX:Float
	Field Vertex2_TY:Float
	
	Field Vertex3_X:Float
	Field Vertex3_Y:Float
	Field Vertex3_Z:Float
	Field Vertex3_Col:Int
	Field Vertex3_TX:Float
	Field Vertex3_TY:Float
	
	Field Vertex4_X:Float
	Field Vertex4_Y:Float
	Field Vertex4_Z:Float
	Field Vertex4_Col:Int
	Field Vertex4_TX:Float
	Field Vertex4_TY:Float
	
	Field tex:Int ' TEXTURES USE INTEGER HANDLES, JUST LIKE BLITZ
	Field Blend:Int ' BLEND MODE - USE BLEND MODE CONSTANTS
	
End Type



And then, when I'm expected to pass a pointer to the hgeQuad Struct, I pass a VarPtr to an instance of my hgeQuad type. But I'm pretty sure it's incorrect, because I'm getting a crash elsewhere in the program which goes away if I comment out the line where I draw this quad.


N(Posted 2006) [#2]
Type hgeQuad
    
    Field Vertex1_X:Float
    Field Vertex1_Y:Float
    Field Vertex1_Z:Float
    Field Vertex1_Col:Int
    Field Vertex1_TX:Float
    Field Vertex1_TY:Float
    
    Field Vertex2_X:Float
    Field Vertex2_Y:Float
    Field Vertex2_Z:Float
    Field Vertex2_Col:Int
    Field Vertex2_TX:Float
    Field Vertex2_TY:Float
    
    Field Vertex3_X:Float
    Field Vertex3_Y:Float
    Field Vertex3_Z:Float
    Field Vertex3_Col:Int
    Field Vertex3_TX:Float
    Field Vertex3_TY:Float
    
    Field Vertex4_X:Float
    Field Vertex4_Y:Float
    Field Vertex4_Z:Float
    Field Vertex4_Col:Int
    Field Vertex4_TX:Float
    Field Vertex4_TY:Float
    
    Field tex:Int ' TEXTURES USE INTEGER HANDLES, JUST LIKE BLITZ
    Field Blend:Int ' BLEND MODE - USE BLEND MODE CONSTANTS
    
    Method GetPtr@ Ptr( )
        Return Varptr Vertex1_X
    End Method

End Type



Gabriel(Posted 2006) [#3]
Thanks Noel. That's tutorial 1 and 2 now working perfectly. Time to see what else I can break.