There is one more step we left unconfigured from the last lesson, but
it's not a terribly long one. Since we are using the express version of
Visual C++ we're going to need to setup the Platform SDK.
You can
follow along their instructions 1, 2, 3 and 4, but we won't need to do
step 5. Basically you need to download the platform SDK, then like after
we installed DirectX go to the Options menu, and add folders to the
executable, include, and library directories (corresponding to the files
we just installed.) Lastly, we update a small file
"corewin_express.vsprops" in our Visual Studio 8/VC/vProjectDefaults/
folder to include a few more libraries. It's pretty self explanatory,
the instructions are linked above and you can contact me if you need
more information.
NOTE: A lot of the functionality we'll be dealing with today you can
look at as a black box, you don't need to know how it works you can just
use it and focus on the rest of the engine. In fact, this source code I
only wrote once, and have built every 3d engine off of it since. You
should probably read it, but don't stress too much over the specifics.
If you REALLY have the desire to know (I know I would, I hate people
telling me not to worry about it) I recommend reading Riemer's Tutorial
on
this same subject. He also wrote it in C#
for those of you who are just having trouble with the language barrier.
Introduction
After all the hard work getting Visual C++ and DirectX installed, it's
time to actually get down to programming. In this lesson, we will be
building the skeleton of our engine class and having it interface with
windows for us. By the end of this lesson you will have initialized
DirectX and windows, and be ready for the next lesson where we will
begin rendering things to the screen. Believe it or not after this
lesson everything gets a lot easier. The learning curve is sharp, and
since I'm trying to quickly run through this initialization stuff I'm
not taking as much time as I should explaining it. However, it's
important to remind you that this workshop is about engine design, so
the specific in's and outs are beyond the scope of our purpose.
Unlike the last lesson, this time I'm going to intentionally move fast
and throw a lot of information at you. You are encouraged to ask
questions and do individual research for the topics that you don't
understand. Unlike a full fledged tutorial, these workshops are meant to
expose you as quickly as possible to the subject. I will do my best to
provide links to other sources for more information where I can.
I recommend you download the source code first, and have it open infront
of you as you follow along. I will not be going through line by line.
Engine.h
Lets dive right in and open up "Engine.h" where the magic happens:
#pragma once
#include
#include
#include
#include
#include
#include "consts.h"
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "Winmm.lib")
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam );
namespace DistortedStudios
{
class cEngine
{
public:
cEngine( );
~cEngine( );
unsigned int flags;
int Initialize( HINSTANCE hInstance, int width, int height, bool windowed);
int BeginMessageLoop( );
void Render( float timeDelta );
IDirect3DDevice9* getDevice( )
{ return pd3dDevice; }
private:
IDirect3DDevice9* pd3dDevice;
HWND winHandle;
};
}
Lemma 2.1 - Pointer Arrays are Bad
If you've cruised around any C++ forum you'll undoubtedly run into the
old dinosaur of a coder who refuses to use STL containers like string
and vector. Instead he'll still be using the age old technique of
building pointer arrays. If you don't know what pointers are, bug
Churroe about it because that's his department. But I'll give you a
basic idea: You allocate sequential memory blocks and then build an
array by physically manipulating the data in memory. The problem with
this is, it leads to memory leaks - ie - situations where you reserve
memory for your data and don't release it. It also causes a lot of
problems managing the data. Now, the point of me telling you this is
that you /should very rarely ever deal with pointer arrays/. STL
containers are /not/ slower, in fact - in certain situations they are
faster. They do not cause you to over allocate and waste memory (they
manage thier own memory) and they will never cause memory leaks (unless
you do something crazy with them). So no matter what people tell you:
STL is your friend - use it .
The include statements are pretty self explanitory. We're going to be
using windows.h header files to interface with windows, d3d9 are the
primary DirectX includes, and d3dx9 are some helper functions and
classes, (these will be discussed more at length in a later lesson). We
are also using the STL (standard template library) classes String and
Vector. While we won't actually be using either yet, it's there to
remind us that, unlike regular C, we will not be using dynamically
allocated char arrays for strings, nor pointer array's for data storage.
If you don't know what I'm talking about, that's fine. Because it means
that you can't mess it up (^_~).