Before I start, I do have the long term goal of creating an engine for DX11, like I did with XNA that I can then use to set up and test my ideas on, this post won't contribute to that engine, in fact, it may be a while before my code become coherent enough to do this, so if you after an engine, please keep with me, we will get there in the end :)
I guess the first thing you need to do is go and download the latest DirectX SDK and get that installed on your system.
At the moment I am using the Visual Studio 12 RC for this, so I guess to avoid confusion, you should to.
Unlike XNA, and forgive me for constantly mentioning XNA, but it’s where I am coming from, you can’t just create a project with it all set up ready to go, well at least not that I know of. So I created an empty Win32 project. So, in your nice new shiny IDE, open up a new project, and create a Win32 Project
In the Application Settings section after that screen, select the Empty Project check box and hit finish
You should now have an empty Win32 C++ project looking something like this
Now, in order for us to start using all the DirectX 11 goodness we need to set up the project so that it knows where DirectX 11 is, so right click on the project name and select Properties and you should get a screen that looks like this.
Select the VC++ Directories and then point the Include Directories and the Library Directories at the relevant folders in the DirectX 11 SDK you have installed, so this is how I set mine up
Select the drop down for the Include Directories and select <Edit…>
In the following popup, select the folder icon, then click the button with the three dots
Now navigate to the include folder in the DirectX SDK you have installed and hit Select Folder
Then hit OK, and now we do the same with the Library Directory
The difference with the lib is we can choose x64 or x86
And you should then have both the folders set like this in the project properties
And hit OK, and we are set up for some basic DirectX stuff, we will have to alter the projects properties again later on, but for now this will be enough to get us going, also I want to show you the sort of compile/linking errors you can get when the project is not set up correctly.
So our project is now ready, the first thing we need to do is create the initial entry point for application, a function called WinMain, and so we are going to add a file to the project called main.cpp
Right click on the Source Files folder in the project and select Add from the popup menu followed by New Item in the next
Now select the C++ File (.cpp) and give it the name main.cpp and click Add
We now have our first source file, first thing we will do is add a #include for the Windows.h header file.
Next we need to create a window, and to do this we are going to write our first class. I am going to call this class RandomchaosDX11Utility, over the next few posts we will add to this utility class and use it for a number of things. As I mentioned earlier, these first posts don’t really have a design in mind, at the moment, the classes are a means to an end.
So now we are going to create our first C++ class, right click on the project as we did before, choose add, but then this time select Class from the last pop up menu
In the next window we will give the class it’s name, and hit finish
This will build us two files a .cpp and a .h file, the .h file holds the definition of our class, and to start with it will have a constructor and a destructor like this
The next block of code is our class definition, as you can see you can specify an access type for a block of class members, in this case the constructor and destructor are declared here as public.
Before we start writing any code in here we need to add the Windows.h file again, this time write it under the #pragma once line.
There are a few things going on here, so lest have a look at the parameters and the return type.
The return type is HWND, this will be a handle to our window, we then have a number of parameters, the first is a LPCTSTR, it’s a string type, this parameter is used to populate the windows title, the next four integers are used to place a size the window, we then have the WNDPROC parameter, and this will be a callback method used to handle messages sent to the window, we will use it in this post to know when the window is being closed.
Now we need to create the function body, open up the .cpp file we have for this class
We also have the constructor and the destructor stubs already written for us, at this point we wont be populating them yet, also note that the methods are prefixed with the class name.
Now we will create the stub for our new function.
We are now going to implement the window creation code, we will go back to main.cpp and at the top we are going to include our new header file, we are going to create an instance of our utility class and create our callback method as well as implement the code to create the window
So, we are going to use a while loop, and while it’s condition is true our game will continue to run. We will control the condition with a global integer called int_AppRunning and define it at the top of our main.cpp file, just under the instantiation of our utility class.
We will define a MSG object to store incoming messages, we can then implement the pump in the game loop.
In our WinProc callback method where we have a switch looking at the incoming messages, we have a case for WM_CLOSE, this is the message passed to our application when the window is being closed, so in here all we have to do is set our global to 0
Now, this next bit, I have totally taken from the DirectX 11 SDK code Tutorials, so the information from here on in is my interpretation on this code.
We are going to have a fair bit going on here, first thing we need to do is add some more includes to our source for the DirectX elements we are going to be using.
In the header file we have for the utility class we are going to add the following
Now that’s set up the first thing I want to do is include two new headers to our utility class.
The method starts off getting the windows size, sets the device creation flags, it then sets up a few arrays to help detect what capabilities the card can do. It then sets up a swap chain definition to describe the device, it then loops through the types until it finds a driver type the card supports, if it doesn’t then it returns HRESULT which I imagine would be –1. It then sets up the back buffer, a render target and a depth stencil.
We then set up the Draw call, which in this post is just us clearing the “GraphicsDevice”
We now need to implement these calls, so we now open up our main.cpp file, after we initialize the window, we will initialize the device, then in the game loop we will make the draw call.
So, what did we do here?
- We set up the project to use DirectX
- We created our first C++ application
- We created our first windowed application
- We created a graphics device
- We cleared the graphics device
Thanks for reading and I hope it will be of some use to you.
You can find the source code for this post here