Multiple 'Screens' in a Game

Cags

Contributor
Joined
Feb 19, 2004
Messages
695
Location
Melton Mowbray, England
First out I should mention that my project is for the Pocket PC and using .Net 1.1, thus there are certain restraints due to the Compact Framework. Essentially what I'm wondering is the best way to handle multiple 'screens' what I mean by this is 'Main Menu', 'Game', 'About', 'Options' etc. Logically speaking I suppose you can think of each 'screen' as a seperate form, but I need to only use a single form.

The way I was thinking about achieving this is to have a 'Screen' class or Interface that would implement methods for Painting and for handling input events such as Key events and Mouse events. Then each screen would Inherit this class/interface. The main form would then have a ChangeScreen method that would unhook the old classes methods from the events and hook up the new ones. Alternatively if there is some speed issue with event hooking/unhooking a switch statement could be used to call the correct method based on which screen is showing. This method would be less dynamic, but as the amount of different screens in the game would be static this wouldn't really matter.

Here's a quick example...
C#:
public interface IScreen
{
	void PaintEvent(object sender, PaintEventArgs e);
	void ClickEvent(object sender, EventArgs e);
	void KeyPressEvent(object sender, KeyPressEventArgs e);
}

public class MenuScreen : IScreen
{
	public void PaintEvent(object sender, PaintEventArgs e)
	{
		// do stuff
	}

	public void ClickEvent(object sender, EventArgs e)
	{
		// do stuff
	}

	public void KeyPressEvent(object sender, KeyPressEventArgs e)
	{
		// do stuff
	}
}

// on the form
public void ChangePage(IScreen screen)
{
	// unhook events
	this.Paint = null;
	this.Click = null;
	this.KeyPress = null;

	// hook the new methods to the event
	this.Paint += new PaintEventHandler(screen.PaintEvent);
	this.Click += new EventHandler(screen.ClickEvent);
	this.KeyPress += new KeyPressEventHandler(screen.KeyPressEvent);
}

It's worth noting at this point that this is all theoretically I haven't tried it. I was just wondering if anyone else had either an opinion on this approach or an alternative approach for achieving the same result.
 
Back
Top