XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   2D Series finished!
My Book: Out Now!
      
       Go to section on this site

Additional info


Latest Forum posts

 Suggestion to change a few lines
  Posted by: Insomnica
  When: 06/09/2010 at 14:37:05

 Collision with series 1
  Posted by: radulph
  When: 05/09/2010 at 13:33:14

 HLSL calculating normals
  Posted by: miroslavign
  When: 04/09/2010 at 17:26:26

 Collision with series 1
  Posted by: ToastbrotX
  When: 04/09/2010 at 16:52:02

 HLSL calculating normals
  Posted by: Rich_Zap
  When: 04/09/2010 at 15:00:20

 Collision with series 1
  Posted by: ToastbrotX
  When: 04/09/2010 at 12:28:41

 HLSL calculating normals
  Posted by: miroslavign
  When: 04/09/2010 at 08:46:31

 Walk along a wall
  Posted by: Anonymous
  When: 03/09/2010 at 10:28:02

 model problems
  Posted by: muffinman
  When: 03/09/2010 at 06:47:32

 Vertices problem
  Posted by: Anonymous
  When: 03/09/2010 at 05:48:35


Ads

Starting your XNA 2.0 Project

Welcome to the first entry of this XNA Tutorial. This tutorial is aimed at people who haven't done any 3D programming so far and would like to see some results in the shortest possible time. Released in December 2006, XNA is a new language, built around DirectX, which eases game programming in a lot of ways.

