源代码:dx10tut03.zip。
本教程开始介绍真正的DirectX 10编程,但只涉及两件事:初始化Direct3D和关闭Direct3D。
更新后的框架
我们需要在在框架中添加一个类用于处理所有Direct3D方法,名称为D3Dclass类。结构图如下:

框架结构图
如你所见,D3Dclass位于GraphicsClass之中。前面的教程我们说过所有与图形相关的类都会封装在GraphicsClass中,所以新的D3Dclass类也应放入其中。
下面看一下GraphicsClass的改变:
Graphicsclass.h
1
2
3
4
5
|
// Filename: graphicsclass.h
#ifndef _GRAPHICSCLASS_H_
#define _GRAPHICSCLASS_H_
|
下面是第一个变化,我们取消了windows.h,取而代之的是d3dclass.h。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
///
// MY CLASS INCLUDES //
///
#include "d3dclass.h"
/
// GLOBALS //
/
const
bool
FULL_SCREEN =
false
;
const
bool
VSYNC_ENABLED =
true
;
const
float
SCREEN_DEPTH = 1000.0f;
const
float
SCREEN_NEAR = 0.1f;
// Class name: GraphicsClass
class
GraphicsClass
{
public
:
GraphicsClass();
GraphicsClass(
const
GraphicsClass&);
~GraphicsClass();
bool
Initialize(
int
,
int
,
HWND
);
void
Shutdown();
bool
Frame();
private
:
bool
Render();
private
:
|
第二个变化是新的指向D3Dclass的指针,名称为m_D3D。我在所有的类变量之前都加了前缀m_,这样当我编写代码时就可以知道哪些是类成员变量而哪些不是。
1
2
3
4
|
D3DClass* m_D3D;
};
#endif
|
Graphicsclass.cpp
上一个教程中的这个类是空的,没有任何代码。现在我们就需要在GraphicsClass类中编写代码进行D3Dclass对象的初始化和关闭。我们还在Render方法中调用BeginScene和EndScene,这样就可以使用Direct3D进行绘制了。第一个变化时在构造函数中,首先基于安全的考虑将指针初始化为null,所有类指针我们都是这样处理的。
1
2
3
4
|
GraphicsClass::GraphicsClass()
{
m_D3D = 0;
}
|
第二个变化在Initialize方法中。我们创建了D3Dclass对象并调用它的Initialize方法,传递的参数为屏幕宽度、屏幕高度,窗口句柄和Graphicsclass.h文件中的4个全局变量。D3Dclass会根据这些参数创建Direct3D系统。在说到d3dclass文件时会涉及更多细节。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
bool
GraphicsClass::Initialize(
int
screenWidth,
int
screenHeight,
HWND
hwnd)
{
bool
result;
// Create the Direct3D object.
m_D3D =
new
D3DClass;
if
(!m_D3D)
{
return
false
;
}
// Initialize the Direct3D object.
result = m_D3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN,
SCREEN_DEPTH, SCREEN_NEAR);
if
(!result)
{
MessageBox(hwnd, L
"Could not initialize Direct3D."
, L
"Error"
, MB_OK);
return
false
;
}
return
true
;
}
|
下一个变化是GraphicsClass中的Shutdown方法。因为所有图形对象的关闭操作位于这个方法内,所以也需将D3Dclass类的关闭代码放入其中。我还检查了指针是否被初始化,如果没有就无需关闭了,这也就是为什么在类的构造函数中将所有指针设为null的原因。如果发现指针已经被初始化后才会关闭D3Dclass并清理指针。
1
2
3
4
5
6
7
8
9
10
11
12
|
void
GraphicsClass::Shutdown()
{
// Release the D3D object.
if
(m_D3D)
{
m_D3D->Shutdown();
delete
m_D3D;
m_D3D = 0;
}
return
;
}
|
Frame方法也更新为可以在每帧调用Render方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
bool
GraphicsClass::Frame()
{
bool
result;
// Render the graphics scene.
result = Render();
if
(!result)
{
return
false
;
}
return
true
;
}
|
最后一个变化时在Render方法中。我们调用D3D对象将屏幕清除为灰色,然后调用EndScene将会在显示在屏幕上。
1
2
3
4
5
6
7
8
9
10
11
|
bool
GraphicsClass::Render()
{
// Clear the buffers to begin the scene.
m_D3D->BeginScene(0.5f, 0.5f, 0.5f, 1.0f);
// Present the rendered scene to the screen.
m_D3D->EndScene();
return
true
;
}
|
下面看一下D3Dclass的头文件:
D3dclass.h
1
2
3
4
5
|
// Filename: d3dclass.h
#ifndef _D3DCLASS_H_
#define _D3DCLASS_H_
|
首先要指定链接的库。前两个库包含了创建并绘制3D图像的所有Direct3D函数。第三个库包含了一些工具,这些工具可以获取诸如屏幕刷新率、使用的显卡等硬件信息。
1
2
3
4
5
6
|
/
// LINKING //
/
#pragma comment(lib, "d3d10.lib")
#pragma comment(lib, "d3dx10.lib")
#pragma comment(lib, "dxgi.lib")
|
下一步需要包含这些库的头文件,只需用到d3d10.h和d3dx10.h。
1
2
|