Cross platform game design and development using OpenGL
March 30, 2012
The following content is a summary of an article I wrote for the Software Developers Journal publication in their March issue of this year, under the same title as above:
“Video and PC games are a big part of our lives today. From the text and vector graphics based games in the past, to the interactive 3D environments we immerse ourselves in today, games have come a long way as have the hardware and software platforms that host them. Today, games could be said to be cross-platform in many contexts, as the many different operating systems, hardware platforms (Xbox, Playstation etc), and web browsers are all platforms that games run on. An HTML5 game could be said to be cross platform as it can be played in a browser on a Mac, PC or Linux environment, but on a higher level it runs on top of one platform: the web. In any case, for our purposes in this article, we will define cross platform in its classical sense: Targeting multiple OS systems such as Windows, Linux or Mac.
Why would anyone want to develop cross platform games, specifically using OpenGL? One main reason for adopting a cross platform strategy would be to reach a wider audience. Startups and open source entities would benefit from this in many ways. Another aspect that is gaining momentum is the online and social gaming phenomenon that has been growing from few years back. The games that are played on browsers and mobile devices are rapidly growing in number and complexity. The 3D environments and complex functions in the traditional PC games can be seen to be morphing into the Browser and handheld devices. An interesting observation in all this is the role that OpenGL is playing silently. OpenGL is a graphics library that interfaces with the video hardware to create the graphical elements we program it for. The majority of mobile devices in use today (including the iOS range of devices) employ a subset of OpenGL known as OpenGL ES (Embedded Systems) to run graphics, animations, and games. On the web, the latest browsers use an underlying implementation of OpenGL ES conveniently named WebGL, to use the GPU (Graphics Processing Unit) of the client machine to accelerate graphics and motion on the browser, enabling amazing 3D effects and environments.
A bit about OpenGL: OpenGL is a graphics API that can be used to render graphics via a set of state machine commands. A state machine is simply a pipeline of variables which are set to some value and retain them till they are changed again. For example, you can assign the variable representing the background color to be green, and it will stay green till the value is changed again to some other color. For this reason, there is a loop that runs the rendering component, and depending on external inputs, conditional changes, the state machine variables will be changed within this loop to provide the graphical output we see as motion, graphics and interactivity.
So how do we go about designing a game to be cross platform compatible? The main issue and whole point of having cross platform libraries is that there will always be platform specific code. The manner in which threads, file operations, network functions and other constructs are implemented in different platforms vary in details, and as a result, the way we have to program varies from platform to platform as well.
The approach I outline is to first create simple abstractions on top of the platform specific code that a game would need to address, and to create self-contained libraries out of them. Examples would be thread creation and operations, network access, file and directory functions etc. Using 3rd party libraries for this is a good reuse practice but sometimes bring on their own learning curves and integration issues. Having said that, there are many good 3rd party libraries out there for creating solid commercial grade games (checkout Intel parallel application blocks for example), which should be researched and scrutinized well before using them in our application. This would be the norm than the exception, as a game needs a myriad of resources, physics, music, sounds, user input handlers and other details that cannot be handled by one developer writing custom code for all this. The end result is this: to have the game logic and functionality as encapsulated as possible, and to only compile our platform specific custom libraries and 3rd party libraries in order to achieve portability. One simple way of visualizing a model as described can be seen in the below image:
Of course, attempting to write a large commercial or open source game can bring the need for more advanced architectures and the employment of game engines or rendering engines. Irrlicht and Ogre are two popular rendering engines which can use OpenGL or DirectX at the lower level. If really ambitious, the IdTech4 game engine (which was used in Doom, Wolfenstein) is open sourced and readily available for analysis or modifications. My advice is to research a simple open source game engine and try and customize it to the needs at hand. It is my hope that I have succeeded in showing the readers that designing and creating games can be as fun as playing them.”