首页 > 应用开发 > IT综合 > DirectX8编程指南-1 (转)

DirectX8编程指南-1 (转)

原创 IT综合 作者:gugu99 时间:2007-10-12 14:19:29 0 删除 编辑
DirectX8编程指南-1 (转)[@more@]

DirectX Tutorial 1: Getting StartedXML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

DirectX 教程一:入门

What you will need


DirectX 8.0 SDK (DOWNLOADable from

DirectX 8.0 SDK (可以从 下载)
Microsoft Visual C++ 6 (SP5)

Microsoft Visual C++ 6 (升级包5)
General knowledge of windows programming

General knowledge of C++ and object-Oriented programming




Welcome to my DirectX tutorials. This is the first in a number of tutorials that should at least help you on the way to make Windows games using Microsoft DirectX 8. I have decided to write these tutorials for two reasons. Firstly, I’m a complete beginner when it comes to DirectX. So, the idea is that as I learn, I can write a short tutorial that should reinforce my knowledge. Secondly, the SDK isn’t the most helpful thing in the world for complete beginners starting out in game development. Also, there isn’t a great deal of stuff out there on the Inte.NET for beginners and DirectX 8, so this should help. One other thing, as I said, I am a beginner. So, if you spot something that is incorrect in these tutorials then please let me know by eMailing me at:">

欢迎你阅读我的DirectX教程。这是在很多教程中第一个应该至少可以在你使用Microsoft DirectX8通往制作Windows游戏道路上帮助你的教程。我决定写这些东西出于很多个原因。首先,当我拿起DirectX的时候我是一个彻底的初学者。因而,在我学习的过程中我有一个想法,我可以写一个小的教程来加深我的理解。其次,对于刚进入游戏开发领域的完全的初学者,SDK不是最有用的东西。而且,在网上没有很多关于Directx8的材料给初学者,因此这个东西可能会很有帮助。另外一方面,正如我所说的,我是一个初学者。因此,如果你在这些教程中发现了一些不正确的东西,请通过发信往webmaster@andypike.com使我知道它。


What is COM? Well, the Component Object Model is basically a library of methods. You can create com objects in your program and then call the methods that they expose to you. Methods are grouped together in collections of related methods. These collections are known as Interfaces. You could think of a COM object as a library of functions arranged by subject. DirectX provides a whole host of these libraries that will enable you to create 3D games. The best part is, that DirectX takes care of a lot of the hard stuff for you, so it is pretty easy to get something simple up and running.


There is a lot more to COM than that, for a full description take a look in the SDK. All you really need to worry about is that you release all of your COM objects/interfaces before your program teRminates. You should make sure that you release them in the reverse order to that which you created them. For example:


1. Create interface A.
2. Create interface B.
3. Release interface B.
4. Release interface A.

You release the COM object by calling their Release method.


Page Flipping


What is Page Flipping? Well, think of a flipbook. This is a number of pages with a slightly different drawing on each page. Then, when you hold the corner and “flip” the pages, it looks like the picture is moving. This is how DirectX Graphics works. You draw all of your objects onto a hidden page, known as the “Back Buffer”. Then when you have finished, flip it to the Front Buffer and repeat the process. As the user is looking at the new front buffer, your program will be drawing onto the back buffer.

什么是Page Flipping?呃,想想flipbook。他是有很多绘有稍微不同的图画的页的书。然后,当你提着一个角然后“flip”这些页,看上去图象就在移动。这就是DirectX Graphics如何工作的。你在一个隐藏的页上绘上你所有的物体。那个隐藏的页就是被称为“Back Buffer”的东西。然后当你绘完之后,把它翻到前面然后重复这个过程。当用户在观看前台的时候,你的程序又在后面绘画。

What would happen without Page Flipping? Without Page Flipping, the user would see each object appear as it was drawn, which isn’t what you want at all.

如果没有Page Flipping会发生什么?没有Page Flipping,用户可能看到每个物体的绘出过程,而这不是你根本不是你想要的。

So, your game will basically consist of a loop, known as the “Game Loop”. Each time around the loop you process your game logic so you know where your objects will be. Next, you clear the Back Buffer. Then draw the current scene onto it. When this is done, flip it to the front and start the loop again. This will continue until the game is shut down. You may have a number of Back Buffers, this is known as a “Swap Chain”.

因此,你的游戏基本上包含一个循环,称为“游戏循环”,每次在循环中,你处理你的游戏逻辑因而你知道你的物体将到哪里。然后,你清除Back Buffer。然后把当前场景绘进去。当这完成了之后,把flip到前面又一次开始循环。这个都要不停的进行,直到你的游戏结束。你可能有很多的Back Buffer,这又称为“Swap Chain”(交换链)。



What is a device? Basically, a device (as far as DirectX Graphics is concerned) is your machines 3D card. You can create an interface that represents your device and then use it to draw objects onto the back buffer.

什么是设备?简单来说,一个设备(就DirectX Graphic来说)是你机器的3D卡。你可以创建一个表示你的设备的接口然后使用它来在back buffer中绘物体。

Game Loop


What is the game loop? Well, the game loop is a code loop that loops until the program is shut down. Inside the game loop is where it all happens: objects are drawn (rendered), game logic is processed (AI, moving objects and SCOring etc) and Windows Messages are processed. Then it's all done again until the program is closed down.


Creating Your First Project


Okay, that’s enough theory lets get started. Follow the step-by-step guide below to create your first DirectX Graphics project.

好的,理论已经足够了,我们开始吧。跟随下面的一步一步的指导来创建你的第一个DirectX Graphics工程

1. In Visual C++ create a new win32 Application.
 a. File > New
 b. From the Projects tab select Win32 Application
 c. Enter a name for your project such as “DX Project 1”
 d. Select a folder for the location of your source code files
 e. Click Next
 f. Select the empty project option.
 g. Click Finish
2. Make sure that your project settings are correct.
 a. Project > Settings...
 b. On the Link tab, make sure that "d3d8.lib" is in the list of Object/Library Modules. If it isn’t simply type it in.
3. Make sure that your search paths are correct.
 a. Tools > Options > Directories Tab
 b. In the "Show directories for" drop-down, select "include files".
 c. If it does not exist already, add the following path: include.
 d. Make sure that this path is at the top of the list by clicking on the up arrow button (if needed).
 e. In the "Show directories for" drop-down, select "library files".
 f. If it does not exist already, add the following path: lib.
 g. Make sure that this path is at the top of the list by clicking on the up arrow button (if needed).
4. Add the source code.
 a. File > New
 b. From the Files tab, select C++ Source File
 c. Enter a filename such as “Main.cpp”
 d. Copy the code segment below, and then paste it into your new file.
5. Build and Run the program.
 a. Press F7 to build your project
 b. Press F5 to run



HRESULT InitialiseD3D(HWND hWnd)
 //First of all, create the main D3D object. If it is created successfully we
 //should get a pointer to an IDirect3D8 interface.

 g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
 if(g_pD3D == NULL)
 return E_FAIL;

 //Get the current display mode

 if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
 return E_FAIL;

 //Create a structure to hold the settings for our device

 ZeroMemory(&d3dpp, sizeof(d3dpp));

 //Fill the structure.
 //We want our program to be windowed, and set the back buffer to a format
 //that matches our current display mode


//我们想要我们的程序用窗口显示,并且设置back buffer为和我们当前显示模式匹配的格式。
 d3dpp.Windowed = TRUE;
 d3dpp.BackBufferFormat = d3ddm.Format;

 //Create a Direct3D device.

 return E_FAIL;
 return S_OK;

void Render()
 if(g_pD3DDevice == NULL)

 //Clear the backbuffer to a green color

 g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0);
 //Begin the scene

 //Rendering of our game objects will go here

 //End the scene

 //Filp the back and front buffers so that whatever has been rendered on the back buffer
 //will now be visible on screen (front buffer).

