02 InitializeGraphics

Last time, I talked about the main loop for a Direct3D application. We never got around to actually drawing anything on the screen, though. We won't this time, either. :) But we will take a major step in that direction by examining the initialization code we need to get a Direct3D program up and running.

Recall the game loop:

using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; namespace Craig.Samples.Direct3D { public class Game : System.Windows.Forms.Form { static void Main() { Game app = new Game(); app.InitializeGraphics(); app.Show(); while (app.Created) { app.Render(); Application.DoEvents(); } app.DisposeGraphics(); } } }

InitializeGraphics is where we're going to spend our time this time. I’ll cut to the chase and show you the relevant code here:

private Device device; protected bool InitializeGraphics() { PresentParameters pres = new PresentParameters(); pres.Windowed = true; pres.SwapEffect = SwapEffect.Discard; device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pres); return true; }

It’s fairly short, but there’s some significant stuff going on here. First, notice the addition of a private field to our class:

private Device device;

It’s of type "Microsoft.DirectX.Direct3D.Device", and it’s absolutely central to the operation of any Direct3D application. The Device object is the object that represents the graphics hardware in your system, and as such it’s involved in just about everything we want to do, from drawing shapes to adding lights.

In order to make use of the device, we need to create an instance of the Device class using the C# new operator.

device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pres);

The constructor takes several arguments. I don’t want to go into them too much detail here, as this is introductory material. Briefly, what this code is saying is, “Give me a representation of the first (zeroth) graphics adapter in the system, I want processing to be performed by hardware, render to this window, process vertices in software, and use the presentation parameters I’ve specified in pres.”

You will probably use the same parameters until you get into more sophisticated Direct3D programming. Like I said, I don’t want to delve into these too much, but I will mention PresentParameters, since you’ll need to create one of these yourself:

PresentParameters pres = new PresentParameters(); pres.Windowed = true; pres.SwapEffect = SwapEffect.Discard;

PresentParameters controls many things about the way the device is created. Here we’ve elected to accept most of the defaults, but we do set the Windowed and SwapEffect properties.

Setting Windowed to true has the effect of running our application in a regular window on the desktop, like any other application. Had we set this to false, our Direct3D application would run in full-screen mode, where it would take over the entire display. While this will generally perform better, and is the way most games work, it requires some additional setup, and we’re going to stay away from it for now.

The other parameter, SwapEffect, controls how Direct3D manages back buffers. By default, when you make rendering calls in Direct3D, you’re not actually drawing to the screen. Instead, you’re drawing to an off-screen buffer, which will be copied to the screen all at once when you’re done drawing. This is much more efficient way of doing things, especially when you consider that drawing to the screen requires moving information from main computer memory across the graphics bus to the graphics card.

By specifying SwapEffect.Discard, we’re saying that once Direct3D has copied a back buffer to the display card, it can do whatever it likes with the back buffer. Other options might require that Direct3D keep the contents back buffer intact, or that it trade what’s on the screen with what’s in the back buffer. We’re keeping it simple, so Discard is great.

Once we’ve created the Device, we store it in the private member variable so we can use it later.

Next time, we’ll talk about the Render method, and we’ll actually draw something on the screen!