0001: //-------------------------------------------------------------
0002: // File: main.cpp
0003: //
0004: // Desc: 双3次フィルタ
0005: //-------------------------------------------------------------
0006: #define STRICT
0007: #include <windows.h>
0008: #include <commctrl.h>
0009: #include <commdlg.h>
0010: #include <basetsd.h>
0011: #include <math.h>
0012: #include <stdio.h>
0013: #include <d3dx9.h>
0014: #include <dxerr9.h>
0015: #include <tchar.h>
0016: #include "DXUtil.h"
0017: #include "D3DEnumeration.h"
0018: #include "D3DSettings.h"
0019: #include "D3DApp.h"
0020: #include "D3DFont.h"
0021: #include "D3DFile.h"
0022: #include "D3DUtil.h"
0023: #include "resource.h"
0024: #include "main.h"
0025: 
0026: // 長いから短縮形を作ってみた
0027: #define RS   m_pd3dDevice->SetRenderState
0028: #define TSS  m_pd3dDevice->SetTextureStageState
0029: #define SAMP m_pd3dDevice->SetSamplerState
0030: 
0031: 
0032: //-------------------------------------------------------------
0033: // 頂点の構造体
0034: //-------------------------------------------------------------
0035: typedef struct {
0036:     FLOAT       p[4];
0037:     FLOAT       tu, tv;
0038: } TVERTEX;
0039: 
0040: typedef struct {
0041:     FLOAT       p[4];
0042:     FLOAT       tu0, tv0;
0043:     FLOAT       tu1, tv1;
0044:     FLOAT       tu2, tv2;
0045:     FLOAT       tu3, tv3;
0046:     FLOAT       tu4, tv4;
0047:     FLOAT       tu5, tv5;
0048:     FLOAT       tu6, tv6;
0049:     FLOAT       tu7, tv7;
0050: } TVERTEX8;
0051: 
0052: //-------------------------------------------------------------
0053: // グローバル変数
0054: //-------------------------------------------------------------
0055: CMyD3DApplication* g_pApp  = NULL;
0056: HINSTANCE          g_hInst = NULL;
0057: 
0058: 
0059: 
0060: //-------------------------------------------------------------
0061: // Name: WinMain()
0062: // Desc: メイン関数
0063: //-------------------------------------------------------------
0064: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0065: {
0066:     CMyD3DApplication d3dApp;
0067: 
0068:     g_pApp  = &d3dApp;
0069:     g_hInst = hInst;
0070: 
0071:     InitCommonControls();
0072:     if( FAILED( d3dApp.Create( hInst ) ) )
0073:         return 0;
0074: 
0075:     return d3dApp.Run();
0076: }
0077: 
0078: 
0079: 
0080: 
0081: //-------------------------------------------------------------
0082: // Name: CMyD3DApplication()
0083: // Desc: アプリケーションのコンストラクタ
0084: //-------------------------------------------------------------
0085: CMyD3DApplication::CMyD3DApplication()
0086: {
0087:     m_pTex                      = NULL;
0088: 
0089:     m_pEffect                   = NULL;
0090:     m_hTechnique                = NULL;
0091: 
0092:     m_dwCreationWidth           = 5 * (3 * 64 + 6 * 2);
0093:     m_dwCreationHeight          = 5 * (32 + 2 + 2);
0094:     m_strWindowTitle            = TEXT( "main" );
0095:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0096:     m_bStartFullscreen          = false;
0097:     m_bShowCursorWhenFullscreen = false;
0098: 
0099:     m_pFont                     = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0100:     m_bLoadingApp               = TRUE;
0101: 
0102:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0103: }
0104: 
0105: 
0106: 
0107: 
0108: //-------------------------------------------------------------
0109: // Name: ~CMyD3DApplication()
0110: // Desc: デストラクタ
0111: //-------------------------------------------------------------
0112: CMyD3DApplication::~CMyD3DApplication()
0113: {
0114: }
0115: 
0116: 
0117: 
0118: 
0119: //-------------------------------------------------------------
0120: // Name: OneTimeSceneInit()
0121: // Desc: 一度だけ行う初期化
0122: //      ウィンドウの初期化やIDirect3D9の初期化は終わってます。
0123: //      ただ、LPDIRECT3DDEVICE9 の初期化は終わっていません。
0124: //-------------------------------------------------------------
0125: HRESULT CMyD3DApplication::OneTimeSceneInit()
0126: {
0127:     // ローディングメッセージを表示する
0128:     SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0129: 
0130:     m_bLoadingApp = FALSE;
0131: 
0132:     return S_OK;
0133: }
0134: 
0135: 
0136: 
0137: 
0138: //-------------------------------------------------------------
0139: // Name: ConfirmDevice()
0140: // Desc: 初期化の時に呼ばれます。必要な能力をチェックします。
0141: //-------------------------------------------------------------
0142: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0143:                      DWORD dwBehavior,    D3DFORMAT Format )
0144: {
0145:     UNREFERENCED_PARAMETER( Format );
0146:     UNREFERENCED_PARAMETER( dwBehavior );
0147:     UNREFERENCED_PARAMETER( pCaps );
0148:     
0149: 
0150:     // ピクセルシェーダバージョンチェック
0151:     if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0152:         return E_FAIL;
0153: 
0154:     return S_OK;
0155: }
0156: 
0157: 
0158: 
0159: 
0160: //-------------------------------------------------------------
0161: // Name: InitDeviceObjects()
0162: // Desc: デバイスが生成された後の初期化をします。
0163: //      フレームバッファフォーマットやデバイスの種類が変わった
0164: //      後に通過します。
0165: //      ここで確保したメモリはDeleteDeviceObjects()で開放します
0166: //-------------------------------------------------------------
0167: HRESULT CMyD3DApplication::InitDeviceObjects()
0168: {
0169:     HRESULT hr;
0170:     LPDIRECT3DSURFACE9      pSurf;
0171:     D3DSURFACE_DESC         desc;
0172: 
0173:     // テクスチャの読み込み
0174:     D3DXCreateTextureFromFile(m_pd3dDevice, "src.bmp"
0175:                             , &m_pTex);
0176:     m_pTex->GetSurfaceLevel(0,&pSurf);
0177:     pSurf->GetDesc(&desc);
0178:     m_TexWidth  = (FLOAT)desc.Width;
0179:     m_TexHeight = (FLOAT)desc.Height;
0180:     SAFE_RELEASE(pSurf);
0181: 
0182:     // シェーダの読み込み
0183:     LPD3DXBUFFER pErr;
0184:     if( FAILED( hr = D3DXCreateEffectFromFile(
0185:                 m_pd3dDevice, "hlsl.fx", NULL, NULL, 
0186:                 D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0187:         MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0188:                     , "ERROR", MB_OK);
0189:         return DXTRACE_ERR( "CreateEffectFromFile", hr );
0190:     }
0191:     m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0192:     D3DXVECTOR4 size = D3DXVECTOR4( (FLOAT)m_TexWidth, (FLOAT)m_TexHeight, 0,0);
0193:     D3DXVECTOR4 d2u  = D3DXVECTOR4( 2.0f/size[0], 0,0,0);
0194:     m_pEffect->SetVector( "vSize", &size );
0195:     m_pEffect->SetVector( "d2u",   &d2u );
0196: 
0197:     // フォント
0198:     m_pFont->InitDeviceObjects( m_pd3dDevice );
0199: 
0200:     return S_OK;
0201: }
0202: 
0203: //-------------------------------------------------------------
0204: // Name: RestoreDeviceObjects()
0205: // Desc: 画面のサイズが変更された時等に呼ばれます。
0206: //      確保したメモリはInvalidateDeviceObjects()で開放します。
0207: //-------------------------------------------------------------
0208: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0209: {
0210:     // レンダリング状態の設定
0211:     RS( D3DRS_DITHERENABLE,   FALSE );
0212:     RS( D3DRS_SPECULARENABLE, FALSE );
0213:     RS( D3DRS_ZENABLE,        TRUE );
0214:     
0215:     SAMP( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0216:     SAMP( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0217:     SAMP( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0218:     SAMP( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0219: 
0220:     // フォント
0221:     m_pFont->RestoreDeviceObjects();
0222: 
0223:     m_pEffect->OnResetDevice();
0224: 
0225:     return S_OK;
0226: }
0227: 
0228: 
0229: 
0230: 
0231: //-------------------------------------------------------------
0232: // Name: FrameMove()
0233: // Desc: 毎フレーム呼ばれます。アニメの処理などを行います。
0234: //-------------------------------------------------------------
0235: HRESULT CMyD3DApplication::FrameMove()
0236: {
0237:     // 入力データの更新
0238:     UpdateInput( &m_UserInput );
0239: 
0240:     return S_OK;
0241: }
0242: //-------------------------------------------------------------
0243: // Name: UpdateInput()
0244: // Desc: 入力データを更新する
0245: //-------------------------------------------------------------
0246: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0247: {
0248:     pUserInput->bRotateUp    = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0249:     pUserInput->bRotateDown  = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0250:     pUserInput->bRotateLeft  = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0251:     pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0252: }
0253: 
0254: 
0255: //-------------------------------------------------------------
0256: // Name: Render()
0257: // Desc: 画面を描画する.
0258: //-------------------------------------------------------------
0259: HRESULT CMyD3DApplication::Render()
0260: {
0261:     D3DXMATRIX m, mT, mR, mView, mProj;
0262: 
0263:     //---------------------------------------------------------
0264:     // 描画
0265:     //---------------------------------------------------------
0266:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0267:     {
0268:         // クリア
0269:         m_pd3dDevice->Clear(0L, NULL
0270:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0271:                         , 0xff0080ff, 1.0f, 0L);
0272: 
0273:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0274:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0275:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0276:         m_pd3dDevice->SetTexture( 0, m_pTex );
0277:         m_pd3dDevice->SetPixelShader(0);
0278: 
0279:         for ( int i = 0; i < 3; i++)
0280:         {
0281:             float x = 5 * (i * 64 + (2 * i + 1)*2);
0282:             float y = 5 * (2);
0283:             float w = 5 * 64;
0284:             float h = 5 * 32;
0285: 
0286:             TVERTEX Vertex[4] = {
0287:                 // x      y    z rhw tu tv
0288:                 {x + 0, y + 0, 0, 1, 0, 0,},
0289:                 {x + w, y + 0, 0, 1, 1, 0,},
0290:                 {x + w, y + h, 0, 1, 1, 1,},
0291:                 {x + 0, y + h, 0, 1, 0, 1,},
0292:             };
0293:             
0294:             float du = 1.0f/m_TexWidth;
0295:             float dv = 1.0f/m_TexHeight;
0296:             TVERTEX8 Vertex8[4] = {
0297:                 // x      y    z rhw  uv0
0298:                 {x + 0, y + 0, 0, 1, 0-1.5f*du, 0-1.5f*dv,  0-1.5f*du, 0-0.5f*dv,  0-1.5f*du, 0+0.5f*dv,  0-1.5f*du, 0+1.5f*dv
0299:                                     ,0-0.5f*du, 0-1.5f*dv,  0-0.5f*du, 0-0.5f*dv,  0-0.5f*du, 0+0.5f*dv,  0-0.5f*du, 0+1.5f*dv},
0300:                 {x + w, y + 0, 0, 1, 1-1.5f*du, 0-1.5f*dv,  1-1.5f*du, 0-0.5f*dv,  1-1.5f*du, 0+0.5f*dv,  1-1.5f*du, 0+1.5f*dv
0301:                                     ,1-0.5f*du, 0-1.5f*dv,  1-0.5f*du, 0-0.5f*dv,  1-0.5f*du, 0+0.5f*dv,  1-0.5f*du, 0+1.5f*dv},
0302:                 {x + w, y + h, 0, 1, 1-1.5f*du, 1-1.5f*dv,  1-1.5f*du, 1-0.5f*dv,  1-1.5f*du, 1+0.5f*dv,  1-1.5f*du, 1+1.5f*dv
0303:                                     ,1-0.5f*du, 1-1.5f*dv,  1-0.5f*du, 1-0.5f*dv,  1-0.5f*du, 1+0.5f*dv,  1-0.5f*du, 1+1.5f*dv},
0304:                 {x + 0, y + h, 0, 1, 0-1.5f*du, 1-1.5f*dv,  0-1.5f*du, 1-0.5f*dv,  0-1.5f*du, 1+0.5f*dv,  0-1.5f*du, 1+1.5f*dv
0305:                                     ,0-0.5f*du, 1-1.5f*dv,  0-0.5f*du, 1-0.5f*dv,  0-0.5f*du, 1+0.5f*dv,  0-0.5f*du, 1+1.5f*dv},
0306:             };
0307: 
0308:             switch(i)
0309:             {
0310:             case 0:// point sampling
0311:                 m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
0312:                 m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
0313:                 m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0314:                 m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0315:                 break;
0316:             case 1:// Bi-linear
0317:                 m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0318:                 m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0319:                 m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0320:                 m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0321:                 break;
0322:             case 2:// Bicubic
0323:                 m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
0324:                 m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
0325:                 m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX8 );
0326:                 if( m_pEffect != NULL ) {
0327:                     m_pEffect->SetTechnique( m_hTechnique );
0328:                     m_pEffect->Begin( NULL, 0 );
0329:                     m_pEffect->Pass( 0 );
0330:                     m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex8, sizeof( TVERTEX8 ) );
0331:                     m_pEffect->End();
0332:                 }
0333:                 break;
0334:             }
0335: 
0336:         }
0337: 
0338:         // ヘルプの表示
0339:         RenderText();
0340: 
0341:         // 描画の終了
0342:         m_pd3dDevice->EndScene();
0343:     }
0344: 
0345:     return S_OK;
0346: }
0347: 
0348: 
0349: 
0350: 
0351: //-------------------------------------------------------------
0352: // Name: RenderText()
0353: // Desc: 状態やヘルプを画面に表示する
0354: //-------------------------------------------------------------
0355: HRESULT CMyD3DApplication::RenderText()
0356: {
0357:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,128,64);
0358:     TCHAR szMsg[MAX_PATH] = TEXT("");
0359: 
0360:     FLOAT fNextLine; // 表示する高さ
0361: 
0362:     // 操作法やパラメータを表示する
0363:     fNextLine = 0;
0364:     lstrcpy( szMsg, TEXT("POINT") );
0365:     m_pFont->DrawText( 140, fNextLine, fontColor, szMsg );
0366:     lstrcpy( szMsg, TEXT("BILINEAR") );
0367:     m_pFont->DrawText( 470, fNextLine, fontColor, szMsg );
0368:     lstrcpy( szMsg, TEXT("BICUBLC") );
0369:     m_pFont->DrawText( 810, fNextLine, fontColor, szMsg );
0370:     
0371:     return S_OK;
0372: }
0373: 
0374: 
0375: 
0376: //-------------------------------------------------------------
0377: // Name: MsgProc()
0378: // Desc: WndProc をオーバーライドしたもの
0379: //-------------------------------------------------------------
0380: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
0381:                                  WPARAM wParam, LPARAM lParam )
0382: {
0383:     switch( msg )
0384:     {
0385:         case WM_PAINT:
0386:         {
0387:             if( m_bLoadingApp )
0388:             {
0389:                 // ロード中
0390:                 HDC hDC = GetDC( hWnd );
0391:                 TCHAR strMsg[MAX_PATH];
0392:                 wsprintf(strMsg, TEXT("Loading... Please wait"));
0393:                 RECT rct;
0394:                 GetClientRect( hWnd, &rct );
0395:                 DrawText( hDC, strMsg, -1, &rct
0396:                         , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0397:                 ReleaseDC( hWnd, hDC );
0398:             }
0399:             break;
0400:         }
0401: 
0402:     }
0403: 
0404:     return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0405: }
0406: 
0407: 
0408: 
0409: 
0410: //-------------------------------------------------------------
0411: // Name: InvalidateDeviceObjects()
0412: // Desc: RestoreDeviceObjects() で作成したオブジェクトの開放
0413: //-------------------------------------------------------------
0414: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0415: {
0416:     // シェーダ
0417:     if( m_pEffect != NULL ) m_pEffect->OnLostDevice();
0418: 
0419:     m_pFont->InvalidateDeviceObjects(); // フォント
0420: 
0421:     return S_OK;
0422: }
0423: 
0424: 
0425: 
0426: 
0427: //-------------------------------------------------------------
0428: // Name: DeleteDeviceObjects()
0429: // Desc: InitDeviceObjects() で作成したオブジェクトを開放する
0430: //-------------------------------------------------------------
0431: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0432: {
0433:     // シェーダ
0434:     SAFE_RELEASE( m_pTex );
0435: 
0436:     // シェーダ
0437:     SAFE_RELEASE( m_pEffect );
0438:     
0439:     // フォント
0440:     m_pFont->DeleteDeviceObjects();
0441: 
0442:     return S_OK;
0443: }
0444: 
0445: 
0446: 
0447: 
0448: //-------------------------------------------------------------
0449: // Name: FinalCleanup()
0450: // Desc: 終了する直線に呼ばれる
0451: //-------------------------------------------------------------
0452: HRESULT CMyD3DApplication::FinalCleanup()
0453: {
0454:     SAFE_DELETE( m_pFont ); // フォント
0455: 
0456:     return S_OK;
0457: }
0458: 
0459: 
0460: 
0461: 
0462: