JunkBox_Win_Lib 1.5.3
Loading...
Searching...
No Matches
Dx9.cpp
Go to the documentation of this file.
1
6#include "Dx9.h"
7
8
9using namespace jbxl;
10using namespace jbxwl;
11
12
13LPDIRECT3D9 jbxwl::GpD3D = NULL;
14LPDIRECTINPUT8 jbxwl::GpDInput = NULL;
15
16LPDIRECTINPUTDEVICE8 jbxwl::GpDMouse = NULL; // マウスのインターフェイス
17LPDIRECTINPUTDEVICE8 jbxwl::GpDKeyBoard = NULL; // キーボードのインターフェイス
18
19
20BOOL jbxwl::Dx9DividePrimitiveMode = TRUE; // 分割描画モード
21BOOL jbxwl::Dx9ReverseZMode = FALSE; // Z軸反転モード(for 3D CT)
22
23
24
30BOOL jbxwl::Dx9CreateInterface(CWinApp* wapp)
31{
32 GpD3D = Direct3DCreate9(D3D_SDK_VERSION);
33 if (GpD3D==NULL) {
34 DEBUG_INFO("jbxwl::Dx9CreateInterface: ERROR: グラフィックデバイスの取得に失敗しました\n");
35 return FALSE;
36 }
37
38 HRESULT hr = DirectInput8Create(wapp->m_hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&GpDInput, NULL);
39 if (GpDInput==NULL) {
40 DEBUG_INFO("jbxwl::Dx9CreateInterface: ERROR: インプットデバイスの取得に失敗しました\n");
42 return FALSE;
43 }
44 else {
45 GpDMouse = Dx9CreateMouse(GpDInput, wapp->m_pMainWnd->m_hWnd);
46 if (GpDMouse==NULL) {
47 Sleep(1000);
48 GpDMouse = Dx9CreateMouse(GpDInput, wapp->m_pMainWnd->m_hWnd);
49 if (GpDMouse==NULL) {
50 DEBUG_INFO("jbxwl::Dx9CreateInterface(): ERROR: マウスデバイスの取得に失敗しました\n");
53 return FALSE;
54 }
55 }
56 GpDKeyBoard = Dx9CreateKeyBoard(GpDInput, wapp->m_pMainWnd->m_hWnd);
57 if (GpDKeyBoard==NULL) {
58 Sleep(1000);
59 GpDKeyBoard = Dx9CreateKeyBoard(GpDInput, wapp->m_pMainWnd->m_hWnd);
60 if (GpDKeyBoard==NULL) {
61 DEBUG_INFO("jbxwl::Dx9CreateInterface(): ERROR: キーボードデバイスの取得に失敗しました\n");
65 return FALSE;
66 }
67 }
68 }
69
70 return TRUE;
71}
72
73
74
82
83
84
93LPDIRECT3DDEVICE9 jbxwl::Dx9CreateGraphic(LPDIRECT3D9 gpD3D, D3DPRESENT_PARAMETERS* d3dParam, HWND hWnd)
94{
95 LPDIRECT3DDEVICE9 d3dDevice=NULL;
96 HRESULT hr;
97
98 hr = gpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, d3dParam, &d3dDevice);
99 if (FAILED(hr)) {
100 hr = gpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dParam, &d3dDevice);
101 if (FAILED(hr)) {
102 hr = gpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dParam, &d3dDevice);
103 if (FAILED(hr)) return (LPDIRECT3DDEVICE9)NULL;
104 }
105 }
106 return d3dDevice;
107}
108
109
110
114LPDIRECTINPUTDEVICE8 jbxwl::Dx9CreateMouse(LPDIRECTINPUT8 gpDInput, HWND hWnd)
115{
116 LPDIRECTINPUTDEVICE8 gpdmouse=NULL;
117
118 HRESULT hr = gpDInput->CreateDevice(GUID_SysMouse, &gpdmouse, NULL);
119 if (FAILED(hr) || gpdmouse==NULL) return NULL;
120
121 hr = gpdmouse->SetDataFormat(&c_dfDIMouse);
122 if (FAILED(hr)) return NULL;
123
124 hr = gpdmouse->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
125 if (FAILED(hr)) return NULL;
126
127// 軸モード
128/* DIPROPDWORD diprop;
129 diprop.diph.dwSize = sizeof(diprop);
130 diprop.diph.dwHeaderSize = sizeof(diprop.diph);
131 diprop.diph.dwObj = 0;
132 diprop.diph.dwHow = DIPH_DEVICE;
133 diprop.dwData = DIPROPAXISMODE_ABS;
134 hr = gpdmouse->SetProperty(DIPROP_AXISMODE, &diprop.diph);
135 if (FAILED(hr)) {
136 DEBUG_INFO("Dx9CreateMouse: SetProprty\n");
137 return NULL;
138 }
139/**/
140 hr = gpdmouse->Acquire();
141 if (FAILED(hr)) return NULL;
142
143 return gpdmouse;
144}
145
146
147
148//
149// 不完全 注意!!!
150//
151LPDIRECTINPUTDEVICE8 jbxwl::Dx9CreateKeyBoard(LPDIRECTINPUT8 gpDInput, HWND hWnd)
152{
153 LPDIRECTINPUTDEVICE8 gpdkeyboard=NULL;
154
155 HRESULT hr = gpDInput->CreateDevice(GUID_SysKeyboard, &gpdkeyboard, NULL);
156 if (FAILED(hr)) return NULL;
157
158 hr = gpdkeyboard->SetDataFormat(&c_dfDIKeyboard);
159 if (FAILED(hr)) return NULL;
160
161 hr = gpdkeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
162 if (FAILED(hr)) return NULL;
163 else return gpdkeyboard;
164}
165
166
167
168/*
169 機能: バックバッファを得る.
170 引数: gpD3DDevice DirectX9デバイス
171 *pgpBackBuffer 獲得したバックバッファ.失敗した場合は NULL
172 戻値: バックバッファのサイズ.エラーの場合は <0
173 説明:
174*/
175POINT jbxwl::Dx9GetBackBuffer(LPDIRECT3DDEVICE9 gpD3DDevice, LPDIRECT3DSURFACE9* pgpBackBuffer)
176{
177 POINT pt;
178 pt.x = pt.y = -1;
179
180 HRESULT hr = gpD3DDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, pgpBackBuffer);
181 if (FAILED(hr)) return pt;
182
183 pt = Dx9GetSurfaceSize(pgpBackBuffer);
184 return pt;
185}
186
187
188
189/*
190 機能: メモリ上の cmnHeadからサーフェイスを作成する.(ラッパー関数)
191 引数: gpD3DDevice DirectX9デバイス
192 *pgpSurface 作成したサーフェイス
193 cmnhead メモリ上のデータを記述する CmnHead 型データ.
194 戻値: 実行結果の BOOL
195 説明: サポートするデータ形式は DirectX9 (jpg, png, bmp, ......), SUN Raster, CT_DATA
196 作成したサーフェイスのメモリ管理は D3DPOOL_DEFAULTになる.
197*/
198BOOL jbxwl::Dx9SurfaceFromMemory(LPDIRECT3DDEVICE9 gpD3DDevice, LPDIRECT3DSURFACE9* pgpSurface, CmnHead cmnHead)
199{
200 BOOL ret;
201
202 if (cmnHead.kind==HEADER_NONE) return FALSE;
203 int chk = cmnHead.kind & 0x00ff;
204
205 if (chk==UN_KNOWN_DATA) ret = Dx9SurfaceFromMemoryFile(gpD3DDevice, pgpSurface, cmnHead);
206 else ret = Dx9SurfaceFromByteMemory(gpD3DDevice, pgpSurface, cmnHead);
207 return ret;
208}
209
210
211
212//
213// メモリ上の cmnHeadからサーフェイスを作成する.
214// DirectX9 がサポートするデータ(UN_KNOWN)用.cmnHead.grptrがメモリ上のファイル形式データを指す.
215//
216BOOL jbxwl::Dx9SurfaceFromMemoryFile(LPDIRECT3DDEVICE9 gpD3DDevice, LPDIRECT3DSURFACE9* pgpSurface, CmnHead cmnHead)
217{
218 //HRESULT hr = gpD3DDevice->CreateOffscreenPlainSurface(
219 // cmnHead.xsize, cmnHead.ysize, (D3DFORMAT)cmnHead.depth, D3DPOOL_DEFAULT, pgpSurface, NULL);
220
221 HRESULT hr = gpD3DDevice->CreateOffscreenPlainSurface(cmnHead.xsize, cmnHead.ysize, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, pgpSurface, NULL);
222 if (FAILED(hr)) return FALSE;
223
224 hr = D3DXLoadSurfaceFromFileInMemory(*pgpSurface, NULL, NULL, cmnHead.grptr, cmnHead.lsize, NULL, D3DX_FILTER_NONE, 0, (D3DXIMAGE_INFO*)cmnHead.buf);
225 if (FAILED(hr)) return FALSE;
226
227 return TRUE;
228}
229
230
231
232//
233// メモリ上の cmnHeadからサーフェイスを作成する.
234// RAS_DATA(SUN Raster), CT_DATAなどの独自ファイル用.表示データは 8bit
235//
236BOOL jbxwl::Dx9SurfaceFromByteMemory(LPDIRECT3DDEVICE9 gpD3DDevice, LPDIRECT3DSURFACE9* pgpSurface, CmnHead cmnHead)
237{
238 HRESULT hr = gpD3DDevice->CreateOffscreenPlainSurface(cmnHead.xsize, cmnHead.ysize, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, pgpSurface, NULL);
239 if (FAILED(hr)) return FALSE;
240
241 RECT rect;
242 rect.left = 0;
243 rect.top = 0;
244 rect.right = cmnHead.xsize - 1;
245 rect.bottom = cmnHead.ysize - 1;
246 hr = D3DXLoadSurfaceFromMemory(*pgpSurface, NULL, NULL, cmnHead.grptr, D3DFMT_L8, cmnHead.xsize, NULL, &rect, D3DX_FILTER_NONE, 0);
247// hr = D3DXLoadSurfaceFromMemory(*pgpSurface, NULL, NULL, cmnHead.grptr, D3DFMT_L16, cmnHead.xsize*2, NULL, &rect, D3DX_FILTER_NONE, 0);
248 if (FAILED(hr)) return FALSE;
249 return TRUE;
250}
251
252
253
254/*
255 機能: メモリ上の cmnHeadからテクスチャを作成する.(ラッパー関数)
256 引数: gpD3DDevice DirectX9デバイス
257 *pgpSurface 作成したテクスチャ
258 cmnhead メモリ上のデータを記述する CmnHead 型データ.
259 戻値: 実行結果の BOOL
260 説明: サポートするデータ形式は DirectX9 (jpg, png, bmp, ......), SUN Raster, CT_DATA
261 作成したテクスチャのメモリ管理は D3DPOOL_DEFAULTになる.
262*/
263BOOL jbxwl::Dx9TextureFromMemory(LPDIRECT3DDEVICE9 gpD3DDevice, LPDIRECT3DTEXTURE9* pgpTexture, CmnHead cmnHead)
264{
265 BOOL ret;
266
267 if (cmnHead.kind==HEADER_NONE) return FALSE;
268 int chk = cmnHead.kind & 0x00ff;
269
270 if (chk==UN_KNOWN_DATA) ret = Dx9TextureFromMemoryFile(gpD3DDevice, pgpTexture, cmnHead);
271 else ret = Dx9TextureFromByteMemory(gpD3DDevice, pgpTexture, cmnHead);
272 return ret;
273}
274
275
276
277//
278// メモリ上の cmnHeadからテクスチャを作成する.
279// DirectX9 がサポートするデータ(UN_KNOWN)用.cmnHead.grptrがメモリ上のファイル形式データを指す.
280//
281BOOL jbxwl::Dx9TextureFromMemoryFile(LPDIRECT3DDEVICE9 gpD3DDevice, LPDIRECT3DTEXTURE9* pgpTexture, CmnHead cmnHead)
282{
283 HRESULT hr = D3DXCreateTextureFromFileInMemoryEx(
284 gpD3DDevice, cmnHead.grptr, cmnHead.lsize, 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, //D3DPOOL_MANAGED,
285 D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR, 0, NULL, NULL, pgpTexture);
286 if (FAILED(hr)) return FALSE;
287 return TRUE;
288}
289
290
291
292//
293// メモリ上の cmnHeadからテクスチャを作成する.
294// RAS_DATA(SUN Raster), CT_DATAなどの独自ファイル用.テクスチャデータは 8bit
295//
296BOOL jbxwl::Dx9TextureFromByteMemory(LPDIRECT3DDEVICE9 gpD3DDevice, LPDIRECT3DTEXTURE9* pgpTexture, CmnHead cmnHead)
297{
298 HRESULT hr;
299 RECT rect;
300 LPDIRECT3DSURFACE9 surface;
301
302 hr = D3DXCreateTexture(gpD3DDevice, cmnHead.xsize, cmnHead.ysize, 0, D3DUSAGE_RENDERTARGET, D3DFMT_L8, D3DPOOL_DEFAULT, pgpTexture);
303 if (FAILED(hr)) return FALSE;
304
305 hr = (*pgpTexture)->GetSurfaceLevel(0, &surface);
306 if (FAILED(hr)) return FALSE;
307
308 rect.left = 0;
309 rect.top = 0;
310 rect.right = cmnHead.xsize - 1;
311 rect.bottom = cmnHead.ysize - 1;
312 hr = D3DXLoadSurfaceFromMemory(surface, NULL, NULL, cmnHead.grptr, D3DFMT_L8, cmnHead.xsize, NULL, &rect, D3DX_FILTER_LINEAR, 0);
313 DXRELEASE(surface);
314
315 if (FAILED(hr)) return FALSE;
316 return TRUE;
317}
318
319
320
321/*
322 機能: DrawPrimitiveのラッパー.Dx9DividePrimitiveModeが TRUEの時,
323 数回に分けて DrawPrimitiveを呼び出す.
324
325 引数: gpD3DDevice DirectX9デバイス
326 ptyepe プリミティブタイプ
327 unit 1個のプリミティブを書くために必要な頂点数.
328 bsize プリミティブの数
329
330 戻値: 実行結果の HRESULT
331
332 説明: ある種のカード は Primitiveの数が多いと誤作動する場合があるので,
333 DXMAXPRIMITIVE個ずつ分けて表示する.
334*/
335HRESULT jbxwl::Dx9DrawPrimitive(LPDIRECT3DDEVICE9 gpD3DDevice, D3DPRIMITIVETYPE ptype, UINT unit, UINT bsize)
336{
337 HRESULT hr;
338
339 if (Dx9DividePrimitiveMode) { // 分割描画モード
340 UINT iter = bsize / DXMAXPRIMITIVE;
341 UINT imod = bsize % DXMAXPRIMITIVE;
342
343 unsigned int i;
344 for (i=0; i<iter; i++) {
345 hr = gpD3DDevice->DrawPrimitive(ptype, i*unit*DXMAXPRIMITIVE, DXMAXPRIMITIVE);
346 if (FAILED(hr)) return hr;
347 }
348 hr = gpD3DDevice->DrawPrimitive(ptype, i*unit*DXMAXPRIMITIVE, imod);
349 //if (FAILED(hr)) Dx9DividePrimitiveMode = FALSE;
350 }
351 else { // ノーマルモード
352 hr = gpD3DDevice->DrawPrimitive(ptype, 0, bsize);
353 //if (FAILED(hr)) Dx9DividePrimitiveMode = TRUE;
354 }
355
356 return hr;
357}
358
359
360
361POINT jbxwl::Dx9GetSurfaceSize(LPDIRECT3DSURFACE9* lpsf)
362{
363 D3DSURFACE_DESC desc;
364 POINT pt;
365
366 (*lpsf)->GetDesc(&desc);
367 pt.x = desc.Width;
368 pt.y = desc.Height;
369
370 return pt;
371}
372
#define DIRECTINPUT_VERSION
Definition Dx9.h:18
#define DXMAXPRIMITIVE
Definition Dx9.h:41
#define DXRELEASE(d)
Definition Dx9.h:39
BOOL Dx9ReverseZMode
Definition Dx9.cpp:21
LPDIRECTINPUTDEVICE8 GpDMouse
Definition Dx9.cpp:16
BOOL Dx9SurfaceFromByteMemory(LPDIRECT3DDEVICE9, LPDIRECT3DSURFACE9 *, CmnHead)
Definition Dx9.cpp:236
BOOL Dx9TextureFromMemoryFile(LPDIRECT3DDEVICE9, LPDIRECT3DTEXTURE9 *, CmnHead)
Definition Dx9.cpp:281
LPDIRECTINPUTDEVICE8 GpDKeyBoard
Definition Dx9.cpp:17
POINT Dx9GetSurfaceSize(LPDIRECT3DSURFACE9 *lpsf)
Definition Dx9.cpp:361
LPDIRECTINPUTDEVICE8 Dx9CreateKeyBoard(LPDIRECTINPUT8, HWND)
Definition Dx9.cpp:151
HRESULT Dx9DrawPrimitive(LPDIRECT3DDEVICE9, D3DPRIMITIVETYPE, UINT, UINT)
Definition Dx9.cpp:335
BOOL Dx9CreateInterface(CWinApp *wapp)
Definition Dx9.cpp:30
LPDIRECT3DDEVICE9 Dx9CreateGraphic(LPDIRECT3D9, D3DPRESENT_PARAMETERS *, HWND)
Definition Dx9.cpp:93
BOOL Dx9SurfaceFromMemoryFile(LPDIRECT3DDEVICE9, LPDIRECT3DSURFACE9 *, CmnHead)
Definition Dx9.cpp:216
POINT Dx9GetBackBuffer(LPDIRECT3DDEVICE9, LPDIRECT3DSURFACE9 *)
Definition Dx9.cpp:175
LPDIRECT3D9 GpD3D
Definition Dx9.cpp:13
LPDIRECTINPUT8 GpDInput
Definition Dx9.cpp:14
LPDIRECTINPUTDEVICE8 Dx9CreateMouse(LPDIRECTINPUT8, HWND)
Definition Dx9.cpp:114
BOOL Dx9SurfaceFromMemory(LPDIRECT3DDEVICE9, LPDIRECT3DSURFACE9 *, CmnHead)
Definition Dx9.cpp:198
BOOL Dx9TextureFromMemory(LPDIRECT3DDEVICE9, LPDIRECT3DTEXTURE9 *, CmnHead)
Definition Dx9.cpp:263
void Dx9ReleaseInterface()
Definition Dx9.cpp:75
BOOL Dx9DividePrimitiveMode
Definition Dx9.cpp:20
BOOL Dx9TextureFromByteMemory(LPDIRECT3DDEVICE9, LPDIRECT3DTEXTURE9 *, CmnHead)
Definition Dx9.cpp:296