-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD3D9Renderer.cpp
145 lines (121 loc) · 3.96 KB
/
D3D9Renderer.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//-----------------------------------------------------------------
// D3D9 renderer class
// C++ Source - D3D9Renderer.cpp
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "D3D9Renderer.h"
//-----------------------------------------------------------------
// Class function definitions
//-----------------------------------------------------------------
D3D9Renderer::D3D9Renderer()
{
m_pD3D = NULL;
m_pD3DDisplayMode = NULL;
m_uiDisplayModeCount = 0;
m_pD3DPresentParam = NULL;
m_pD3DDevice = NULL;
m_pD3DVertexBuffer = NULL;
}
D3D9Renderer::~D3D9Renderer()
{
if(m_pD3DDisplayMode != NULL)
{
if(m_pD3DPresentParam->Windowed)
{
SAFE_DELETE(m_pD3DDisplayMode);
}
else
{
SAFE_DELETEARRAY(m_pD3DDisplayMode);//full screen application has a series of display modes.
}
}
SAFE_DELETE(m_pD3DPresentParam);
if(m_pD3DDevice != NULL)
m_pD3DDevice->Release();
if(m_pD3DVertexBuffer != NULL)
m_pD3DVertexBuffer->Release();
if(m_pD3D != NULL)
m_pD3D->Release();
}
BOOL D3D9Renderer::Initialise(HWND hWindow)
{
//create a D3D object
m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(m_pD3D == NULL)
{
MessageBox(hWindow,TEXT("error in creating D3D object"),TEXT("ERROR"),MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
//create a present paramter object
m_pD3DPresentParam = new D3DPRESENT_PARAMETERS;
if(m_pD3DPresentParam == NULL)
{
MessageBox(hWindow,TEXT("error in creating D3D present param object"),TEXT("ERROR"),MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
return TRUE;
}
BOOL D3D9Renderer::EnumDisplayModes(D3DFORMAT format, BOOL windowed)
{
if(windowed)
{
//for window mode, you can only use the display mode the current desktop uses.
m_pD3DDisplayMode = new D3DDISPLAYMODE;
m_uiDisplayModeCount = 1;
if(m_pD3DDisplayMode == NULL)
return FALSE;
if(FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, m_pD3DDisplayMode)))//get the current display mode
return FALSE;
}
else
{
//for fullscreen mode, you specify a surface format and then query d3d if it is supported.
//if yes, you can ask for all the resolution/refresh rate supported and then pick one for display mode.
m_uiDisplayModeCount = m_pD3D->GetAdapterModeCount(D3DADAPTER_DEFAULT,format);
if(m_uiDisplayModeCount == 0)
return FALSE;//something is wrong in getting the total number of the display modes
m_pD3DDisplayMode = new D3DDISPLAYMODE[m_uiDisplayModeCount];
if(m_pD3DDisplayMode == NULL)
return FALSE;
for(UINT i =0;i < m_uiDisplayModeCount;i++)
{
if((m_pD3D->EnumAdapterModes(D3DADAPTER_DEFAULT,format,i,m_pD3DDisplayMode+i)) != D3D_OK)
return FALSE;
}
}
return TRUE;
}
BOOL D3D9Renderer::CreateD3DDevice(UINT Adapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags)
{
if(FAILED(m_pD3D->CreateDevice(Adapter,DeviceType, hFocusWindow,
BehaviorFlags,m_pD3DPresentParam, &m_pD3DDevice)))
return FALSE;
return TRUE;
}
BOOL D3D9Renderer::CreateVertexBuffer(UINT Length,DWORD Usage,DWORD FVF,D3DPOOL Pool)
{
if(FAILED(m_pD3DDevice->CreateVertexBuffer(Length,Usage,FVF,Pool,&m_pD3DVertexBuffer,NULL)))
return FALSE;
return TRUE;
}
void D3D9Renderer::RestoreDevice()
{
HRESULT result = m_pD3DDevice->TestCooperativeLevel();
while(result == D3DERR_DEVICELOST)
{
while(result != D3DERR_DEVICENOTRESET)
{
Sleep(1000);//give up the cpu for others
//while waiting,incoming messages should still be taken care of
MSG Message;
PeekMessage(&Message,0,0,0,PM_REMOVE);
TranslateMessage(&Message);
DispatchMessage(&Message);
result = m_pD3DDevice->TestCooperativeLevel();
}
if(FAILED(m_pD3DDevice->Reset(m_pD3DPresentParam)))
result = D3DERR_DEVICELOST;
}
}