OSDN Git Service

directx: 例外コードのhead
[roast/roast_ex_windows.git] / roast_ex / include / roast / graphics / directx / dx9 / idirect3d_device.hpp
1 //      Roast+ License
2
3 #ifndef __SFJP_ROAST_EX__graphics__directx__dx9__idirect3d_device_HPP__
4 #define __SFJP_ROAST_EX__graphics__directx__dx9__idirect3d_device_HPP__
5
6 #include "roast/graphics/directx/dx9/idirect3d.hpp"
7 #include "roast/windows/window.hpp"
8
9 namespace roast
10 {
11         namespace directx
12         {
13                 namespace dx9
14                 {
15                         namespace graphics
16                         {
17                                 ///////////////////////////////////////////////////////////////////////////
18
19                                 template <int _DeviceModeN>
20                                 struct common_device_mode_2_d3d_device_type{ static const ::D3DDEVTYPE value = D3DDEVTYPE_HAL; };
21                                 template <> struct common_device_mode_2_d3d_device_type<::roast::directx::device_mode::values::hardware>{ static const ::D3DDEVTYPE value = D3DDEVTYPE_HAL; };
22                                 template <> struct common_device_mode_2_d3d_device_type<::roast::directx::device_mode::values::reference>{ static const ::D3DDEVTYPE value = D3DDEVTYPE_REF; };
23                                 template <> struct common_device_mode_2_d3d_device_type<::roast::directx::device_mode::values::no_rendering>{ static const ::D3DDEVTYPE value = D3DDEVTYPE_NULLREF; };
24                                 template <> struct common_device_mode_2_d3d_device_type<::roast::directx::device_mode::values::user_renderer>{ static const ::D3DDEVTYPE value = D3DDEVTYPE_SW; };
25
26                                 ///////
27                                 
28                                 class device : public device_base, public iunknown_<idirect3d_device>
29                                 {
30                                 public:
31                                         enum exception_codes
32                                         {
33                                                 exception_codes__head = exception_code_root::device,
34
35                                                 start__IDirect3D9_CreateDevice__Failed,
36                                                 begin_scene__BeginScene_Failed,
37                                                 end_scene__EndScene_Failed,
38                                                 present__Present_Failed,
39                                                 clear__Clear_Failed
40                                         };
41                                 protected:
42                                         first& m_first;
43                                 public:
44                                         device(first &f) : m_first(f){};
45
46                                         ///////////////
47
48                                         template <int _DeviceModeN>
49                                         void start(
50                                                 const ::roast::windows::window &w,
51                                                 bool fullscreen,
52                                                 int adapter_no,
53                                                 const ::roast::directx::device_mode::_<_DeviceModeN> &device_mode)
54                                         {
55                                                 ::D3DDEVTYPE d3d_device_type = common_device_mode_2_d3d_device_type<_DeviceModeN>::value;
56
57                                                 DWORD behavior_flags_vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
58                                                 if ( d3d_device_type == D3DDEVTYPE_HAL )
59                                                         behavior_flags_vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
60                                                 DWORD behavior_flags = behavior_flags_vp;
61
62                                                 ///////////////////////////////////////////////////////////
63
64                                                 //      Get current display mode
65                                                 disp_mode dispmode = m_first.get_current_display_mode(adapter_no);
66
67                                                 //      Present Parameters
68                                                 D3DPRESENT_PARAMETERS pp;
69                                                 ::ZeroMemory( &pp, sizeof(pp) );
70
71                                                 pp.BackBufferHeight = w.get_height();
72                                                 pp.BackBufferWidth = w.get_width();
73                                                 pp.BackBufferFormat = (D3DFORMAT)dispmode.get_format();
74                                                 pp.BackBufferCount = 1;
75
76                                                 pp.Windowed = (fullscreen ? FALSE : TRUE);
77                                                 pp.MultiSampleType = D3DMULTISAMPLE_NONE;
78                                                 pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
79                                                 //pp.hDeviceWindow = hWnd;
80                                                 pp.FullScreen_RefreshRateInHz  = D3DPRESENT_RATE_DEFAULT;
81
82                                                 ///////////////////////////////////////////////////////////
83
84                                                 //      IDirect3D::CreateDevice
85                                                 HRESULT hr = m_first.get_idirect3d_ptr()->CreateDevice(
86                                                         adapter_no, d3d_device_type, w.get_hwnd(), behavior_flags, &pp, &m_if );
87                                                 
88                                                 if ( hr != D3D_OK ){
89                                                         //      Retry to Mixed Vertex Processing
90                                                         hr = m_first.get_idirect3d_ptr()->CreateDevice(adapter_no, d3d_device_type, w.get_hwnd(), behavior_flags | D3DCREATE_MIXED_VERTEXPROCESSING, &pp, &m_if );
91                                                         if ( hr != D3D_OK ){
92                                                                 //      Retry to Software Vertex Processing
93                                                                 hr = m_first.get_idirect3d_ptr()->CreateDevice(adapter_no, d3d_device_type, w.get_hwnd(), behavior_flags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pp, &m_if );
94                                                                 if ( hr != D3D_OK ){
95                                                                         if ( _DeviceModeN == ::roast::directx::device_mode::values::_auto )
96                                                                                 start<>(w,fullscreen,adapter_no,::roast::directx::device_mode::reference);
97                                                                         else
98                                                                                 throw api_error(start__IDirect3D9_CreateDevice__Failed,
99                                                                                 "IDirect3D9::CreateDevice() Failed.", hr);
100                                                                 }
101                                                         }
102                                                 }
103                                         }
104
105                                         void start(
106                                                 const ::roast::windows::window &w,
107                                                 bool fullscreen=false,
108                                                 int adapter_no=0)
109                                         {
110                                                 start<>(w,fullscreen,adapter_no,::roast::directx::device_mode::auto_);
111                                         }
112                                         
113                                         //////////////////////////////////////////////////////////////////////////////
114                                         
115                                         void begin_scene()
116                                         {
117                                                 //if ( m_if == NULL )
118                                                 //      throw exception("device::begin_scene()  not normally initialized.");
119
120                                                 HRESULT hr = m_if->BeginScene();
121                                                 if ( hr != D3D_OK )
122                                                         throw api_error(begin_scene__BeginScene_Failed,"IDirect3DDevice9::BeginScene() Failed.", hr);
123                                         }
124                                         
125                                         void end_scene()
126                                         {
127                                                 HRESULT hr = m_if->EndScene();
128                                                 if ( hr != D3D_OK )
129                                                         throw api_error(end_scene__EndScene_Failed,"IDirect3DDevice9::EndScene() Failed.", hr);
130                                         }
131
132                                         void present(const ::RECT *source_rect=NULL, const ::RECT *dest_rect=NULL, ::HWND dest_window_override=NULL, const ::RGNDATA* dirty_region=NULL)
133                                         {
134                                                 HRESULT hr = m_if->Present(source_rect, dest_rect, dest_window_override, dirty_region);
135                                                 if ( hr != D3D_OK )
136                                                         throw api_error(present__Present_Failed,"IDirect3DDevice9::Present() Failed.", hr);
137                                         }
138                                         //void present(){ present(::RECT(), ::RECT()); }
139                                         //void present(const ::RECT &source_rect, const ::RECT &dest_rect){ present(source_rect, dest_rect, NULL, ::RGNDATA()); }
140                                         /*void present(const ::RECT &source_rect, const ::RECT &dest_rect, ::HWND dest_window_override, const ::RGNDATA& dirty_region)
141                                         {
142                                                 HRESULT hr = m_if->Present(&source_rect, &dest_rect, dest_window_override, &dirty_region);
143                                                 if ( hr != D3D_OK )
144                                                         throw api_error("IDirect3DDevice9::Present() Failed.", hr);
145                                         }*/
146                                         
147                                         void clear(const color_t& cl=colordef::black)
148                                         {
149                                                 clear(0,NULL,D3DCLEAR_TARGET,cl);
150                                         }
151                                         void clear(int rect_count, const ::RECT *rect_array, DWORD targets, D3DCOLOR color, float z=1.0, DWORD stencil=0)
152                                         {
153                                                 HRESULT hr = m_if->Clear(rect_count, (D3DRECT*)rect_array, targets, color, z, stencil);
154                                                 if ( hr != D3D_OK )
155                                                         throw api_error(clear__Clear_Failed,"IDirect3DDevice9::Clear() Failed.", hr);
156                                         }
157                                         
158                                         //////////////////////////////////////////////////////////////////////////////
159                                         
160                                         idirect3d_device* get_idirect3d_device_ptr(){ return get_internal_ptr(); }
161                                 };
162
163                                 ///////////////////////////////////////////////////////////////////////////
164                         }
165                 }
166         }
167 }
168
169 #endif//__SFJP_ROAST_EX__graphics__directx__dx9__idirect3d_device_HPP__