Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Do you have any tips/recommendations when creating a cross-platform game in C/C++?

share|improve this question
add comment

closed as too broad by Josh Petrie Dec 16 '13 at 18:07

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

5 Answers

up vote 42 down vote accepted

Hide anything platform-specific behind abstraction layers

This means stuff like rendering, audio, user input, and file IO. One common trick to abstracting that without inducing a run-time performance penalty is platform-agnostic headers with platform-specific implementation files:

// FileSystem.h
class FileSystem {
public:
    FileHandle OpenFile(const char * path);
    // other stuff...
}

// FileSystemWindows.cpp
FileHandle FileSystem::OpenFile(const char * path) {
    // call windows API...
}

Then configure the builds for each platform to build against the proper .cpp file.

Make your content pipelines read in platform-independent assets and output platform-specific content for each platform

For example, author your textures as .tga or just .psd, then have a pipeline that can automatically convert those to the different platform-specific formats you need.

Don't assume a specific memory layout or endianness in your data

Platforms may vary in byte order, padding, alignment, and word size. Any code that cares about that must be thoroughly tested on all platforms.

Test on all platforms, all the time

You will get bitten by platform-specific bugs. Would you rather get bitten now, or right when you're trying to get the game out the door?

share|improve this answer
    
lack of #ifdef PLATFORM_WIN ftw –  tenpn Jul 18 '10 at 14:16
    
You'll never escape that. You can just hope to minimize it. –  munificent Jul 18 '10 at 18:06
7  
A good way to do that is to move all the platform specific code into a set shared libraries with the same interface then you can limit the #ifdef's to bootstrapping the correct library for the platform. –  Neel Jul 22 '10 at 10:44
1  
+1, great comment. Remember that even the STL is platform-dependant (and yes, some functions do different things on different compilers). –  Simon Aug 12 '10 at 6:18
    
+1 for the last point (always test) –  Bart van Heukelom Aug 24 '10 at 11:10
show 2 more comments
  • Use an api like opengl/sdl which will give you minimum hassle when going from platform to platform.

  • Make sure you know what platforms you want to support. Don't use opengl just because you think you might want to support multi platform in the future. Start as you mean to go on.

  • Know what hardware limitations are on each of the platforms you want to support.

share|improve this answer
    
The second point cannot be stressed enough. Make sure you know who the game is aimed for (meaning that, if it's a casual game, you'd want to have it on at least OSX and Windows, thus OpenGL would be the best option). As for the third point - and this is where the engine comes into play - is also important. It would be illogical to use a feature-rich, resource-heavy engine like Unreal to make a DS game. The point is also related to what I said regarding the second one: Establish who the game is aimed for. If you're going for a casual game, most people's computers won't handle, say, particles. –  Paker Jul 14 '10 at 20:58
add comment

Try to avoid scattering #if defined(_FOO_) ... #elif defined(_BAR_) ... #else ... #endif all over the code.

Sometimes it looks like the it's the easier way, but it will give you troubles in the long run. You should try to limit them to their own file, so that every platform implementation is self contained.

Adding a platform should be mostly about adding new implementation files, and just modify the few needed existing ones.

share|improve this answer
add comment

There are plenty of libraries that are themselves cross-platform; you don't need to write directly in OpenGL to run anywhere. OGRE is a great example of a rendering engine that works on any platform you'd care to name.

In my experience, the biggest concern is your build system. If you develop on Windows with Visual Studio, your code might compile in Linux, but you'll have a tough time getting it to build easily. Look at build systems like CMake that can use the same build instructions to generate platform-specific project files (makefiles on linux, VS Projects on windows, XCode Projects on Mac, etc).

share|improve this answer
add comment

When writing your functions for saving and loading files, or communicating over a network, don't forget about endianness.

Instead of reading a big structure with a single call to fread/fwrite or something low level like that, create a ReadByte/WriteByte and ReadFloat/WriteFloat. Then you will have fewer places to make changes if you decide to target a different platform later.

share|improve this answer
    
+1 * 9000. I guess this would really only apply to mobile phones in terms of modern/used architectures (RISC); but still crucial. –  Jonathan Dickinson Nov 17 '11 at 22:21
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.