dxManager.h
dxManager.h
1 #pragma once
2 #pragma comment(lib, "d3d9.lib")
3 #pragma comment(lib, "d3dx9.lib")
4
5 #include <d3d9.h>
6 #include <d3dx9tex.h>
7 #include <assert.h>
8
9 #define _SCRN_WIDTH_ 640
10 #define _SCRN_HEIGHT_ 480
11
12 class dxManager
13 {
14 public:
15 dxManager(void);
16 ~dxManager(void);
17
18 bool Init(HWND hwnd);
19
20 void Destroy();
21
22 void BeginRender();
23
24 void EndRender();
25
26 IDirect3DSurface9* GetSurfaceFromBitmap(const char* filename);
27
28 IDirect3DSurface9* GetBackBuffer();
29
30 void FlippingSurface(IDirect3DSurface9* pSrcSurface, RECT* pSrcRect, RECT* pDestRect);
31
32 LPDIRECT3DVERTEXBUFFER9 createVertexBuffer(int size, DWORD usage);
33
34 void drawVertexBuffer(LPDIRECT3DVERTEXBUFFER9 buffer, UINT Stride, DWORD bufferFormat);
35
36 private:
37 IDirect3D9* pD3D;
38 IDirect3DDevice9* pd3dDevice;
39 };
2 #pragma comment(lib, "d3d9.lib")
3 #pragma comment(lib, "d3dx9.lib")
4
5 #include <d3d9.h>
6 #include <d3dx9tex.h>
7 #include <assert.h>
8
9 #define _SCRN_WIDTH_ 640
10 #define _SCRN_HEIGHT_ 480
11
12 class dxManager
13 {
14 public:
15 dxManager(void);
16 ~dxManager(void);
17
18 bool Init(HWND hwnd);
19
20 void Destroy();
21
22 void BeginRender();
23
24 void EndRender();
25
26 IDirect3DSurface9* GetSurfaceFromBitmap(const char* filename);
27
28 IDirect3DSurface9* GetBackBuffer();
29
30 void FlippingSurface(IDirect3DSurface9* pSrcSurface, RECT* pSrcRect, RECT* pDestRect);
31
32 LPDIRECT3DVERTEXBUFFER9 createVertexBuffer(int size, DWORD usage);
33
34 void drawVertexBuffer(LPDIRECT3DVERTEXBUFFER9 buffer, UINT Stride, DWORD bufferFormat);
35
36 private:
37 IDirect3D9* pD3D;
38 IDirect3DDevice9* pd3dDevice;
39 };
dxManager.cpp
1 #include "dxManager.h"
2
3 dxManager::dxManager(void)
4 {
5 pD3D = NULL;
6 pd3dDevice = NULL;
7 }
8
9 dxManager::~dxManager(void)
10 {
11 }
12
13 bool dxManager::Init( HWND hwnd )
14 {
15 //Create direct3D ojbect
16 if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
17 return false;
18
19 D3DPRESENT_PARAMETERS pd3dpp;
20 ZeroMemory(&pd3dpp, sizeof(pd3dpp));
21 pd3dpp.BackBufferCount = 1;
22 pd3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
23 pd3dpp.BackBufferWidth = _SCRN_WIDTH_;
24 pd3dpp.BackBufferHeight = _SCRN_HEIGHT_;
25 pd3dpp.hDeviceWindow = hwnd;
26 pd3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
27 pd3dpp.Windowed = true;
28
29 //Create direct3d device
30 HRESULT hr = pD3D->CreateDevice(
31 D3DADAPTER_DEFAULT,
32 D3DDEVTYPE_HAL,
33 hwnd,
34 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
35 &pd3dpp,
36 &pd3dDevice
37 );
38
39 //Turn off culling, so we see the front and back of the triangle
40 pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
41
42 //Turn off D3D lighting, since we are providing our own vertex colors
43 pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
44
45 return !(FAILED(hr));
46 }
47
48 void dxManager::Destroy()
49 {
50 //release d3d object and device
51 if(NULL != pd3dDevice)
52 {
53 pd3dDevice->Release();
54 pd3dDevice = NULL;
55 }
56 if(NULL != pD3D)
57 {
58 pD3D->Release();
59 pD3D = NULL;
60 }
61 }
62
63 void dxManager::BeginRender()
64 {
65 assert( NULL != pd3dDevice);
66 //Clean monitor
67 pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
68 pd3dDevice->BeginScene();
69 }
70
71 void dxManager::EndRender()
72 {
73 assert( NULL != pd3dDevice);
74
75 //call EndScence() before Present()
76 pd3dDevice->EndScene();
77 //draw front buffer on monitor
78 pd3dDevice->Present(NULL, NULL, NULL, NULL);
79 }
80
81 IDirect3DSurface9* dxManager::GetSurfaceFromBitmap( const char* filename )
82 {
83 assert( NULL != pd3dDevice);
84
85 D3DXIMAGE_INFO imageInfo;
86 HRESULT hr;
87 //Get Bitmap information
88 hr = D3DXGetImageInfoFromFile(filename, &imageInfo);
89 if(FAILED(hr))
90 return NULL;
91
92 //Create an empty back buffer
93 IDirect3DSurface9* surface = NULL;
94 hr = pd3dDevice->CreateOffscreenPlainSurface(
95 imageInfo.Width,
96 imageInfo.Height,
97 D3DFMT_X8R8G8B8,
98 D3DPOOL_DEFAULT,
99 &surface,
100 NULL
101 );
102 if(FAILED(hr))
103 return NULL;
104
105 //Fill back buffer with bitmap that file path specified
106 hr = D3DXLoadSurfaceFromFile(
107 surface,
108 NULL,
109 NULL,
110 filename,
111 NULL,
112 D3DX_DEFAULT,
113 0,
114 NULL
115 );
116 if(FAILED(hr))
117 return NULL;
118 return surface;
119 }
120
121 IDirect3DSurface9* dxManager::GetBackBuffer()
122 {
123 assert( NULL!= pd3dDevice);
124
125 IDirect3DSurface9* backbuffer;
126 //Get back buffer that created previous.(Create by method CreateOffscreenPlainSurface()).
127 HRESULT hr = pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
128 return FAILED(hr) ? NULL : backbuffer;
129 }
130
131 void dxManager::FlippingSurface( IDirect3DSurface9* pSrcSurface, RECT* pSrcRect, RECT* pDestRect )
132 {
133 assert( NULL!= pd3dDevice);
134 //Fill front buffer with specific area on back buffer
135 pd3dDevice->StretchRect(pSrcSurface, pSrcRect, this->GetBackBuffer(), pDestRect, D3DTEXF_NONE);
136 }
137
138 LPDIRECT3DVERTEXBUFFER9 dxManager::createVertexBuffer( int size, DWORD usage )
139 {
140 LPDIRECT3DVERTEXBUFFER9 buffer;
141 if(FAILED(pd3dDevice->CreateVertexBuffer(
142 size,
143 0,
144 usage,
145 D3DPOOL_DEFAULT,
146 &buffer,
147 NULL)))
148 {
149 return NULL;
150 }
151
152 return buffer;
153 }
154
155 void dxManager::drawVertexBuffer( LPDIRECT3DVERTEXBUFFER9 buffer, UINT Stride, DWORD bufferFormat )
156 {
157 pd3dDevice->SetStreamSource(0, buffer, 0, Stride);
158 pd3dDevice->SetFVF(bufferFormat);
159 pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);
160 }
161
2
3 dxManager::dxManager(void)
4 {
5 pD3D = NULL;
6 pd3dDevice = NULL;
7 }
8
9 dxManager::~dxManager(void)
10 {
11 }
12
13 bool dxManager::Init( HWND hwnd )
14 {
15 //Create direct3D ojbect
16 if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
17 return false;
18
19 D3DPRESENT_PARAMETERS pd3dpp;
20 ZeroMemory(&pd3dpp, sizeof(pd3dpp));
21 pd3dpp.BackBufferCount = 1;
22 pd3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
23 pd3dpp.BackBufferWidth = _SCRN_WIDTH_;
24 pd3dpp.BackBufferHeight = _SCRN_HEIGHT_;
25 pd3dpp.hDeviceWindow = hwnd;
26 pd3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
27 pd3dpp.Windowed = true;
28
29 //Create direct3d device
30 HRESULT hr = pD3D->CreateDevice(
31 D3DADAPTER_DEFAULT,
32 D3DDEVTYPE_HAL,
33 hwnd,
34 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
35 &pd3dpp,
36 &pd3dDevice
37 );
38
39 //Turn off culling, so we see the front and back of the triangle
40 pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
41
42 //Turn off D3D lighting, since we are providing our own vertex colors
43 pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
44
45 return !(FAILED(hr));
46 }
47
48 void dxManager::Destroy()
49 {
50 //release d3d object and device
51 if(NULL != pd3dDevice)
52 {
53 pd3dDevice->Release();
54 pd3dDevice = NULL;
55 }
56 if(NULL != pD3D)
57 {
58 pD3D->Release();
59 pD3D = NULL;
60 }
61 }
62
63 void dxManager::BeginRender()
64 {
65 assert( NULL != pd3dDevice);
66 //Clean monitor
67 pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
68 pd3dDevice->BeginScene();
69 }
70
71 void dxManager::EndRender()
72 {
73 assert( NULL != pd3dDevice);
74
75 //call EndScence() before Present()
76 pd3dDevice->EndScene();
77 //draw front buffer on monitor
78 pd3dDevice->Present(NULL, NULL, NULL, NULL);
79 }
80
81 IDirect3DSurface9* dxManager::GetSurfaceFromBitmap( const char* filename )
82 {
83 assert( NULL != pd3dDevice);
84
85 D3DXIMAGE_INFO imageInfo;
86 HRESULT hr;
87 //Get Bitmap information
88 hr = D3DXGetImageInfoFromFile(filename, &imageInfo);
89 if(FAILED(hr))
90 return NULL;
91
92 //Create an empty back buffer
93 IDirect3DSurface9* surface = NULL;
94 hr = pd3dDevice->CreateOffscreenPlainSurface(
95 imageInfo.Width,
96 imageInfo.Height,
97 D3DFMT_X8R8G8B8,
98 D3DPOOL_DEFAULT,
99 &surface,
100 NULL
101 );
102 if(FAILED(hr))
103 return NULL;
104
105 //Fill back buffer with bitmap that file path specified
106 hr = D3DXLoadSurfaceFromFile(
107 surface,
108 NULL,
109 NULL,
110 filename,
111 NULL,
112 D3DX_DEFAULT,
113 0,
114 NULL
115 );
116 if(FAILED(hr))
117 return NULL;
118 return surface;
119 }
120
121 IDirect3DSurface9* dxManager::GetBackBuffer()
122 {
123 assert( NULL!= pd3dDevice);
124
125 IDirect3DSurface9* backbuffer;
126 //Get back buffer that created previous.(Create by method CreateOffscreenPlainSurface()).
127 HRESULT hr = pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
128 return FAILED(hr) ? NULL : backbuffer;
129 }
130
131 void dxManager::FlippingSurface( IDirect3DSurface9* pSrcSurface, RECT* pSrcRect, RECT* pDestRect )
132 {
133 assert( NULL!= pd3dDevice);
134 //Fill front buffer with specific area on back buffer
135 pd3dDevice->StretchRect(pSrcSurface, pSrcRect, this->GetBackBuffer(), pDestRect, D3DTEXF_NONE);
136 }
137
138 LPDIRECT3DVERTEXBUFFER9 dxManager::createVertexBuffer( int size, DWORD usage )
139 {
140 LPDIRECT3DVERTEXBUFFER9 buffer;
141 if(FAILED(pd3dDevice->CreateVertexBuffer(
142 size,
143 0,
144 usage,
145 D3DPOOL_DEFAULT,
146 &buffer,
147 NULL)))
148 {
149 return NULL;
150 }
151
152 return buffer;
153 }
154
155 void dxManager::drawVertexBuffer( LPDIRECT3DVERTEXBUFFER9 buffer, UINT Stride, DWORD bufferFormat )
156 {
157 pd3dDevice->SetStreamSource(0, buffer, 0, Stride);
158 pd3dDevice->SetFVF(bufferFormat);
159 pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);
160 }
161