Xna Screen Manager

I know there’s lot’s of this stuff over the internet but I keep bumping into people asking for this.

A way to easily switch from a Game Screen to a Menu or Options without having tons of flags and “if” clauses on the class Game.

I’ve made a small project with a Screen Manager. The ScreenManager is static and can contain Screens. Instead of having typical Draw Update functions drawing SpriteBatches on the Game class we should have something like this:

 protected override void Update(GameTime gameTime)
        {
            // Tell ScreenManager to Update
            SCREEN_MANAGER.Update(gameTime);
            base.Update(gameTime);
        }
 
        protected override void Draw(GameTime gameTime)
        {
            // Tell ScreenManager to draw
            SCREEN_MANAGER.Draw(gameTime);
 
            base.Draw(gameTime);
        }

This way we are telling the Screen Manager to handle the draws and Updates. And the ScreenManager then tell the active screen to Draw/Update.

In this sample if you press the keys ‘m’ or ‘n’ you can switch from screen1 to screen2 and vice-versa. Notice that the color changes, you are inside a different Screen now.

In order to add another screen it would be something simple as:

    class Screen3 : Screen
    {
        public Screen1(GraphicsDevice device) :base(device,"screen3")
        {
        }
 
        public override bool Init()
        {
            return base.Init();
        }
 
        public override void Shutdown()
        {
            base.Shutdown();
        }
 
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }
 
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
    }

You can tell the Screen Manager to switch screen by doing:

SCREEN_MANAGER.goto_screen("screen3");

Screen Manager then calls the virtual function shutdown() of the current screen and then the init() of the requested Screen, so you might want to always override them for your purposes.

You’ll understand better by viewing the code itself. Use it in any way you want. I now this example may not be the best solution but it gives a good start to those wanting to implement their own.

Download the source.

EDIT:

(04-02-2010) Removed unused member _current; (Thanks Jason)

19 Comments

  1. Pingback: XNA Screen Manager « Sgt. Conker

  2. Jason

    Hi david, excellent Screen Manager, very simple which i appreciate as i’m just cutting my teeth on the concepts. I have a question reguarding a member in the manager ‘static private Screen _current = null;’ this never seems to be used, maybe it was your initial ActiveScreen and just became left out?

  3. Pingback: The XDSK | Nelxon Studios

  4. Michael

    Hey I know it’s a pretty late reply but I just wanted to say thanks for the post, and especially for the source. XNA is usually pretty well documented but I couldn’t seem to find much about this in the documentation.

    Cheers.

  5. Musiek

    Hi. I am just wondering how can i load content on those screens? Can I or I must write code in game1 file?

  6. Pingback: XDSK2 – XNA 4.0 Resources | Nelxon Studio

  7. w

    Hey, thanks for the tutorial!

    Any chance you could update it for XNA 4.0 / VC# 2010?

    thanks,
    w

  8. Matt

    is there any way that you could make a simple example static ContentManager class? I have been trying for hours to get this right! but great job on this, too.

  9. Dude_Meister

    i’m trying to use a particle system within the gameplay screen to no avail. my previous implementation of it was passing it through the graphics (graphics = new GraphicsDeviceManager(this);) in game.cs but i’ve since moved the implementation to the gameplayscreen. is there a way i could still pass it through the graphicsdevicemanager?

  10. joejexy

    Is there a way to load a picture on the menu system, ive tried making a static class with content manager on it, but it keeps giving an error saying the file im trying to load is not found. please help me this is the perfect menu system ive been searching for but i cant load anything on it, its like getting a lamborghini for a birthday present and not being able to open it 🙂 .

  11. Pingback: Resources for XNA indie developers | Indie Games Bundle - indieGamesPack.com

  12. Pingback: XDSK2 (XNA 4.0 Resources) « Nelxon

  13. Pingback: Link – A simple game state management guide | josh-co.com

  14. Pingback: XNA – Simple Screen Management « Josh-CO Dev

  15. Greg

    Whats the different between using this or a singleton pattern ? What benefits

  16. Pingback: XDSK2 – XNA Developer’s Survival Kit 2 – Nelson Hurst

Leave a Reply