mort8088 Just another WordPress site

6Mar/11Off

XNA 4.0 – Tutorial 0 – Intro


Originally posted 2009 - A quick run through of the new project code, explaining what gets called when. If you’ve never looked at XNA or are just looking for a beginners explanation of what happens when you run an XNA application this is for you, some C# knowledge assumed. I've updated things to reflect using VS-2010 and XNA 4.0

 If you have never looked at programming before go through my friends tutorials on the subject first.

New Project

Let's start at the beginning. Fire up Microsoft C# 2010 Express or what ever version you're using and select new project. From the new Project dialog select XNA Game Studio 4.0 in Project types and Windows Game(4.0) under Templates.

Give the project a meaningful name and click [OK]. Visual Studio will present you with a solution that is functional but does nothing special.

Let's go through the code and see what it does do, so you understand what's going on.

Program.cs

This code file is the application entry point, this is a standard code layout for any windows program. When compiled, for the application to start it must have one static void Main and only one.

The #if ... #endif is new to XNA4 and I not sure why it's there unless it's to remove the Program class when you're building a WP7 Game you see #if is checking if the project has ether the WINDOWS or XBOX constants defined in a Windows Phone 7 Project they would not be found and this code would not be used.

The using line creates an instance of your Game class in a block that when it gets to it's closing bracket will clear up the memory it was using and dispose of the object without you having to do it yourself, while it is in this block it calls the run method for your class and your game code will start. The run method is an inherited method that we will cover in the next section.

The Game Loop

Your Game class it inherits from Microsoft.Xna.Framework.Game which takes care of all the technical stuff that sends DirectX developers into a foaming rage. For example when I was doing Managed DirectX there was a huge argument over the best way to implement a tight game loop this is the constantly running code sequence that would update the game data and draw the elements to the screen without taking all the system resources ie running the game without stopping everything else. Managed DirectX had already made starting a game simple but there was still some work to do, and now we have XNA you don't get to write the game loop, it's done for you and it works very well.

Let's look at the way things are called when you run your code :-

When the Game.Run() method is called it calls some internal setup code to get the XNA Framework set up, it then calls your Initialization method.

Game1.Initialize() method is where you put code to create the games data structures like the camera object, player object and any components or services. Basically anything that isn't content. Once done you call the base classes Initialize method this in turn calls your LoadContent method.

Game1.LoadContent() is where you load your spritefonts, sounds, music, Textures and 3Dmodels into the new project it also creates the SpriteBatch object that you will use to do your drawing with.

Once the content is loaded control passes back to the Game.Run method and it starts the Game loop this involves calling the Update methods then the Draw methods, but if the Update methods take too long then the draw methods will be skipped but also if the updates don't take very long then the loop will pause until it is time for the Draw methods. the game will stay in this loop until you call the this.Exit() method that signals to exit the main loop and perform the clean-up and quit the application.

Game1.cs

  • The constructor method- To start with the game only contains two lines:-

    creates a new GraphicsDeviceManager and registers it to handle the configuration and management of the graphics device for the Game.

    Sets the root directory associated with this ContentManager, this is relative to the application directory.
  • protected override void Initialize() - This method only has it's call to base.Initialize() because there are no game object that need setting up just yet.  If the call to base.Initialize() was missing then the game would not make a call to LoadContent().
  • protected override void LoadContent() - As I pointed out in the previous section creates a SpriteBatch object for drawing to the screen, and as with the Initialize method there is nothing else here, yet.
  • protected override void UnloadContent() - Is called during the clean up phase of the game, this is where you can dispose all the objects that are not loaded by the Content object. nothing to see here, move along now...
  • protected override void Update(GameTime gameTime)- This is where it get's more interesting:-

    The if statement checks to see if the player has pressed the back button on the Xbox 360 Controller and if they have then the Exit method is called to start the clean-up process.

    this call to the base classes Update method, I don't believe it's needed but I keep it anyway 😉
  • protected override void Draw(GameTime gameTime)- this method is responsible for everything you see

    this line uses the GraphicsDevice object to clear the screen like wiping a blackboard, only cleaner.

    this call to the base classes Draw method, I don't believe it's needed but I keep it anyway 😉

Conclusion

Well that's the code you get for a new project if you hit F5 it will compile and give you a nice window cleared to a lovely cornflower blue.

It doesn't do anything and it will only close if you press back on your Xbox 360 Controller or click the close window button on the window frame. but it's a start and before you know it you'll have a something to show your friends and family or sell on the Xbox LIVE Indie Games channel just think of the possibilities...

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.