OSDN Git Service

#xxxxx DTXViewerのプロジェクトを追加。
[dtxmania/dtxmania.git] / DTXViewerプロジェクト / @FDK10プロジェクト / CD3DApplication.cpp
diff --git a/DTXViewerプロジェクト/@FDK10プロジェクト/CD3DApplication.cpp b/DTXViewerプロジェクト/@FDK10プロジェクト/CD3DApplication.cpp
new file mode 100644 (file)
index 0000000..c92684b
--- /dev/null
@@ -0,0 +1,777 @@
+#include "stdafx.h"
+#include "Debug.h"
+#include "CD3DDisplay.h"
+#include "CTimer.h"
+#include "WindowPositions.h"
+#include "FDKError.h"
+#include "CTexture.h"
+#include "CD3DApplication.h"
+
+namespace FDK {
+       namespace AppBase {
+
+// static \82È\82â\82Â\82ç
+LPDIRECTDRAW7                  CD3DApplication::pDD = NULL;
+LPDIRECTDRAWSURFACE7   CD3DApplication::pddsFrontBuffer = NULL;
+LPDIRECTDRAWSURFACE7   CD3DApplication::pddsBackBuffer = NULL;
+LPDIRECT3D7                            CD3DApplication::pD3D = NULL;
+LPDIRECT3DDEVICE7              CD3DApplication::pD3DDevice = NULL;
+CD3DDisplay                            CD3DApplication::D3DDisplay;
+HWND                                   CD3DApplication::hWnd = NULL;
+int                                            CD3DApplication::nBpp = 16;
+
+// \8bÇ\8f\8a\95Ï\90\94
+static CD3DApplication* s_pD3DApp = NULL;              // WndProc\97p
+
+// static WndProc
+LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
+{
+       return ( s_pD3DApp ) ? s_pD3DApp->WndProc( hWnd, uMsg, wParam, lParam ) : DefWindowProc( hWnd, uMsg, wParam, lParam );
+}
+//
+CD3DApplication::CD3DApplication()
+{
+       // \8aO\95\94\95Ï\90\94\82Ì\8f\89\8aú\89»
+       s_pD3DApp                                       = this;
+
+       // \93à\95\94\95Ï\90\94\82Ì\8f\89\8aú\89»
+       this->nWidth                            = 640;
+       this->nHeight                           = 480;
+       this->nBpp                                      = 16;
+       this->b\91S\89æ\96Ê\83\82\81[\83h                     = false;
+       this->hWnd                                      = NULL;
+       this->b\90\82\92¼\8bA\90ü\93¯\8aú                     = true;
+       this->dw\95`\89æ\8aÔ\8au                        = 3;
+       this->bReady                            = false;
+       this->bActive                           = false;
+    this->strWindowTitle               = _T("FDK10 Application");
+    this->strWindowClass               = _T("FDK10 WindowClass");
+       this->dw\83E\83B\83\93\83h\83E\83X\83^\83C\83\8b      = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_VISIBLE;
+       this->dw\91S\89æ\96Ê\83X\83^\83C\83\8b          = WS_POPUP | WS_VISIBLE;
+       this->bScreenSaverEnable        = false;
+}
+HRESULT CD3DApplication::Create( HINSTANCE hInst, LPCTSTR strCmdLine )
+{
+       HRESULT hr;
+
+       this->hInstance   = hInst;
+
+       // IME \82ð\96³\8cø\89»
+       {
+               WINNLSEnableIME( NULL, FALSE );
+       }
+       //
+       // \83E\83B\83\93\83h\83E\8dì\90¬\82ª\82Ü\82¾\82È\82ç\82±\82±\82Å\8dì\90¬\82·\82é
+       {
+               if( this->hWnd == NULL )
+               {
+                       WNDCLASS wndClass = {
+                               0,                                                                              // \83N\83\89\83X\83X\83^\83C\83\8b
+                               FDK::AppBase::WndProc,                                  // \83\81\83b\83Z\81[\83W\83v\83\8d\83V\81[\83W\83\83
+                               0,                                                                              // ClsExtra
+                               0,                                                                              // WndExtra
+                               this->hInstance,                                                // \83C\83\93\83X\83^\83\93\83X
+                               NULL,                                                                   // \83E\83B\83\93\83h\83E\83A\83C\83R\83\93
+                               LoadCursor( NULL, IDC_ARROW ),                  // \83}\83E\83X\83A\83C\83R\83\93
+                               (HBRUSH)GetStockObject( WHITE_BRUSH ),  // \94w\8ci\83u\83\89\83V
+                               NULL,                                                                   // \83\81\83j\83\85\81[\96¼
+                               this->strWindowClass                                    // \83N\83\89\83X\96¼
+                       };
+                       ::RegisterClass( &wndClass );
+
+                       RECT rc = { 0, 0, this->nWidth, this->nHeight };
+                       ::AdjustWindowRect( &rc, this->dw\83E\83B\83\93\83h\83E\83X\83^\83C\83\8b, FALSE );
+                       this->hWnd = ::CreateWindow(
+                               this->strWindowClass,
+                               this->strWindowTitle,
+                               (this->b\91S\89æ\96Ê\83\82\81[\83h) ? this->dw\91S\89æ\96Ê\83X\83^\83C\83\8b : this->dw\83E\83B\83\93\83h\83E\83X\83^\83C\83\8b,
+                               CW_USEDEFAULT, CW_USEDEFAULT,
+                               (rc.right-rc.left), (rc.bottom-rc.top), 0L,
+                               NULL, this->hInstance, 0L );
+               }
+       }
+       //
+       // \83E\83B\83\93\83h\83E\97Ì\88æ\81i\83X\83N\83\8a\81[\83\93\8dÀ\95W\81j\82Ì\8eæ\93¾\81E\95Û\91
+       {
+               this->t\8c»\8dÝ\82Ì\83E\83B\83\93\83h\83E\82Ì\83E\83B\83\93\83h\83E\97Ì\88æ\82Æ\83N\83\89\83C\83A\83\93\83g\97Ì\88æ\82ð\8eæ\93¾\82·\82é();
+       }
+       //
+       // \83A\83_\83v\83^\81^\83f\83o\83C\83X\81^\83\82\81[\83h\82Ì\97ñ\8b\93
+       {
+               if( FAILED( hr = this->D3DDisplay.EnumerateDevices() ) )
+                       return hr;      // \8e¸\94s
+       }
+       //
+       // \82R\82c\8aÂ\8b«\82Ì\8d\\92z
+       {
+               if( FAILED( hr = t3D\8aÂ\8b«\82Ì\8d\\92z() ) )
+                       return hr;
+       }
+       //
+       // \83A\83v\83\8a\82Ì\88ê\93x\82¾\82¯\82Ì\8f\89\8aú\89»
+       {
+               if( FAILED( hr = OneTimeSceneInit() ) ) {
+                       t3D\8aÂ\8b«\82Ì\94j\8aü();
+                       return hr;
+               }
+       }
+       //
+       // \83f\83o\83C\83X\88Ë\91\83I\83u\83W\83F\83N\83g\82Ì\8f\89\8aú\89»
+       {
+               if( FAILED( hr = InitDeviceObjects() ) ) {
+                       t3D\8aÂ\8b«\82Ì\94j\8aü();
+                       return hr;
+               }
+       }
+       //
+       // \83f\83o\83C\83X\88Ë\91\83I\83u\83W\83F\83N\83g\82Ì\8d\\92z
+       {
+               if( FAILED( hr = RestoreDeviceObjects() ) ) {
+                       t3D\8aÂ\8b«\82Ì\94j\8aü();
+                       return hr;
+               }
+       }       
+       //
+       // \8f\80\94õ\8a®\97¹
+       this->bReady = true;
+
+       return S_OK;
+}
+HRESULT CD3DApplication::t3D\8aÂ\8b«\82Ì\8d\\92z()
+{
+       HRESULT hr;
+       
+       D3DAdapterInfo* pAdapter = this->D3DDisplay.pAdapter;
+       D3DDeviceInfo*  pDevice  = pAdapter->pDevice;
+
+       // \83\82\83j\83^\82Ì\93d\8c¹\90Ý\92è\82ð\88ê\8e\9e\93I\82É\96³\8cø\82É\82·\82é
+       {
+               this->exeState = ::SetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
+       }
+       //
+       // \83X\83N\83\8a\81[\83\93\83Z\81[\83o\82Ì\97L\8cø\81E\96³\8cø\82ð\8eæ\93¾\82µ\81A\82»\82Ì\8cã\88ê\8e\9e\93I\82É\96³\8cø\82É\82·\82é
+       {
+               HKEY    hKeyScreenSaver = NULL;
+               long    lReturn = 0;
+               long    lScreenSaver = 0;
+               DWORD   dwData = 0;
+
+               if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop"), 0, KEY_QUERY_VALUE, &hKeyScreenSaver ) == ERROR_SUCCESS )
+               {
+                       this->bScreenSaverEnable = 
+                               ( RegQueryValueEx( hKeyScreenSaver, TEXT("SCRNSAVE.EXE"), NULL, NULL, NULL, &dwData ) == ERROR_SUCCESS ) ? true : false;
+               }
+               RegCloseKey( hKeyScreenSaver );
+               hKeyScreenSaver = NULL;
+
+               // \96³\8cø\82É\82·\82é
+               SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, FALSE, 0, SPIF_SENDWININICHANGE );
+       }
+       //
+       // \91S\83E\83B\83\93\83h\83E\88Ê\92u\82Ì\95Û\91
+       {
+               WindowPositions::Save();
+       }
+       //
+       // \83^\83C\83}\82Ì\8f\89\8aú\89»; \83^\83C\83}\8eí\95Ê\82ð\95Ï\8dX\82·\82é\8fê\8d\87\82Í\81A\88ê\93x Term() \82µ\82Ä Init() \82µ\82È\82¨\82·\82±\82Æ\81B
+       {
+               this->timer.Init( TIMERTYPE_TIMEGTTIME );
+       }
+       //
+       // D3DX \82Ì\8f\89\8aú\89»
+       {
+               if( FAILED( hr = D3DXInitialize() ) )
+                       return hr;
+       }
+       //
+       // DirectDraw7 \82Ì\8dì\90¬
+       {
+               if( FAILED( hr = DirectDrawCreateEx( NULL, (VOID**)&this->pDD, IID_IDirectDraw7, NULL ) ) ) {
+                       D3DXUninitialize();
+                       return hr;
+               }
+       }
+       //
+       // \8b¦\92²\83\82\81[\83h\82Ì\90Ý\92è
+       {
+               if( FAILED( hr = this->pDD->SetCooperativeLevel( this->hWnd, this->b\91S\89æ\96Ê\83\82\81[\83h ? (DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN) : (DDSCL_NORMAL|DDSCL_NOWINDOWCHANGES) ) ) ) 
+               {
+                       SAFE_RELEASE( this->pDD );
+                       D3DXUninitialize();
+                       return hr;
+               }
+       }
+       //
+       // \83f\83B\83X\83v\83\8c\83C\83\82\81[\83h\82Ì\90Ý\92è\81i\91S\89æ\96Ê\83\82\81[\83h\8e\9e\82Ì\82Ý\81j
+       {
+               if( this->b\91S\89æ\96Ê\83\82\81[\83h )
+               {
+                       if( FAILED( hr = this->pDD->SetDisplayMode( this->nWidth, this->nHeight, this->nBpp, 0, 0 ) ) )
+                       {
+                               SAFE_RELEASE( this->pDD );
+                               D3DXUninitialize();
+                               return hr;
+                       }
+               }
+       }
+       //
+       // \83v\83\89\83C\83}\83\8a\81\95\83o\83b\83N\83T\81[\83t\83F\83C\83X\82Ì\8dì\90¬
+       {
+               // a. \91S\89æ\96Ê\83\82\81[\83h\82Ì\8fê\8d\87
+               if( this->b\91S\89æ\96Ê\83\82\81[\83h )
+               {
+                       DDSURFACEDESC2 ddsd;
+                       ZeroMemory( &ddsd, sizeof( ddsd ) );
+                       ddsd.dwSize     = sizeof( ddsd );
+                       ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
+                       ddsd.ddsCaps.dwCaps     = DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
+                       ddsd.dwBackBufferCount = 1;
+                       if( FAILED( hr = this->pDD->CreateSurface( &ddsd, &this->pddsFrontBuffer, NULL ) ) ) {
+                               D3DXUninitialize();
+                               SAFE_RELEASE( this->pDD );
+                               return hr;      // \83v\83\89\83C\83}\83\8a\83T\81[\83t\83F\83C\83X\82Ì\8dì\90¬\82É\8e¸\94s
+                       }
+                       DDSCAPS2 ddscaps;
+                       ZeroMemory( &ddscaps, sizeof( ddscaps ) );
+                       ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
+                       if( FAILED( hr = this->pddsFrontBuffer->GetAttachedSurface( &ddscaps, &this->pddsBackBuffer ) ) )
+                       {
+                               D3DXUninitialize();
+                               SAFE_RELEASE( this->pddsFrontBuffer );
+                               SAFE_RELEASE( this->pDD );
+                               return hr;      // \83o\83b\83N\83T\81[\83t\83F\83C\83X\82Ì\8eæ\93¾\82É\8e¸\94s
+                       }
+               }
+               //
+               // b. \83E\83B\83\93\83h\83E\83\82\81[\83h\82Ì\8fê\8d\87
+               else
+               {
+                       DDSURFACEDESC2 ddsd;
+                       ZeroMemory( &ddsd, sizeof( ddsd ) );
+                       ddsd.dwSize = sizeof( ddsd );
+                       ddsd.dwFlags = DDSD_CAPS;
+                       ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE;
+                       if( FAILED( hr = this->pDD->CreateSurface( &ddsd, &this->pddsFrontBuffer, NULL ) ) ) {
+                               SAFE_RELEASE( this->pDD );
+                               D3DXUninitialize();
+                               return hr;      // \83v\83\89\83C\83}\83\8a\83T\81[\83t\83F\83C\83X\82Ì\8dì\90¬\82É\8e¸\94s
+                       }
+                       ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
+                       ddsd.ddsCaps.dwCaps = DDSCAPS_3DDEVICE | DDSCAPS_OFFSCREENPLAIN;
+                       ddsd.dwWidth = this->nWidth;
+                       ddsd.dwHeight = this->nHeight;
+                       if( FAILED( hr = this->pDD->CreateSurface( &ddsd, &this->pddsBackBuffer, NULL ) ) ) {
+                               SAFE_RELEASE( this->pddsFrontBuffer );
+                               SAFE_RELEASE( this->pDD );
+                               D3DXUninitialize();
+                               return hr;      // \83o\83b\83N\83T\81[\83t\83F\83C\83X\82Ì\8dì\90¬\82É\8e¸\94s
+                       }
+               }
+       }
+       //
+       // \83N\83\8a\83b\83p\81[\82Ì\8dì\90¬\81i\83E\83B\83\93\83h\83E\83\82\81[\83h\8e\9e\82Ì\82Ý\81j
+       {
+               if( ! this->b\91S\89æ\96Ê\83\82\81[\83h )
+               {
+                       LPDIRECTDRAWCLIPPER pClipper;
+                       if( FAILED( hr = this->pDD->CreateClipper( 0, &pClipper, NULL ) ) )
+                       {
+                               SAFE_RELEASE( this->pddsBackBuffer );
+                               SAFE_RELEASE( this->pddsFrontBuffer );
+                               SAFE_RELEASE( this->pDD );
+                               D3DXUninitialize();
+                               return hr;      // \83N\83\8a\83b\83p\81[\82Ì\8dì\90¬\82É\8e¸\94s
+                       }
+                       pClipper->SetHWnd( 0, this->hWnd );
+                       this->pddsFrontBuffer->SetClipper( pClipper );
+                       SAFE_RELEASE( pClipper );
+               }
+       }
+       //
+       // Direct3D \82Ì\8eæ\93¾
+       {
+               if( FAILED( hr = this->pDD->QueryInterface( IID_IDirect3D7, (LPVOID *) &this->pD3D ) ) )
+               {
+                       if( ! this->b\91S\89æ\96Ê\83\82\81[\83h ) SAFE_RELEASE( this->pddsBackBuffer );
+                       SAFE_RELEASE( this->pddsFrontBuffer );
+                       SAFE_RELEASE( this->pDD );
+                       D3DXUninitialize();
+                       return hr;      // Direct3D7 \82Ì\8eæ\93¾\82É\8e¸\94s
+               }
+       }
+       //
+       // Direct3D \83f\83o\83C\83X\82Ì\8dì\90¬
+       {
+               if( FAILED( hr = this->pD3D->CreateDevice( IID_IDirect3DTnLHalDevice, this->pddsBackBuffer, &this->pD3DDevice ) ) )                             // TnLHAL \82Å\83g\83\89\83C
+               {
+                       if( FAILED( hr = this->pD3D->CreateDevice( IID_IDirect3DHALDevice, this->pddsBackBuffer, &this->pD3DDevice ) ) )                        // HAL \82Å\83g\83\89\83C
+                       {
+                               if( FAILED( hr = this->pD3D->CreateDevice( IID_IDirect3DMMXDevice, this->pddsBackBuffer, &this->pD3DDevice ) ) )                // MMX \82Å\83g\83\89\83C
+                               {
+                                       if( FAILED( hr = this->pD3D->CreateDevice( IID_IDirect3DRGBDevice, this->pddsBackBuffer, &this->pD3DDevice ) ) )        // RGB \82Å\83g\83\89\83C
+                                       {
+                                               SAFE_RELEASE( this->pD3D );
+                                               if( ! this->b\91S\89æ\96Ê\83\82\81[\83h ) SAFE_RELEASE( this->pddsBackBuffer );
+                                               SAFE_RELEASE( this->pddsFrontBuffer );
+                                               SAFE_RELEASE( this->pDD );
+                                               D3DXUninitialize();
+                                               return hr;      // Direct3DDevice \82Ì\8dì\90¬\82É\8e¸\94s
+                                       } else
+                                               Debug::Msg( _T("Direct3D RGB \83f\83o\83C\83X\82ð\8dì\90¬\82µ\82Ü\82µ\82½\81B\n") );
+                               } else
+                                       Debug::Msg( _T("Direct3D MMX \83f\83o\83C\83X\82ð\8dì\90¬\82µ\82Ü\82µ\82½\81B\n") );
+                       } else
+                               Debug::Msg( _T("Direct3D HAL \83f\83o\83C\83X\82ð\8dì\90¬\82µ\82Ü\82µ\82½\81B\n") );
+               } else
+                       Debug::Msg( _T("Direct3D T&L HAL \83f\83o\83C\83X\82ð\8dì\90¬\82µ\82Ü\82µ\82½\81B\n") );
+       }
+       //
+       // \83f\83o\83C\83X\82É\83r\83\85\81[\83|\81[\83g\82ð\90Ý\92è
+       {
+               D3DVIEWPORT7 vp = { 0, 0, this->nWidth, this->nHeight, 0.0f, 1.0f };
+               if( FAILED( hr = this->pD3DDevice->SetViewport( &vp ) ) )
+               {
+                       SAFE_RELEASE( this->pD3DDevice );
+                       SAFE_RELEASE( this->pD3D );
+                       if( ! this->b\91S\89æ\96Ê\83\82\81[\83h ) SAFE_RELEASE( this->pddsBackBuffer );
+                       SAFE_RELEASE( this->pddsFrontBuffer );
+                       SAFE_RELEASE( this->pDD );
+                       D3DXUninitialize();
+                       return hr;      // \83r\83\85\81[\83|\81[\83g\82Ì\90Ý\92è\82É\8e¸\94s
+               }
+       }
+       //
+       // \95s\95Ï\82Ì\83\8c\83\93\83_\83\8a\83\93\83O\83X\83e\81[\83^\83X\82Ì\90Ý\92è
+       {
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_LIGHTING, FALSE );
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_ZENABLE, FALSE );
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_ANTIALIAS, FALSE );
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE );
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_ALPHATESTENABLE, TRUE );
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_ALPHAREF, 10 );
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATER );
+               this->pD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,              D3DTOP_SELECTARG1 );
+               this->pD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1,    D3DTA_TEXTURE );
+               this->pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,              D3DTOP_MODULATE );
+               this->pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1,    D3DTA_TEXTURE );
+               this->pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2,    D3DTA_DIFFUSE );
+               this->pD3DDevice->SetTextureStageState( 0, D3DTSS_MINFILTER,    D3DTFN_POINT );
+               this->pD3DDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER,    D3DTFN_POINT );
+       }
+       //
+       // \93K\8bX\95Ï\8dX\82³\82ê\82é\83\8c\83\93\83_\83\8a\83\93\83O\83X\83e\81[\83^\83X\82Ì\90Ý\92è
+       {
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_COLORKEYENABLE, FALSE );
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_ALPHABLENDENABLE, FALSE );
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA );
+               this->pD3DDevice->SetRenderState( D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA );
+       }
+       //
+       // \8eË\89e\8ds\97ñ\82ð\83f\83t\83H\83\8b\83g\82Ì\82à\82Ì\82©\82ç\95Ï\8dX
+       {
+               float d, t, a;
+               D3DXMATRIX proj;
+               CD3DApplication::GetProjectionParam( &d, &t, &a );
+               D3DXMatrixPerspectiveFovLH( &proj, t, 1.0f, 0.0000001f, 50.0f );
+               D3DMATRIX pm = proj;
+               this->pD3DDevice->SetTransform( D3DTRANSFORMSTATE_PROJECTION, &pm );
+       }
+       //
+       // \83}\83E\83X\83J\81[\83\\83\8b\95\\8e¦ON/OFF
+       {
+               if( this->b\91S\89æ\96Ê\83\82\81[\83h ) {
+                       while( ShowCursor( FALSE ) >= 0 )
+                               Sleep(2);
+               } else {
+                       ShowCursor( TRUE );
+               }
+       }
+
+       return S_OK;
+}
+void   CD3DApplication::t3D\8aÂ\8b«\82Ì\94j\8aü()
+{
+       this->bActive = false;
+       this->bReady  = false;
+
+       // \83A\83v\83\8a\91¤\82Ì\8fI\97¹\8f\88\97\9d
+       {
+               InvalidateDeviceObjects();
+               DeleteDeviceObjects();
+               FinalCleanup();
+       }
+       //
+       // \83f\83B\83X\83v\83\8c\83C\83\82\81[\83h\82Ì\95\9c\8c³
+       {
+               if( this->b\91S\89æ\96Ê\83\82\81[\83h )
+                       this->pDD->RestoreDisplayMode();
+       }
+       //
+       // \8ae DirectX \83I\83u\83W\83F\83N\83g\82Ì\89ð\95ú
+       {
+               this->pDD->SetCooperativeLevel( this->hWnd, DDSCL_NORMAL | DDSCL_NOWINDOWCHANGES );
+               SAFE_RELEASE( this->pD3DDevice );
+               SAFE_RELEASE( this->pD3D );
+               if( ! this->b\91S\89æ\96Ê\83\82\81[\83h ) SAFE_RELEASE( this->pddsBackBuffer );
+               SAFE_RELEASE( this->pddsFrontBuffer );
+               SAFE_RELEASE( this->pDD );
+               D3DXUninitialize();
+       }
+       //
+       // \91S\83E\83B\83\93\83h\83E\82Ì\88Ê\92u\82ð\95\9c\8c³\81i\91S\89æ\96Ê\83\82\81[\83h\8e\9e\82Ì\82Ý\81j
+       {
+               if( this->b\91S\89æ\96Ê\83\82\81[\83h )
+                       WindowPositions::Load();
+               WindowPositions::Clear();               // \93à\95\94\83\8a\83X\83g\89ð\95ú
+       }
+       //
+       // \83^\83C\83}\8fI\97¹
+       {
+               this->timer.Term();
+       }
+       //
+       // \83X\83N\83\8a\81[\83\93\83Z\81[\83o\82ª\97L\8cø\82¾\82Á\82½\82È\82ç\82à\82Æ\82É\96ß\82·
+       {
+               if( this->bScreenSaverEnable )
+                       SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, TRUE, 0, SPIF_SENDWININICHANGE );
+       }
+       //
+       // \83\82\83j\83^\93d\8c¹\90Ý\92è\82Ì\95\9c\8c³
+       {
+               ::SetThreadExecutionState( this->exeState );
+       }
+}
+HRESULT CD3DApplication::t3D\8aÂ\8b«\82Ì\8dÄ\8d\\92z( bool b\90V\91S\89æ\96Ê\83\82\81[\83h )
+{
+       HRESULT hr;
+
+       this->bReady = false;
+       
+       // \82·\82×\82Ä\82Ì\83f\83o\83C\83X\88Ë\91\83I\83u\83W\83F\83N\83g\82ð\89ð\95ú\82·\82é
+       {
+               InvalidateDeviceObjects();
+       }
+       //
+       // \8c»\8dÝ\91S\89æ\96Ê\83\82\81[\83h\82È\82ç\83f\83B\83X\83v\83\8c\83C\83\82\81[\83h\82ð\95\9c\8c³\82·\82é
+       {
+               if( this->b\91S\89æ\96Ê\83\82\81[\83h )
+                       this->pDD->RestoreDisplayMode();
+       }
+       //
+       // \8ae DirectX \83I\83u\83W\83F\83N\83g\82Ì\89ð\95ú
+       {
+               this->pDD->SetCooperativeLevel( this->hWnd, DDSCL_NORMAL | DDSCL_NOWINDOWCHANGES );
+               SAFE_RELEASE( this->pD3DDevice );
+               SAFE_RELEASE( this->pD3D );
+               if( ! this->b\91S\89æ\96Ê\83\82\81[\83h ) SAFE_RELEASE( this->pddsBackBuffer );
+               SAFE_RELEASE( this->pddsFrontBuffer );
+               SAFE_RELEASE( this->pDD );
+               D3DXUninitialize();
+       }
+       //
+       // \83E\83B\83\93\83h\83E\83X\83^\83C\83\8b\82Ì\90Ý\92è
+       {
+               // \83E\83B\83\93\83h\83E\83X\83^\83C\83\8b\82ð\95Ï\8dX\82µ\82½\8fê\8d\87\82Í\81ASetWindowsPos() \82Å\93à\95\94\83L\83\83\83b\83V\83\85\82ð\83N\83\8a\83A\82·\82é\95K\97v\82ª\82 \82é\81B
+               if( b\90V\91S\89æ\96Ê\83\82\81[\83h )
+               {
+                       ::SetWindowLongPtr( this->hWnd, GWL_STYLE, this->dw\91S\89æ\96Ê\83X\83^\83C\83\8b );
+                       ::SetWindowPos( this->hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED );
+               }
+               else
+               {
+                       ::SetWindowLongPtr( this->hWnd, GWL_STYLE, this->dw\83E\83B\83\93\83h\83E\83X\83^\83C\83\8b );
+                       ::SetWindowPos( this->hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED | SWP_SHOWWINDOW );
+               }
+       }
+       //
+       // \91S\83E\83B\83\93\83h\83E\88Ê\92u\82Æ\83T\83C\83Y\82Ì\95\9c\8c³\81i\91S\89æ\96Ê\83\82\81[\83h\8e\9e\82Ì\82Ý\81j
+       {
+               if( this->b\91S\89æ\96Ê\83\82\81[\83h )
+               {
+                       // \83E\83B\83\93\83h\83E\88Ê\92u\95\9c\8c³
+                       WindowPositions::Load();
+                       WindowPositions::Clear();
+
+                       this->t\8c»\8dÝ\82Ì\83E\83B\83\93\83h\83E\82Ì\83E\83B\83\93\83h\83E\97Ì\88æ\82Æ\83N\83\89\83C\83A\83\93\83g\97Ì\88æ\82ð\8eæ\93¾\82·\82é();
+               }
+       }
+       //
+       // \83f\83o\83C\83X\82Ì\8d\\92z
+       {
+               bool b\95Û\91¶ = this->b\91S\89æ\96Ê\83\82\81[\83h;
+               this->b\91S\89æ\96Ê\83\82\81[\83h = b\90V\91S\89æ\96Ê\83\82\81[\83h;          // t3D\8aÂ\8b«\82Ì\8d\\92z() \82Ì\91O\82É this->b\91S\89æ\96Ê\83\82\81[\83\82Ì\90Ý\92è\82ª\95K\97v
+       
+               if( FAILED( hr = this->t3D\8aÂ\8b«\82Ì\8d\\92z() ) ) {
+                       this->b\91S\89æ\96Ê\83\82\81[\83h = b\95Û\91¶;
+                       return hr;
+               }
+       }
+       //
+       // \83f\83o\83C\83X\88Ë\91\83I\83u\83W\83F\83N\83g\82Ì\95\9c\8c³\82Ì\91O\82É\81AWait a moment \89æ\91\9c\82ð\8fo\82µ\82Ä\82¨\82­\81B\81i\95\9c\8c³\82ª\92·\88ø\82¢\82½\82Æ\82«\82Ì\82½\82ß\81j
+       {
+               CTexture::BeginScene();
+               this->tWaitAMoment\82Ì\95`\89æ();
+               CTexture::EndScene();
+               this->tFlip\82Ü\82½\82ÍBlt();
+       }
+       //
+       // \83f\83o\83C\83X\88Ë\91\83I\83u\83W\83F\83N\83g\82Ì\95\9c\8c³
+       {
+               if( FAILED( hr = this->RestoreDeviceObjects() ) )
+                       return hr;
+       }
+       //
+       // \8f\80\94õ\8a®\97¹
+       this->bReady = true;
+
+       return S_OK;
+}
+HRESULT CD3DApplication::t3D\8aÂ\8b«\82Ì\95`\89æ()
+{
+       HRESULT hr;
+
+       // \8b¦\92²\83\8c\83x\83\8b\82ð\83`\83F\83b\83N\82·\82é
+       {
+               if( FAILED( hr = this->pDD->TestCooperativeLevel() ) )
+               {
+                       switch( hr )
+                       {
+                       case DDERR_EXCLUSIVEMODEALREADYSET:             // \81i\8e©\95ª\82ª\83E\83B\83\93\83h\83E\83\82\81[\83h\82Ì\82Æ\82«\81j\91¼\82Ì\83A\83v\83\8a\82ª\94r\91¼\83\82\81[\83h\82É\93ü\82Á\82½
+                       case DDERR_NOEXCLUSIVEMODE:                             // \94r\91¼\82ð\8e¸\82Á\82½\81iAlt+TAB\82È\82Ç\81j
+                               Sleep( 1000 );          // \91¼\82Ì\83A\83v\83\8a\82ª\94r\91¼\83\82\81[\83h\82É\93ü\82Á\82Ä\82¢\82é\82Ì\82Å\91Ò\8b@\82·\82é\81B\96³\91Ê\82ÉCPU\82ð\8eg\82í\82È\82¢(2006/5/6)
+                               return S_OK;
+
+                       case DDERR_WRONGMODE:                                   // \83f\83B\83X\83v\83\8c\83C\83\82\81[\83h\82ª\95Ï\8dX\82³\82ê\82½\81B\91S\83T\81[\83t\83F\83C\83X\82ð\94j\8aü\82µ\82Ä\8dì\82è\92¼\82µ\82ª\95K\97v\81B
+                               if( this->D3DDisplay.pAdapter->pDevice->bWindowed )
+                               {
+                                       hr = this->t3D\8aÂ\8b«\82Ì\8dÄ\8d\\92z( this->b\91S\89æ\96Ê\83\82\81[\83h );
+                                       return hr;
+                               }
+                               break;
+                       }
+                       return hr;
+               }
+       }
+       //
+       // \90i\8ds\95`\89æ
+       {
+               if( this->Render() )
+               {
+                       // Render() \82ª true \82ð\95Ô\82µ\82½\82ç WM_CLOSE \82ð\91\97\90M\82·\82é\81B
+                       SendMessage( this->hWnd, WM_CLOSE, 0, 0 );
+                       return S_OK;
+               }
+
+               this->tFPS\82ð\8eZ\8fo\82·\82é();
+       }
+       //
+       // \95\\8e¦
+       {
+               if( FAILED( hr = this->tFlip\82Ü\82½\82ÍBlt() ) )
+               {
+                       if( hr == DDERR_SURFACELOST )
+                       {
+                               // \83T\81[\83t\83F\83C\83X\82ð\82·\82×\82Ä\95\9c\8c³\82µ\81A\8dÄ\8d\\92z\81B
+                               this->pDD->RestoreAllSurfaces();
+                               return this->RestoreDeviceObjects();
+                       }
+                       else
+                               return hr;      // \82»\82Ì\91¼\82Ì\83G\83\89\81[
+               }
+       }
+       //
+       return S_OK;
+}
+HRESULT CD3DApplication::tFlip\82Ü\82½\82ÍBlt()
+{
+       HRESULT hr = S_OK;
+
+    if( this->pddsFrontBuffer == NULL )
+               return FDKERR_\8f\89\8aú\89»\82³\82ê\82Ä\82È\82¢;
+
+    // \91S\89æ\96Ê\83\82\81[\83h\8e\9e\81F
+       if( this->b\91S\89æ\96Ê\83\82\81[\83h )
+    {
+               if( this->b\90\82\92¼\8bA\90ü\93¯\8aú )
+               {
+                       hr = this->pddsFrontBuffer->Flip( NULL, DDFLIP_WAIT );
+                       return hr;
+               }
+               else
+               {
+                       return this->t\90\82\92¼\93¯\8aú\82ð\8eg\82í\82È\82¢\83E\83F\83C\83g\82ÆBlt( NULL );
+               }
+    }
+
+    // \83E\83B\83\93\83h\83E\83\82\81[\83h\8e\9e\81F
+    else
+       {
+               return this->t\90\82\92¼\93¯\8aú\82ð\8eg\82í\82È\82¢\83E\83F\83C\83g\82ÆBlt( &this->rc\83N\83\89\83C\83A\83\93\83g\97Ì\88æ );
+       }
+
+       return hr;
+}
+HRESULT        CD3DApplication::t\90\82\92¼\93¯\8aú\82ð\8eg\82í\82È\82¢\83E\83F\83C\83g\82ÆBlt( LPRECT prc\83N\83\89\83C\83A\83\93\83g\97Ì\88æ )
+{
+       static bool   bBltFast\82ð\8eg\82¤ = true;            // \8f\89\89ñ\82Ì\82Ý BltFast \82Å\8e\8e\82µ\81A\83_\83\81\82È\82ç\82»\82ê\88È\8d~\82ÍBlt\82ð\8eg\82¤\81B
+       static double db\91O\89ñ\82Ì\95`\89æ\8aJ\8en\8e\9e\8d\8f = INIT_TIME;
+
+       HRESULT hr = S_OK;
+
+       if( db\91O\89ñ\82Ì\95`\89æ\8aJ\8en\8e\9e\8d\8f != INIT_TIME )
+       {
+               double db\8co\89ß\8e\9e\8aÔ = this->timer.GetSysTime() - db\91O\89ñ\82Ì\95`\89æ\8aJ\8en\8e\9e\8d\8f;
+               double db\97]\8fè\8e\9e\8aÔ = (double)this->dw\95`\89æ\8aÔ\8au - db\8co\89ß\8e\9e\8aÔ;
+               if( db\97]\8fè\8e\9e\8aÔ > 0 )
+                       ::Sleep( (DWORD)db\97]\8fè\8e\9e\8aÔ );           // \97]\8fè\8e\9e\8aÔ\82¾\82¯\96°\82é
+       }
+
+retry:
+       if( bBltFast\82ð\8eg\82¤ )
+       {
+               RECT rcBack = { 0, 0, this->nWidth, this->nHeight };
+               if( FAILED( hr = this->pddsFrontBuffer->BltFast( 0, 0, this->pddsBackBuffer, &rcBack, DDBLTFAST_WAIT ) ) )
+               {
+                       bBltFast\82ð\8eg\82¤ = false; // BltFast() \82É\8e¸\94s\82µ\82½\82Ì\82Å\81A\88È\8cã\82Í Blt() \82ð\8eg\82¤\81B
+                       goto retry;
+               }
+       }
+       else
+               hr = this->pddsFrontBuffer->Blt( prc\83N\83\89\83C\83A\83\93\83g\97Ì\88æ, this->pddsBackBuffer, NULL, DDBLT_WAIT, NULL );
+
+       db\91O\89ñ\82Ì\95`\89æ\8aJ\8en\8e\9e\8d\8f = this->timer.GetSysTime();
+       
+       return hr;
+}
+//
+void   CD3DApplication::t\91S\89æ\96Ê\81E\83E\83B\83\93\83h\83E\83\82\81[\83h\82ð\90Ø\82è\91Ö\82¦\82é()
+{
+       HRESULT hr;
+
+       // \8c»\8dÝ\83E\83B\83\93\83h\83E\83\82\81[\83h\82È\82ç\91S\83E\83B\83\93\83h\83E\82Ì\88Ê\92u\82ð\95Û\91\82·\82é
+       if( ! this->b\91S\89æ\96Ê\83\82\81[\83h )
+               WindowPositions::Save();
+
+       // \83f\83o\83C\83X\90Ø\82è\91Ö\82¦
+       if( FAILED( hr = t3D\8aÂ\8b«\82Ì\8dÄ\8d\\92z( ! this->b\91S\89æ\96Ê\83\82\81[\83h ) ) )
+       {
+               // \8e¸\94s\82µ\82½\82ç\8c³\82É\96ß\82·\81i\82P\89ñ\82¾\82¯\81j
+               if( FAILED( hr = t3D\8aÂ\8b«\82Ì\8dÄ\8d\\92z( this->b\91S\89æ\96Ê\83\82\81[\83h ) ) )
+                       return; // \82±\82ê\82Å\82à\83G\83\89\81[\82ª\8fo\82½\82ç\96³\8e\8b
+       }
+}
+
+void   CD3DApplication::t\8c»\8dÝ\82Ì\83E\83B\83\93\83h\83E\82Ì\83E\83B\83\93\83h\83E\97Ì\88æ\82Æ\83N\83\89\83C\83A\83\93\83g\97Ì\88æ\82ð\8eæ\93¾\82·\82é()
+{
+       // \83E\83B\83\93\83h\83E\97Ì\88æ\82Ì\8eæ\93¾
+       GetWindowRect( this->hWnd, &this->rc\83E\83B\83\93\83h\83E\97Ì\88æ );
+
+       // \83N\83\89\83C\83A\83\93\83g\97Ì\88æ\82Ì\8eæ\93¾
+       GetClientRect( this->hWnd, &this->rc\83N\83\89\83C\83A\83\93\83g\97Ì\88æ );
+       ClientToScreen( this->hWnd, (POINT*)&this->rc\83N\83\89\83C\83A\83\93\83g\97Ì\88æ.left );           // left, top     \82ð\83X\83N\83\8a\81[\83\93\8dÀ\95W\82Ö
+       ClientToScreen( this->hWnd, (POINT*)&this->rc\83N\83\89\83C\83A\83\93\83g\97Ì\88æ.right );          // right, bottom \82ð\83X\83N\83\8a\81[\83\93\8dÀ\95W\82Ö
+}
+
+void   CD3DApplication::tWaitAMoment\82Ì\95`\89æ()
+{
+/*
+       \88È\89º\82Í\83T\83\93\83v\83\8b
+
+       CSurface sf;
+       sf.InitDeviceObjectsFromFile( _T("Wait a moment"), _T("sysdata/ses_frame.jpg") );
+       if( SUCCEEDED( sf.RestoreDeviceObjects() ) )
+       {
+               sf.Draw( 0, 0 );
+               sf.InvalidateDeviceObjects();
+       }
+       sf.DeleteDeviceObjects();
+*/
+}
+
+void   CD3DApplication::tFPS\82ð\8eZ\8fo\82·\82é()
+{
+       static double fpstime = INIT_TIME;
+       static int fps = 0;
+
+       // \8f\89\8aú\89»
+       if( fpstime == INIT_TIME )
+       {
+               fpstime = this->timer.GetSysTime();
+               this->nFPS = fps = 0;
+       }
+
+       // \82P\95b\8co\89ß\82²\82Æ\82É\8cv\8eZ
+       while( this->timer.GetSysTime() - fpstime >= 1000.0 )
+       {
+               this->nFPS = fps;
+               fps = 0;
+               fpstime += 1000.0;
+       }
+       fps ++;
+}
+//
+INT            CD3DApplication::Run()
+{
+    MSG  msg;
+
+       while( true )
+    {
+               if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
+               {
+                       if( msg.message == WM_QUIT )
+                               break;
+                       TranslateMessage( &msg );
+                       DispatchMessage( &msg );
+               }
+               else
+               {
+                       if( this->bReady && this->bActive )
+                       {
+                               if( FAILED( t3D\8aÂ\8b«\82Ì\95`\89æ() ) ) {
+                                       Debug::Msg( _T("3D\8aÂ\8b«\82Ì\95`\89æ\82É\8e¸\94s\82µ\82Ü\82µ\82½\81B\8fI\97¹\82µ\82Ü\82·\81B\n") );
+                                       SendMessage( this->hWnd, WM_CLOSE, 0, 0 );
+                               }
+                       }
+               }
+       }
+    return (INT)msg.wParam;
+}
+LRESULT CD3DApplication::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
+{
+    switch( uMsg )
+    {
+
+       case WM_CLOSE:
+               this->bActive = false;
+               this->t3D\8aÂ\8b«\82Ì\94j\8aü();
+               break;
+
+       case WM_DESTROY:
+               PostQuitMessage( 0 );
+               return 0;
+
+       case WM_SIZE:
+               // \83E\83B\83\93\83h\83E\82ª\91S\95\94\89B\82ê\82½\82è\8dÅ\8f¬\89»\82³\82ê\82½\82è\82µ\82½\82ç this->bActive = false \82É\82·\82é\81B
+               this->bActive = ( wParam == SIZE_MAXHIDE || wParam == SIZE_MINIMIZED ) ? false : true;
+               break;
+
+       case WM_MOVE:
+               // \90V\82µ\82¢\97Ì\88æ\8dÀ\95W\82ð\8eæ\93¾\82µ\82Ä\82¨\82­
+               this->t\8c»\8dÝ\82Ì\83E\83B\83\93\83h\83E\82Ì\83E\83B\83\93\83h\83E\97Ì\88æ\82Æ\83N\83\89\83C\83A\83\93\83g\97Ì\88æ\82ð\8eæ\93¾\82·\82é();
+               break;
+
+       case WM_GETMINMAXINFO:
+               // \83A\83v\83\8a\82Ì\8dÅ\8f¬\83T\83C\83Y\82Ì\96â\82¢\8d\87\82í\82¹\82Ö\82Ì\89ñ\93\9a\81B
+               ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
+               ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
+               return 0;
+
+       case WM_NCHITTEST:
+               if( this->b\91S\89æ\96Ê\83\82\81[\83h )
+                       return HTCLIENT;                // \82·\82×\82Ä\82ª\83N\83\89\83C\83A\83\93\83g\97Ì\88æ
+               break;
+       }
+       return DefWindowProc( hWnd, uMsg, wParam, lParam );
+}
+       }//AppBase
+}//FDK
\ No newline at end of file