Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XNA weird 3D painting when painting text

Tags:

c#

xna

paint

sprite

I'm working on a 3D Spaceship game with XNA 3.1

I tried to separate my painting code from the game logic (altought XNA makes this almost).

I'm using a special static class that paints my models on screen... the main Game class uses this code at painting:

protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.Black);

        // Draws the stage (the skybox and objects like rocks)
        stageManager.Draw(gameTime, GraphicsDevice, camera);


        // Draws each player and each Enemy on stage given the camera
        foreach (Player p in players)
            p.Draw(camera);

        foreach(Enemy e in enemies)
            e.Draw(camera);

        if(Configuration.Debug)
            col.renderColBoundings(GraphicsDevice, camera);

        GraphicHelper.drawOverlayText("50", "10"); // "Error" line...

        base.Draw(gameTime);
    }

But when I paint text, something weird occurs... Here's an image (original):

As you can see, everything looks overlapped (but in place)... Like the spaceship turbines.

The code inside GraphicHelper.drawOverlayText is this:

public static void drawOverlayText(String p1Hp, String currEnemies)
    {
        string text1 = "Player1: " + p1Hp;
        string text2 = "Enemies: " + currEnemies + "/10";
        string text3 = "Space Hogs Beta";

        spriteBatch.Begin();

        // Draw the string twice to create a drop shadow, first colored black
        // and offset one pixel to the bottom right, then again in white at the
        // intended position. This makes text easier to read over the background.
        spriteBatch.DrawString(font, text1, new Vector2(651, 11), Color.Gray);
        spriteBatch.DrawString(font, text1, new Vector2(650, 10), Color.White);

        spriteBatch.DrawString(font, text2, new Vector2(851, 11), Color.Gray);
        spriteBatch.DrawString(font, text2, new Vector2(850, 10), Color.White);

        spriteBatch.DrawString(font, text3, new Vector2(741, 611), Color.Gray);
        spriteBatch.DrawString(font, text3, new Vector2(740, 610), Color.White);

        spriteBatch.End();
    }

And this static class has this attributes:

static ContentManager content;
    static GraphicsDevice graphics;
    static Camera camera;
    static Dictionary<String, Model> models = new Dictionary<string, Model>();
    static SpriteBatch spriteBatch;
    static SpriteFont font;

    public static void initHelper(ContentManager c, GraphicsDevice g, Camera cam)
    {
        content = c;
        graphics = g;
        camera = cam;
        spriteBatch = new SpriteBatch(g);
        font = c.Load<SpriteFont>("Fonts/main");
    }

Hope you can help me :)

like image 588
RodrigoCR Avatar asked Feb 25 '23 20:02

RodrigoCR


1 Answers

The odd rendering you are seeing is because the depth buffer is turned off. It gets turned off when you use SpriteBatch. This is a known oddity of the XNA 3.1 API. XNA 4.0 at least makes it a bit more obvious that this is happening.

Here is an explanation of what render states are changed by SpriteBatch in XNA 3.1. And here is the same thing for XNA 4.0.

The solution is to basically set your render states back to what you want them to be, after using SpriteBatch. In this case, at least, set:

GraphicsDevice.RenderState.DepthBufferEnable = true;

(There may also be some other states in there you want to change back.)

like image 171
Andrew Russell Avatar answered Mar 06 '23 17:03

Andrew Russell