Search code examples
c#user-interfacexnaoverlayprojects

Drawing a GUI in XNA


I'm currently working on making an XNA 2D platformer, and I'm wondering if it's possible to draw a kind of static GUI over the game?

What i mean here is that while the game updates and the players position changes etc, the GUI would be drawn on a kind of static layer (overlay?), that would be the size of the window the game is being run in. That way the game wouldn't have to constantly update the GUI's position and thus be a little bit nicer to handle.

Any ideas?

Thanks for your time


Solution

  • Yeah, you can just draw them using spritebatch :)

    //HudTexture is a transparent texture the size of the window/screen, with hud drawn onto it.
    SpriteBatch.Draw(HudTexture, Vector2.Zero, Color.White);
    //Let's say that it works well to draw your score at [100,100].
    SpriteBatch.DrawString(HudFont, Points.ToString(), new Vector2(100,100), Color.White);
    

    if you plan on making more GUI (buttons etc) you might want to check out this answer I wrote to an other post: XNA DrawString() draws only a partial string

    Also; For drawing scores etc, I have experienced quite a bit of boxing/unboxing because of ToString. For that reason, I have often had an array of strings to represent the score:

    String[] ScoreText = new String[100000]; //As big as your max score
    
    //In Initializing method:
    for (int i = 0; i < ScoreText.Length; i++)
        ScoreText[i] = i.ToString();
    
    //In draw:
    SpriteBatch.DrawString(HudFont, ScoreText[Points], new Vector2(100,100), Color.White);