OSDN Git Service

Direct3DでUIパーツを描き始めることにする。 master
authorSFPGMR <sfpg@git.sourceforge.jp>
Sun, 26 Feb 2012 10:02:21 +0000 (19:02 +0900)
committerSFPGMR <sfpg@git.sourceforge.jp>
Sun, 26 Feb 2012 10:02:21 +0000 (19:02 +0900)
winwrapper/application.h
winwrapper/sf_windows.cpp
winwrapper/sf_windows.h
winwrapper/test_window.cpp
winwrapper/test_window.h

index 1682c37..fa69770 100644 (file)
@@ -71,6 +71,7 @@ private:
 \r
   std::wstring base_directory_;\r
   std::unique_ptr<test_window> window_;\r
+\r
 //--------------------------------\r
 // シーケンサ\r
 //--------------------------------\r
index 3df3b5f..245f127 100644 (file)
@@ -22,6 +22,7 @@
 #pragma comment( lib, "d3dx9.lib" )   \r
 #pragma comment( lib, "Shlwapi.lib" ) \r
 #pragma comment( lib, "DWMApi.lib" )\r
+#pragma comment( lib,"msimg32.lib")\r
 \r
 #define THROW_IFERR(hres) \\r
   if (FAILED(hres)) { throw sf::win32_error_exception(hres); }\r
@@ -38,20 +39,26 @@ namespace sf
   {\r
     HRESULT hr = S_OK;\r
 \r
-    ////Create and populate the BlurBehind structre\r
-    //DWM_BLURBEHIND bb = {0};\r
-    ////Enable Blur Behind and Blur Region;\r
-    //bb.dwFlags = DWM_BB_ENABLE;\r
-    //bb.fEnable = true;\r
-    //bb.hRgnBlur = NULL;\r
+    //Create and populate the BlurBehind structre\r
+    DWM_BLURBEHIND bb = {0};\r
+    //Enable Blur Behind and Blur Region;\r
+    bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;\r
+    bb.fEnable = true;\r
+    bb.hRgnBlur = ::CreateRectRgn(-1,-1,0,0);\r
 \r
-    ////Enable Blur Behind\r
-    //hr = DwmEnableBlurBehindWindow(hwnd, &bb);\r
+    //Enable Blur Behind\r
+    hr = DwmEnableBlurBehindWindow(hwnd, &bb);\r
+    //BOOL allow_ncpaint = TRUE;\r
+    //DwmSetWindowAttribute(hwnd,DWMWA_ALLOW_NCPAINT,&allow_ncpaint,sizeof(BOOL));\r
+    //DWMNCRENDERINGPOLICY policy = DWMNCRP_ENABLED;\r
+    //DwmSetWindowAttribute(hwnd,DWMWA_NCRENDERING_POLICY,&policy,sizeof(policy));\r
+\r
+    \r
     //if (SUCCEEDED(hr))\r
     //{\r
     //  //do more things\r
-    MARGINS mgn = {-1};\r
-    hr = DwmExtendFrameIntoClientArea( hwnd, &mgn );\r
+    //MARGINS mgn = {-1};\r
+    //hr = DwmExtendFrameIntoClientArea( hwnd, &mgn );\r
     /*   }*/\r
     return hr;\r
   }\r
@@ -276,6 +283,10 @@ namespace sf
       return on_timer(wParam);\r
     case WM_NOTIFY:\r
       return on_notify(reinterpret_cast<NMHDR*>(lParam));\r
+    case WM_DWMCOMPOSITIONCHANGED:\r
+      return on_dwm_composition_changed();\r
+    //case WM_DWMCOLORIZATIONCOLORCHANGED:\r
+    //  return on_dwm_colorlizationcolor_changed\r
     }\r
 \r
     // 他のWindowメッセージを派生クラス側でフックできるようにする\r
@@ -307,7 +318,7 @@ namespace sf
   }\r
 \r
   template <typename ProcType> \r
