New Project
Ok you've installed the SDK and #Develop, now what?
When you first start #Develop you are shown its start page, click on the "New Combine" button(why combine?).
You'll see the New Project dialog like fig. 1. Select C# under Categories and Direct3D Application under Templates. Enter a name for your project and click create.

fig. 1 - New Project Dialog.
Now press F5 to run the project it will compile and show you a application window with a blue background. Well done you just built your first DirectX program are you excited yet?
Didn't think so, but at this stage you have made a tiny step on the road to writting games theres still a long way to go.
Next we'll take a look at what you get to start with in the basic project. starting in the same place as the compiler with the main entry point.
/// <SUMMARY>
/// The main entry point for the application
/// </SUMMARY>
static void Main()
{
using (MainClass mainClass = new MainClass())
{
if (!mainClass.InitializeGraphics())
{
MessageBox.Show("Error while initializing Direct3D");
return;
}
mainClass.Show();
mainClass.Run();
}
}
The main function is rather basic in that its only perpose is to create an instace of your application class do any preperation then show and run it. don't worry if you don't understand what these functions do I'll be explaining the ones you need to know about as we go along.
Starting with the definition of the application class, as you can see the main class inherits from the Form Class so there are a lot of Methods, propertys and Events that have already been written that you have access to if you want them.
/// <SUMMARY>
/// This is the main class of my Direct3D application
/// </SUMMARY>
public class MainClass : Form
{
.
.
.
public MainClass()
{
this.ClientSize = new System.Drawing.Size(640, 480);
this.Text = "Direct3D Project";
}
.
.
.
}
Lines 22-26 contain the MainClass constructer that was automaticly called from line 122 of the Main function this sets some basic propertys of the Form like the size of the window and the title that will appear on the task bar.
Now on to something more interesting, Initializing the Graphics system.
public bool InitializeGraphics()
{
try {
// Now let's setup the Direct3D stuff
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
// Create the device
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.SoftwareVertexProcessing, presentParams);
// Setup the event handlers for the device
device.DeviceLost += new EventHandler(this.InvalidateDeviceObjects);
device.DeviceReset += new EventHandler(this.RestoreDeviceObjects);
device.Disposing += new EventHandler(this.DeleteDeviceObjects);
device.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);
return true;
} catch (DirectXException) {
return false;
}
}
As you can see there is basic error handaling in the project template in the form of a try...catch if any of the code in the try block fails for what ever reason then the program flow jumps to the apropriate exception catch block, in this case we are only expecting "DirectXExceptions" if anything else goes wrong then the program will still crash.
Looking at lines 32 to 34 this is the PresentParameters for the device that we are about to create PresentParameters are a collection of properties that we want to applie to the device things like if it should be fullscreen or not, how is the backbuffer used, what format the zbuffer should be in, and do we want light effects, for your first application the ones here are good enough.
Line 37/38 is the creation of the device you can think of this as the screen output, well a class that you will use for screen output, the paramiters are a bit vague so here is the MSDN explanation.
If you under stood all that, why are you reading this???
Lines 40 to 43 add some event handalers that you will need to use in the future but ignore tham for now :-)
The next function is realy where you arer going to do a large part of work the run function.
/// <SUMMARY>
/// Our mainloop
/// </SUMMARY>
public void Run() {
// While the form is still valid, render and process messages
while (Created) {
FrameMove();
Render();
Application.DoEvents();
}
}
In the beginning this is a fine game loop that works well for the begginer BUT it's no good for a project of any real size, there are currently a number of discutions going on over the internet on what is the best way to construct the perfect game loop, but untill one has been agreed then this is what I'll be working with.
The basics of this function as it stands is this :-
- While the user has not quit the application continue.
- Move whatever needs to be moved.
- Draw whatever needs to be drawn.
- Proccess any windows events.
Both FrameMove() & Render() contain nothing that can't wait untill we start on the render engine, so untill next time...
MORT.
Disclaimer
This document and all files and code provided with it are provided "as is" and without any warranty at all, not even the implied warranty of fitness of use for any particular purpose. I am not responsible for any harm that might come from use or misuse of either this document or any of the files available for download from this site. Neither is there any guarantee that the information in this document is correct.
|