OSDN Git Service

dx9/idirect3d_device.hpp: clear()を実装(でもクリアされない・・・)
[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                                 protected:
31                                         first& m_first;
32                                 public:
33                                         device(first &f) : m_first(f){};
34
35                                         ///////////////
36
37                                         template <int _DeviceModeN>
38                                         void start(
39                                                 const ::roast::windows::window &w,
40                                                 bool fullscreen,
41                                                 int adapter_no,
42                                                 const ::roast::directx::device_mode::_<_DeviceModeN> &device_mode)
43                                         {
44                                                 ::D3DDEVTYPE d3d_device_type = common_device_mode_2_d3d_device_type<_DeviceModeN>::value;
45
46                                                 DWORD behavior_flags_vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
47                                                 if ( d3d_device_type == D3DDEVTYPE_HAL )
48                                                         behavior_flags_vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
49                                                 DWORD behavior_flags = behavior_flags_vp;
50
51                                                 ///////////////////////////////////////////////////////////
52
53                                                 //      Get current display mode
54                                                 disp_mode dispmode = m_first.get_current_display_mode(adapter_no);
55
56                                                 //      Present Parameters
57                                                 D3DPRESENT_PARAMETERS pp;
58                                                 ::ZeroMemory( &pp, sizeof(pp) );
59
60                                                 pp.BackBufferHeight = w.get_height();
61                                                 pp.BackBufferWidth = w.get_width();
62                                                 pp.BackBufferFormat = (D3DFORMAT)dispmode.get_format();
63                                                 pp.BackBufferCount = 1;
64
65                                                 pp.Windowed = (fullscreen ? FALSE : TRUE);
66                                                 pp.MultiSampleType = D3DMULTISAMPLE_NONE;
67                                                 pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
68                                                 //pp.hDeviceWindow = hWnd;
69                                                 pp.FullScreen_RefreshRateInHz  = D3DPRESENT_RATE_DEFAULT;
70
71                                                 ///////////////////////////////////////////////////////////
72
73                                                 //      IDirect3D::CreateDevice
74                                                 HRESULT hr = m_first.get_idirect3d_ptr()->CreateDevice(
75                                                         adapter_no, d3d_device_type, w.get_hwnd(), behavior_flags, &pp, &m_if );
76                                                 
77                                                 if ( hr != D3D_OK ){
78                                                         //      Retry to Mixed Vertex Processing
79                                                         hr = m_first.get_idirect3d_ptr()->CreateDevice(adapter_no, d3d_device_type, w.get_hwnd(), behavior_flags | D3DCREATE_MIXED_VERTEXPROCESSING, &pp, &m_if );
80                                                         if ( hr != D3D_OK ){
81                                                                 //      Retry to Software Vertex Processing
82                                                                 hr = m_first.get_idirect3d_ptr()->CreateDevice(adapter_no, d3d_device_type, w.get_hwnd(), behavior_flags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pp, &m_if );
83                                                                 if ( hr != D3D_OK ){
84                                                                         if ( _DeviceModeN == ::roast::directx::device_mode::values::_auto )
85                                                                                 start<>(w,fullscreen,adapter_no,::roast::directx::device_mode::reference);
86                                                                         else
87                                                                                 throw api_error("IDirect3D9::CreateDevice() Failed.", hr);
88                                                                 }
89                                                         }
90                                                 }
91                                         }
92
93                                         void start(
94                                                 const ::roast::windows::window &w,
95                                                 bool fullscreen=false,
96                                                 int adapter_no=0)
97                                         {
98                                                 start<>(w,fullscreen,adapter_no,::roast::directx::device_mode::auto_);
99                                         }
100                                         
101                                         //////////////////////////////////////////////////////////////////////////////
102                                         
103                                         void begin_scene()
104                                         {
105                                                 //if ( m_if == NULL )
106                                                 //      throw exception("device::begin_scene()  not normally initialized.");
107
108                                                 HRESULT hr = m_if->BeginScene();
109                                                 if ( hr != D3D_OK )
110                                                         throw api_error("IDirect3DDevice9::BeginScene() Failed.", hr);
111                                         }
112                                         
113                                         void end_scene()
114                                         {
115                                                 HRESULT hr = m_if->EndScene();
116                                                 if ( hr != D3D_OK )
117                                                         throw api_error("IDirect3DDevice9::EndScene() Failed.", hr);
118                                         }
119                                         
120                                         void clear(const color_t& cl=colordef::black)
121                                         {
122                                                 clear(0,NULL,D3DCLEAR_TARGET,cl);
123                                         }
124                                         void clear(int rect_count, const ::RECT *rect_array, DWORD targets, D3DCOLOR color, float z=1.0, DWORD stencil=0)
125                                         {
126                                                 HRESULT hr = m_if->Clear(rect_count, (D3DRECT*)rect_array, targets, color, z, stencil);
127                                                 if ( hr != D3D_OK )
128                                                         throw api_error("IDirect3DDevice9::Clear() Failed.", hr);
129                                         }
130                                         
131                                         //////////////////////////////////////////////////////////////////////////////
132                                         
133                                         idirect3d_device* get_idirect3d_device_ptr(){ return get_internal_ptr(); }
134                                 };
135
136                                 ///////////////////////////////////////////////////////////////////////////
137                         }
138                 }
139         }
140 }
141
142 #endif//__SFJP_ROAST_EX__graphics__directx__dx9__idirect3d_device_HPP__