Jump to content
  • Advertisement
Sign in to follow this  
scarypajamas

[c++] cross platform development

This topic is 3743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have three questions about making a cross platform, opengl app. 1) I've been trying to port my opengl app from pc to mac for a while now. Except all my file formats (tga, md2) don't load under mac, but when I do get them to load or find code from the internet that can get them to load on mac, I can't get my file's to load on pc again. I know about endians but I've written endian independent code (I swear!). Is there anything else I could be missing? Could I have done it wrong? 2) Also, on nehe's lesson 10, the tutorial loads geometry from a "world.txt" file. I tried porting the pc code of this tutorial to mac and I did get it to compile, but the world.txt and code from windows ends the program with a run-time error. It's the exact same file in the same format! The code is all the same! But the program terminates. Only the mac version of nehe's lesson 10 will load the world.txt. Why is this? Does this have to do with endian? 3) Also, I have read this article here, http://www.gamedev.net/reference/articles/article2091.asp The article says: "The short answer is, endians come into play whenever your code assumes that a certain endian is being used. The most frequent time this happens is when you're reading from a file." Am I to assume that whenever I'm reading or writing a file on a system I will have to worry about endians? I know these questions might seem silly, but I have created multi-platform 2d games in the past with SDL and now I want to do a 3d game with opengl and I'm running into problems with just loading the files! NOTE: my pc is an AMD and my mac is a G4 if it makes a difference.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by scarypajamas
I know about endians but I've written endian independent code (I swear!). Is there anything else I could be missing? Could I have done it wrong?


Yes. It sounds to me like you're still having a problem with endian-ness. Could you show us one of the functions you've attempted to get working?

In general you're have to decide on a single convention for your files, either big-endian or little-endian.

Quote:
Original post by scarypajamas
NOTE: my pc is an AMD and my mac is a G4 if it makes a difference.


This is part of your problem. AMD chips are Intel Architecture which are little-endian; G4 chips are PowerPC architecture which are big-endian

In case you didn't already know, on the MAC the CoreFoundation API provides byte swap routines.

Share this post


Link to post
Share on other sites
You can continue using SDL in conjunction with OpenGL to take care of some of the complexity of cross-platform development. It also provides image loading and endian swapping functions.

Share this post


Link to post
Share on other sites
Here’s a function for reading and swapping an integer. Other values, like floats and shorts are the same except I just change the functions type. It’s taken from the md2 model loading code I'm using.


int MD2Model::ReadInt(FILE *filePtr)
{
int value = 0;
fread(&value, sizeof(int), 1, filePtr);

#if MD2_BYTEORDER == MD2_BIG_ENDIAN
value = MD2SwapInt(value);
#endif

return value;
}





I determine what processor I’m using this condition:


#if defined(__i386__) || defined(__ia64__) || defined(WIN32) || (defined(__alpha__) || defined(__alpha)) || defined(__arm__) || (defined(__mips__) && defined(__MIPSEL__)) || defined(__SYMBIAN32__) || defined(__x86_64__) || defined(__LITTLE_ENDIAN__)

#define MD2_BYTEORDER MD2_LIL_ENDIAN
#else
#define MD2_BYTEORDER MD2_BIG_ENDIAN
#endif





Here, declare “MD2SwapInt” which actually swaps the integer in ReadInt function above.


#ifndef MD2SwapInt
static __inline__ Uint32 MD2SwapInt(Uint32 D) {
return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
}
#endif





This all looks good to me, but I’m still lost in the endian world… When I run this code it compiles but I get runtime errors on my pc. But the code and md2 works and loads on my mac so it must be endian, right? Could it still be a problem with my own code?

Share this post


Link to post
Share on other sites
I think the problem has to do with the fact that you are doing byte swapping indiscriminately on each platform.

It has to do with the line #if MD2_BYTEORDER == MD2_BIG_ENDIAN which always evaluates to true.

Write: #ifdef MD2_BIG_ENDIAN instead.

PS. I think it would be "nicer" if you used the CoreFoundation swap functions.

[edit]

Your whole naming system is kind of confusing. First of all the MD2 files are always little-endian because (I assume) these files were all created on a PC.

What your program is doing is finding the endianness of the host machine. That determines whether you need to do any byte conversion.

Share this post


Link to post
Share on other sites
Quote:
Write: #ifdef MD2_BIG_ENDIAN instead.


[smile] fpsgamer your a saint! I tested this with my pc and it worked! I was able to load my md2 model. I haven’t tested this with my mac yet but if I loaded it before, I'm confident it will work again!

But could you clarify one last thing for me… Do I have to worry about endians whenever I read from a file? Does the file have to be in a certain form, like binary, or it really doesn’t matter.

Share this post


Link to post
Share on other sites
Quote:
Original post by scarypajamas
Do I have to worry about endians whenever I read from a file?


Sorta.

You have to be concerned about endian-ness whenever you have to deal with "multi-byte" values that originate from a machine other than the host machine.

So you don't need to worry about endian-ness when opening an ASCII file regardless of the machine from which it originated. That is because ASCII values are each a single byte in size, in other words they are not multi-byte values.

However if you are getting data from over a network, a binary file or a text file encoded in anything other than UTF-8 or ASCII (or any other single byte encodings I've missed), then you'll have to at least think about doing byte swapping.

Share this post


Link to post
Share on other sites
Yes, I think I understand endians now, or at least have a better understanding. Your posts have helped me a lot fpsgamer (especially that last one). Thanks for your help! And I too, enjoy first person shooters, it's my favorite genre of games [grin]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!