# Platform Independance

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

## Recommended Posts

Hiya folks! The man is back from his glorious vacation, and is full to bursting with questions! So I'll just siphon a few of them regarding platform independance, and put them down here [smile] As always, any and all help is appeciated. Ok, I'm on my second rewrite (didn't like the previous architecture) of my core code, and I'm going to work in platform independance from the offset here. Now, for starters, is this feasible, without having access to a Linux box? That is, I only have Windows XP to test on here, so will it be impossible for me to make something platform independant, even if I only implement the Windows side of it right now? Secondly, and, this one ashames me to ask it, how does one check, without a doubt, which platform one is developing (working, compiling, and testing) on? I thought I knew this one, but I guess not, because no matter how I wrack my brains, the answer escapes.. Note that I'm only trying for Linux/Windows independance here. Last but certainly not least, is this a viable solution to, at least partially, solving the platform independance? Basically, you have an abstract base class, OSInterface, with a number of standard functions, open file, read, write, etc, create window, etc. Just the works. Then, you have subclasses, LinuxOSInterface, and WindowsOSInterface, which implement these various common-to-all-OS functions. At startup, a global (or otherwise globally-accessable) pointer is assigned to an instance of either class, based on platform. So, instead of directly calling the OS functions, you access all the common functions through it, so it acts like a thin wrapper. But, would this work? Are Linux and Windows alike enough in that to be able to do something like this? With some specialization of course, but alike enough in the basics that this is workable? Thanks!

##### Share on other sites
To check which OS you are on, you can use some defines that your compiler does for you. On Windows, gcc and MSVC++ define '_WIN32', on Linux gcc (a.o.) defines __linux. This way you can do something like:
#ifdef _WIN32   g_interface = new WindowsOSInterface();#elif defined __linux   g_interface = new LinuxOSInterface();#else   #error "Could not determine OS"#endif

To try testing your code, you could download Knoppix. It is a Linux-distribution that runs from a bootable CD. This way you can test, without having to create a dual-boot system or having a separate Linux-box.

##### Share on other sites
Thanks, now that I see the answer, I feel like kicking myself in the face, because the moment I layed eyes on #ifdef _WIN32 I remembered the rest of it [smile] Anyway, thanks!

Regarding Knoppix, I'm hesitant to do anything like that with this computer, simply because we have a very shaky truce, and I don't plan to do anything that might even barely endanger it.. Which includeds booting a new operating system, even off a CD. Plus, I wasn't aware Linux had NTFS support, which all my drives happen to be. I'll probably end up getting some junky Linux box, along with the standard "thick ol' manual" [grin] Thanks anyway though.

Unfortunately, that really doesn't answer my important question, which was, is my solution workable, and are the 2 OSes that alike that I could do something like that without massive hacks to get one or the other OS to work?

[EDIT] Oh, also, does GNU define something like _GNU or a version number, as with MSVC?

##### Share on other sites
Quote:
 Original post by SirLuthorLast but certainly not least, is this a viable solution to, at least partially, solving the platform independance? Basically, you have an abstract base class, OSInterface, with a number of standard functions, open file, read, write, etc, create window, etc. Just the works. Then, you have subclasses, LinuxOSInterface, and WindowsOSInterface, which implement these various common-to-all-OS functions. At startup, a global (or otherwise globally-accessable) pointer is assigned to an instance of either class, based on platform. So, instead of directly calling the OS functions, you access all the common functions through it, so it acts like a thin wrapper.

It'd work, but why not use an existing library? SDL does basically that (provides a common interface to a set of cross-platform functionality) although the implementation might be compile time rather than runtime like you suggest.

##### Share on other sites
Quote:
 Original post by OrangyTangIt'd work, but why not use an existing library? SDL does basically that (provides a common interface to a set of cross-platform functionality) although the implementation might be compile time rather than runtime like you suggest.

I would have, if I was doing just another app. But I'm working on an application platform for anything else I might wish to do, which might even be defined as an engine, which may also get distributed at some point, so I don't really wish to be using another library's core that will have to be distributed as well, not to mention licensing, etc. Damn. I should shorten my sentences. Anyway, ya, thanks but no thanks [smile]

##### Share on other sites
For things like file I/O, memory allocation, etc. I believe the standard C libraries, as well as STL, are platform independent. You still have to recompile on the differente platforms, but the same code should work.

The tricky part is for stuff like making and controling windows, which is where SDL or wxWidgits or the GTK come in handy.

##### Share on other sites
Quote:
 Original post by SirLuthorI would have, if I was doing just another app. But I'm working on an application platform for anything else I might wish to do, which might even be defined as an engine, which may also get distributed at some point, so I don't really wish to be using another library's core that will have to be distributed as well, not to mention licensing, etc. Damn. I should shorten my sentences. Anyway, ya, thanks but no thanks [smile]

I understand your point, but distributing one or two extra DLLs on Windows isn't going to kill you. Most modern projects have like 10-20 DLL dependencies (at least).

You're certainly welcome to work from first principals, but quite honestly you're never going to get anywhere. There's just too many things that are platform dependent and require a layer above them. I'd highly recommend using SDL or something similar to abstract these basic operations (creating a window, input, threads, etc).

##### Share on other sites
Quote:
 Original post by AndyTXI understand your point, but distributing one or two extra DLLs on Windows isn't going to kill you. Most modern projects have like 10-20 DLL dependencies (at least).You're certainly welcome to work from first principals, but quite honestly you're never going to get anywhere. There's just too many things that are platform dependent and require a layer above them. I'd highly recommend using SDL or something similar to abstract these basic operations (creating a window, input, threads, etc).

Sure, they might not kill you. But I know that I can do whatever I damn well please with whatever I write. The same can not be said of SDL, or whatever else I choose to link in. Constraints are something I cannot stand. If, as a result I never get anywhere, well, I'm prefectly cool with that too, at least I'll have gone out trying, and will have learned something en-route. Who knows? I may even prove you wrong.. Nothing on you of course, but nothing would please me more [grin]

kingnosis: Aware. Thanks anyway though!

##### Share on other sites
Quote:
Original post by SirLuthor
Quote:
 Original post by OrangyTangIt'd work, but why not use an existing library? SDL does basically that (provides a common interface to a set of cross-platform functionality) although the implementation might be compile time rather than runtime like you suggest.

I would have, if I was doing just another app. But I'm working on an application platform for anything else I might wish to do, which might even be defined as an engine, which may also get distributed at some point, so I don't really wish to be using another library's core that will have to be distributed as well, not to mention licensing, etc. Damn. I should shorten my sentences. Anyway, ya, thanks but no thanks [smile]

IIRC, the more recent Unreal games use SDL (for at least cross platform display creation I'd guess). Redistribution really shouldn't be a problem. Equally theres nothing stopping you writing your own cross-platform sections when you find SDL/etc. too limiting and want to replace a specific part. But that doesn't mean you have to write *everything* yourself.

You've already said you're on your second re-write, and the odds are you won't get the platform independant bits right first time either (no-one does!). Especially if you're asking questions about how similar two file systems are. Why waste time producing an inferiour library when a good, free, debugged one already exists?

##### Share on other sites
Quote:
 Original post by OrangyTangIIRC, the more recent Unreal games use SDL (for at least cross platform display creation I'd guess). Redistribution really shouldn't be a problem. Equally theres nothing stopping you writing your own cross-platform sections when you find SDL/etc. too limiting and want to replace a specific part. But that doesn't mean you have to write *everything* yourself.You've already said you're on your second re-write, and the odds are you won't get the platform independant bits right first time either (no-one does!). Especially if you're asking questions about how similar two file systems are. Why waste time producing an inferiour library when a good, free, debugged one already exists?

That's quite interesting.. Might even tilt the balance and make me have a look at those parts of SDL mentioned, windowing, threads, and such. However, as for reasons why I would write my own, hell, my time is my own, is it not? Can't I be stubborn and make mistakes if I want to? There's something to be said for learning by falling and picking up again, surely? I enjoy learning things. If it means lots of effort, well, so do lots of things. I can live with it. :Þ

Cheers!

(Oh well, I guess I'll cave in and have a look at SDL...)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 15
• 14
• ### Forum Statistics

• Total Topics
634070
• Total Posts
3015330
×