Tuesday, August 7, 2012

XNA4.0 – 02 – Back from Greece

Yo, back from Greece, yo! Pretty good trip! Hotel was good, not great, but who stays in the hotel room anyways? Food was mostly from restaurants and I managed to eat sea food at almost every meal, except for when I tried some traditional Greek cooking. Great beaches. Kind of light on fun/party side, but that was mostly due to the people I was with, not any inherent ability of that place to provide fun. And they had really fucking huge cats (and with this the family friendly status of this blog is reworked). Not just fat cats, but thin huge cats! My cat is not small by any means, but Jesus! Anyway, very expensive, but overall it was worth it.

So a short post today, where I explain the auto-generated code for a new XNA project and try to add the full project sources on Dropbox. Dropbox should work better for small projects to write once and never update, like tutorials, while GIT is going be used for updating the real project, where I need versioning and what not.

So after creating a new XNA Windows Game, like we did in part one, we can open up "Game.cs" and after the using statements, we should get this:


We have the "Game" class inside the "XNATut01" namespace with two member fields and a constructor. These two fields are somewhat similar to what we had in DXUT. We need to be able to interact with the graphics device on multiple occasions, so we create a manager for it in the constructor and store it for further use. The "SpriteBatch" is used for blitting and other texture based operations, and while this first example does not do anything with it, we'll start using it and never stop. In the constructor we also set up a content root folder. XNA manages the loadable resources based on content folders. Hopefully this is flexible enough and won't spend ages going over the content folders once they grow in size like Ogre3D did. We'll see.


Above we have the "Initialize" method. If you read its description you'll see that this is were we initialize and load non-graphic related resources. Thus is very similar to the DXUT callback system, but shorter and without glue code for the basic app.


Above we have the LoadContent/UnloadContent pair or resource management functions, again very similar to DXUT callbacks and again the comment do a good job of explaining what these functions are for. You will probably have andeasier time deciding where to put your loading and initialization code in XNA when compared to DXUT. In the loading method we only create a SpriteBatch object. Do not ask me why we create it with the GraphicsDevice class as parameter instead of an instance. I haven't programmed C# in years and I am completely new to XNA. I'll update once I figure it out. There is nothing to do in the unload method.

And that's it for the initialization part. Now we get to the game logic, that has two main parts, the first being the "Update" function that is called once a frame and updates your game's internal state:


Again, very simple. Here we only check a gamepad button press to exit the application. Starting with sample two here we'll handle keyboard input, camera movement, animations, etc. The only parameter is the "gameTime", giving us the most important information: elapsed time between frames (and a few more bits of information, like total time).

And finally the draw method, that just clears the screen with the lovely cornflower blue and that's it:


Not a lot of new or useful information in this post, just walking you trough a simple sample. In the second tutorial we'll draw some text, show FPS and handle some keys and in the third we'll be drawing our first triangles.

And now for the test: XNATut01.rar. Not sure how persistent this link is, in the future I'll probably gather all the tutorials into a single archive and put them somewhere safe and public.

No comments:

Post a Comment