Is opengl cross platform? If not, how a game be ported?

pipsqueaker117 picture pipsqueaker117 · Dec 23, 2012 · Viewed 19.4k times · Source

Ok, so say I'm writing a game in c++ with opengl. The game draws a few, simple shapes like squares on the screen. Now say I want to run the "game" on windows. How would I go about porting it? The actual c++ wouldn't be the problem, as it can be recompiled to fit the environment from what I understand. But the calls to the openGL library would be broken wouldn't they? The actual opengl library which would be included for development woul be compiled for the os, which would necessitate redownloading the library, which would lead to modifications to the source. Which would break the program on the original environment, right?

tl;dr Is there any way to build an opengl game for multiple operating systems without having to modify the source code to be platform specific or downloading external wrappers or libraries?

Answer

Joseph Mansfield picture Joseph Mansfield · Dec 23, 2012

OpenGL is platform independent, rather than cross platform. It is just a specification for the interface of a graphics library. It has no concern for the platform it is being implemented on. It just describes functions, what they're called and what they do.

Specific implementations of the OpenGL library may be cross platform or may be not. For example, Mesa is cross platform to some degree, despite being mainly developed for Linux. You'll need to find implementations of OpenGL on each of the platforms you want to compile on.

However, there is some code that needs to be executed before you can start using the OpenGL interface. This code is for setting up the OpenGL rendering context. How you do that is platform specific. For example, on Linux, GLX is an extension to the X Window System that provides an interface for creating the rendering context. This will only work on systems that are running the X Window System. On other systems, you will have to do something different to do the same job.

There are cross platform libraries for doing this job of creating the rendering context for you. For example, take a look at GLFW. It's a modern alternative to a library called GLUT, but GLUT is not maintained any longer. You can avoid having platform specific code in your source and give that job to the library to manage.

Note that OpenGL only provides a graphics library. Nothing more. There may be many other libraries and interfaces that a game uses that will not be platform independent, such as input, sound, etc. If your game only contains pure C++ and the only library is OpenGL then the source may port very smoothly to another platform, as long as your C++ code doesn't rely on any unspecified behaviour. You may not even have to change the rendering context set up if you used a library like GLFW.

It's also worth mentioning things like SFML and SDL. These libraries are cross-platform and provide an interface for all sorts of multimedia. They will set up your OpenGL rendering context, create windows for you, draw to the screen, deal with I/O and sound, and much more. They can be limited in some respects, but they're powerful in that they get you to a great position on all platforms very quickly.