Reputation: 69
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);
}
Upvotes: 3
Views: 1755
Reputation: 3745
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.
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:
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 cornerwidth
and height
are the size of the rectanglexoffset
and yoffset
is the offset on screenxadvance
is the distance to the preceding characterpage
is the index of the bitmap that contains the characterText 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:
The font example was created with a free Bitmap Font Generator.
Upvotes: 3