3 #ifndef __SFJP_ROAST_EX__graphics__directx__dx9__idirect3d_device_HPP__
4 #define __SFJP_ROAST_EX__graphics__directx__dx9__idirect3d_device_HPP__
6 #include "roast/graphics/directx/dx9/idirect3d.hpp"
7 #include "roast/windows/window.hpp"
17 ///////////////////////////////////////////////////////////////////////////
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; };
28 class device : public device_base, public iunknown_<idirect3d_device>
33 exception_codes__head = exception_code_root::device,
35 start__IDirect3D9_CreateDevice__Failed,
36 begin_scene__BeginScene_Failed,
37 end_scene__EndScene_Failed,
38 present__Present_Failed,
44 device(first &f) : m_first(f){};
48 template <int _DeviceModeN>
50 const ::roast::windows::window &w,
53 const ::roast::directx::device_mode::_<_DeviceModeN> &device_mode)
55 ::D3DDEVTYPE d3d_device_type = common_device_mode_2_d3d_device_type<_DeviceModeN>::value;
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;
62 ///////////////////////////////////////////////////////////
64 // Get current display mode
65 disp_mode dispmode = m_first.get_current_display_mode(adapter_no);
68 D3DPRESENT_PARAMETERS pp;
69 ::ZeroMemory( &pp, sizeof(pp) );
71 pp.BackBufferHeight = w.get_height();
72 pp.BackBufferWidth = w.get_width();
73 pp.BackBufferFormat = (D3DFORMAT)dispmode.get_format();
74 pp.BackBufferCount = 1;
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;
82 ///////////////////////////////////////////////////////////
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 );
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 );
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 );
95 if ( _DeviceModeN == ::roast::directx::device_mode::values::_auto )
96 start<>(w,fullscreen,adapter_no,::roast::directx::device_mode::reference);
98 throw api_error(start__IDirect3D9_CreateDevice__Failed,
99 "IDirect3D9::CreateDevice() Failed.", hr);
106 const ::roast::windows::window &w,
107 bool fullscreen=false,
110 start<>(w,fullscreen,adapter_no,::roast::directx::device_mode::auto_);
113 //////////////////////////////////////////////////////////////////////////////
117 //if ( m_if == NULL )
118 // throw exception("device::begin_scene() not normally initialized.");
120 HRESULT hr = m_if->BeginScene();
122 throw api_error(begin_scene__BeginScene_Failed,"IDirect3DDevice9::BeginScene() Failed.", hr);
127 HRESULT hr = m_if->EndScene();
129 throw api_error(end_scene__EndScene_Failed,"IDirect3DDevice9::EndScene() Failed.", hr);
132 void present(const ::RECT *source_rect=NULL, const ::RECT *dest_rect=NULL, ::HWND dest_window_override=NULL, const ::RGNDATA* dirty_region=NULL)
134 HRESULT hr = m_if->Present(source_rect, dest_rect, dest_window_override, dirty_region);
136 throw api_error(present__Present_Failed,"IDirect3DDevice9::Present() Failed.", hr);
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)
142 HRESULT hr = m_if->Present(&source_rect, &dest_rect, dest_window_override, &dirty_region);
144 throw api_error("IDirect3DDevice9::Present() Failed.", hr);
147 void clear(const color_t& cl=colordef::black)
149 clear(0,NULL,D3DCLEAR_TARGET,cl);
151 void clear(int rect_count, const ::RECT *rect_array, DWORD targets, D3DCOLOR color, float z=1.0, DWORD stencil=0)
153 HRESULT hr = m_if->Clear(rect_count, (D3DRECT*)rect_array, targets, color, z, stencil);
155 throw api_error(clear__Clear_Failed,"IDirect3DDevice9::Clear() Failed.", hr);
158 //////////////////////////////////////////////////////////////////////////////
160 idirect3d_device* get_idirect3d_device_ptr(){ return get_internal_ptr(); }
163 ///////////////////////////////////////////////////////////////////////////
169 #endif//__SFJP_ROAST_EX__graphics__directx__dx9__idirect3d_device_HPP__