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 device(first &f) : m_first(f){};
37 template <int _DeviceModeN>
39 const ::roast::windows::window &w,
42 const ::roast::directx::device_mode::_<_DeviceModeN> &device_mode)
44 ::D3DDEVTYPE d3d_device_type = common_device_mode_2_d3d_device_type<_DeviceModeN>::value;
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;
51 ///////////////////////////////////////////////////////////
53 // Get current display mode
54 disp_mode dispmode = m_first.get_current_display_mode(adapter_no);
57 D3DPRESENT_PARAMETERS pp;
58 ::ZeroMemory( &pp, sizeof(pp) );
60 pp.BackBufferHeight = w.get_height();
61 pp.BackBufferWidth = w.get_width();
62 pp.BackBufferFormat = (D3DFORMAT)dispmode.get_format();
63 pp.BackBufferCount = 1;
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;
71 ///////////////////////////////////////////////////////////
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 );
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 );
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 );
84 if ( _DeviceModeN == ::roast::directx::device_mode::values::_auto )
85 start<>(w,fullscreen,adapter_no,::roast::directx::device_mode::reference);
87 throw api_error("IDirect3D9::CreateDevice() Failed.", hr);
94 const ::roast::windows::window &w,
95 bool fullscreen=false,
98 start<>(w,fullscreen,adapter_no,::roast::directx::device_mode::auto_);
101 //////////////////////////////////////////////////////////////////////////////
105 //if ( m_if == NULL )
106 // throw exception("device::begin_scene() not normally initialized.");
108 HRESULT hr = m_if->BeginScene();
110 throw api_error("IDirect3DDevice9::BeginScene() Failed.", hr);
115 HRESULT hr = m_if->EndScene();
117 throw api_error("IDirect3DDevice9::EndScene() Failed.", hr);
120 void clear(const color_t& cl=colordef::black)
122 clear(0,NULL,D3DCLEAR_TARGET,cl);
124 void clear(int rect_count, const ::RECT *rect_array, DWORD targets, D3DCOLOR color, float z=1.0, DWORD stencil=0)
126 HRESULT hr = m_if->Clear(rect_count, (D3DRECT*)rect_array, targets, color, z, stencil);
128 throw api_error("IDirect3DDevice9::Clear() Failed.", hr);
131 //////////////////////////////////////////////////////////////////////////////
133 idirect3d_device* get_idirect3d_device_ptr(){ return get_internal_ptr(); }
136 ///////////////////////////////////////////////////////////////////////////
142 #endif//__SFJP_ROAST_EX__graphics__directx__dx9__idirect3d_device_HPP__