Links of Interest
Main
Download
Prerequisites
Screenshots

Articles/Code Snippets
DBP Warmup Entries
Blog feed icon
Contact/Feedback

Quick Downloads:
bullet Diaspora
bullet XNA Requirements Installer

External Links
Heather [layout designer]
XNA

Game Screen Extension
Using this article on game screens as a base for my game engine structure, I decided to add to the class to be more versatile than the one described in the article.

First off, I found myself wanting to pop mutiple screens off a stack, when quitting the game in pause screen for example. So I added this simple function, which utilizes Types.

public void PopUntil(Type type)
{
    while (screens.Count > 0)
    {
        if (screens.Peek().GetType().Equals(type))
            break;
        else
            screens.Pop();
    }
}

The usage is pretty simple:

PopUntil(typeof(MainMenuScreen));

All it does is continue popping screens off the stack until it gets to a screen of the specified type. You could add error checking in case you specify a type that isn't contained in the stack, but I didn't feel a need to add that for my game.

Second, I added event-driven keyboard input. This is trivial to do, but it is not always an obvious thing to add. This way, you won't get duplicate commands to underlying screens. Hook the events:

KeyboardHelper.OnKeyDown += new KeyboardHelper.KeyEventHandler(KeyboardHelper_OnKeyDown);
KeyboardHelper.OnKeyPressed += new KeyboardHelper.KeyEventHandler(KeyboardHelper_OnKeyPressed);
KeyboardHelper.OnKeyUp += new KeyboardHelper.KeyEventHandler(KeyboardHelper_OnKeyUp);

And the actual methods:

void KeyboardHelper_OnKeyUp(Keys key)
{
    foreach (GameScreen gs in screens)
    {
        gs.OnKeyUp(key);
        if (gs.BlockUpdate)
            return;
    }
}

void KeyboardHelper_OnKeyDown(Keys key)
{
    foreach (GameScreen gs in screens)
    {
        gs.OnKeyDown(key);
        if (gs.BlockUpdate)
            return;
    }
}

void KeyboardHelper_OnKeyPressed(Keys key)
{
    foreach (GameScreen gs in screens)
    {
        gs.OnKeyPressed(key);
        if (gs.BlockUpdate)
            return;
    }
}

Third, I decided to register my GameScreenManager a service so I don't need to expose the manager through my game class. Declare the interface and then extend your game screen manager class.

interface IGameScreenService
{
    void Push(GameScreen screen);
    void Pop();
    void PopUntil(Type type);
}
 

Then, inside my main game class, I registered my game screen service.

gameScreenManager = new GameScreenManager(this);
Services.AddService(typeof(IGameScreenService), gameScreenManager);

And to retrieve it from any class with a reference to Game:

IGameScreenService gameScreenService = (IGameScreenService)game.Services.GetService(typeof(IGameScreenService));
gameScreenService.Pop();

This website and its content are the property of ascii reversal of the ascii of the hex reversal of the hex of the bit inverse of 11001111 11001111 11001000 11001010 11000110 11001001 10111100 11001001 10111100 11001001 11000110 11001001 11001110 11001001 10111011 11001001 11001111 11001101 10111100 11001011 11000110 11001001.