The software required to start writing your own XNA code is completely free:

  • Microsoft XNA Game Studio 2.0, the programming environment (free) (link)
  • Microsoft Visual Studio C# Express (free) (link, make sure you select the C# edition, marked in green). XNA Game Studio 2.0 will also work with the full version of Visual Studio 2005.

    With this free software installed, you can start up XNA game studio, which you can find in the start menu. Next, go to the File menu, and select New Project. If your have the full version of Visual Studio 2005, first you’ll have to select “XNA Game Studio 2.0” as project type in the list on the left.

    As project template, we need Windows Game (2.0). Deploying your game on an Xbox360 will be discussed later. Fill in XNAtutorial as name for the project, and hit the OK button!

    A small XNA project is being made for you. In the Solution Explorer on the right side of your screen you see your project already contains 2 code files: Game1.cs and Program.cs. You can look at the code in the files by right clicking on them and selecting View code. Your program starts in the Program.cs file, in the Main method. The main method simply calls up the code in the Game1.cs file. There’s nothing we need to change in the Program.cs file.

    Open the code in the Game1.cs file. Although it's littered with comments, we can discover the structure of a XNA game program:

  • The constructor method Game1() is called once at startup. It is used to load some variables needed by the XNA framework.
  • The Initialize method is also called once on startup. This is the method where we should put our initialization code.
  • The (Un)LoadContent method is used for importing media (such as images, objects and audio) as well as data related to the graphics card.
  • The Update method is called every frame exactly 60 times. Here we will put the code that needs to be updates throughout the lifetime of our program, such as the code that reads the keyboard and updates the geometry of our scene.
  • As often as you computer (and especially your graphics card) allows, the Draw method is called. This is where we should put the code that actually draws our scene to the screen.

    As you can see, there is no code needed to open a window, as this will be done automatically for us. When you run your project by pressing F5, you will already get a nice blue window.

    Let’s move on, and discuss the graphics device. In short, a device is a direct link to your graphical adapter. It is an object that gives you direct access to the piece of hardware inside your computer. This variable is readily available in our code as the GraphicsDevice variable, but as we’ll be using this a lot (really, a lot) we’ll make a shortcut for this. First, we’ll declare this variable, by adding this line to the top of your class, exactly above the Game1() method:

     GraphicsDevice device;

    Obviously we need to fill this variable. Add this line to your LoadContent method:

     device = graphics.GraphicsDevice;

    Next, we’re going to specify some extra stuff related to our window such as its size and title. Add this code to the Initialize method:

     graphics.PreferredBackBufferWidth = 500;
     graphics.PreferredBackBufferHeight = 500;
     graphics.IsFullScreen = false;
     graphics.ApplyChanges();
     Window.Title = "Riemer's XNA Tutorials -- Series 1";

    The first line sets the size of our backbuffer, which will contain what will be drawn to the screen. We also indicate we want our program to run in a window, after which we apply the changes. The last line sets the title of our window.

    When you run this code, you should see a window of 500x500 pixels, with the title you set, as shown below.




    DirectX Tutorial 1 - Starting a project

    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !



    Click here to go to the forum on this chapter!

    Or click on one of the topics on this chapter to go there:
  • Draw mesh in C++ using effect file
          [code] VOID Render() { // Clear the backbu...
  • GO RIEMER, GO RIEMER!!!!!
          Man, riemer, u've really outdone yourself with th...
  • No given Type Game1
           Hello, I'm totally new to XNA and downloaded...
  • noob question on tutorial 1 of XNA
          Hello, I just found this site, and it is really...
  • Handle to window
          Hi. I have a windows form application where I h...
  • I get an error at the effect part
          > XNAtutorial.exe!XNAtutorial.Game1.SetUpXNADevice...
  • Two Thumbs up for Riemer
          All I can say is that these tutorials are amazing,...
  • I run into errors when i try and compile
          When i attempt to compile the code through the fir...
  • Internet games
          Hi Riemer, thank you once again for your tuts, ...
  • Error
          Could not find a Direct3D device that has a Direct...
  • Bug Report
          Caching GraphicsDeviceManager.Device into a field ...
  • XNA
          Hi Riemer, I've been through your first series...
  • XNA Update error?
          I have XNA Beta 2. in the starting xna game projec...
  • Change of code for Beta 2
          Hi, In you code for setting the width and heig...



    After each chapter I will suggest some short exercises, so you practice what you’ve learned in the chapter. After the exercise, the whole code of the chapter is listed, with the changes of the current chapter highlighted. I have stripped away all the comments, so it looks a bit more compact.

    You can try these exercises to practice what you've learned:
  • Instead of creating a windowed game, switch to 800x600 fullscreen mode (use Alt+F4 to terminate your program)
     using System;
     using System.Collections.Generic;
     using Microsoft.Xna.Framework;
     using Microsoft.Xna.Framework.Audio;
     using Microsoft.Xna.Framework.Content;
     using Microsoft.Xna.Framework.GamerServices;
     using Microsoft.Xna.Framework.Graphics;
     using Microsoft.Xna.Framework.Input;
     using Microsoft.Xna.Framework.Net;
     using Microsoft.Xna.Framework.Storage;
     
     namespace XNAtutorial
     {
         public class Game1 : Microsoft.Xna.Framework.Game
         {
             GraphicsDeviceManager graphics;
             SpriteBatch spriteBatch;
             GraphicsDevice device;
     
             public Game1()
             {
                 graphics = new GraphicsDeviceManager(this);
                 Content.RootDirectory = "Content";
             }
     
             protected override void Initialize()
             {
                 graphics.PreferredBackBufferWidth = 500;
                 graphics.PreferredBackBufferHeight = 500;
                 graphics.IsFullScreen = false;
                 graphics.ApplyChanges();
                 Window.Title = "Riemer's XNA Tutorials -- Series 1";
     
                 base.Initialize();
             }
     
             protected override void LoadContent()
             {
                 device = graphics.GraphicsDevice;
                 spriteBatch = new SpriteBatch(GraphicsDevice);
             }
     
             protected override void UnloadContent()
             {
             }
     
             protected override void Update(GameTime gameTime)
             {
                 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                     this.Exit();
     
                 base.Update(gameTime);
             }
     
             protected override void Draw(GameTime gameTime)
             {
                 graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
     
                 base.Draw(gameTime);
             }
         }
     }
     


    Google
     
    Webwww.riemers.net


    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !



    - Website design & XNA + DirectX code : Riemer Grootjans -
    ©2003 - 2008 Riemer Grootjans
  • Translations

    This site in English
    This site in Korean
    This site in Czech

    Microsoft MVP Award



    2007 - 2009 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    Chapter 1
    Chapter 2
    Chapter 3
    Chapter 4
    Chapter 5
    Chapter 6
    Chapter 7
    Chapter 8
    XNA 3.0 Recipes Book (8)
    Chapter 1
    Chapter 2
    Chapter 3
    Chapter 4
    Chapter 5
    Chapter 6
    Chapter 7
    Chapter 8
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Community Projects (1)
    Team Project (1)
    News
    Tutorials (160)
    XNA 3.0 using C# (89)
    2D Series: Shooters! (22)
    Starting a project
    Drawing fullscreen images
    Positioning images
    SpriteBatch.Draw()
    Rotation
    Keyboard input
    Writing text
    Angle to Direction
    Direction to Angle
    Smoke trail
    Manual texture creation
    Random terrain
    Texture to Colors
    Coll Detection Overview
    Coll Detection Matrices
    Putting CD into practice
    Particles
    Additive alpha blending
    Particle engine
    Adding craters
    Sound in XNA
    Resolution independency
    3D Series 1: Terrain (13)
    Starting a project
    The effect file
    The first triangle
    World space
    Rotation - translation
    Indices
    Terrain basics
    Terrain from file
    Keyboard
    Adding colors
    Lighting basics
    Terrain lighting
    VertexBuffer & IndexBuffer
    3D Series 2: Flightsim (14)
    Starting point
    Textures
    Loading the floorplan
    Creating the 3D city
    Loading a Model
    Ambient and diffuse
    Quaternion camera
    Flight kinematics
    Collision detection
    Adding targets
    Point sprites
    Alpha blending
    Skybox
    Camera delay
    3D Series 3: HLSL (18)
    Starting point
    HLSL introduction
    Vertex format
    Vertex shader
    Pixel shader
    Per-pixel colors
    Textured triangle
    Triangle strip
    World transform
    World normals
    Per-pixel lighting
    Shadow map
    Render to texture
    Projective texturing
    Real shadow
    Shaping the light
    2D screen processing
    Preshaders
    3D Series 4: Adv. terrain (19)
    Starting code
    Mouse camera
    Textured terrain
    Multitexturing
    Adding detail
    Skydome
    The water technique
    Refraction map
    Reflection map
    Perfect mirror
    Ripples
    The Fresnel term
    Moving water
    Specular highlights
    Billboarding
    Region growing
    Billboarding renderstates
    Perlin noise
    Gradient skybox
    Short Tuts (3)
    Run XNA on older pcs
    MessageBox in XNA
    Normal generation
    DirectX using C# (54)
    Series 1:Terrain (14)
    Opening a window
    Linking to the Device
    Drawing a triangle
    Camera
    Rotation - Translation
    Indices
    Terrain creation
    Terrain from file
    DirectInput
    Importing bmp files
    Colored vertices
    DirectX Light basics
    Mesh creation
    Mesh lighting
    Series 2: Flightsim (19)
    Starting code
    Textures
    The floorplan
    Creating the 3D City
    Meshloading from file
    Ambient light
    Action
    Flight kinematics
    Collision detection
    Skybox
    Texture filtering
    Adding targets
    Point sprites
    Alpha blending
    DirectSound
    Sounds in 3D
    Playing MP3 files
    Displaying text
    Going fullscreen
    Series 3: HLSL (19)
    Starting point
    HLSL Introduction
    Vertex Shader
    Shaded triangle
    Pixel Shader
    Textured Triangle
    Triangle Strip
    World transform
    Adding normals
    The first light
    Shadow mapping
    Render To Texture
    Projective texturing
    The first shadow
    Shaping the light
    Preshaders
    Multiple lights
    Adjusting Z values
    Finishing touch
    Short Tuts (2)
    Resizing problem
    Checking Device caps
    DirectX using C++ (15)
    Series 1: Terrain (15)
    Opening a window
    Ending the game loop
    Linking to the Device
    Clearing your window
    Drawing a triangle
    Culling
    Camera
    Rotation - Translation
    Indices
    Terrain creation
    Terrain from file
    DirectInput
    Importing .bmp files
    Adding colors
    DirectX Light basics
    DirectX using VB (2)
    Series 1: Intro (2)
    The first triangle
    Rotation - translation
    -- Tree view --


    Thank you!

    Support this site --
    any amount is welcome !

    Stay up-to-date

    I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!