I’m implementing a simple game for an assignment that would ideally run both on Unix(using X11’s graphics context) and Windows(using Window’s GDI) with both of their window manager. What would be the best way to structure the code so that I can simply plug in the correct C++ class to make it run on each OS?
More specifically, how do I completely separate out the graphics & windowing component out of the actual game component? (e.g. use observer pattern so that the game component would observe on the platform-specific windowing component for user input, create a common interface for loading/displaying sprite, create another common interface for repainting, etc). Who should be responsible for copying and pasting each object’s graphics/device context to the main screen’s gc/dc? Which component should contain the main eventloop/window callback(if it’s the main component, then do I need two versions of the main component for each platform?) Also, how do I make each object(e.g. blocks, characters etc) hold platform specific datatypes(gc for X11 and DC for GDI)?
The two basic requirements are that I can only use X11 and GDI and it cannot be multithreaded.
Games are rarely intrinsically platform dependent. Simple games can be made with GLUT and it is very easy to maintain two versions of the code using #defines. The callbacks are handled by GLUT.
In this way GLUT will enable you write nearly the same code for Windows and Linux. (Because issues such as making the context are already handled by GLUT).
Unless your project is to explicitly re-write GLUT, I recommend you stick with it and get on with writing your game.