2. How Can We Reduce Platform Dependence

Because messages are generated by the system, one must always rely on a platform dependent library of functions. The best way to reduce such dependence is to create a Game object whose event/message handlers are called by the event/message handlers of the "system." The code of the Game object will be self-contained at the expense of an extra function call for each message/event.

Example 1: Here is how the arrangement might look using MFC (Microsoft Foundation Classes). The pertinent MFC object is the ChildView, so we would have:

// in file ChildView.h
class CChildView : public CWnd
{
	/* ..... */
public:
	CGame m_Game;
	/* ..... */
}
// in file ChildView.cpp
/* .... */
void CChildView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	m_Game.Display(&dc);
}
/* .... */
void CChildView::OnMouseMove(UINT nFlags, CPoint point)
{
	m_Game.MouseMove(nFlags, point.x, point.y);
}
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
	m_Game.LeftButtonDown(point.x, point.y);
}
/* .... */

Example 2: Here is a piece of similar code in Java.

// inside a class that extends panel
	public void paint(Graphics g)
	{
		game.Display(g);
	}
	
	public void mouseMoved(MouseEvent e)
	{
		// contruct flag from e 
		game.MouseMove(flag , e.getX(), e.getY());
	}
	public void mousePressed(MouseEvent e)
	{
		if(e.getButton()==BUTTON1) game.LeftButtonDown(e.getX(), e.getY());
	}

In this way the code to be changed during moves across platforms is small and outside the main game code. The Display() function is the most challenging because the argument is different in each case. One might ask why bother with such a partition since we will have to rewrite all the code anyway by going from one language into another. Two reasons: we may change window system without changing the language; and it is much easier to rewrite code that deals with numerical and logical operations than code that deals with interactions with the environment. For such operations the codes for Java, C# and C++ are quite similar to each other.

In the sequel we shall the use the term "mouse" to denote any device that controls the cursor on the screen. It may be a touch-pad, a trackball, or even arrow keys. The particular device may affect the function wrapped around m_Game.MouseMove(), but not the latter.

Previous Section Next Section