Do you have any tips/recommendations when creating a cross-platform game in C/C++?
|
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:
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? |
|||||||||||||||
|
|
Try to avoid scattering 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. |
|||
|
|
|
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. |
|||
|
|
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). |
|||
|
|
|
|||
|