XNA is a fantastic technology and I want to learn it well.
If you don’t know nothing about 2D and 3D game development, I strongly recommend to buy the book “Learning XNA 3.0”. This is really easy to understand and it introduce to many concepts in a simple step by step approach. I read other three books about XNA but this is definitely the best as a starting point.
I would like to share with you my demo programs, without the purpose to be exhaustive but just to share interesting stuffs that I can find during my learning process.
It is quite useful to have a way to print text on the screen for debugging help. For this reason I created the ConsoleComponent class.
Firstly, I’ll show you how to use the console class in the game:
public class MyGame : Game
{
GraphicsDeviceManager graphics;
ConsoleComponent console; public MyGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
console = new ConsoleComponent(this, 17, Color.Yellow);
Components.Add(console); } protected override void Update(GameTime gameTime)
{
console.Clear(); console.WriteLine("GraphicsDeviceManager.DefaultBackBufferHeight = " + GraphicsDeviceManager.DefaultBackBufferHeight);
console.WriteLine("GraphicsDeviceManager.DefaultBackBufferWidth = " + GraphicsDeviceManager.DefaultBackBufferWidth);
console.WriteLine(); console.WriteLine("ValidAdapterFormats: "); foreach (SurfaceFormat surfaceFormat in GraphicsDeviceManager.ValidAdapterFormats)
{
console.WriteLine(" - " + surfaceFormat);
}
console.WriteLine();
console.WriteLine("ValidBackBufferFormats: ");
foreach (SurfaceFormat surfaceFormat in GraphicsDeviceManager.ValidBackBufferFormats)
{
console.WriteLine(" - " + surfaceFormat);
}
console.WriteLine();
console.WriteLine("ValidDeviceTypes: ");
foreach (DeviceType deviceType in GraphicsDeviceManager.ValidDeviceTypes)
{
console.WriteLine(" - " + deviceType);
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
base.Draw(gameTime);
}
}
It is extremely simple to use. You create an instance of the ConsoleComponent class passing a reference to the game object, the interline and the text color. Then you can use the WriteLine() method to draw a text while with the Clear() method you can clear all the text.
This is the code of the component:
public class ConsoleComponent : DrawableGameComponent
{
private SpriteBatch spriteBatch;
private SpriteFont consoleFont;
private float interline;
private Color textColor;
private List<string> messages;
public ConsoleComponent(MyGame game, float interline, Color defaultTextColor) : base(game)
{
this.interline = interline;
this.textColor = defaultTextColor;
this.messages = new List<string>();
spriteBatch = new SpriteBatch(game.GraphicsDevice);
consoleFont = game.Content.Load<SpriteFont>("consoleFont");
}
public void Clear()
{
messages.Clear();
}
public void WriteLine()
{
messages.Add("");
}
public void WriteLine(string text)
{
messages.Add(text);
}
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
spriteBatch.Begin();
float y = interline;
foreach (string text in messages)
{
spriteBatch.DrawString(consoleFont, text, new Vector2(interline, y), textColor);
y += interline;
}
spriteBatch.End();
}
}
In this simple example you can see how it is easier to draw 2D strings using XNA.
This is a screenshot of the application:
The complete Visual Studio 2008 project is available to the following link:
http://cid-1dcae6b548e3761c.skydrive.live.com/self.aspx/.Public/XNA/ConsoleComponent.zip