Platform Independent Programming

Status
Not open for further replies.

hatfarm

Ars Scholae Palatinae
1,134
Subscriptor++
Hello, I am writing a saved game editor for an old, old, old DOS game called X-Com:UFO Defense. I'll be writing it for TFTD too... but I digress... Actually, I've already written it a few times, and it was always written with Visual Basic (VB6, then VB.net). Which was fine, a bit clunky and everything but very easy to just dive right in. Well, I got to thinking about the fact that with the advent of DosBox/Steam you can play X-Com on any operating system you want. So, I got to thinking that I should make something that would be platform independent so that anyone who wanted to could use it.

I thought about Java, but I've never really used it and I like C++ and would rather use that to hone my C++ skills. I've got some VERY basic windows programming in C++ under my belt, so I think I understand how that works on a basic level. So, what is the best way to achieve platform independence painlessly? I was considering openGL, since I can load windows and images without having to worry about what operating system the user is running, GLUT would allow me to take input from the keyboard and mouse without worrying about that stuff too. However, I feel like that's got a whole lot of stuff I will never use and I wonder if there might be some better system out there. So, that's why I'm here, looking for some advice. It's pretty modular and I'm developing it so I can just develop a Windows and Linux version separately but keep all the file handling the same.

That's another thing, this edits files, do I need to worry about that moving to Linux from Windows? Will an fstream work the same in Linux as it does in Windows, or do I need to find an fstream for Linux? I've tried searching for these answers but it seems since I taught my wife how to Google, my ability has only decreased. Thanks ahead of time.

-hatfarm
 

fitten

Ars Legatus Legionis
55,637
Subscriptor++
Well... for Linux you'll technically need to worry about endianness (Linux runs on both big and little endian machines). Other than that, you might want to think about a common UI framework (if you're doing it GUI, pick a framework that works on all the platforms you want to target.. Qt maybe?). Or, you might take a MVC type approach and make all your data manipulation cross platform and then use something more 'native' on each target platform for your GUI stuff. The build process on each platform would compile the appropriate parts and ignore the other parts.

You might even think about using WINE on Linux to run it ;)

Sorry not to be very specific, but these are more high-level decisions to make perhaps even before picking a language.
 

hatfarm

Ars Scholae Palatinae
1,134
Subscriptor++
Well, WINE would be more of a user choice, and that's the choice I don't want to force them to make. I've already started rewriting my code in C++, so I'm gonna stick with that. Qt does look like what I'm looking for, thank you for the heads up on that.

