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.
- Menu: Choose File
- Menu: Choose New Project
- In Project types tree: Choose XNA Game Studio 3.0
- In Templates list: choose Windows Games (3.0)
- In the footer: Name your project
- In the footer: Choose your project Location
- Then: Click OK
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.
- In our project in the solution explorer: Right click on Content
- In the popup menu: Choose Add
- In the submenu: Choose New Item
- In the template list of the poping dialog box: Choose Template Sprite Font
- Rename our font: For this tutorial, please, name your font 'MyFont'
- Then: Click on Add
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();
spriteBatch.DrawString(myFont, "Hello World!", Vector2.Zero, Color.White);
spriteBatch.End();
That's all. Run your game and enjoy.
Aucun commentaire:
Enregistrer un commentaire