質問

I am very new to programming, specifically creating GUIs; I've previously only written console programs in C#, and am now attempting to build a game with XNA. Could anyone help me by letting me know how to print to the screen in a GUI? I'm currently trying variations of System.Console.WriteLine("");, but will not print to screen. The code I am using is below. Can anyone tell me where I'm going wrong?

ResourceCounter.cs:

      public ResourceCounter(Vector2 pos, GameTime gameTime)`
       {
        position = pos;
        over = false;
        clicked = false;
        gameTime = new GameTime();`

        currentTime += (float)gameTime.ElapsedGameTime.TotalSeconds; 
        System.Console.WriteLine("Resources: ", pSourceCount);

        if (currentTime >= countDuration)
        {
            pSourceCount++;
            **System.Console.WriteLine("Resources: ", pSourceCount);**
        }
        if (pSourceCount >= limit)
        {
            pSourceCount = 0;//Reset the counter;

        }


    }

and in my main GameScreen.cs class:

    public override void Update(GameTime gameTime)
    {
        MouseState mState = Mouse.GetState();
        menu.Update(mState, cursor, gameTime);

        if (menu.currentstate == State.campaign)
        {
            campaign.Update(pTower);
            //campaign.Update(eTower);
            **System.Console.WriteLine("Resources: ", pSourceCount);**
        }
        base.Update(gameTime);
    }
役に立ちましたか?

解決

Use the SpriteBatch.DrawString method for rendering text with XNA.

When you print text with System.Console.WriteLine it is written to the standard output stream. The data in this stream is then presented through the console. Your game window takes no notice of this at all.


Text Rendering

It's fair to say that everything you draw to your game window consists of (textured) triangles. Text is no exception. A single character is rendered using a quad (rectangle), that again consists of 2 triangles.

The text XNA! would be drawn using a total of 4 sprites (8 triangles), one sprite for every character:

text rendered in "Fill" and "Wireframe" mode

Bitmap Font

Common font formats store the characters as vectors. For bitmap fonts (the kind of font usually used in games) the actual type face is stored in one or more bitmaps (textures). This method is much faster, as the character shapes themselves are already rasterized and only have to be sampled from the texture. The obvious drawback is, that the characters all come in a fixed size. You need one bitmap font per font face per size per weight.

This is a sprite map for the Arial font, size 20, standard weight:

For drawing a certain character, the renderer has to know where on the bitmap the character is located. As you can see in the image above, each character occupies a non overlapping rectangular area. The dimensions of those rectangles can be stored in an additional text or XML file.

For example, this could be a definition of the A character for the sprite map above:

<char id="65" x="235" y="47" width="12" height="13" xoffset="0" yoffset="3" xadvance="11" page="0" />
  • id is the ASCII value (A=65)
  • x and y are the position of the top left corner
  • width and height are the size of the rectangle
  • xoffset and yoffset is the offset on screen
  • xadvance is the distance to the preceding character
  • page is the index of the bitmap that contains the character

Conclusion

Text rendering for games is not as trivial as one would assume. XNA greatly simplifies the process with the SpriteBatch class. A summary of the steps involved:

  • Convert a standard font to a bitmap font
  • Load the font files in your game application
  • Create the sprites for a given string
  • Render the sprites to the screen

The font example was created with a free Bitmap Font Generator.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top