jeudi 5 mars 2009

Very First Step with XNA framework (part 2/2) : Hello World

Eh, what's up? Ready for an exiting tutorial.

Today we will code our first XNA game... hmm... not really a game. Let's say a classic "Hello World" program.

Step 0: Create our project
Let's create a new project with Visual Studio Project Creation Wizard.
  1. Menu: Choose File
  2. Menu: Choose New Project
  3. In Project types tree: Choose XNA Game Studio 3.0
  4. In Templates list: choose Windows Games (3.0)
  5. In the footer: Name your project
  6. In the footer: Choose your project Location
  7. Then: Click OK
Step 1: We need a font
There is many way to create font. Remember that in all the ways, fonts are 2D textures with some extra informations to locate letter in their canvas.
I will show the simplest method. We will just use a Windows Font and the built-in font importer/converter to do the job for us.

Let's add our font sprite resource.
  1. In our project in the solution explorer: Right click on Content
  2. In the popup menu: Choose Add
  3. In the submenu: Choose New Item
  4. In the template list of the poping dialog box: Choose Template Sprite Font
  5. Rename our font: For this tutorial, please, name your font 'MyFont'
  6. Then: Click on Add
Let's open our font resource file to customize our font. Just double-click on it in the solution explorer. 'spriteresource' files are, in fact, XML files describing the font attributes. We can freely modify it and choose font name, size, style, spacing, and character regions.
Let's choose Arial, 20, Regular, 0, and 32 to 126. Save the file and close it.

Step 2: Load the font
Now we need to load it in our game.

First, please add a SpriteFont variable in your. Let's name it myFont.
Then, in the LoadContent method, let's add this line :
myFont = Content.Load<SpriteFont>("MyFont");


Step 3: Display the text
Now we will modify our Draw method to display our text.
XNA built-in text display use a SpriteBatch object to display.

Just add the 3 lines just after the GraphicsDevice.Clear(...); line
spriteBatch.Begin();
spriteBatch.DrawString(myFont, "Hello World!", Vector2.Zero, Color.White);
spriteBatch.End();

That's all. Run your game and enjoy.

Aucun commentaire:

Enregistrer un commentaire