-  void base_win32_window<ProcType>::create_window()\r
+  void base_win32_window<ProcType>::create_window(HWND parent)\r
   {\r
     // Create the application window.\r
     //\r
@@ -320,15 +331,15 @@ namespace sf
     // Windowを作成する\r
     // Windowを作成する\r
     CreateWindowEx(\r
-       WS_EX_APPWINDOW | WS_EX_LAYERED ,\r
+       WS_EX_APPWINDOW/* | WS_EX_LAYERED */,\r
       name_.c_str(),\r
       title_.c_str(),\r
-      WS_OVERLAPPEDWINDOW,\r
+      /*WS_OVERLAPPEDWINDOW*/ WS_POPUP,\r
       CW_USEDEFAULT,\r
       CW_USEDEFAULT,\r
       static_cast<uint32_t>(width_),\r
       static_cast<uint32_t>(height_),\r
-      NULL,\r
+      parent,\r
       NULL,\r
       HINST_THISCOMPONENT,\r
       this\r
@@ -711,8 +722,23 @@ namespace sf
 \r
 \r
     // ビュー行列のセットアップ\r
-    XMVECTOR eye = XMVectorSet( 0.0f, 3.0f, -6.0f, 0.0f );\r
-    XMVECTOR at = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );\r
+       //基本値設定\r
+       float aspect = (float) width_ / height_;            //アスペクト比(高さを1としたときの幅)\r
+       float depth = 1.5f;                                                                             //奥行きZ\r
+       float fovy  = (float)atan(1.0f / depth) * 2.0f;                                 //視野をZ=0でデバイスの幅と高さに合わせる\r
+       \r
+       //座標を揃える\r
+       //D3DXMATRIX mat_view;\r
+       //D3DXMatrixLookAtLH( &mat_view, \r
+       //      &D3DXVECTOR3( aspect, -1.0f,-depth),\r
+       //      &D3DXVECTOR3( aspect, -1.0f, 0.0f),\r
+       //      &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );\r
+\r
+    //XMVECTOR eye = XMVectorSet( 0.0f, 3.0f, -6.0f, 0.0f );\r
+    //XMVECTOR at = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );\r
+    //XMVECTOR up = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );\r
+    XMVECTOR eye = XMVectorSet( aspect, -1.0f, -depth, 0.0f );\r
+    XMVECTOR at = XMVectorSet( aspect, -1.0f, 0.0f, 0.0f );\r
     XMVECTOR up = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );\r
     mat_view_ = XMMatrixLookAtLH( eye, at, up );\r
     cb_never_changes cnc;\r
@@ -724,7 +750,8 @@ namespace sf
 \r
     // 投影行列のセットアップ\r
 \r
-    mat_projection_ = XMMatrixPerspectiveFovLH( XM_PIDIV4, /*(rc.right - rc.left)/(rc.bottom - rc.top)*/width_ / height_, 0.01f, 100.0f );\r
+    //mat_projection_ = XMMatrixPerspectiveFovLH( XM_PIDIV4, /*(rc.right - rc.left)/(rc.bottom - rc.top)*/width_ / height_, 0.01f, 100.0f );\r
+    mat_projection_ = XMMatrixPerspectiveFovLH( fovy, aspect, 1.0f, 100.0f );\r
     cb_change_on_resize ccor;\r
     ccor.mProjection = XMMatrixTranspose( mat_projection_ );\r
     // 定数バッファに格納\r
@@ -939,6 +966,7 @@ namespace sf
       ++i;\r
     }\r
   }\r
+\r
   template <typename ProcType>\r
   typename base_win32_window<ProcType>::result_t  base_win32_window<ProcType>::on_size(uint32_t flag,uint32_t width,uint32_t height)\r
   {\r
@@ -970,8 +998,8 @@ namespace sf
       d3d_context_->UpdateSubresource( cb_change_on_resize_, 0, NULL, &ccor, 0, 0 );\r
 \r
       // リージョンの設定\r
-                 HRGN rgn = CreateRectRgn(0, 0, width_, height_);\r
-                 SetWindowRgn(hwnd_, rgn, FALSE);\r
+                 //HRGN rgn = CreateRectRgn(0, 0, width_, height_);\r
+                 //SetWindowRgn(hwnd_, rgn, FALSE);\r
 \r
     }\r
     return std::is_same<proc_t,wndproc>::value?0:FALSE;  \r
@@ -981,9 +1009,9 @@ namespace sf
   typename base_win32_window<ProcType>::result_t base_win32_window<ProcType>::on_create(CREATESTRUCT *p)\r
   {\r
     // DWM API\r
-    //BOOL dwmEnable;\r
-    //DwmIsCompositionEnabled (&dwmEnable); \r
-    //if (dwmEnable) EnableBlurBehind(hwnd_);\r
+    BOOL dwmEnable;\r
+    DwmIsCompositionEnabled (&dwmEnable); \r
+    if (dwmEnable) EnableBlurBehind(hwnd_);\r
     //DwmEnableComposition(DWM_EC_DISABLECOMPOSITION );\r
 \r
     // ウィンドウ全体を半透明にする\r
@@ -995,8 +1023,8 @@ namespace sf
 \r
     // ウィンドウの指定領域を半透明にする\r
     // リージョンの設定\r
-               HRGN rgn = CreateRectRgn(0, 0, width_, height_);\r
-               SetWindowRgn(hwnd_, rgn, FALSE);\r
+               //HRGN rgn = CreateRectRgn(0, 0, width_, height_);\r
+               //SetWindowRgn(hwnd_, rgn, FALSE);\r
 \r
     create_device();\r
 \r
@@ -1018,7 +1046,7 @@ namespace sf
     //  }\r
     //}\r
     render();\r
-    return std::is_same<proc_t,wndproc>::value?0:FALSE;\r
+    return  std::is_same<proc_t,wndproc>::value?0:FALSE;\r
   }\r
 \r
   template <typename ProcType>\r
