-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDrawDevice.cpp
More file actions
268 lines (203 loc) · 6.38 KB
/
DrawDevice.cpp
File metadata and controls
268 lines (203 loc) · 6.38 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/************************************************************************
* *
* device.cpp -- Manages the Direct3D device *
* *
* Copyright (c) Microsoft Corp. All rights reserved. *
* *
* This code is licensed under the terms of the *
* Microsoft Kinect for Windows SDK (Beta) *
* License Agreement: http://kinectforwindows.org/KinectSDK-ToU *
* *
************************************************************************/
#include "stdafx.h"
#include "DrawDevice.h"
const DWORD NUM_BACK_BUFFERS = 2;
inline LONG Width(const RECT& r)
{
return r.right - r.left;
}
inline LONG Height(const RECT& r)
{
return r.bottom - r.top;
}
//-------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------
DrawDevice::DrawDevice() :
m_hwnd(NULL),
m_pD3D(NULL),
m_pDevice(NULL),
m_pSwapChain(NULL),
m_height(0),
m_lDefaultStride(0)
{
}
//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------
DrawDevice::~DrawDevice()
{
DestroyDevice();
}
//-------------------------------------------------------------------
// CreateDevice
//
// Create the Direct3D device.
//-------------------------------------------------------------------
HRESULT DrawDevice::CreateDevice(HWND hwnd)
{
if (m_pDevice)
{
return S_OK;
}
// Create the Direct3D object.
if (m_pD3D == NULL)
{
m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (m_pD3D == NULL)
{
return E_FAIL;
}
}
HRESULT hr = S_OK;
D3DPRESENT_PARAMETERS pp = { 0 };
D3DDISPLAYMODE mode = { 0 };
hr = m_pD3D->GetAdapterDisplayMode(
D3DADAPTER_DEFAULT,
&mode
);
if (FAILED(hr)) { goto done; }
hr = m_pD3D->CheckDeviceType(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
mode.Format,
D3DFMT_X8R8G8B8,
TRUE // windowed
);
if (FAILED(hr)) { goto done; }
pp.BackBufferFormat = D3DFMT_X8R8G8B8;
pp.SwapEffect = D3DSWAPEFFECT_COPY;
pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
pp.Windowed = TRUE;
pp.hDeviceWindow = hwnd;
hr = m_pD3D->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE,
&pp,
&m_pDevice
);
if (FAILED(hr)) { goto done; }
m_hwnd = hwnd;
done:
return hr;
}
//-------------------------------------------------------------------
// SetVideoType
//
// Set the video format.
//-------------------------------------------------------------------
HRESULT DrawDevice::SetVideoType( int Width, int Height, int Stride )
{
HRESULT hr = S_OK;
D3DPRESENT_PARAMETERS pp = { 0 };
// Get the frame size.
m_lDefaultStride = Stride;
// Create Direct3D swap chain.
if(m_pSwapChain!=NULL)
{
m_pSwapChain->Release( );
m_pSwapChain=NULL;
}
pp.BackBufferWidth = Width;
pp.BackBufferHeight = m_height = Height;
pp.Windowed = TRUE;
pp.SwapEffect = D3DSWAPEFFECT_FLIP;
pp.hDeviceWindow = m_hwnd;
pp.BackBufferFormat = D3DFMT_X8R8G8B8;
pp.Flags =
D3DPRESENTFLAG_VIDEO | D3DPRESENTFLAG_DEVICECLIP |
D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
pp.BackBufferCount = NUM_BACK_BUFFERS;
hr = m_pDevice->CreateAdditionalSwapChain(&pp, &m_pSwapChain);
if (FAILED(hr)) { goto done; }
// Update the destination rectangle for the correct
// aspect ratio.
GetClientRect(m_hwnd, &m_rcDest);
done:
return hr;
}
//-------------------------------------------------------------------
// DrawFrame
//
// Draw the video frame.
//-------------------------------------------------------------------
HRESULT DrawDevice::DrawFrame( BYTE * pBits )
{
HRESULT hr = S_OK;
D3DLOCKED_RECT lr;
IDirect3DSurface9 *pSurf = NULL;
IDirect3DSurface9 *pBB = NULL;
if (m_pDevice == NULL || m_pSwapChain == NULL)
{
return S_OK;
}
// Get the swap-chain surface.
hr = m_pSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pSurf);
if (FAILED(hr)) { goto done; }
// Lock the swap-chain surface.
hr = pSurf->LockRect(&lr, NULL, D3DLOCK_NOSYSLOCK );
if (FAILED(hr)) { goto done; }
// Convert the frame. This also copies it to the Direct3D surface.
BYTE * pDst = (BYTE*) lr.pBits;
BYTE * pSrc = pBits;
for( int y = 0 ; y < (int) m_height ; y++ )
{
memcpy( pDst, pSrc, m_lDefaultStride );
pDst += lr.Pitch;
pSrc += m_lDefaultStride;
}
hr = pSurf->UnlockRect();
if (FAILED(hr)) { goto done; }
// Color fill the back buffer.
hr = m_pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBB);
if (FAILED(hr)) { goto done; }
hr = m_pDevice->ColorFill(pBB, NULL, D3DCOLOR_XRGB(0, 0, 0x80));
if (FAILED(hr)) { goto done; }
// Blit the frame.
hr = m_pDevice->StretchRect(pSurf, NULL, pBB, &m_rcDest, D3DTEXF_LINEAR);
if (FAILED(hr)) { goto done; }
// Present the frame.
hr = m_pDevice->Present(NULL, NULL, NULL, NULL);
done:
if(pSurf != NULL)
pSurf->Release();
if(pBB!= NULL)
pBB->Release();
return hr;
}
//-------------------------------------------------------------------
// DestroyDevice
//
// Release all Direct3D resources.
//-------------------------------------------------------------------
void DrawDevice::DestroyDevice()
{
if(m_pSwapChain!=NULL)
{
m_pSwapChain->Release( );
m_pSwapChain=NULL;
}
if(m_pDevice!=NULL)
{
m_pDevice->Release( );
m_pDevice=NULL;
}
if(m_pD3D!=NULL)
{
m_pD3D->Release( );
m_pD3D=NULL;
}
}