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.

Constructor
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....

Aucun commentaire:

Enregistrer un commentaire