Howdy, those of you who remember me may be happy to know my 2D game engine was finished and I'm now working on a 3D engine.
My engine has been going rather spiffingly well, after some battles with DirectX and OpenGL i managed to get them working side-by-side. Wrote a working wavefront OBJ model loader (i'll happily hand that out sometime) and generally was having a great time.. Now here comes the problem.
Pretty-much all my code so far is cross platform, the parts that aren't have alternate versions for platforms (window management, etc). I'm about to impliment my Physics Engine but before I can i need to save model-data in my own format (for sheer speed of loading versus the general "oh lol i load existing format that has no relationship to my internal data structures via a impossibly slow single-platform library" approach.

Now the problem doesn't lie with my model saving loading, that's all hickity boo (that means good), my issue is with the portability.

I'm using ofstream in binary mode, and char* reinterpret casts to output my data. Just like i was taught.
The problem is when i'm outputing say... may bigass array of vertex data from a dynamic float array:

//first we past a reinterpreted pointer of the vert array (char)
		//then we need to allocate enough room in the stream.
		//this is pretty simple actually, the calulation is:
		//	the number of verts X ( Size of a float X 3)
		//this is because each vert is made of three values.
		OutFile->write((char*) VertArray,NumVerts*(sizeof(float)*3));

Now here comes the problem, as far as I'm aware the exact defintion of the size of a float is pretty much 'Is bigger than an Int', it could be 24bits, 32bits, 64bits, over 9000bits. It's entirely up to the platform.
That's not a problem though you say, the code will work fine on any platform... and it will..
But the file data being output (my model files) will most likely be distributed with my game, I could recompile all the models for my target platform, sure... but I also want to be able to support model-transfer over a network, such as... the many hampster filled tubes of the internet! I don't want somone playing on a mac to only be able to connect with mac users. (though to be honest... playing games on a mac? heck owning a mac... the very idea makes me feel rather nausiated).
Thus the question comes.. How, without using a library written by someone else (I'm so dedicated to using my own code I wrote my own replacement for std::vector, that and a speed issue with resizing) can I possibly write my model files on one platform, and load on another without either: using text files, or wasting space in my file? (file size is obviously an issue when transfering over a network).

Anyhow hopefully my rant isn't too long to follow (or to wordy for the second-language-english-speakers (sorry >_<)) and someone will have the slightest clue what i'm waffeling on about.

P.S.
on "//error checking saves your ass"
my ass isn't flapping in the wind so much any more! Just lightly fluttering....

>How, without using a library written by someone else <snip> can I
>possibly write my model files on one platform, and load on another
>without either: using text files, or wasting space in my file?

Your problem is binary. Any binary output is inherently non-portable, and the only way to fix that is to normalize your output to a common format. This normalization is usually done with text conversions, but you can manually deconstruct and reconstruct your objects in a byte-wise manner as well. You seem to be leaning toward the latter, which means you need to pick a binary format, then when porting to a different system convert between that format and the system format when reading and writing your files.

Naturally this incurs an overhead cost of doing the conversion, so add that to your list of things to consider.

commented: narue continues to save us from programming hell! +1

Thank's Narue, your correct in that I am leaning towards the latter. I was hoping that there would be some neat little trick for binary conversion or hiding climate change in c++ i wasn't aware of, apparently not so.
Hopefully the overhead won't be so large as to outweigh the advantages of compessing my data into a binary format. I'll start trying a few different approches and see what comes out the fastest on my primary platform.
While I do consider the thread solved If you or anyone else has any good ideas as far as an exact implimentation (bitfields, conversion, etc) I'd greatly appreciate the help.

Thank's Narue, your correct in that I am leaning towards the latter. I was hoping that there would be some neat little trick for binary conversion or hiding climate change in c++ i wasn't aware of, apparently not so.
Hopefully the overhead won't be so large as to outweigh the advantages of compessing my data into a binary format. I'll start trying a few different approches and see what comes out the fastest on my primary platform.
While I do consider the thread solved If you or anyone else has any good ideas as far as an exact implimentation (bitfields, conversion, etc) I'd greatly appreciate the help.

>Hopefully the overhead won't be so large as to outweigh the
>advantages of compessing my data into a binary format.

In my experience it's not significantly better or worse than text conversions in terms of overhead, so I don't think you'll find that to be an issue. The biggie is complexity. As your data gets more complicated, so does the conversion. For example, with integers you really only have to break up the bytes and make sure the ordering is consistent, but with floating-point you also need to break down the value into something normalized (take a look at frexp and ldexp) before considering storage. It all adds up and can end up being pretty hairy if you're not careful.

your code-godess-ish-ness once more saves a meagre mortal from hours of keyboard V.S. forehead combat.
I should point out that I don't mind a fair bit of complexity, if you've had any experience with the nightmare that is a functional games engine you'll know that simple isn't a word that exists outside of function-scope.
anyhow I thank you again and will post an update if/when i get it working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.