@@ -1090,7 +1118,7 @@ namespace sf
 \r
       d3d_context_->OMSetRenderTargets( 1, &view_.GetInterfacePtr(), depth_view_ );\r
 \r
-      float color[4] = { 1.0f, 0.0f, 0.0f, 0.0f };    \r
+      float color[4] = { 0.0f, 1.0f, 0.0f, 0.25f };    \r
       // 描画ターゲットのクリア\r
       d3d_context_->ClearRenderTargetView(view_,color);\r
       // 深度バッファのクリア\r
@@ -1131,7 +1159,8 @@ namespace sf
       HDC sdc;\r
       THROW_IF_ERR(surface->GetDC( FALSE, &sdc ));\r
 \r
-      get_dc ddc(NULL);\r
+      //get_dc ddc(hwnd_);\r
+      get_window_dc ddc(hwnd_);\r
       RECT rc;\r
       GetWindowRect(hwnd_,&rc);\r
       POINT wnd_pos = {rc.left,rc.top};\r
@@ -1140,12 +1169,14 @@ namespace sf
       blend.BlendOp = AC_SRC_OVER;\r
       blend.BlendFlags = 0;\r
       blend.SourceConstantAlpha = 128; // 不透明度(レイヤードウィンドウ全体のアルファ値)\r
-      blend.AlphaFormat = 0;//AC_SRC_ALPHA;\r
+      blend.AlphaFormat = AC_SRC_ALPHA;\r
       // デバイスコンテキストにおけるレイヤの位置\r
       POINT po;\r
       po.x = po.y = 0;\r
       BOOL err;\r
-      err = UpdateLayeredWindow(hwnd_, ddc.get(), &wnd_pos, &wnd_size, sdc, &po, RGB(255,0,0), &blend, ULW_ALPHA | ULW_COLORKEY );\r
+      err = BitBlt(ddc.get(),0,0,width_,height_,sdc,0,0,SRCCOPY);\r
+      //err = AlphaBlend(ddc.get(),0,0,width_,height_,sdc,0,0,width_,height_,blend);\r
+      //err = UpdateLayeredWindow(hwnd_, ddc.get(), &wnd_pos, &wnd_size, sdc, &po, RGB(255,0,0), &blend, ULW_ALPHA | ULW_COLORKEY );\r
       BOOST_ASSERT(err == TRUE);\r
       surface->ReleaseDC( NULL);\r
       surface.Release();\r
@@ -1155,6 +1186,17 @@ namespace sf
 \r
   }\r
 \r
+ template <typename ProcType>\r
+  typename base_win32_window<ProcType>::result_t base_win32_window<ProcType>::on_dwm_composition_changed()\r
+  {\r
+    BOOL enabled;\r
+    DwmIsCompositionEnabled(&enabled);\r
+    if(enabled)\r
+    {\r
+      EnableBlurBehind(hwnd_);\r
+    }\r
+    return  std::is_same<proc_t,wndproc>::value?0:FALSE;\r
+  }\r
   template struct base_win32_window<wndproc>;\r
   template struct base_win32_window<dlgproc>;\r
 \r
index e5075e5..c8b9995 100644 (file)
@@ -174,6 +174,15 @@ namespace sf{
     HWND hwnd_;\r
   };\r
 \r
