Author : Ian Fleeton
Page : << Previous 2 Next >>
new header and library files are
- Contributed by Erik Fagerstrom
What Compiler?
I recommend using Microsoft Visual C++ 6.0 for programming games for Windows 9x. There is a standard edition and a professional edition. The latter is for anybody who wants to make seriously fast releases of their game. Borland's compilers are also very good, but for straight games programming nothing beats MSVC.
Dev-C++
Give the free Dev-C++ 4 from Bloodshed Software a try, you might like it. Paul Gerfen of GameCoding UK has put together a DirectX8 package for Dev-C++ on his download page. And did I mention that it's Open Source? :)
I am not really qualified to talk about the Component Object Model (COM). Not to worry though because it is not a requirement to really understand it but DirectX is structured around the technique. One of the objectives of COM is that components written with it can be upgraded without having to upgrade the software that uses those components. You've most probably installed a game that uses DirectX, and you've probably been doing it for a few years now. Some of my older games use DirectX 5. I now have DirectX 8 installed and those old games work just fine and, possibly, marginally better.
One thing that you, as a DirectX programmer, should know about COM is that it uses reference counting. Personally I think reference counting should be a feature of many programming languages. Reference counting is where an object keeps a count of the number of users referencing it. When, and only when, the number of users referencing it drops to zero should the object destroy itself. As C/C++ programmers, you are used to using new/malloc and delete/free. This could cause problems if the memory you are releasing is still being referenced by another area of the program. By having the object release itself when it knows it is not needed, this problem disappears. DirectX objects use reference counting. When they are created, they automatically set their counter to one. When you reference that resource from another area of the program you should use the method AddRef(). Instead of performing a delete/free on the object you should use Release(). This causes the object to decrease its reference count and if the count is zero, free itself.
Games programmers have enough to worry about besides COM. Thankfully, Microsoft have made it transparent for the most part.
MS Visual C++ 6
Your DirectX games will typically be written using a small amount of the Windows API. You could use MFC but it's not necessary. Your project should be a Win32 Application. You can create one by following these steps:
In MSVC go to the File menu and click New
Under the Projects tab, click Win32 Application
Type in the name of your project and press OK.
On the next step, choose "A simple Win32 application"
I opt for this as opposed to the empty project because it sets up stdafx.h to be my precompiled header. Precompiled headers are used for speeding up the build process. In stdafx.h you should include the header files that most of your source code files will use. These should be the ones that are never (or rarely) changed. Good examples are "windows.h", <fstream> and <string>.
Other Compilers
If you know the procedure for starting a DirectX project in another compiler/IDE then please send me an e-mail with step-by-step instructions so that it can be included here.
When you're writing a DirectX programming, you're not going to get all you need just by writing #include "windows.h" and compiling. DirectX has its own set of headers and library files which must be included and linked. For DirectDraw and DirectInput you will need to do the following. First, include the headers. Preferably, put them in a header file which is precompiled. That means all of your source files will be able to use DirectDraw and DirectInput.
#include "ddraw.h"
#include "dinput.h"
Secondly you need to set your linker options to link with the new DirectX libraries. Not doing so is the most common cause for DirectX related linker errors. To do this with MSVC go to Project->Settings and choose the Link tab. In the box labelled "Object/library modules" type in the extra file names:
ddraw.lib, dinput8.lib, dxguid.lib
dxguid.lib? What's that? This file contains some globally unique identifiers (huge 128 bit numbers) which identify the COM objects. Every COM object needs its own GUID but there's a hell of a lot to choose from. The GUIDs that you will need are #defined in these headers so that they are more manageable.
MS Visual C++ 6 Specific
In MSVC the precompilable header is "stdafx.h" by default. This is where you should include the headers that won't change much (or at all) as you write your programs.
Introduction
My background to DirectX was a bit too theoretical so to make up for it I'm going to try and squash in as much practical information as possible into this tutorial. If you're new to DirectX and haven't yet read the background, go do it now.
We're going to spend most of the DirectDraw tutorials covering exclusive fullscreen mode as this is the typical use for games because it is faster for the amount of pixel-space you get and simpler for 256 colour mode as you don't have to worry about sharing with Windows' palette.
I've mentioned this before, but it's such a good point I'll say it again. There is a lot of useful documentation included with the SDK which covers every interface method, function and structure as well as a few tutorials and as such I won't be copying out any function prototypes or structures. It's there to help you.
Warning
DirectDraw isn't very high level. If I wanted to create a game I'd like to start it with:
int main(int argc, char* argv[])
{
initDirectDraw(FULLSCREEN, 640, 480, 8);
while(true)
{
nextFrame();
// Render frame
}
return 0;
}
With DirectX you don't have that luxury. What you do have, though, is a lot of control and speed, but with it there's a lot of tedious stuff you'll need to do before you see any results. And it is only tedious - not difficult. Once you've written the DirectX initialisation code you can pretty much forget it until the next version of DirectX. As a C++ programmer, you'll probably see that there's a lot of scope for making a nicely wrapped C++ library. If you do this, though, keep in mind that efficiency is paramount in most game genres.
If something doesn't work first time, check that you've coded it properly and re-read the part of the tutorial in question. If you're convinced I've messed up or that I haven't explained myself very well you can e-mail me.
Before we start you'll need a project. Create a Win32 Application project (or equivalent if you're not using MSVC) and make sure you #include the headers <ddraw.h> and <windows.h> and link with the extra libraries "dxguid.lib" and "ddraw.lib".
Now let's get the basic code written. If you've done any Windows programming before, this will look familiar. It is used to create a window and begin a message loop. Each section of code has a dissection which examines every non-trivial part of it. You can view this by clicking on the dissect link above each piece.
The program below should compile with very little modification. You can save yourself some typing (or copy and pasting) by downloading the project ddraw1_a. This project is compatible with MSVC but you can copy the code over to another compiler with ease. If you don't have MSVC then you'll have to #include the headers correctly yourself and (recommended) set up a precompiled header. The background tutorial explains the use of precompiled headers.
Nothing much will happen when you run the program except that you should notice a button on the taskbar. To close the program, make sure the button is clicked and press Alt+F4.
g_running and g_active are two state variables that indicate whether we should update the game or not. There is a subtle difference between the two. The first, g_running, is set to true from the time when the window is created all the way up until the WM_DESTROY message is sent by Windows. We use this to make sure that we don't update the game after the DirectX components have been cleared up. g_active is set to true when the window is showing and is active. This isn't as important as g_running but prevents the game from updating when the user has the program minimised. g_appName is simply a constant C-style string used to contain the game's title. This is used to register and title the window.
update() is an empty function for the moment. We'll be putting our game loop inside here. A game
Page : << Previous 2 Next >>