MULTIMEDIA

Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 1)

1/22/2011 4:31:13 PM
You are now going to start from scratch with your first program. It won’t actually do much—you won’t be writing your own version of Halo just yet—but it does give you an insight into what XNA does and how you can write your own C# bits to produce a cool mood light.

1. Creating Your First Project

A computer game is not just a program—it is also lots of other bits and pieces that make playing the game fun and interesting. Just about every game has graphics, sounds, 3-D models of game objects, and all sorts of other items that must be created along with the code. This means that when you make a game, you have to manage all these other resources, too. The good news is that the XNA designers have thought of this, and they provide a comprehensive content management solution that looks after all these resources. You can just give your game resources to the Content Manager, and it makes sure that they are available to the programs that you write. Later on in the book, I’ll show you how to add some content of your own so that you can use a picture of your mom or your dog as a game character.

The content management is part of the project mechanism provided by XNA Game Studio. What this means right now is that to create your first game program, you actually have to create an XNA Game Studio project.


Note:

This menu is created when XNA Game Studio is installed and contains a link to the version of Visual Studio 2008 installed on your Windows PC. If you are using a different version of Visual Studio 2008 (perhaps you installed XNA Game Studio on top your installation of Visual Studio 2008 Professional Edition) you will see a link to your version.


In XNA Game Studio, select New Project from the File menu, as shown in Figure 1. This automatically creates the entire project and the file into which you are going to put your code.

Figure 1. Opening the New Project dialog box


XNA Game Studio can make a whole range of different projects depending on what you actually want to build. The skeletons for each of these types of program are contained in project templates and Starter Kits. You can download and install other Starter Kits from the Creators Club Web site. Right now, you are going to use a template to create an empty XNA project.

Figure 2 shows all the possible types of projects that can be created. You need to select the project that matches the device on which you want to run the game.


Note:

Make sure that you create a "Game" and NOT a "Game Library"; otherwise, you will not be able to make your program run.


Figure 2. Creating a new project


You should just choose the one that you want to use; the way that the program works is identical for all. Call the project "MoodLight" because that is what we are building first. You can use the Browse button to select an appropriate destination for the project. You should ensure that the Create Directory For Solution check box is selected so that all the files for this game are held in one place. Once you have done this, click OK to get XNA Game Studio to build the project for you.

When the project has been created, you should see a screen that looks like the one in Figure 1-5. Yours might not look quite the same (it certainly won’t have the big arrow pointing at the Start Debugging button), but it should look similar. There are a lot of controls that you can play with. At the moment quite a few are disabled and can’t be used, but it still looks confusing the first time that you see it. The key here is not to panic. You are going to use only a few of the buttons to start with, and I’ll explain the other ones as you need them.

2. Running Your First Program

If you are running your program on a PC, you can just click the Start Debugging button (indicated by the arrow in Figure 3), and the program runs. If you are sending your program to an Xbox 360, you must make sure that the XNA Game Studio Connect application is running on your Xbox 360 and that the Xbox has been connected to your Windows PC. If you are sending your program to a Zune, make sure that it is connected to your computer and that the Zune application is not running.

Figure 3. XNA Game Studio and MoodLight


When you click the button to run the program, a number of things happen in quick succession:

  1. XNA Game Studio compiles the source code files. The source code of the program is all the lines of C# code that you and XNA Game Studio create that actually describe what you want the computer to do. A compiler is a program that takes source code and creates a set of machine instructions that can be loaded into the computer’s processor to control what the computer does. The C# language has a particular specification, and the compiler knows all about the rules in the specification. The compiler rejects any program that it thinks is not correct and tells you about the compilation errors. You are going to have to live with the fact that you will see a lot of these errors if you decide to become a computer programmer.

  2. Your project might contain a large number of different source files; each of them must be compiled. If all the program source files compile correctly, they are then combined with any resources (for example, images and sounds) that are part of the project.

  3. If you are using an external device, either Xbox 360 or Zune, the compiled files are now transferred into it.

  4. Finally, XNA Game Studio starts the program running. If you are using a Windows PC, the program runs in a window on the desktop. If you are using an external device, the program takes it over completely. At this point, the window or target device is under the control of your program statements.

