Oh Goody, Code Time!
Alright recruits, we’re about to test your compiling and linking skills. I’ve added a zipfile called BASECODE1 on my website
that I’d like you to download now (don’t unzip it yet though!). The zipfile contains the following files:
- Globals.h – the main header file for the game template
- WinBase.cpp – the windows application overhead we’ve discussed
- InitTerm.cpp – the initialization/shutdown functions
- GameMain.cpp – where the game code goes
- Utils.h – prototypes for our helper functions
- Utils.cpp – the helper functions themselves
- Resource.bmp – a sample bitmap
- Sample.wav – a sample WAV file
Once you have this file in your possession, it’s time to create a project for our game template. Folks, the following discussion pertains specifically to those using the Microsoft Visual C++ (MSVC) IDE (a part of Microsoft Visual Studio). If you’re doing something else, you might need to raise a fuss by replying to this article if you need assistance.
Create a New Project
To create a project, Choose File|New
, and select Win32 Application
from the list of project types. Provide a name and location for this project (BaseCode1
would be a good idea) and let the IDE create an empty project for you. Once it’s done, there will be a new directory created for you with some standard project files already there. This is where we’d like the BaseCode1
files to end up, so go ahead and unzip the archive to there. The end result is that you’ll have a single directory (perhaps called BaseCode1
) that contains all of these files.
Adding these files to our new project involves nothing more than selecting the FileView
tab on the bottom left of the IDE (or wherever you placed the project view) and right-clicking on BaseCode1 files
. Add them all in by selecting them and there you have it!
Ah, but wait!
We’re not ready to attempt building the project yet. A couple more steps are involved.
Tell the IDE Where to Look
Move to Tools|Options|Directories
, and you’ll see a list of locations that the IDE searches through when looking for project files. We’re not talking about our files here – they’re all in the project’s directory and therefore easy for the IDE to find. No, what the compiler is really going to have a hard time finding without our help is all of the DirectX header files that we intend to use.
So, here’s what you do. Where did you install your DirectX SDK? Mine is at C:\DXVCSDK
, so I’ll use that as an example (substitute with your own). All of the DirectX header files are located in the INCLUDE subdirectory, so you’d add an entry like this to the list:
Now that you have it in the list, and this is very important, move the entry up until it’s the very first one. The reason for this is because MSVC comes with some older DirectX header files, and if the IDE looks in the MSVC default directories before trying yours, things will be screwed-up.
Tell the Linker Where to Look
The IDE may now know where all of the header files are (in our project directory and in the DirectX SDK include directory), but it also needs to know where the ‘real’ DirectX code libraries are. After all, the header files only contain prototypes (kind of like usage instructions) for the DirectX components – the components themselves are in DLL files on your system. What the linker needs to do is know which libraries (files with linking instructions) are needed for our project. We’ll be looking at compiling and linking in a later article, but for now here’s what I’m talking about:
Our project uses these files:
…which contains prototypes for the DirectDraw, DirectInput and DirectSound components we’re using. The actual components (the DirectX runtimes) are called:
…which are the files that everyone has for playing DirectX games with. Finally, we have library files that connect code using DirectX components:
Okay, so just read this next sentence very carefully and you’ll be fine: Our source code is compiled with instructions contained within header files, and linked with instructions contained in library files, which ultimately allow our code to use the components in the runtime (DLL) files.
So, where was I? Oh yeah… Head to Project|Settings|Link
, and you’ll see an edit field called Object/library modules
. You may even see some entries already here, all ending in .LIB. All we need to do is add our library files here, and they happen to be located in a directory called LIB inside our main DirectX folder. What I do is add this to the front of the library list:
If there are other libraries in this list already, leave them after yours (separated by a space). Now, there happens to be one other library that we’re using (for our sound code), so you’ll need to add WINMM.LIB
to this list as well. So, to recap, let’s say that when you first showed up in this dialog, the edit box showed something like:
SOMELIB.LIB OTHERLIB.LIB YETMORE.LIB…
(Whatever their names actually are, who cares). Now it should be:
C:\DXVCSDK\LIB\*.lib WINMM.LIB SOMELIB.LIB OTHERLIB.LIB…
Now for the big moment. Build the project (by hitting F7), and pray that you see ‘0 errors, 0 warnings
’ when it’s done. If you are having problems, first realize that the build process involves two stages, compiling and linking, both with their own types of errors. For instance, the link process won’t even begin if there were errors in the compile stage. Either you saw ‘Linking…’ in the output window or you didn’t.
If you are getting complaints like:
c:\gdn\basecode\globals.h(87) : error C2146: syntax error : missing ';' before identifier 'lpDD'
c:\gdn\basecode\globals.h(87) : error C2501: 'LPDIRECTDRAW7' : missing storage-class or type specifiers
c:\gdn\basecode\globals.h(87) : error C2501: 'lpDD' : missing storage-class or type specifiers
c:\gdn\basecode\globals.h(88) : error C2146: syntax error : missing ';' before identifier 'lpDDSPrimary'
c:\gdn\basecode\globals.h(88) : error C2501: 'LPDIRECTDRAWSURFACE7' : missing storage-class or type specifiers
c:\gdn\basecode\globals.h(88) : error C2501: 'lpDDSPrimary' : missing storage-class or type specifiers
c:\gdn\basecode\globals.h(89) : error C2146: syntax error : missing ';' before identifier 'lpDDSBack'
c:\gdn\basecode\globals.h(89) : error C2501: 'LPDIRECTDRAWSURFACE7' : missing storage-class or type specifiers
c:\gdn\basecode\globals.h(89) : error C2501: 'lpDDSBack' : missing storage-class or type specifiers
c:\gdn\basecode\globals.h(90) : error C2146: syntax error : missing ';' before identifier 'lpDDSRes'
c:\gdn\basecode\globals.h(90) : error C2501: 'LPDIRECTDRAWSURFACE7' : missing storage-class or type specifiers
c:\gdn\basecode\globals.h(90) : error C2501: 'lpDDSRes' : missing storage-class or type specifiers
c:\gdn\basecode\globals.h(94) : error C2146: syntax error : missing ';' before identifier 'lpDI'
c:\gdn\basecode\globals.h(94) : error C2501: 'LPDIRECTINPUT7' : missing storage-class or type specifiers
(…on and on…) then you probably need to make sure that you’re pointing the IDE to the DirectX header files (look in Tools|Options|Directories
). If you have other compile errors, try to get ahold of me (firstname.lastname@example.org or 70816765 on ICQ) or post a reply to this article with your questions.
As for the linking, well, that’s primarily the job of the LIB list we prepared under Project|Settings|Link. Here’s an example of having it entered incorrectly:
InitTerm.obj : error LNK2001: unresolved external symbol _DirectDrawCreateEx@16
InitTerm.obj : error LNK2001: unresolved external symbol _IID_IDirectDraw7
InitTerm.obj : error LNK2001: unresolved external symbol _c_dfDIKeyboard
InitTerm.obj : error LNK2001: unresolved external symbol _GUID_SysKeyboard
InitTerm.obj : error LNK2001: unresolved external symbol _IID_IDirectInputDevice7A
InitTerm.obj : error LNK2001: unresolved external symbol _DirectInputCreateEx@20
InitTerm.obj : error LNK2001: unresolved external symbol _IID_IDirectInput7A
InitTerm.obj : error LNK2001: unresolved external symbol _DirectSoundCreate@12
Debug/basecode.exe : fatal error LNK1120: 8 unresolved externals
Error executing link.exe.
basecode.exe - 9 error(s), 0 warning(s)
As you can see, since the libraries weren’t included properly, the poor linker was lost when it came to including these symbols. Once again, let us know if you’re stumped.
When we added the libraries in for the linker, you may have noticed on the left side of the screen that there was a box that had ‘Settings for: Win32 Debug
’. Keep in mind that these libraries that you’re adding are only valid when in Debug mode. You can either change this box to read ‘All Configurations
’ before entering your libraries, or enter them a second time when in Release mode.
So Far So Good…
If you managed to reach the finish line, ‘0 errors, 0 warnings
’, run the program (CTRL-F5) and hopefully you’ll be rewarded with some text on the screen. The space bar will cause a sound to play, and ESCAPE will exit the program. If you aren’t here yet, don’t bother continuing in the forum! That’s why this is called the ‘Readiness Test’… if you can’t get this stuff to run you need to let us know so that we can help rectify the situation.
What about the program itself? On the surface, it doesn’t seem to do much. In fact, it uses DirectDraw, DirectInput and DirectSound in proper form (initialization, use and shutdown), so it’s a good learning example. In later articles we will be using and building on this framework, so it’s a good idea to get your DirectX online documentation ready and try going through the code. Of course, the documentation provides excellent introductory material on each of these components, and if you take the time to read some of it you’ll be ahead of the game.
The Ladder series was designed to represent ‘prerequisite knowledge’. You know have everything you need to start on your path to enlightenment:
- An introduction to game development
- A basic windows application framework
- C language resources
- A quick overview of some of the basic tools required
- Some insight into how a game might be written
- Information on resolutions, colors and pixel plotting
- The role of DirectX in our games
- A first run with some game template code
You have your compiler/linker in order, the DirectX runtimes and SDK installed, the online documentation at your fingertips, and some template code that sits before you like an open book with blank pages – ready to be filled in with the knowledge and practice that is the rite of passage for any apprentice.
Get ready folks, because next up is the proverbial ‘Square One’. See you there.
Questions? Comments? Can’t get the damned code to build? Reply to this article – help is on the way!
Edited by - teej on April 20, 2001 11:10:00 AM