Any tips for creating cross-platform games?

Matias Valdenegro
  • Any tips for creating cross-platform games? Matias Valdenegro

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

    • 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.

  • 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).

  • 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 {
        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?

  • 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.

  • 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.

c++ c cross-platform
Related questions and answers
  • The answers to this cross-platform related question are useful, however I want my (C++) game to run on Windows and Linux (and be packaged for Ubuntu) only. I'm not interested in platforms like mobile or consoles. Are there any specific tips or suggestions for this case? Also, if I get the game to work on linux, is it difficult to add support for OSX? Updating with more information: It will be a 3D game simulating the motion of spacecraft in the solar system. We will use Ogre3D for the graphics. We're still in the design phase, so there's no code written yet.

  • Are you aware of a complete (or almost complete) cross platform math library for use in OpenGL ES 2.0 games? The library should contain: Matrix2x2, Matrix 3x3, Matrix4x4 classes Quaternions Vector2, Vector3, Vector4 Classes Euler Angle Class Operations amongh the above mentioned classes, conversions, etc.. Standardly used math operations in 3D graphics (Dot Product, Cross Product, SLERP, etc...) Is there such Math API available either standalone or as a part of any package? Programming Language: Visual C++ but planned to be ported to OS X and Android OS.

  • I'm looking into building a cross-platform opensource 2D RPG style game engine for ChaiScript. I want to be able to do all of the graphics with SVG and need joystick input. I also need the libraries I use to be opensource and compatible with the BSD license. I'm familiar with allegro, ClanLib, and SDL. As far as I can tell, none of these libraries have built in or obvious integration for SVG... support in a 2D C++ library while minimizing dependencies as much as possible (preferably avoiding Qt altogether)?

  • I'm creating a component-based game object system. Some tips: GameObject is simply a list of Components. There are GameSubsystems. For example, rendering, physics etc. Each GameSubsystem contains pointers to some of Components. GameSubsystem is a very powerful and flexible abstraction: it represents any slice (or aspect) of the game world. There is a need in a mechanism of registering... Components. 3: Component registers itself in GameSubsystem(s). We know at compile-time that there is a GameSubsystemRenderer, so let's ComponentImageRender will call something like

  • What is the best cross-platform game engine, being the most commercially supported, there is for free available for development on Linux? I'm planning on creating multiple, non-commercial, fps games to gain real industry experience in C++ game development.

  • I'm trying to get a 2 pass post-processing system going in OpenGL in a cross-platform manor using FBOs. I'm starting the dev on mac OSX (since in the past I've found it the most finicky to get...(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D...); glDrawArrays(GL_TRIANGLES, 0, 6); glBindTexture(GL_TEXTURE_2D, 0); } Tried everything I can think of or find in a FBO tutorial or have read about. I don't get any errors and it returns as complete

  • I'm making a game and want to use vector graphics. I started re-coding it using Cairo and the performance is horrific. So, I'm looking for a different library. It needs to be for C++ and cross-platform (e.g. no Direct2D). There's another question someone asked before like this, but there weren't any suitable answers. There's got to be something...?

  • This subject, as with any optimisation problem, gets hit on a lot, but I just couldn't find what I (think) I want. A lot of tutorials, and even SO questions have similar tips; generally covering...), and received almost no performance change. Whilst I am receiving around 40FPS in my implementation, which is by no means problematic, I am still curious as to where these optimisation 'tips' actually come into use? My CPU is idling around 20-50% during rendering, therefore I assume I am GPU bound for increasing performance. Note: I am looking into gDEBugger at the moment Cross posted at StackOverflow

  • I've been using SDL 1.2.14 for awhile without using many of the features from newer OpenGL versions. I'm now interested working only with the 3.3 API (not using the deprecated functions, etc... that's the latest my graphics card supports), but I'm having trouble getting set up. Here's some information about my development environment: Windows 7 64 bit Eclipse Helios CDT Mingw Toolchain C++ I've tried building SDL 1.3 and following this tutorial. This ended up being a pretty big pain and I gave up amid a stream of compile errors - I'd prefer to not go this route if possible. I know also