3 ==============================================================================
5 This file is part of the Shooting3
6 Copyright 2005-7 by Satoshi Fujiwara.
8 Shooting3 can be redistributed and/or modified under the terms of the
9 GNU General Public License, as published by the Free Software Foundation;
10 either version 2 of the License, or (at your option) any later version.
12 S.F.Tracker is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with S.F.Tracker; if not, visit www.gnu.org/licenses or write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 Boston, MA 02111-1307 USA
22 ==============================================================================
24 // Windows Header Files:
25 #include "exception.h"
29 _COM_SMARTPTR_TYPEDEF(ID2D1Factory,__uuidof(ID2D1Factory));
30 _COM_SMARTPTR_TYPEDEF(IWICImagingFactory, __uuidof(IWICImagingFactory));
31 _COM_SMARTPTR_TYPEDEF(IDWriteFactory , __uuidof(IDWriteFactory));
32 _COM_SMARTPTR_TYPEDEF(IDWriteGdiInterop , __uuidof(IDWriteGdiInterop));
33 _COM_SMARTPTR_TYPEDEF(IDWriteFontFace , __uuidof(IDWriteFontFace));
34 _COM_SMARTPTR_TYPEDEF(IDWriteFont , __uuidof(IDWriteFont));
35 _COM_SMARTPTR_TYPEDEF(IDWriteFontFamily , __uuidof(IDWriteFontFamily));
36 _COM_SMARTPTR_TYPEDEF(IDWriteFontCollection , __uuidof(IDWriteFontCollection));
37 _COM_SMARTPTR_TYPEDEF(IDWriteLocalizedStrings , __uuidof(IDWriteLocalizedStrings));
38 _COM_SMARTPTR_TYPEDEF(ID2D1HwndRenderTarget , __uuidof(ID2D1HwndRenderTarget));
39 _COM_SMARTPTR_TYPEDEF(ID2D1BitmapRenderTarget , __uuidof(ID2D1BitmapRenderTarget));
40 _COM_SMARTPTR_TYPEDEF(ID2D1GdiInteropRenderTarget , __uuidof(ID2D1GdiInteropRenderTarget));
41 _COM_SMARTPTR_TYPEDEF(IDWriteTextFormat, __uuidof(IDWriteTextFormat));
42 _COM_SMARTPTR_TYPEDEF(IDWriteTextLayout, __uuidof(IDWriteTextLayout));
43 _COM_SMARTPTR_TYPEDEF(ID2D1PathGeometry , __uuidof(ID2D1PathGeometry));
44 _COM_SMARTPTR_TYPEDEF(ID2D1LinearGradientBrush , __uuidof(ID2D1LinearGradientBrush));
45 _COM_SMARTPTR_TYPEDEF(ID2D1GradientStopCollection , __uuidof(ID2D1GradientStopCollection));
46 _COM_SMARTPTR_TYPEDEF(ID2D1SolidColorBrush , __uuidof(ID2D1SolidColorBrush));
47 _COM_SMARTPTR_TYPEDEF(ID2D1BitmapBrush , __uuidof(ID2D1BitmapBrush));
48 _COM_SMARTPTR_TYPEDEF(ID2D1Bitmap , __uuidof(ID2D1Bitmap));
49 _COM_SMARTPTR_TYPEDEF(IWICBitmapDecoder,__uuidof(IWICBitmapDecoder));
50 _COM_SMARTPTR_TYPEDEF(IWICBitmapFrameDecode,__uuidof(IWICBitmapFrameDecode));
51 _COM_SMARTPTR_TYPEDEF(IWICStream,__uuidof(IWICStream));
52 _COM_SMARTPTR_TYPEDEF(IWICFormatConverter,__uuidof(IWICFormatConverter));
53 _COM_SMARTPTR_TYPEDEF(IWICBitmapScaler,__uuidof(IWICBitmapScaler));
55 template <class COM_SMART_PTR > inline void safe_release(COM_SMART_PTR& ptr)
67 ID2D1BitmapPtr load_bitmap_from_file(
68 ID2D1HwndRenderTargetPtr render_target,
69 IWICImagingFactoryPtr wic_factory,
71 boost::uint32_t destination_width = 0,
72 boost::uint32_t destination_height = 0
75 /** WNDCLASSEXラッパクラス */
76 struct window_class_ex
79 const wchar_t* menu_name ,
80 const std::wstring& class_name ,
81 HINSTANCE hInstance = NULL,
82 WNDPROC lpfnWndProc = ::DefWindowProcW,
83 boost::uint32_t style = CS_HREDRAW | CS_VREDRAW,
84 boost::int32_t cbClsExtra = 0,
85 HICON hIcon = ::LoadIcon(NULL,IDI_APPLICATION),
86 HCURSOR hCursor = ::LoadCursor(NULL, IDC_ARROW),
87 HBRUSH hbrBackground = NULL,
89 ) : is_register_(false)
92 if(::GetClassInfoExW(hInstance,class_name.c_str(),&wndclass_) == 0)
94 if(::GetLastError() == ERROR_CLASS_DOES_NOT_EXIST)
96 ::ZeroMemory(&wndclass_,sizeof(wndclass_));
97 wndclass_.lpszMenuName = (LPCWSTR)menu_name;
98 wndclass_.lpszClassName = class_name.c_str();
99 wndclass_.cbSize = sizeof(::WNDCLASSEXW);
100 wndclass_.cbWndExtra = sizeof(LONG_PTR);
101 wndclass_.hInstance = hInstance;
102 wndclass_.lpfnWndProc = lpfnWndProc;
103 wndclass_.style = style;
104 wndclass_.cbClsExtra = cbClsExtra;
105 wndclass_.hIcon = hIcon;
106 wndclass_.hCursor = hCursor;
107 wndclass_.hbrBackground = hbrBackground;
108 wndclass_.hIconSm = hIconSm;
109 atom_ = ::RegisterClassExW(&wndclass_) ;
110 BOOST_ASSERT(atom_ != 0);
113 throw win32_error_exception();
116 is_register_ = false;
123 ::UnregisterClassW(wndclass_.lpszClassName,wndclass_.hInstance);
130 ::WNDCLASSEXW wndclass_;
136 get_dc(HWND hwnd) : hwnd_(hwnd),hdc_(GetDC(hwnd)) {}
137 HDC get(){return hdc_;}
138 ~get_dc(){::ReleaseDC(hwnd_,hdc_);}
144 struct compatible_dc {
145 compatible_dc(HDC hdc) : hdc_(::CreateCompatibleDC(hdc)){};
146 ~compatible_dc(){::DeleteDC(hdc_);};
147 HDC get() { return hdc_;};
153 ref_dc(HDC& hdc) : hdc_(hdc) {};
155 HDC get() { return hdc_;};
161 d2_dc(ID2D1GdiInteropRenderTargetPtr& ptr,D2D1_DC_INITIALIZE_MODE mode) :hdc_(0),ptr_(ptr)
163 hr_ = ptr->GetDC(mode,&hdc_);
165 ~d2_dc(){ptr_->ReleaseDC(NULL);};
166 HDC get() { return hdc_;};
170 ID2D1GdiInteropRenderTargetPtr& ptr_;
173 template <typename Holder>
174 struct device_context
176 explicit device_context(Holder* holder) : holder_(holder){};
178 operator HDC(){return holder_->get();}
180 boost::scoped_ptr<Holder> holder_;
183 typedef device_context<d2_dc> d2_dc_type;
187 paint_struct(HWND hwnd) : hwnd_(hwnd)
189 ::BeginPaint(hwnd,&paintstruct_);
191 ~paint_struct() {::EndPaint(hwnd_,&paintstruct_);}
192 PAINTSTRUCT* operator->(){return &paintstruct_;}
195 PAINTSTRUCT paintstruct_;
198 template <typename T>
201 begin_draw(T& render_target) : render_target_(render_target) ,is_end_(false) {render_target->BeginDraw();}
206 hr = render_target_->EndDraw();
212 ~begin_draw(){ if(!is_end_) { render_target_->EndDraw();}}
220 mouse() : x_(0.0f),y_(0.0f),left_button_(false),middle_button_(false),right_button_(false){}
223 bool left_button_,middle_button_,right_button_;
229 typedef boost::signals2::signal<LRESULT (HWND,boost::uint32_t,WPARAM, LPARAM) > on_message_type;
230 on_message_type on_message;
231 typedef boost::signals2::signal<void ()> on_render_type;
232 on_render_type on_render;
236 const std::wstring& title,
237 const std::wstring& name,bool fit_to_display,
238 float width,float height);
240 void register_class (
242 boost::uint32_t style,
243 boost::int32_t cbClsExtra = 0,
244 HICON hIcon = ::LoadIcon(NULL,IDI_APPLICATION),
245 HCURSOR hCursor = ::LoadCursor(NULL, IDC_ARROW),
246 HBRUSH hbrBackground = NULL,
251 void register_class();
252 void create_window();
254 void show(boost::uint32_t show_flag);
255 virtual void discard_device();
256 virtual void create_device();
257 virtual void create_device_independent_resources();
258 //ID2D1FactoryPtr factory();
259 //ID2D1HwndRenderTargetPtr render_target();
260 //IDWriteFactoryPtr write_factory();
262 virtual LRESULT window_proc(HWND hwnd,boost::uint32_t message, WPARAM wParam, LPARAM lParam);
264 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
266 base_window* ptr = reinterpret_cast<base_window*>(hwnd);
268 return ptr->window_proc(hwnd,message,wParam,lParam);
271 struct hwnd_this_thunk : public Xbyak::CodeGenerator {
272 hwnd_this_thunk(base_window* impl,WNDPROC proc)
275 mov(eax,ptr[esp + 8]);
276 mov(ptr[&(impl->hwnd_)],eax);
277 mov(eax,(DWORD)impl);
278 mov(ptr[esp + 8],eax);
284 //ID3D11DevicePtr d3d_device_;
285 //D3D_DRIVER_TYPE driver_type_;
286 //D3D_FEATURE_LEVEL feature_level_;
287 //IDXGISwapChainPtr swap_chain_;
288 //ID3D11DeviceContextPtr device_context_;
289 //ID3D11RenderTargetViewPtr render_target_view_;
290 //ID3DX11EffectPtr effect_;
291 //ID3DX11EffectTechnique* technique_;
292 //ID3D11InputLayoutPtr vertex_layout_;
293 //ID3D11BufferPtr vertex_buffer_;
294 //ID3D11BufferPtr index_buffer_;
295 //ID3D11ShaderResourceViewPtr texture_rv_;
296 //ID3D11ShaderResourceViewPtr sprite_tex_;
297 //ID3DX11EffectMatrixVariable* world_variable_;
298 //ID3DX11EffectMatrixVariable* view_variable_;
299 //ID3DX11EffectMatrixVariable* projection_variable_;
300 //ID3DX11EffectVectorVariable* mesh_color_variable_;
301 //ID3DX11EffectShaderResourceVariable* diffuse_variable_;
304 //D3DXMATRIX projection_;
305 //D3DXVECTOR4 mesh_color_;
306 //ID3DX10SpritePtr sprite_;
307 //D3DX10_SPRITE sprite_buffer_[1];
308 //ID3D11BlendStatePtr sprite_blend_state_;
311 ID2D1FactoryPtr factory_;
312 // ID2D1DCRenderTargetPtr render_target_;
313 ID2D1HwndRenderTargetPtr render_target_;
314 IDWriteFactoryPtr write_factory_;
315 IWICImagingFactoryPtr wic_imaging_factory_;
316 //ID2D1BitmapPtr bitmap_;
317 hwnd_this_thunk thunk_;
320 float width_,height_;
321 bool fit_to_display_;
322 boost::shared_ptr<sf::window_class_ex> wnd_class_;
324 // boost::shared_ptr<input> input_;
327 //struct toplevel_window;
328 //typedef boost::shared_ptr<toplevel_window> toplevel_window_ptr;
330 ///** toplevel_window を生成する関数 */
331 //toplevel_window_ptr create_toplevel_window (
332 // const std::wstring& menu_name,
333 // const std::wstring& name,
334 // const boost::uint32_t show_flag = SW_SHOWNORMAL,
335 // bool fit_to_display = false,
336 // float width = 640,
337 // float height = 480
342 ///** toplevel ウィンドウクラス */
343 ///* このクラスは、create_toplevel_window 関数からのみ生成可能 */
344 //struct toplevel_window : public base_window
346 // friend toplevel_window_ptr create_toplevel_window
348 // const std::wstring& menu_name,
349 // const std::wstring& name,
350 // const boost::uint32_t show_flag,
351 // bool fit_to_display ,
358 // toplevel_window(const std::wstring& menu_name,const std::wstring& name,bool fit_to_display,float width = 640,float height = 480) : base_window(menu_name,name,fit_to_display,width,height)
360 // on_render.connect(boost::bind(&toplevel_window::render,this));
362 // LRESULT toplevel_window::window_proc(HWND hwnd,boost::uint32_t message, WPARAM wParam, LPARAM lParam);
365 struct av_mm_thread_characteristics
367 av_mm_thread_characteristics(std::wstring& str) : task_name_(str)
369 handle_ = ::AvSetMmThreadCharacteristicsW(str.c_str(),(LPDWORD)&task_index_);
372 bool set_priority(AVRT_PRIORITY p){return (::AvSetMmThreadPriority(handle_,p) == TRUE);}
374 ~av_mm_thread_characteristics()
376 ::AvRevertMmThreadCharacteristics(handle_);
380 std::wstring task_name_;
381 boost::uint32_t task_index_;
391 typedef sf::begin_draw<ID2D1BitmapRenderTargetPtr> begin_draw_bitmap;
392 typedef sf::begin_draw<ID2D1HwndRenderTargetPtr> begin_draw_hwnd;