Codetopia

A place to talk about things related to XNA, .NET, and enterprise application development.
Welcome to Codetopia Sign in | Join | Help
Home My Book Blogs Forums Photos Files Roller

CH04 - 2D Graphics

File Details
Downloads: 1174 File Size: 37kB
Posted By: groundh0g Views: 1516
Date Added: Fri, Jan 18 2008

This ZIP archive contains source code from the book, XNA Game Studio Express: Developing Games for Windows and the Xbox 360, by Joseph Hall. The following paragraphs describe the changes that have been made to the code since it was first released with the book.

New Game1 Member Variables

The Microsoft.Xna.Framework.Game class now has a SpriteBatch member variable, named "spriteBatch". We don't need to add one, so you can replace any reference in the text to "m_batch" with the provided member variable, "spriteBatch". Of course, this means that you'll want to remove this auto-generated SpriteBatch logic for game projects in which you won't be using any sprites (which is the case with the example 3D code in Chapter 06).

The Game class now provides a member variable (called "Content") which exposes the ContentManager class. We don't need to manage our own reference to the content manager now, so you can replace any reference in the text to "content" with the provided member variable, "Content" (notice the change in capitalization).

The Game class also provides a member variable (called "GraphicsDevice") which exposes the GraphicsDevice object which represents the active graphics device. You can replace any reference in the text to "graphics.GraphicsDevice" with the provided member variable, "GraphicsDevice". You'll see this change in the auto-generated Game1 class when you create a new XNA 2.0 game. The spriteBatch member variable is initialized using "new SpriteBatch(GraphicsDevice);" rather than the previous "new SpriteBatch(graphics.GraphicsDevice);".

LoadContent vs. LoadGraphicsContent

The LoadGraphicsContent and UnloadGraphicsContent methods of the Microsoft.Xna.Framework.Game class (technically, they're members of the DrawableGameComponent class, from which Game is derived) have been replaced with the LoadContent and UnloadContent methods. The old content management methods still work, but the compiler will generate deprecation warnings for them, and there's a good chance that the old methods will be removed from future releases of the XNA Framework.

Content Folder

All content is now stored under the Content folder of your project. Prefix any content subdirectory names in the code with "Content\". For example, most of the book's examples store game media in the "media" subdirectory, which is located just off the root directory of the project. An image named "myImage.png" within that subdirectory would have been refered to as "media\myImage" in XNA 1.0 and XNA 1.0 Refresh. In XNA 2.0, that same resource would be refered to as "Content\media\myImage".

The Content folder actually represents a subproject called "Content" which manages the building, converting, and importing of media for your game. Any media that is accessed via the ContentManager class should live here. This includes items such as textures, models, and XACT projects. Any game-specific content data that you manage manually, such as level data and persisted game data, is not housed within the Content directory.

Intuitive File Names

I used longer, more descriptive filenames for the solution files, project files, and directory names. The new names make it much easier to locate the source code for a specific chapter. This may cause issues if you do your development in a deeply-nested subdirectory, like the default Visual Studio project folder of "C:\Documents and Settings\{username}\My Documents\Visual Studio 2005\Projects\".

I suggest creating a folder off the root of the drive from which you wish to develop your XNA games. I use "C:\projects\" on my laptop, and "E:\projects\" on my desktop.

ZIP Anatomy

All of the ZIP files which host the book's source code use a parallel directory structure. I expect the user to download the ZIP files into a common directory, then extract the files from each ZIP directly into that directory, preserving the ZIP file's internal directory structure. For example, if you download PT1_CH04.zip, PT1_CH07.zip, and PT2_18.zip into the "C:\projects\" directory on your local PC, and then extract the files to that same folder, you should have the following directory structure on your local drive.

   C:\projects\Part 1 - Introduction\CH04 - Graphics 2D\
   C:\projects\Part 1 - Introduction\CH07 - Input GamePad\
   C:\projects\Part 2 - Genre Studies\CH18 - Board Games\
Comments
 

XtheWiz said:

First , May I tell you that I very newbie in C#. I had found that you didn't declare the m_rectSmiley and m_originSmiley variables in your book. After I downloaded source code from this page to find out these two variables, then I have a question about why you use "static readonly" for these variables ? thank you, Lee
Thu, Feb 21 2008 1:48 PM
 

XtheWiz said:

Oh , I jst finished chapter 4 I had found a few incorrect... 1. the constant value SCREEN_WIDTH and SCREEN_HEIGHT aren't declared in your book. 2. For XNA 2.0, if you're put your content in folder media, you no need to prefix with "Content\" as well. Thanks
Thu, Feb 21 2008 3:08 PM
 

groundh0g said:

XtheWiz, For primitive types like int and string, you define constants using the const keyword. You can't declare classes and structs as "const". The closest equivalent is "static" (shared across all instances of the class) "readonly" (the value cannot be changed once it has been initialized). -- joe
Mon, Mar 31 2008 12:40 PM
 

groundh0g said:

XtheWiz, Thanks for the notes. As for the first point, I had originally planned to have the complete code listing printed at the end of each chapter, but when I was finally done writing the book, my text was around 1000 pages (400 pages over my budget), so the source listings were the first things to get cut. I kept the relevant source code snippets embedded within the text, but not every line of every example is listed. As you discovered, you'll need to download source to see the complete listing. -- joe
Mon, Mar 31 2008 12:43 PM
 

pisedoff said:

It's annoying when you download code and right off the bat, it won't compile. " m_texSmiley = Content.Load<Texture2D>(@"media\smiley"); can't find file " Especially for a beginner!
Sat, Jun 28 2008 4:46 AM
 

AnnoyedByYourShit said:

I would never buy your book! Your code sucks. It won't compile and it's a waste of time!You should give up XNA coding if you can't get your *** together!
Sat, Jul 05 2008 6:43 PM
 

Compile problems said:

Yes, these compile problems are very annoying. Also, the source you provided is only for xbox360? I am interested in windows programming not xbox360 and I can't do anything with this zip file you provided because I haven't configured my computer for an xbox360 =( What am I supposed to do with this screen width and height problems, compile problems, oudated 1.0 instead of 2.0 etc etc? The book provides a lot of helpful content, but these issues are a major problem.
Wed, Jul 30 2008 7:04 AM
 

Compile problems solved said:

This download had the source code, I must have been downloading it from the wrong location. I got it working thank you =)
Wed, Jul 30 2008 7:06 AM
 

Compiled said:

Very satisfying when I finally got this working. I haven't worked much with C# and I think that is what held me back, along with the other people complaining here. You have to make the content file and add the images to it =)
Wed, Jul 30 2008 7:29 AM
 

Beginner said:

I have downloaded xna game studio 3.0, and I received the following error when trying to compile the example code from chapter 4. anyone else run into this, and how did you fix it? Error loading "media\smiley". This file was compiled using the wrong version of the XNA Framework. thanks for any help :/
Tue, Jan 27 2009 4:08 PM

Add Comment

Add
Name
Web Site
Comment
Powered by Community Server, by Telligent Systems