dimanche 8 mars 2009

Hello, our first component

Let's redo our "Hello World" game, but in another way.
This time will make an "Hello World" component.

This time, we will not directly code in our Game class, we will create a Component and simply use it in our game.

Create a XNA 3.0 Windows Game project from scratch with the wizard (let's name it HelloComponents), then add a a sprite font content to our project (like the steps 0 & 1 of the previous tutorial).

Step 1: Create our Hello World Component
To create our component:
  1. In our project in the solution explorer: Right click on our (HelloComponents)project node
  2. In the popup menu: Choose Add
  3. In the submenu: Choose New Item
  4. In the categories tree: Choose Visual C# Items/XNA Game Studio 3.0
  5. In the template list: Choose Game Component
  6. Rename our game component: For this tutorial, please, name your component 'HelloComponent'
  7. Then: Click on Add
By default new Game Component inherit from GameComponent class. This class is usable for us now, because it is not 'drawable'. So just change the base class of our component to DrawableGameComponent .

Step 2: Customize our component
We need 2 fields. One for the sprite font like in the previous tutorial and one for the sprite batch because in the previous tutorial we use the game's one, but the wizard does not add one in component.
SpriteBatch spriteBatch;
SpriteFont myFont;

Then we need to override two methods. First one to load the sprite font content and create the sprite batch. Second one to actually display the text.
protected override void LoadContent()

spriteBatch = new SpriteBatch(GraphicsDevice);
myFont = Game.Content.Load<SpriteFont>("MyFont");

public override void Draw(GameTime gameTime)

spriteBatch.DrawString(myFont, "Hello components' World", Vector2.Zero, Color.Yellow);

Step 3: Add our component to our game
In our game class, in the Initialize method, just add a line:
protected override void Initialize()
Components.Add(new HelloComponent(this));

That's all.

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.DrawString(myFont, "Hello World!", Vector2.Zero, Color.White);

That's all. Run your game and enjoy.

lundi 2 mars 2009

Very First Step with XNA framework (part 1/2)

There is so many things to say. I guess many other sites (especially theXNA community site) deals with many samples and tutorials. I invite you to view these sites. For my part, I will only address the outline.

Creating the solution and project for your game is very simple thanks to the Wizard for creating Visual project.

The core of your game will be implemented in a class inherited from Microsoft.Xna.Framework.Game
All you have to do is implement 6 methods of this call to make a game.
Theses methods are : the constructor, Initialize, LoadContent, UnloadContent, Update and Draw.
That's all. :)

Let's describe theses methods.

By default, there are only two instructions to initialize two values.

The first value is graphics a member field of GraphicsDeviceManager type.
GraphicsDeviceManager is a class that provides access to the functionalities of the graphics hardware.

The second value Content.RootDirectory specifies the location of the assets in the project. By default they are located in the folder named 'Content'. As a general rule you should avoid placing here the code.

As a general rule you should avoid placing more code there. But rules can be broken....
Generally, that can be done in the constructor can be done in the method 'Initialize' which is there for that.

Initiliaze method
It is called just after the creation of the GraphicsDevice.

This method is where you should initialize all needed data of your game before its start. It is also where you can query for services, load any non-graphic related content, and add components.

You should not forget to call base.Initialize() to enumerate through any components and initialize them as well, and then invoke the LoadContent method.

LoadContent & UnloadContent methods
The LoadContent method is called when graphics resources need to be loaded.
This UnloadContent method is called when graphics resources need to be unloaded.

To load/unload you need a ContentManager... Your game object contains a ContentManager, so in most of the game you do not need to create it yourself (for some reasons it could be a good idea to use your own ContentManagers, but we will see this in a future post).

You should not forget to call the method of the base class to call the matching method of all components belonging to your game.

Update method
This method contains your game logic. It is called when the game determined that it should be.
In it, you should update your game world, check for collisions, gather input, play audio, manage the network, etc...

You should not forget to call the Update method of the base class to call Update method of all updatable components belonging to your game.

Draw method
This is called when the game should draw itself.

Avoid placing in this method treatment other than those requested to render your scene on the screen.

You should not forget to call the Draw method of the base class to call Draw method of all drawable components belonging to your game.

to be continued....

What is XNA?

XNA game studio is a free tool, provided by Microsoft to ease the development of games for XBox 360, Windows and Zune platforms.

Today it is at version 3.0. It is built on top of Microsoft Visual Studio 2008 (no matter the version: standard, profesional, even the express one). Former versions should be used with the Visual Studio 2005 Serie.

The XNA is a crossplatform framework that allows the most efficiency on the platform on which it runs. You can manage the access to your graphics, your audio, your inputs, your storage device the same way on each platform. Its pipeline eases the import of many type of assets. The memory is managed that makes the development even easier.

You code your game using C#

With XNA, Microsoft is the first console maker to give to its community the ability to create its own games and to sell them on the XBox Live marketplace. Note that for this last aspect you must be a premium member (the cost is $99 per year).

You can see more here on the XNA official community site.

And the light was...

So this is my first post...

You want to know who I am?
I'm a professional game developer. I've been working on almost all consoles (Xbox360, PS3, Wii, PS2, XBox, NGC, Dreamcast, N64, PC) since 1998.
Oh... english is a foreign language for me.

Why do I do this blog?
This blog is a kind of diary where I will write things that may be interesting (I hope) for XNA developers. I write it in my spare time as well as I develop on XNA.
But my real purpose is, if things go well, to describe my experience to develop a game from scratch until it is sold on the Xbox Live Marketplace.
I admit that I do not know what this game is yet.

I'm still discovering C# and XNA... so please be gentle.