//翻动back和front buffer因而无论在back buffer中如何渲染可以在屏幕上可见(front //buffer)。
 g_pD3DDevice->Present(NULL, NULL, NULL, NULL);

void CleanUp()
 if(g_pD3DDevice != NULL)
 g_pD3DDevice = NULL;

 if(g_pD3D != NULL)
 g_pD3D = NULL;

void GameLoop()
 //Enter the game loop

 MSG msg;
 BOOL fMessage;

 PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
 while(msg.message != WM_QUIT)
 fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);

 //Process message

 //No message to process, so render the current scene



//The windows message handler

LRESULT WINapi WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 return 0;
 case WM_KEYUP:
 switch (wParam)
 case VK_ESCAPE:
 //User has pressed the escape key, so quit

 return 0;


 return DefWindowProc(hWnd, msg, wParam, lParam);

//Application entry point

 //Register the window class

 WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
 GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
 "DX Project 1", NULL};

 //Create the application's window

 HWND hWnd = CreateWindow("DX Project 1", " Tutorial 1",
 WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
 GetDesktopWindow(), NULL, wc.hInstance, NULL);

 //Initialize Direct3D

 //Show our window

 ShowWindow(hWnd, SW_SHOWDEFAULT);

 //Start game running: Enter the game loop


 UnregisterClass("DX Project 1", wc.hInstance);
 return 0;

You should finish up with a window with a green background (shown below). Okay, it’s not much I know, but everyone has to start somewhere.


ASPectratio="t" v:ext="edit">图片略

So, what is going on here?


This is the applications entry point. Code execution will start here. This is where we register, create and show our window. Once that is complete, we initialise Direct3D and enter our game loop.


This is the applications message handler. Whenever Windows sends a message to our application, it will be handled by this function. Notice that there are two messages that our application will handle: WM_DESTROY and WM_KEYUP, all other messages are passed to DefWindowProc for default message processing.


This is a pointer to an IDirect3D8 interface. From this interface we will create our Direct3D Device.

这会是一个指向IDirect3D8接口的指针。用这个接口我们将要创建我们的Direct3D 设备。

This is a pointer to an IDirect3DDevice8 interface. This will actually represent your hardware graphics card.


This does exactly that: initialise Direct3D. First of all, we create the IDirect3D8 object. From this object we can determine the users current display mode. Finally, we use this information to create a compatible device.


Once our window is created this function is called. This function contains the main game loop. If there are no windows messages to handle, it calls our Render() function.


Firstly we clear the back buffer ready for drawing. Then we use the BeginScene method of our device object to tell DirectX that we are about to start drawing. We can then start to draw our game objects (Tutorial 2). Once we have finished drawing, we use the EndScene method of our device object to tell DirectX that we have finished drawing. The final step is to "flip" (present) the back buffer, this will display our game objects to the user.

首先我们清除了back buffer来准备绘画。然后我们使用我们设备对象的BeginScene函数来告诉Directx我们要开始绘画了。然后我们可以开始绘我们游戏的物体(教程2)。一旦我们完成了绘图,我们使用我们设备对象的EndScene函数来告诉DirectX我们已经完成了绘图。最后一步是“flip(翻动)”(呈现)back buffer,这将显示我们的游戏物体给用户。

Simply cleans up by releasing our objects.




Ok, that’s it for the first tutorial. I know that the finished program wasn’t the most spectacular thing in the world, but just wait for the next tutorial when we will be drawing some shapes!


来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
  • 博文量
  • 访问量