When XNA Game Studio produces an empty project, it actually creates a program that will compile and run, so you can just click the Start Debugging button (if you haven’t already) and turn the program loose.

When you run the program, the screen turns blue. That’s it—nothing else. All that work to turn the screen blue? You could have done that with a can of paint in 30 seconds. The "empty" project from XNA just turns the screen blue. You’re going to make a light that can display millions of possible colors, an ever-changing mood lamp, and finally, the world’s first-ever color-changing game.


Note:

One slightly irritating thing about Visual Studio is that when the program is running, the organization of the controls in Visual Studio changes. This can confuse a first-time user because menus, toobars, and panes suddenly don’t seem to be where they used to be. If you carefully compare Figure 1-5 and Figure 1-6, you notice that a new toolbar has appeared that has buttons on it that you can use to pause or stop the program.

Other  
  •  Programming with DirectX : Shading and Surfaces - Additional Texturing Topics
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 4) - Enabling Textures with ES2::RenderingEngine
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 3) - Enabling Textures with ES1::RenderingEngine
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 2) - Generating Texture Coordinates
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 1) - Enhancing IResourceManager
  •  Programming with DirectX : Shading and Surfaces - Implementing Texture Mapping (part 2) - Multi Texture Demo
  •  Programming with DirectX : Shading and Surfaces - Implementing Texture Mapping (part 1) - 2D Texture Mapping Demo
  •  Building Out Of Browser Silverlight Applications - Using COM Interoperability and File System Access
  •  Building Out Of Browser Silverlight Applications - Controlling the Application Window
  •  iPhone 3D Programming : Adding Depth and Realism - Loading Geometry from OBJ Files
  •  iPhone 3D Programming : Adding Depth and Realism - Better Wireframes Using Polygon Offset
  •  Programming with DirectX : Textures in Direct3D 10 (part 2)
  •  Programming with DirectX : Textures in Direct3D 10 (part 1) - Textures Coordinates
  •  Programming with DirectX : Shading and Surfaces - Types of Textures
  •  iPhone 3D Programming : Adding Shaders to ModelViewer (part 2)
  •  iPhone 3D Programming : Adding Shaders to ModelViewer (part 1) - New Rendering Engine
  •  iPhone 3D Programming : Adding Depth and Realism - Shaders Demystified
  •  Programming with DirectX : Transformation Demo
  •  Programming with DirectX : View Transformations
  •  Programming with DirectX : World Transformations
  •  
    Most View
    Rosewill Launches Armor Evolution Mid-Tower Case
    Keep Kids Online Safely (Part 1)
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 2)
    Buyer’s Guide: e-Readers That Fits Your Needs Best (Part 1)
    Thunderbolt External Drives (Part 1)
    Apple iPhone 5 - Fails To Return To The Top (Part 2)
    Syncing And Streaming (Part 2) - Apple TV, The remote app
    Preparing Your Windows 8 PC : Adding Devices in Windows 8 (part 1) - Viewing Installed Devices
    Group Test: Free Office Suites (Part 3) - LibreOffice
    Turn An Old Computer Into A Server Using Ubuntu (Part 2)
    Top 10
    The NZXT Kraken X40 Compact Liquid Cooler Review (Part 3)
    The NZXT Kraken X40 Compact Liquid Cooler Review (Part 2)
    T-Mobile’s Samsung Galaxy Note II Review (Part 6)
    T-Mobile’s Samsung Galaxy Note II Review (Part 5)
    T-Mobile’s Samsung Galaxy Note II Review (Part 4)
    T-Mobile’s Samsung Galaxy Note II Review (Part 3)
    T-Mobile’s Samsung Galaxy Note II Review (Part 2)
    T-Mobile’s Samsung Galaxy Note II Review (Part 1)
    Sony Cybershot DSC-TF1 - Affordable Water-Resistant Camera
    Buffalo MiniStation Slim 500GB External Hard Drive