How do I deal with endianness? X-Com was written with little endianness, but I deal with it all on a byte-by-byte level and do all the conversions manually (since I don't want to have to worry about the endianness of the system). However, is there more to worry about? Thanks.

EDIT:Forgot the name WINE.
 

fitten

Ars Legatus Legionis
55,637
Subscriptor++
hatfarm":2whv4m6s said:
Well, WINE would be more of a user choice, and that's the choice I don't want to force them to make. I've already started rewriting my code in C++, so I'm gonna stick with that. Qt does look like what I'm looking for, thank you for the heads up on that.

How do I deal with endianness? X-Com was written with little endianness, but I deal with it all on a byte-by-byte level and do all the conversions manually (since I don't want to have to worry about the endianness of the system). However, is there more to worry about? Thanks.

EDIT:Forgot the name WINE.

Well... "technically" you have to deal with it because someone will want to compile your app on a PPC or something running in big endian mode then trash you because it won't work right ;) However, you can stipulate that it's little endian only and make it where it won't compile on anything but one of the x86 variants. However, to answer your question, there are lots of ways to deal with endianness... a quick search turned up this, which talks about it in more detail: http://www.ibm.com/developerworks/aix/l ... ml?ca=drs-
 

hatfarm

Ars Scholae Palatinae
1,134
Subscriptor++
I thought about that, but doesn't the user have to have Mono installed on their computer too? The reason this is not something I want to do is because I get a million questions already about very small simple things. I'd like to keep it as easy as possible for the user. I'd prefer to not require any more than I have to of the user. I realize Linux users are most likely going to be a bit more experienced user, but I'd still rather not have to worry about the questions I'd get, considering my only limited experience with Linux.
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
fitten":1jkqboxg said:
Well... for Linux you'll technically need to worry about endianness (Linux runs on both big and little endian machines)
Not if he codes it properly.

The savegame files will have a fixed format and fixed endianness, and it's not hard to write endian-independent code (just read/write a byte at a time).
 

ShuggyCoUk

Ars Legatus Legionis
10,388
Subscriptor++
No, that does not make you endian-independent. How could it?

Assuming CHAR_BIT == 8 and the functions to write non char sized values to the file do the conversion to the file format desired values why wouldn't it?

i.e.
Code:
writeInt32()
{ 
  // in here convert the 32bit value representing number x to the 4 octets   
  // which when written to the file will cause the game to read it as number x 
}

A corresponding readInt32 would be required of course.

Genuine question, what am I missing here (or was it just the CHAR_BIT point you were eluding to?)
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
ShuggyCoUk":2qo6667i said:
do the conversion to the file format desired values
Also known as: doing the byte swapping, and which means you're doing more than just reading/write a byte at a time. This still requires endianness detection and the whole nine yards, it just means you're baking it in the file I/O code. Which isn't necessarily a pointless thing to do, but it does mean you're still doing the conversions. They're entirely unavoidable is point I was trying to make.
 

fitten

Ars Legatus Legionis
55,637
Subscriptor++
stickboy":30r4ump4 said:
fitten":30r4ump4 said:
Well... for Linux you'll technically need to worry about endianness (Linux runs on both big and little endian machines)
Not if he codes it properly.

The savegame files will have a fixed format and fixed endianness, and it's not hard to write endian-independent code (just read/write a byte at a time).

I guess you could do that... but sometimes it's actually useful to manipulate an integer as an integer (adding values to it, multiplying, etc.). And yeah, the file has a fixed format and you can use the various ntoi and iton macros (for example) to make it one set of code (which is the 'coding it properly' bit) that works on everything. But you have to do that rather than assume the CPU is little endian and just read sizeof(int) and manipulate it as an integer.
 

hatfarm

Ars Scholae Palatinae
1,134
Subscriptor++
For the most part, most values are just a single byte (except the strings representing names), so it's not a big deal. There's only 1 32bit number I know of, but there are several 16bit integers. The way I had been handling it is multiplying the high byte by 256 and then adding the low byte (for 16bit numbers), which works but isn't as efficient as the IBM link above, which I'll be using. However, I didn't take into account the fact that someone's system could be big endian, so my conversion back to single bytes when writing the files would've been wrong on big endian systems. If I do just use my VB.net code, I'll have to figure out how to check for endianness without pointers. I guess I could just write a simple c++ library to check for it.
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
LordHunter317":nhpyenmb said:
ShuggyCoUk":nhpyenmb said:
do the conversion to the file format desired values
Also known as: doing the byte swapping, and which means you're doing more than just reading/write a byte at a time. This still requires endianness detection and the whole nine yards, it just means you're baking it in the file I/O code. Which isn't necessarily a pointless thing to do, but it does mean you're still doing the conversions. They're entirely unavoidable is point I was trying to make.
The point I'm trying to make is that you can write code that is endian agnostic; you don't need to care about the specific endianness of the platform your code is compiled for. (The endianness of the file format itself is still important, yes, but that's a fixed format.)
Code:
uint32_t
ReadUInt32BE(FILE* fp)
{
   uint32_t val;
   val  = (getchar(fp) << 24);
   val |= (getchar(fp) << 16);
   val |= (getchar(fp) <<  8);
   val |=  getchar(fp);
   return val;
}

uint32_t
ReadUInt32LE(FILE* fp)
{
   uint32_t val;
   val  =  getchar(fp);
   val |= (getchar(fp) <<  8);
   val |= (getchar(fp) << 16);
   val |= (getchar(fp) << 24);
   return val;
}
And yes, you could perceive that as baking it into the file I/O code, but it still wouldn't need to "detect" the platform's native endiannness.
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
fitten":2z6uqpya said:
I guess you could do that... but sometimes it's actually useful to manipulate an integer as an integer (adding values to it, multiplying, etc.).
Well, you'd also want to separate the file serialization code from the rest of it. So you'd read the file from disk into memory in an endian-agnostic way, and thereafter you can do whatever manipulations you want (since the data will be in the native endianness).
 

fitten

Ars Legatus Legionis
55,637
Subscriptor++
stickboy":2fki246m said:
And yes, you could perceive that as baking it into the file I/O code, but it still wouldn't need to "detect" the platform's native endiannness.

Perhaps we are getting off into pedantry, but that code above is *still* written to deal with endianness issues ("baked into the IO as you call it... which is exactly where all the endian handling code would be regardless - hint: endianness is only an issue in IO... once it crosses the IO boundary, you don't worry about it anymore... you only convert it on the IO boundaries). You may call it "agnostic" but I'd say that it isn't and the evidence is that the name your functions have BE and LE in them (I'm assuming they mean Big Endian and Little Endian).

Personally, I'd go with the macro method, still, because it isn't tied to any particular input/output method (file, network, etc). Just define the macro and put it in everywhere you need IO changes and off you go. Not that it's a big deal but with macros, you can convert entire blocks of memory and then do block writes instead of writing each primitive one at a time. For example, a big array of ints... convert them all and write the entire array as one operation as opposed to 100s or 1000s of individual byte writing calls. I can guess which one might be faster ;)

Granted, this project isn't as grand a scale so it really doesn't matter in the grand scheme of things.
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
fitten":36te9tzx said:
You may call it "agnostic" but I'd say that it isn't and the evidence is that the name your functions have BE and LE in them (I'm assuming they mean Big Endian and Little Endian).
No, that's to represent the endianness of the file format, not the platform.

If the file format is defined to store 32-bits in little endian form, you'd always call ReadLE32, and the ReadLE32 implementation would be the same on all platforms.
 
Status
Not open for further replies.