分享
 
 
 

DirectX8编程指南-1

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

DirectX Tutorial 1: Getting Started

DirectX 教程一:入门

What you will need

你需要准备什么

DirectX 8.0 SDK (Downloadable from http://msdn.microsoft.com/directx)

DirectX 8.0 SDK (可以从 http://msdn.microsoft.com/directx 下载)

Microsoft Visual C++ 6 (SP5)

Microsoft Visual C++ 6 (升级包5)

General knowledge of Windows programming

对于Windows编程的大体上的认识

General knowledge of C++ and Object-Oriented programming

关于C++和面向对象编程的总体知识

Introduction

导言

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 Internet 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: webmaster@andypike.com.

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

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.

什么是COM?呃,组件对象模型基本上只是一个函数库。你可以在你的程序中创建COM对象然后调用他提供给你的函数。函数和相关的函数集合在一起。这些吉他被称之为接口(Interface)。你可以把COM对象想象成函数按不同的主题排列的库。DirectX提供可以使你创建3D游戏的库的一个完整host。最好的部分是,DirectX为你作了许多苦差事,因而要使一些简单的东西动起来相当的容易。

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:

关于COM有许多比那些多很多的内容,要得到完整描述看看SDK。你所有真正需要关心的是在你程序终止之前释放你所有的COM对象/接口。你应当确保你以和你创建他们时相反的顺序释放他们。例如:

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.

你通过调用Release函数释放COM对象。

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”(交换链)。

Devices

设备

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.

何为游戏循环?呃,游戏循环是一个代码循环知道你的程序结束为止。在游戏循环发生了下面的事情:物体被绘出(渲染),游戏逻辑得到处理(人工智能,移动物体,统计得分等)和处理Windows消息。然后有一次重复这些知道程序结束。

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: <SDK INSTALL 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: <SDK INSTALL 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

#include <d3d8.h>

LPDIRECT3D8 g_pD3D = NULL;

LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;

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.

//首先,创建一个主要的D3D物体。如果它被成功创建了我们就获得了一个指向Idirect3D8接口的指针。

g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);

if(g_pD3D == NULL)

{

return E_FAIL;

}

//Get the current display mode

//获得当前的显示模式

D3DDISPLAYMODE d3ddm;

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

{

return E_FAIL;

}

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

//创建一个结构来保存我们设备的设置信息

D3DPRESENT_PARAMETERS d3dpp;

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.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;

d3dpp.BackBufferFormat = d3ddm.Format;

//Create a Direct3D device.

//创建一个Direct3D设备

if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,

D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))

{

return E_FAIL;

}

return S_OK;

}

void Render()

{

if(g_pD3DDevice == NULL)

{

return;

}

//Clear the backbuffer to a green color

//把backbuffer清除为绿色的

g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0);

//Begin the scene

//开始场景

g_pD3DDevice->BeginScene();

//Rendering of our game objects will go here

//我们游戏物体的渲染在这里进行

//End the scene

//结束场景

g_pD3DDevice->EndScene();

//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->Release();

g_pD3DDevice = NULL;

}

if(g_pD3D != NULL)

{

g_pD3D->Release();

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);

if(fMessage)

{

//Process message

//处理消息

TranslateMessage(&msg);

DispatchMessage(&msg);

}

else

{

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

//没有消息要处理,因此来渲染当前场景

Render();

}

}

}

//The windows message handler

//窗口消息处理函数

LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

{

switch(msg)

{

case WM_DESTROY:

PostQuitMessage(0);

return 0;

break;

case WM_KEYUP:

switch (wParam)

{

case VK_ESCAPE:

//User has pressed the escape key, so quit

//用户按下了escape键,退出

DestroyWindow(hWnd);

return 0;

break;

}

break;

}

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

}

//Application entry point

//程序进入点

INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)

{

//Register the window class

//注册窗口类

WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,

GetModuleHandle(NULL), NULL, NULL, NULL, NULL,

"DX Project 1", NULL};

RegisterClassEx(&wc);

//Create the application's window

//创建程序的窗口

HWND hWnd = CreateWindow("DX Project 1", "www.andypike.com: Tutorial 1",

WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,

GetDesktopWindow(), NULL, wc.hInstance, NULL);

//Initialize Direct3D

//初始化Direct3D

if(SUCCEEDED(InitialiseD3D(hWnd)))

{

//Show our window

//显示我们的窗口

ShowWindow(hWnd, SW_SHOWDEFAULT);

UpdateWindow(hWnd);

//Start game running: Enter the game loop

//开始程序的运行:进入游戏循环

GameLoop();

}

CleanUp();

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.

你将看到一个绿色背景的窗口显示出来(如下)。好的,所知不多,但是每个人必须要从某处入手。

图片略

So, what is going on here?

呃,这里干了一些什么?

WinMain

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.

这是程序的入口点。程序执行从这里开始。这是我们注册,创建和显示我们窗口的地方。一旦完成这些,我们初始化Direct3D并进入我们的游戏循环。

WinProc

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.

这是程序消息的处理函数。无论何时Windows发送一个消息给我们的程序,它将被这个函数处理。注意我们程序要处理两个消息:WM_DESTROY和WM_KEYUP,所有其它的消息被发送给DefWindowProc来进行缺省的消息处理。

g_pD3D

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

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

g_pD3DDevice

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

这是一个指向Idirect3Ddevice8的接口。这实际代表你的硬件的图形卡。

InitialiseD3D

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.

这实际是这样作的:初始化Direct3D。首先,我们创建Idirect3D8对象。用这个对象,我们可以决定用户当前的显示模式。最终,我们使用这些信息来创建一个兼容的设备。

GameLoop

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.

一旦我们窗口被创建,这个函数就被调用。这个函数包含一个主游戏循环。如果没有Windows消息要处理,它调用我们的Render()函数。

Render

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,这将显示我们的游戏物体给用户。

CleanUp

Simply cleans up by releasing our objects.

简单地通过释放我们的对象来作清理工作。

Summary

总结

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!

好的,第一章就是这样的了。我知道完成的程序不是世界上最引人注目的程序,但只要等到下一个教程当我们就可以画一些图形了!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有