2010-11-14 4 views
4

Je travaille sur un jeu Spaceship 3D avec XNA 3.1XNA étrange peinture 3D lors de la peinture texte

J'ai essayé de séparer mon code de peinture de la logique du jeu (altought XNA en fait presque).

J'utilise une classe statique spéciale qui peint mes modèles à l'écran ... la classe principale du jeu utilise ce code à la peinture:

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); 
    } 

Mais quand je peins texte, quelque chose de bizarre se produit ... Voici une image (original):

Comme vous pouvez le voir, tout semble chevauchée (mais en place) ... Comme les turbines de vaisseau spatial.

Le code à l'intérieur GraphicHelper.drawOverlayText est la suivante:

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(); 
    } 

Et cette classe statique a cet attribut:

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"); 
    } 

espère que vous pouvez me aider :)

Répondre

8

L'étrange vous voyez rendu est parce que le tampon de profondeur est désactivé. Il est désactivé lorsque vous utilisez SpriteBatch. C'est une bizarrerie connue de l'API XNA 3.1. XNA 4.0 au moins, il est un peu plus évident que cela se passe.

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

La solution consiste à définir fondamentalement vos états de rendu à ce que vous voulez qu'ils soient, après avoir utilisé SpriteBatch. Dans ce cas, au moins, ensemble:

GraphicsDevice.RenderState.DepthBufferEnable = true; 

(Il peut aussi y avoir d'autres états là-dedans que vous voulez revenir en arrière.)

+0

Merci Andrew, qui l'a fait! Je savais que c'était quelque chose à voir avec le périphérique graphique mais pas exactement quoi: P – RodrigoCR

Questions connexes