Thread: Can someone explain to me what this code means

  1. #1
    Shadow12345
    Guest

    Can someone explain to me what this code means

    This is from a tutorial I'm reading
    Code:
    // byte-align structures
    #ifdef _MSC_VER
    #	pragma pack( push, packing )
    #	pragma pack( 1 )
    #	define PACK_STRUCT
    #elif defined( __GNUC__ )
    #	define PACK_STRUCT	__attribute__((packed))
    #else
    #	error you must byte-align these structures with the appropriate compiler directives
    #endif
    I'm not exactly sure what it means...'byte align structures' ?

    Is this for like determining an operating system or something?
    It would be cool if you could explain what each line means, not just explain what the general purpose is.
    Thanks

  2. #2
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    Try compiling the code and seeing what it does.

  3. #3
    Shadow12345
    Guest
    I already know what the project does, I don't know what those individual lines of code mean

    EDIT:
    What I posted was incomplete, I'm sorry. I still don't know what it means, but here it is in its completeness
    Code:
    // byte-align structures
    #ifdef _MSC_VER
    #	pragma pack( push, packing )
    #	pragma pack( 1 )
    #	define PACK_STRUCT
    #elif defined( __GNUC__ )
    #	define PACK_STRUCT	__attribute__((packed))
    #else
    #	error you must byte-align these structures with the appropriate compiler directives
    #endif
    
    typedef unsigned char byte;
    typedef unsigned short word;
    
    // File header
    struct MS3DHeader
    {
    	char m_ID[10];
    	int m_version;
    } PACK_STRUCT;
    
    // Vertex information
    struct MS3DVertex
    {
    	byte m_flags;
    	float m_vertex[3];
    	char m_boneID;
    	byte m_refCount;
    } PACK_STRUCT;
    
    // Triangle information
    struct MS3DTriangle
    {
    	word m_flags;
    	word m_vertexIndices[3];
    	float m_vertexNormals[3][3];
    	float m_s[3], m_t[3];
    	byte m_smoothingGroup;
    	byte m_groupIndex;
    } PACK_STRUCT;
    
    // Material information
    struct MS3DMaterial
    {
        char m_name[32];
        float m_ambient[4];
        float m_diffuse[4];
        float m_specular[4];
        float m_emissive[4];
        float m_shininess;	// 0.0f - 128.0f
        float m_transparency;	// 0.0f - 1.0f
        byte m_mode;	// 0, 1, 2 is unused now
        char m_texture[128];
        char m_alphamap[128];
    } PACK_STRUCT;
    
    //	Joint information
    struct MS3DJoint
    {
    	byte m_flags;
    	char m_name[32];
    	char m_parentName[32];
    	float m_rotation[3];
    	float m_translation[3];
    	word m_numRotationKeyframes;
    	word m_numTranslationKeyframes;
    } PACK_STRUCT;
    
    // Keyframe data
    struct MS3DKeyframe
    {
    	float m_time;
    	float m_parameter[3];
    } PACK_STRUCT;
    
    // Default alignment
    #ifdef _MSC_VER
    #	pragma pack( pop, packing )
    #endif
    
    #undef PACK_STRUCT
    I don't know what any of the '#' statements mean, nor why PACK_STRUCT comes after each data structure.
    Last edited by Shadow12345; 12-22-2002 at 12:01 PM.

  4. #4
    Shadow12345
    Guest
    Each compiler handles "byte-align structures" in different ways, and this is how someone has attempted to get some portability into the code.
    okay that's basically what I thought it was but I wasn't sure. Is gcc the unix compiler?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. explain code
    By manzoor in forum C++ Programming
    Replies: 8
    Last Post: 09-15-2008, 05:28 AM
  2. Need help to understand this STL code.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 01:59 PM
  3. explain this code please
    By lastresort in forum C++ Programming
    Replies: 1
    Last Post: 02-06-2005, 12:02 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM