Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Long story short: I'm trying to intergrate my game with Microsoft's Game State Management. In doing so I've run into some errors, and the latest one is in the title. I'm not able to display my HUD for the reasons listed above.

Previously, I had much of my code in my Game.cs class, but the GSM has a bit of it in Game1, and most of what you have drawn for the main screen in your GameplayScreen class, and that is what is causing confusion on my part.

I've created an instance of the GameplayScreen class to be used in the HUD class (as you can see below). Before integrating with the GSM however, I created an instance of my Game class, and all worked fine.

It seems that I need to define my graphics device somewhere, but I am not sure of where exactly. I've left some code below to help you understand.

 public class GameStateManagementGame : Microsoft.Xna.Framework.Game
{
    #region Fields

    GraphicsDeviceManager graphics;
    ScreenManager screenManager;
            // Creates a new intance, which is used in the HUD class
    public static Game  Instance;


    // By preloading any assets used by UI rendering, we avoid framerate glitches
    // when they suddenly need to be loaded in the middle of a menu transition.
    static readonly string[] preloadAssets =
    {
        "gradient",
    };


    #endregion

    #region Initialization


    /// <summary>
    /// The main game constructor.
    /// </summary>
    public GameStateManagementGame()
    {
        Content.RootDirectory = "Content";

        graphics = new GraphicsDeviceManager(this);
        graphics.PreferredBackBufferWidth = 1280;
        graphics.PreferredBackBufferHeight = 720;
        graphics.IsFullScreen = false;
        graphics.ApplyChanges();

        // Create the screen manager component.
        screenManager = new ScreenManager(this);

        Components.Add(screenManager);

        // Activate the first screens.
        screenManager.AddScreen(new BackgroundScreen(), null);
        //screenManager.AddScreen(new MainMenuScreen(), null);
        screenManager.AddScreen(new PressStartScreen(), null);
    }


namespace Pong
{
    public class HUD
    {
        public void Update(GameTime gameTime)
        {

         // Used in the Draw method  
            titleSafeRectangle = new Rectangle
                (GameplayScreen.Instance.GraphicsDevice.Viewport.TitleSafeArea.X,
                GameplayScreen.Instance.GraphicsDevice.Viewport.TitleSafeArea.Y,
                GameplayScreen.Instance.GraphicsDevice.Viewport.TitleSafeArea.Width,
                GameplayScreen.Instance.GraphicsDevice.Viewport.TitleSafeArea.Height);

        }
    }
}

   class GameplayScreen : GameScreen
{
    #region Fields
    ContentManager content;
    public static GameStates gamestate;
    private GraphicsDeviceManager graphics;
    public int screenWidth;
    public int screenHeight;
    private Texture2D backgroundTexture;
    private SpriteBatch spriteBatch;
    private Menu menu;
    private SpriteFont arial;
    private HUD hud;
    Animation player;


    // Creates a new intance, which is used in the HUD class
    public static GameplayScreen Instance;

   public GameplayScreen()
    {
       TransitionOnTime = TimeSpan.FromSeconds(1.5);
       TransitionOffTime = TimeSpan.FromSeconds(0.5);          
    }

    protected void Initialize()
    {

        lastScored = false;
        menu = new Menu();
        resetTimer = 0;
        resetTimerInUse = true;
        ball = new Ball(content, new Vector2(screenWidth, screenHeight));
        SetUpMulti();
        input = new Input();
        hud = new HUD();

        // Places the powerup animation inside of the surrounding box
        // Needs to be cleaned up, instead of using hard pixel values
        player = new Animation(content.Load<Texture2D>(@"gfx/powerupSpriteSheet"), new Vector2(103, 44), 64, 64, 4, 5);

        // Used by for the Powerups
        random = new Random();
        vec = new Vector2(100, 50);
        vec2 = new Vector2(100, 100);
        promptVec = new Vector2(50, 25);
        timer = 10000.0f; // Starting value for the cooldown for the powerup timer
        timerVector = new Vector2(10, 10);

        //JEP - one time creation of powerup objects
        playerOnePowerup = new Powerup();
        playerOnePowerup.Activated += PowerupActivated;
        playerOnePowerup.Deactivated += PowerupDeactivated;

        playerTwoPowerup = new Powerup();
        playerTwoPowerup.Activated += PowerupActivated;
        playerTwoPowerup.Deactivated += PowerupDeactivated;

        //JEP - moved from events since these only need set once
        activatedVec = new Vector2(100, 125);
        deactivatedVec = new Vector2(100, 150);
        powerupReady = false;


    }
share|improve this question

5 Answers 5

up vote 4 down vote accepted

You could pass your instance of GraphicsDevice as a parameter in the constructor of you GameplayScreen and set your global variable you have already declared as the value of your input parameter like so

    class GameplayScreen : GameScreen
    {
        ...
        private GraphicsDeviceManager graphics;

        public GameplayScreen(GraphicsDeviceManager graphicsDeviceManager)
        {
           this.graphics = graphicsDeviceManager;
           TransitionOnTime = TimeSpan.FromSeconds(1.5);
           TransitionOffTime = TimeSpan.FromSeconds(0.5);          
        }
        ...
    }

This is assuming however that you have access to your graphics device instance from where ever you are creating an instance of this class (I assume GameStateManagementGame)

share|improve this answer

First rather than doing the whole static Instance thing, pass (this) as a parameter when you create the HUD also you have a GraphicsDeviceManager in the GameScreen which isn't needed as you have a reference to the GameStatemanager which should also have a reference to a GraphicsDeviceManager.

but to answer you main query you access the GraphicsDevice by GraphicsDeviceManager.GraphicsDevice so from your GameplayScreen class it would be

graphics.GraphicsDevice

which isn't public atm :-)

share|improve this answer
    
What isn't public? And where am I placing this graphics.GraphicsDevice ? –  Dave Voyles May 15 '12 at 22:26

ScreenManager has a reference to the Game, and Game has a reference to the GraphicsDevice. So:

GraphicsDevice gfx = ScreenManager.Game.GraphicsDevice;
share|improve this answer

You can always grab the graphics device from the Game class ... so as long as you have a reference to that you can do:

IGraphicsDeviceService graphicsservice = (IGraphicsDeviceService)Services.GetService( typeof(IGraphicsDeviceService) );

Reference here: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.services.aspx

share|improve this answer

I'm not sure ,but did you check for it in the ScreenManager? Some architectures for state management i used kept the graphics device reference in there .

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.