Sign in to follow this  
yckx

Problem with X file loader

Recommended Posts

After pointing AcePilot to the article here on Gamedev about writing an X file loader, I thought I'd try it out. I'm having problems, and I can't seem to solve them. Here's the code: xdefs.h
#define XOFFILE_FORMAT_MAGIC 	((unsigned long)'x' + ((unsigned long)'o' << 8) + ((unsigned long)'f' << 16) + ((unsigned long)' ' << 24))


struct XHeader
{
	unsigned long	magic;
	unsigned short	major_version;
	unsigned short	minor_version;
	unsigned long	format;
	unsigned long	float_size;
};

xfileloader.cpp
Model* XFileLoader::Load(char* filename)
{
	std::fstream fin;
	fin.open(filename, std::ios::in);
	
	if(current_file.bad() )
	{
		//throw error
		return NULL;
	}
	
	XHeader header;
	
	fin.read((char*)&header, 16);
	
	if(header.magic != XOFFILE_FORMAT_MAGIC)
	{
		//throw error
		return NULL;
	}
	...//rest of function
}

The X file
xof 0303txt 0032 //initial line of the x file
...

This is pretty much lifted straight from the example code. But it's not reading the file header correctly. If I break header.magic into bytes I'm getting -8, 38, 62, 0 instead of the 120, 111, 102, 32 that corresponds to 'x' 'o' 'f' ' '. I don't know where I'm going wrong. Like I said, this is essentialy cut/pasted from the example code. Any help wuold be appreciated. yckx

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What are you using? Unix, Windows, Mac. It sounds strange but all computers are not a like. Sounds like you are suffering from the endianess problem. You are saying what is that. Well on any machine the number 2 as a short int in memory is stored either as 0002 or 0200 in hexidecimal. When you read in the "xof ". I am not sure if the "xof " is stored as big- or little- endian but the endianess does not match what you have in XOFFILE_FORMAT_MAGIC.

I would try using
(Note: Function was not tested.)

bool endianess(){
short test = x0002;
if (test == 2){
return true;
else
return false;
}

depending on the result, compare the XHeader.magic to the approperate hexidecimal number.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just thought of another idea

struct XHeader
{
char magic[4];
unsigned short major_version;
unsigned short minor_version;
unsigned long format;
unsigned long float_size;
};


and then compare each byte in that array to your "xof "

May need to do this for the other fields like format, etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this