+  struct get_window_dc {\r
+    get_window_dc(HWND hwnd) : hwnd_(hwnd),hdc_(GetWindowDC(hwnd)) {}\r
+    HDC get(){return hdc_;}\r
+    ~get_window_dc(){::ReleaseDC(hwnd_,hdc_);}\r
+  private:\r
+    HDC hdc_;\r
+    HWND hwnd_;\r
+  };\r
+\r
   struct compatible_dc {\r
     compatible_dc(HDC hdc) : hdc_(::CreateCompatibleDC(hdc)){}; \r
     ~compatible_dc(){::DeleteDC(hdc_);};\r
@@ -418,7 +427,7 @@ namespace sf{
 \r
     /** デフォルト設定 */\r
     void register_class();\r
-    void create_window();\r
+    void create_window(HWND parent = NULL);\r
 \r
     void calc_client_size()\r
     {\r
@@ -499,7 +508,7 @@ namespace sf{
     //virtual bool on_mouse_enter(uint32_t mouse_key,int x,int y) {  return false; }\r
     virtual result_t on_mouse_leave() {  return std::is_same<proc_t,wndproc>::value?0:FALSE; }\r
     virtual result_t on_destroy(){   \r
-      ::PostQuitMessage(0);\r
+      //::PostQuitMessage(0);\r
       return std::is_same<proc_t,wndproc>::value?0:FALSE;\r
     }\r
 \r
@@ -523,7 +532,8 @@ namespace sf{
       render();\r
       return std::is_same<proc_t,wndproc>::value?0:FALSE; \r
     } \r
-    virtual result_t on_notify(NMHDR* nmhdr)  { return std::is_same<proc_t,wndproc>::value?0:FALSE; } \r
+    virtual result_t on_notify(NMHDR* nmhdr)  { return std::is_same<proc_t,wndproc>::value?0:FALSE; }\r
+    virtual result_t on_dwm_composition_changed();\r
     virtual void render();\r
   protected:\r
     void get_dxgi_information();\r
index 7abf07f..14551d8 100644 (file)
@@ -33,11 +33,6 @@ EXTERN_C IMAGE_DOS_HEADER __ImageBase;
 \r
 namespace sf \r
 {\r
-\r
-\r
-\r
-\r
-\r
   test_window::test_window(\r
     const std::wstring& title,\r
     const std::wstring& name,bool fit_to_display,\r
@@ -88,6 +83,28 @@ namespace sf
     }\r
     return 0;\r
   }\r
+\r
+ test_child_window::test_child_window(\r
+    base_window& window,\r
+    const std::wstring& title,\r
+    const std::wstring& name,bool fit_to_display,\r
+    float width,float height) : base_win32_window_t(title,name,fit_to_display,width,height),parent_window_(window)\r
+ {\r
+    register_class();\r
+    create();\r
+    ::MoveWindow(hwnd_,500,300,320,240,FALSE);\r
+//    update();\r
+  }\r
+\r
+\r
+  test_child_window::~test_child_window()\r
+  {\r
+  }\r
+\r
+  void test_child_window::create()\r
+  {\r
+    create_window(reinterpret_cast<HWND>(parent_window_.raw_handle()));\r
+  }\r
   //base_win32_window_t::result_t test_window::on_create(CREATESTRUCT *p)\r
   //{\r
   //  return 0;\r
index f14d822..8ba1ae1 100644 (file)
@@ -3,6 +3,19 @@
 #include "sf_windows.h"\r
 \r
 namespace sf {\r
+  struct test_child_window : public base_win32_window_t\r
+  {\r
+    test_child_window(\r
+      base_window& parent,\r
+      const std::wstring& title,\r
+      const std::wstring& name,bool fit_to_display,\r
+      float width,float height);\r
+    virtual ~test_child_window();\r
+    virtual void create();\r
+  private:\r
+    base_window& parent_window_;\r
+  };\r
+\r
   struct test_window : public base_win32_window_t\r
   {\r
     test_window(\r
@@ -11,17 +24,31 @@ namespace sf {
       float width,float height);\r
     virtual ~test_window();\r
     virtual void create();\r
+    virtual result_t on_create(CREATESTRUCT *p)\r
+    {\r
+      base_win32_window_t::on_create(p);\r
+      // 子ウィンドウを生成する\r
+      window_.reset(new test_child_window(*this,L"child",L"child",false,640.0f,480.0f));\r
+      return 0;\r
+    }\r
     virtual result_t on_left_mouse_button_down(uint32_t mouse_key,int x,int y ) ;//{ return std::is_same<proc_t,wndproc>::value?0:FALSE; }\r
     virtual result_t on_left_mouse_button_up(uint32_t mouse_key,int x,int y) ;//{ return std::is_same<proc_t,wndproc>::value?0:FALSE; }\r
     virtual result_t on_mouse_move(uint32_t mouse_key,int x,int y);// {return std::is_same<proc_t,wndproc>::value?0:FALSE; }\r
+    virtual result_t on_destroy(){   \r
+      ::PostQuitMessage(0);\r
+      return 0;\r
+    }\r
 \r
     //virtual result_t on_erase_backgroud(HDC dc) {return ::DefWindowProcW(hwnd_,WM_ERASEBKGND,(WPARAM)dc,0);}\r
 \r
-//   virtual result_t on_create(CREATESTRUCT *p);\r
+    //   virtual result_t on_create(CREATESTRUCT *p);\r
   private:\r
+    std::unique_ptr<test_child_window> window_;\r
     test_window();\r
     int x_,y_;\r
     bool click_;\r
   };\r
+\r
+\r
 }\r
 \r