Getting game portability right from the start!

Whenever I want to start to make a game I try my best to make it independent from the platform as much as possible, no matter the platform I’m writing it for, and in the end it all will come down to use. I’d say it all starts up with a decent design to spare you the trouble and time you would take to fix troubles you could have avoided.

I believe that the best way to think about making decent software is to layer your design, in fact separating your software into different modules that are connected by “interfaces” is very elegant way of dealing with software problems, getting to break a problem into several small pieces allow you to deal with each of these problems alone, and thus giving you a clear picture of what really to do rather than having a goad to accomplish with no specific plan of how to achieve it.

For example it would better to understand and take action when you hear:

1. Take your car to the insurance company and complete the procedures
2. Take your car to the DMV and complete the procedures with them.
3. Take all your papers and give them to the Post Office to be able to receive the confirmation.

Rather than just saying:

X. Register your car with the DMV.


That was a random example but you get the idea, let’s say that you are about to build a game from the ground up and you are trying to keep things as portable as possible, I would really recommend constructing this kind of layers:

Basically, you get to write your code into three significant parts, that is to make sure you preserve compatibility among other platforms with the least code modifications as possible, starting from bottom:

1. Operating system specific code

Here is where you should put the code which is specific to a particular platform, say you’re trying to write a code which takes the width and height of your screen and draws the background starting from the top left point on screen.

int width = Screen.getWidth();
int height = Screen.getHeight();

System.drawImage(background, 0, 0, width, height);

 

Just imagine these are specific system calls to a particular operating system that would allow you get the width and height to be able to draw the “background” starting from (0, 0) all the way to fill the whole screen.

2. Container Class

Now this code will surely be different on different platforms and that’s why we need to build a container class to abstract the specific platform implementation from getting exposed and used. Say in this case our container would be a class named “Drawer” and it will contain the code wrote earlier to that specific platform:

class Drawer{

private:

	Bitmap background;

public:

	static void drawBackground(){
	
		int width = Screen.getWidth();
		int height = Screen.getHeight();
		System.drawImage(background, 0, 0, width, height);
	
	}
}

 

Now the magic resides in this container class, everything that would be platform dependent would reside in this class.

3. Game code

When you are writing your game code and you want to draw the background all you have to call is the drawBackground function which resides in the container class “Drawer” in this way:

Drawer.drawBackground();

 

Now say you want to port this game to another platform, guess what you  have to do..? Exactly, all you have to do is change the container class which is our case is “Drawer” to hold the specific code of the new platform, and as you observe the game code won’t be changed.

To summarize, you’ll notice that your game code will never change at all among the platforms you’re porting your game to! Keeping your code clean and neat! And the only work would be in the container class code where you really will be putting platform independent code.

Leave a Reply