0001: //-----------------------------------------------------------------------------
0002: // File: CheckDeviceFormatViewer.cpp
0003: //
0004: // Desc: DirectX MFC dialog application created by the DirectX AppWizard
0005: //-----------------------------------------------------------------------------
0006: #define STRICT
0007: #include "stdafx.h"
0008: #include "resource.h"
0009: #include "CheckDeviceFormatViewer.h"
0010: 
0011: 
0012: 
0013: 
0014: //-----------------------------------------------------------------------------
0015: // Application globals
0016: //-----------------------------------------------------------------------------
0017: TCHAR*          g_strAppTitle       = _T( "CheckDeviceFormatViewer" );
0018: CApp            g_App;
0019: HINSTANCE       g_hInst = NULL;
0020: CAppForm*       g_AppFormView = NULL;
0021: 
0022: 
0023: 
0024: 
0025: //-----------------------------------------------------------------------------
0026: // The MFC macros are all listed here
0027: //-----------------------------------------------------------------------------
0028: IMPLEMENT_DYNCREATE( CAppDoc,      CDocument )
0029: IMPLEMENT_DYNCREATE( CAppFrameWnd, CFrameWnd )
0030: IMPLEMENT_DYNCREATE( CAppForm,     CFormView )
0031: 
0032: 
0033: 
0034: 
0035: BEGIN_MESSAGE_MAP( CApp, CWinApp )
0036:     //{{AFX_MSG_MAP(CApp)
0037:     //}}AFX_MSG_MAP
0038: END_MESSAGE_MAP()
0039: 
0040: 
0041: 
0042: 
0043: BEGIN_MESSAGE_MAP( CAppForm, CFormView )
0044:     //{{AFX_MSG_MAP(CAppForm)
0045:     ON_COMMAND(    IDC_VIEWFULLSCREEN, OnToggleFullScreen )
0046:     ON_BN_CLICKED(IDC_CHANGEDEVICE, OnChangeDevice)
0047:     //}}AFX_MSG_MAP
0048: //  ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButtonCheckDeviceFormat)
0049: ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButtonCheckDeviceFormat)
0050: END_MESSAGE_MAP()
0051: 
0052: 
0053: 
0054: 
0055: BEGIN_MESSAGE_MAP(CAppDoc, CDocument)
0056:     //{{AFX_MSG_MAP(CAppDoc)
0057:             // NOTE - the ClassWizard will add and remove mapping macros here.
0058:             //    DO NOT EDIT what you see in these blocks of generated code!
0059:     //}}AFX_MSG_MAP
0060: END_MESSAGE_MAP()
0061: 
0062: 
0063: 
0064: 
0065: BEGIN_MESSAGE_MAP(CAppFrameWnd, CFrameWnd)
0066:     //{{AFX_MSG_MAP(CAppFrameWnd)
0067:     ON_COMMAND(IDM_CHANGEDEVICE, OnChangeDevice)
0068:     //}}AFX_MSG_MAP
0069: END_MESSAGE_MAP()
0070: 
0071: static const MyUsage s_usage[] = {
0072:     {D3DUSAGE_RENDERTARGET, "D3DUSAGE_RENDERTARGET"},
0073:     {D3DUSAGE_DEPTHSTENCIL,"D3DUSAGE_DEPTHSTENCIL"},
0074:     {D3DUSAGE_DYNAMIC,"D3DUSAGE_DYNAMIC"},
0075: // When passed to CheckDeviceFormat, D3DUSAGE_AUTOGENMIPMAP may return
0076: // D3DOK_NOAUTOGEN if the device doesn't support autogeneration for that format.
0077: // D3DOK_NOAUTOGEN is a success code, not a failure code... the SUCCEEDED and FAILED macros
0078: // will return true and false respectively for this code.
0079:     {D3DUSAGE_AUTOGENMIPMAP,"D3DUSAGE_AUTOGENMIPMAP"},
0080:     {D3DUSAGE_DMAP,"D3DUSAGE_DMAP"},
0081:     {0,""}
0082: };
0083: const MyUsage *CAppForm::m_Usages=s_usage;
0084: 
0085: static const MyFormat s_format[] = {
0086:     {D3DFMT_R8G8B8,"D3DFMT_R8G8B8"},
0087:     {D3DFMT_A8R8G8B8,"D3DFMT_A8R8G8B8"},
0088:     {D3DFMT_X8R8G8B8,"D3DFMT_X8R8G8B8"},
0089:     {D3DFMT_R5G6B5,"D3DFMT_R5G6B5"},
0090:     {D3DFMT_X1R5G5B5,"D3DFMT_X1R5G5B5"},
0091:     {D3DFMT_A1R5G5B5,"D3DFMT_A1R5G5B5"},
0092:     {D3DFMT_A4R4G4B4,"D3DFMT_A4R4G4B4"},
0093:     {D3DFMT_R3G3B2,"D3DFMT_R3G3B2"},
0094:     {D3DFMT_A8,"D3DFMT_A8"},
0095:     {D3DFMT_A8R3G3B2,"D3DFMT_A8R3G3B2"},
0096:     {D3DFMT_X4R4G4B4,"D3DFMT_X4R4G4B4"},
0097:     {D3DFMT_A2B10G10R10,"D3DFMT_A2B10G10R10"},
0098:     {D3DFMT_A8B8G8R8,"D3DFMT_A8B8G8R8"},
0099:     {D3DFMT_X8B8G8R8,"D3DFMT_X8B8G8R8"},
0100:     {D3DFMT_G16R16,"D3DFMT_G16R16"},
0101:     {D3DFMT_A2R10G10B10,"D3DFMT_A2R10G10B10"},
0102:     {D3DFMT_A16B16G16R16,"D3DFMT_A16B16G16R16"},
0103: 
0104:     {D3DFMT_A8P8,"D3DFMT_A8P8"},
0105:     {D3DFMT_P8,"D3DFMT_P8"},
0106: 
0107:     {D3DFMT_L8,"D3DFMT_L8"},
0108:     {D3DFMT_A8L8,"D3DFMT_A8L8"},
0109:     {D3DFMT_A4L4,"D3DFMT_A4L4"},
0110: 
0111:     {D3DFMT_V8U8,"D3DFMT_V8U8"},
0112:     {D3DFMT_L6V5U5,"D3DFMT_L6V5U5"},
0113:     {D3DFMT_X8L8V8U8,"D3DFMT_X8L8V8U8"},
0114:     {D3DFMT_Q8W8V8U8,"D3DFMT_Q8W8V8U8"},
0115:     {D3DFMT_V16U16,"D3DFMT_V16U16"},
0116:     {D3DFMT_A2W10V10U10,"D3DFMT_A2W10V10U10"},
0117: 
0118:     {D3DFMT_UYVY,"D3DFMT_UYVY"},
0119:     {D3DFMT_R8G8_B8G8,"D3DFMT_R8G8_B8G8"},
0120:     {D3DFMT_YUY2,"D3DFMT_YUY2"},
0121:     {D3DFMT_G8R8_G8B8,"D3DFMT_G8R8_G8B8"},
0122:     {D3DFMT_DXT1,"D3DFMT_DXT1"},
0123:     {D3DFMT_DXT2,"D3DFMT_DXT2"},
0124:     {D3DFMT_DXT3,"D3DFMT_DXT3"},
0125:     {D3DFMT_DXT4,"D3DFMT_DXT4"},
0126:     {D3DFMT_DXT5,"D3DFMT_DXT5"},
0127: 
0128:     {D3DFMT_D16_LOCKABLE,"D3DFMT_D16_LOCKABLE"},
0129:     {D3DFMT_D32,"D3DFMT_D32"},
0130:     {D3DFMT_D15S1,"D3DFMT_D15S1"},
0131:     {D3DFMT_D24S8,"D3DFMT_D24S8"},
0132:     {D3DFMT_D24X8,"D3DFMT_D24X8"},
0133:     {D3DFMT_D24X4S4,"D3DFMT_D24X4S4"},
0134:     {D3DFMT_D16,"D3DFMT_D16"},
0135: 
0136:     {D3DFMT_D32F_LOCKABLE,"D3DFMT_D32F_LOCKABLE"},
0137:     {D3DFMT_D24FS8,"D3DFMT_D24FS8"},
0138: 
0139: 
0140:     {D3DFMT_L16,"D3DFMT_L16"},
0141: 
0142:     {D3DFMT_Q16W16V16U16,"D3DFMT_Q16W16V16U16"},
0143: 
0144:     {D3DFMT_MULTI2_ARGB8,"D3DFMT_MULTI2_ARGB8"},
0145: 
0146:     // Floating point surface formats
0147:     // s10e5 formats (16-bits per channel)
0148:     {D3DFMT_R16F,"D3DFMT_R16F"},
0149:     {D3DFMT_G16R16F,"D3DFMT_G16R16F"},
0150:     {D3DFMT_A16B16G16R16F,"D3DFMT_A16B16G16R16F"},
0151: 
0152:     // IEEE s23e8 formats (32-bits per channel)
0153:     {D3DFMT_R32F,"D3DFMT_R32F"},
0154:     {D3DFMT_G32R32F,"D3DFMT_G32R32F"},
0155:     {D3DFMT_A32B32G32R32F,"D3DFMT_A32B32G32R32F"},
0156: 
0157:     {D3DFMT_CxV8U8,"D3DFMT_CxV8U8"},
0158: 
0159: 
0160:     {D3DFMT_FORCE_DWORD,"D3DFMT_FORCE_DWORD"},
0161: };
0162: const MyFormat *CAppForm::m_Formats = (const MyFormat*)s_format;
0163: 
0164: 
0165: 
0166: //-----------------------------------------------------------------------------
0167: // Name: CAppForm()
0168: // Desc: Constructor for the dialog resource form.  Paired with ~CAppForm()
0169: //       Member variables should be initialized to a known state here.  
0170: //       The application window has not yet been created and no Direct3D device 
0171: //       has been created, so any initialization that depends on a window or 
0172: //       Direct3D should be deferred to a later stage. 
0173: //-----------------------------------------------------------------------------
0174: CAppForm::CAppForm()
0175:          :CFormView( IDD_FORMVIEW )
0176:          , m_bCheckRenderTarget(TRUE)
0177:          , m_bCheckDepthStencil(FALSE)
0178:          , m_bCheckDynamic(FALSE)
0179:          , m_bCheckAutoGenMipmap(FALSE)
0180:          , m_bCheckDMap(FALSE)
0181:          , m_iResourceType(0)
0182: {
0183:     //{{AFX_DATA_INIT(CAppForm)
0184:         // NOTE: the ClassWizard will add member initialization here
0185:     //}}AFX_DATA_INIT
0186: 
0187:     g_AppFormView          = this;
0188:     m_hwndRenderWindow     = NULL;
0189:     m_hwndRenderFullScreen = NULL;
0190:     m_hWndTopLevelParent   = NULL;
0191: 
0192:     // Override some CD3DApplication defaults:
0193:     m_dwCreationWidth           = 500;
0194:     m_dwCreationHeight          = 375;
0195:     m_strWindowTitle            = TEXT( "CheckDeviceFormatViewer" );
0196:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0197:     m_bStartFullscreen          = false;
0198:     m_bShowCursorWhenFullscreen = false;
0199: 
0200:     m_pFont                     = NULL;
0201:     m_bLoadingApp               = TRUE;
0202: 
0203:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0204:     m_fWorldRotX                = 0.0f;
0205:     m_fWorldRotY                = 0.0f;
0206: 
0207:     for(m_nFormat=0; this->m_Formats[m_nFormat].val!=D3DFMT_FORCE_DWORD; m_nFormat++);
0208:     m_pbResult = new BOOL[m_nFormat];
0209:     for(int i=0; i<m_nFormat; i++) m_pbResult[i] = FALSE;
0210: }
0211: 
0212: 
0213: 
0214: 
0215: //-----------------------------------------------------------------------------
0216: // Name: CAppForm::OneTimeSceneInit()
0217: // Desc: Paired with FinalCleanup().
0218: //       The window has been created and the IDirect3D9 interface has been
0219: //       created, but the device has not been created yet.  Here you can
0220: //       perform application-related initialization and cleanup that does
0221: //       not depend on a device.
0222: //-----------------------------------------------------------------------------
0223: HRESULT CAppForm::OneTimeSceneInit()
0224: {
0225:     // TODO: perform one time initialization
0226: 
0227:     m_bLoadingApp = FALSE;
0228: 
0229:     return S_OK;
0230: }
0231: 
0232: 
0233: 
0234: 
0235: 
0236: 
0237: 
0238: 
0239: 
0240: //-----------------------------------------------------------------------------
0241: // Name: ConfirmDevice()
0242: // Desc: Called during device initialization, this code checks the display device
0243: //       for some minimum set of capabilities
0244: //-----------------------------------------------------------------------------
0245: HRESULT CAppForm::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
0246:                                           D3DFORMAT Format )
0247: {
0248:     UNREFERENCED_PARAMETER( pCaps );
0249:     UNREFERENCED_PARAMETER( dwBehavior );
0250:     UNREFERENCED_PARAMETER( Format );
0251:     BOOL bCapsAcceptable;
0252: 
0253:     // TODO: Perform checks to see if these display caps are acceptable.
0254:     bCapsAcceptable = TRUE;
0255: 
0256:     if( bCapsAcceptable )         
0257:         return S_OK;
0258:     else
0259:         return E_FAIL;
0260: }
0261: 
0262: 
0263: 
0264: 
0265: //-----------------------------------------------------------------------------
0266: // Name: CAppForm::InitDeviceObjects()
0267: // Desc: Paired with DeleteDeviceObjects()
0268: //       The device has been created.  Resources that are not lost on
0269: //       Reset() can be created here -- resources in D3DPOOL_MANAGED,
0270: //       D3DPOOL_SCRATCH, or D3DPOOL_SYSTEMMEM.  Image surfaces created via
0271: //       CreateOffScreenPlainSurface are never lost and can be created here.  Vertex
0272: //       shaders and pixel shaders can also be created here as they are not
0273: //       lost on Reset().
0274: //-----------------------------------------------------------------------------
0275: HRESULT CAppForm::InitDeviceObjects()
0276: {
0277:     // TODO: create device objects
0278:     HRESULT hr;
0279: 
0280:     // Initialize the font
0281:     HDC hDC = ::GetDC( NULL );
0282:     int nHeight = -MulDiv( 12, GetDeviceCaps(hDC, LOGPIXELSY), 72 );
0283:     ::ReleaseDC( NULL, hDC );
0284:     if( FAILED( hr = D3DXCreateFont( m_pd3dDevice, nHeight, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, 
0285:                          OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, 
0286:                          TEXT("Arial"), &m_pFont ) ) )
0287:         return DXTRACE_ERR( "D3DXCreateFont", hr );
0288:     return S_OK;
0289: }
0290: 
0291: 
0292: 
0293: 
0294: //-----------------------------------------------------------------------------
0295: // Name: CAppForm::RestoreDeviceObjects()
0296: // Desc: Paired with InvalidateDeviceObjects()
0297: //       The device exists, but may have just been Reset().  Resources in
0298: //       D3DPOOL_DEFAULT and any other device state that persists during
0299: //       rendering should be set here.  Render states, matrices, textures,
0300: //       etc., that don't change during rendering can be set once here to
0301: //       avoid redundant state setting during Render() or FrameMove().
0302: //-----------------------------------------------------------------------------
0303: HRESULT CAppForm::RestoreDeviceObjects()
0304: {
0305:     // TODO: setup render states
0306: 
0307:     // Setup a material
0308:     D3DMATERIAL9 mtrl;
0309:     D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
0310:     m_pd3dDevice->SetMaterial( &mtrl );
0311: 
0312:     // Set up the textures
0313:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
0314:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0315:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0316:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
0317:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
0318:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
0319:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0320:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0321: 
0322:     // Set miscellaneous render states
0323:     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   FALSE );
0324:     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
0325:     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE );
0326:     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,        0x000F0F0F );
0327: 
0328:     // Set the world matrix
0329:     D3DXMATRIX matIdentity;
0330:     D3DXMatrixIdentity( &matIdentity );
0331:     m_pd3dDevice->SetTransform( D3DTS_WORLD,  &matIdentity );
0332: 
0333:     // Set up our view matrix. A view matrix can be defined given an eye point,
0334:     // a point to lookat, and a direction for which way is up. Here, we set the
0335:     // eye five units back along the z-axis and up three units, look at the
0336:     // origin, and define "up" to be in the y-direction.
0337:     D3DXMATRIX matView;
0338:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0339:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0340:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0341:     D3DXMatrixLookAtLH( &matView, &vFromPt, &vLookatPt, &vUpVec );
0342:     m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
0343: 
0344:     // Set the projection matrix
0345:     D3DXMATRIX matProj;
0346:     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0347:     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0348:     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
0349: 
0350:     // Set up lighting states
0351:     D3DLIGHT9 light;
0352:     D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, -1.0f, -1.0f, 2.0f );
0353:     m_pd3dDevice->SetLight( 0, &light );
0354:     m_pd3dDevice->LightEnable( 0, TRUE );
0355:     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
0356: 
0357:     if( m_pFont )
0358:         m_pFont->OnResetDevice();
0359: 
0360:     return S_OK;
0361: }
0362: 
0363: 
0364: 
0365: 
0366: //-----------------------------------------------------------------------------
0367: // Name: CAppForm::FrameMove()
0368: // Desc: Called once per frame, the call is the entry point for animating
0369: //       the scene.
0370: //-----------------------------------------------------------------------------
0371: HRESULT CAppForm::FrameMove()
0372: {
0373:     // TODO: update world
0374: 
0375:     // Update user input state
0376:     UpdateInput( &m_UserInput );
0377: 
0378:     // Update the world state according to user input
0379:     D3DXMATRIX matWorld;
0380:     D3DXMATRIX matRotY;
0381:     D3DXMATRIX matRotX;
0382: 
0383:     if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0384:         m_fWorldRotY += m_fElapsedTime;
0385:     else if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0386:         m_fWorldRotY -= m_fElapsedTime;
0387: 
0388:     if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0389:         m_fWorldRotX += m_fElapsedTime;
0390:     else if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0391:         m_fWorldRotX -= m_fElapsedTime;
0392: 
0393:     D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0394:     D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0395: 
0396:     D3DXMatrixMultiply( &matWorld, &matRotX, &matRotY );
0397:     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
0398: 
0399:     return S_OK;
0400: }
0401: 
0402: 
0403: 
0404: 
0405: //-----------------------------------------------------------------------------
0406: // Name: UpdateInput()
0407: // Desc: Update the user input.  Called once per frame 
0408: //-----------------------------------------------------------------------------
0409: void CAppForm::UpdateInput( UserInput* pUserInput )
0410: {
0411:     pUserInput->bRotateUp    = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0412:     pUserInput->bRotateDown  = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0413:     pUserInput->bRotateLeft  = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0414:     pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0415: }
0416: 
0417: 
0418: 
0419: 
0420: //-----------------------------------------------------------------------------
0421: // Name: CAppForm::Render()
0422: // Desc: Called once per frame, the call is the entry point for 3d
0423: //       rendering. This function sets up render states, clears the
0424: //       viewport, and renders the scene.
0425: //-----------------------------------------------------------------------------
0426: HRESULT CAppForm::Render()
0427: {
0428:     // Clear the viewport
0429:     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0430:                          0x000000ff, 1.0f, 0L );
0431: 
0432:     // Begin the scene
0433:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0434:     {
0435:         // TODO: render world
0436:         
0437:         // Render stats and help text  
0438:         RenderText();
0439: 
0440:         // End the scene.
0441:         m_pd3dDevice->EndScene();
0442:     }
0443: 
0444:     return S_OK;
0445: }
0446: 
0447: 
0448: 
0449: 
0450: //-----------------------------------------------------------------------------
0451: // Name: RenderText()
0452: // Desc: Renders stats and help text to the scene.
0453: //-----------------------------------------------------------------------------
0454: HRESULT CAppForm::RenderText()
0455: {
0456:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
0457:     TCHAR szMsg[MAX_PATH] = TEXT("");
0458:     RECT rct;
0459:     ZeroMemory( &rct, sizeof(rct) );       
0460: 
0461:     rct.left   = 2;
0462:     rct.right  = m_d3dsdBackBuffer.Width - 20;
0463: 
0464:     // Output display stats
0465:     INT nNextLine = 40; 
0466: 
0467:     lstrcpy( szMsg, m_strDeviceStats );
0468:     nNextLine -= 20; rct.top = nNextLine; rct.bottom = rct.top + 20;    
0469:     m_pFont->DrawText( NULL, szMsg, -1, &rct, DT_NOCLIP, fontColor );
0470: 
0471:     lstrcpy( szMsg, m_strFrameStats );
0472:     nNextLine -= 20; rct.top = nNextLine; rct.bottom = rct.top + 20;    
0473:     m_pFont->DrawText( NULL, szMsg, -1, &rct, DT_NOCLIP, fontColor );
0474: #if 0
0475:     // Output statistics & help
0476:     nNextLine = m_d3dsdBackBuffer.Height; 
0477:     wsprintf( szMsg, TEXT("Arrow keys: Up=%d Down=%d Left=%d Right=%d"), 
0478:               m_UserInput.bRotateUp, m_UserInput.bRotateDown, m_UserInput.bRotateLeft, m_UserInput.bRotateRight );
0479:     nNextLine -= 20; rct.top = nNextLine; rct.bottom = rct.top + 20;    
0480:     m_pFont->DrawText( NULL, szMsg, -1, &rct, DT_NOCLIP, fontColor );
0481:     sprintf( szMsg, TEXT("World State: %0.3f, %0.3f"), 
0482:                     m_fWorldRotX, m_fWorldRotY );
0483:     nNextLine -= 20; rct.top = nNextLine; rct.bottom = rct.top + 20;    
0484:     m_pFont->DrawText( NULL, szMsg, -1, &rct, DT_NOCLIP, fontColor );
0485:     lstrcpy( szMsg, TEXT("Use arrow keys to update input") );
0486:     nNextLine -= 20; rct.top = nNextLine; rct.bottom = rct.top + 20;    
0487:     m_pFont->DrawText( NULL, szMsg, -1, &rct, DT_NOCLIP, fontColor );
0488:     lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
0489:     nNextLine -= 20; rct.top = nNextLine; rct.bottom = rct.top + 20;    
0490:     m_pFont->DrawText( NULL, szMsg, -1, &rct, DT_NOCLIP, fontColor );
0491: #endif
0492:     return S_OK;
0493: }
0494: 
0495: 
0496: 
0497: 
0498: //-----------------------------------------------------------------------------
0499: // Name: DoDataExchange()
0500: // Desc: DDX/DDV support
0501: //-----------------------------------------------------------------------------
0502: void CAppForm::DoDataExchange(CDataExchange* pDX)
0503: {
0504:     CFormView::DoDataExchange(pDX);
0505:     //{{AFX_DATA_MAP(CAppForm)
0506:     //}}AFX_DATA_MAP
0507:     DDX_Check(pDX, IDC_CHECK_RENDERTARGET, m_bCheckRenderTarget);
0508:     DDX_Check(pDX, IDC_CHECK_DEPTHSTENCIL, m_bCheckDepthStencil);
0509:     DDX_Check(pDX, IDC_CHECK_DYNAMIC, m_bCheckDynamic);
0510:     DDX_Check(pDX, IDC_CHECK_AUTOGENMIPMAP, m_bCheckAutoGenMipmap);
0511:     DDX_Check(pDX, IDC_CHECK_DMAP, m_bCheckDMap);
0512:     DDX_CBIndex(pDX, IDC_COMBO_RESOURCE_TYPE, m_iResourceType);
0513: }
0514: 
0515: 
0516: 
0517: 
0518: //-----------------------------------------------------------------------------
0519: // Name: WindowProc()
0520: // Desc: 
0521: //-----------------------------------------------------------------------------
0522: LRESULT CAppForm::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
0523: {
0524:     return CFormView ::WindowProc(message, wParam, lParam);
0525: }
0526: 
0527: 
0528: 
0529: 
0530: //-----------------------------------------------------------------------------
0531: // Name: OnChangeDevice()
0532: // Desc: Needed to enable dlg menu item 
0533: //-----------------------------------------------------------------------------
0534: void CAppFrameWnd::OnChangeDevice() 
0535: {
0536:     g_AppFormView->OnChangeDevice();
0537: }
0538: 
0539: 
0540: 
0541: 
0542: //-----------------------------------------------------------------------------
0543: // Name: CAppForm::InvalidateDeviceObjects()
0544: // Desc: Invalidates device objects.  Paired with RestoreDeviceObjects()
0545: //-----------------------------------------------------------------------------
0546: HRESULT CAppForm::InvalidateDeviceObjects()
0547: {
0548:     // TODO: Cleanup any objects created in RestoreDeviceObjects()
0549:     if( m_pFont )
0550:         m_pFont->OnLostDevice();
0551: 
0552:     return S_OK;
0553: }
0554: 
0555: 
0556: 
0557: 
0558: //-----------------------------------------------------------------------------
0559: // Name: CAppForm::DeleteDeviceObjects()
0560: // Desc: Paired with InitDeviceObjects()
0561: //       Called when the app is exiting, or the device is being changed,
0562: //       this function deletes any device dependent objects.  
0563: //-----------------------------------------------------------------------------
0564: HRESULT CAppForm::DeleteDeviceObjects()
0565: {
0566:     // TODO: Cleanup any objects created in InitDeviceObjects()
0567:     SAFE_RELEASE( m_pFont );
0568: 
0569:     return S_OK;
0570: }
0571: 
0572: 
0573: 
0574: 
0575: //-----------------------------------------------------------------------------
0576: // Name: CAppForm::FinalCleanup()
0577: // Desc: Paired with OneTimeSceneInit()
0578: //       Called before the app exits, this function gives the app the chance
0579: //       to cleanup after itself.
0580: //-----------------------------------------------------------------------------
0581: HRESULT CAppForm::FinalCleanup()
0582: {
0583:     // TODO: Perform any final cleanup needed
0584:     return S_OK;
0585: }
0586: 
0587: 
0588: 
0589: 
0590: //-----------------------------------------------------------------------------
0591: // Name: InitInstance()
0592: // Desc: This is the main entry point for the application. The MFC window stuff
0593: //       is initialized here. See also the main initialization routine for the
0594: //       CAppForm class, which is called indirectly from here.
0595: //-----------------------------------------------------------------------------
0596: BOOL CApp::InitInstance()
0597: {
0598:     // Asscociate the MFC app with the frame window and doc/view classes
0599:     AddDocTemplate( new CSingleDocTemplate( IDR_MAINFRAME,
0600:                                             RUNTIME_CLASS(CAppDoc),
0601:                                             RUNTIME_CLASS(CAppFrameWnd),
0602:                                             RUNTIME_CLASS(CAppForm) ) );
0603: 
0604:     // Dispatch commands specified on the command line (req'd by MFC). This
0605:     // also initializes the the CAppDoc, CAppFrameWnd, and CAppForm classes.
0606:     CCommandLineInfo cmdInfo;
0607:     ParseCommandLine( cmdInfo );
0608:     if( !ProcessShellCommand( cmdInfo ) )
0609:         return FALSE;
0610: 
0611:     if( !g_AppFormView->IsReady() )
0612:         return FALSE;
0613: 
0614:     g_AppFormView->GetParentFrame()->RecalcLayout();
0615:     g_AppFormView->ResizeParentToFit( FALSE ); 
0616:     
0617:     m_pMainWnd->SetWindowText( g_strAppTitle );
0618:     m_pMainWnd->UpdateWindow();
0619: 
0620:     return TRUE;
0621: }
0622: 
0623: 
0624: 
0625: 
0626: //-----------------------------------------------------------------------------
0627: // Name: LoadFrame()
0628: // Desc: Uses idle time to render the 3D scene.
0629: //-----------------------------------------------------------------------------
0630: BOOL CAppFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle, CWnd* pParentWnd, CCreateContext* pContext) 
0631: {
0632:     BOOL bResult = CFrameWnd::LoadFrame(nIDResource, dwDefaultStyle, pParentWnd, pContext);
0633:     
0634:     LoadAccelTable( MAKEINTRESOURCE(IDR_MAIN_ACCEL) );
0635: 
0636:     return bResult;
0637: }
0638: 
0639: 
0640: 
0641: 
0642: //-----------------------------------------------------------------------------
0643: // Name: OnIdle()
0644: // Desc: Uses idle time to render the 3D scene.
0645: //-----------------------------------------------------------------------------
0646: BOOL CApp::OnIdle( LONG )
0647: {
0648:     // Do not render if the app is minimized
0649:     if( m_pMainWnd->IsIconic() )
0650:         return FALSE;
0651: 
0652:     TCHAR strStatsPrev[200];
0653: 
0654:     lstrcpy(strStatsPrev, g_AppFormView->PstrFrameStats());
0655: 
0656:     // Update and render a frame
0657:     if( g_AppFormView->IsReady() )
0658:     {
0659:         g_AppFormView->CheckForLostFullscreen();
0660:         g_AppFormView->RenderScene();
0661:     }
0662: 
0663:     // Keep requesting more idle time
0664:     return TRUE;
0665: }
0666: 
0667: 
0668: 
0669: 
0670: //-----------------------------------------------------------------------------
0671: // Name: PreCreateWindow()
0672: // Desc: Change the window style (so it cannot maximize or be sized) before
0673: //       the main frame window is created.
0674: //-----------------------------------------------------------------------------
0675: BOOL CAppFrameWnd::PreCreateWindow( CREATESTRUCT& cs )
0676: {
0677:     cs.style = WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX;
0678: 
0679:     return CFrameWnd::PreCreateWindow( cs );
0680: }
0681: 
0682: 
0683: 
0684: 
0685: //-----------------------------------------------------------------------------
0686: // Name: ~CAppForm()
0687: // Desc: Destructor for the dialog resource form. Shuts down the app
0688: //-----------------------------------------------------------------------------
0689: CAppForm::~CAppForm()
0690: {
0691:     Cleanup3DEnvironment();
0692:     SAFE_RELEASE( m_pD3D );
0693:     FinalCleanup();
0694: 
0695:     SAFE_DELETE_ARRAY(m_pbResult);
0696: }
0697: 
0698: 
0699: 
0700: 
0701: //-----------------------------------------------------------------------------
0702: // Name: OnToggleFullScreen()
0703: // Desc: Called when user toggles the fullscreen mode
0704: //-----------------------------------------------------------------------------
0705: void CAppForm::OnToggleFullScreen()
0706: {
0707:     ToggleFullscreen();
0708: }
0709: 
0710: 
0711: 
0712: 
0713: //-----------------------------------------------------------------------------
0714: // Name: OnChangeDevice()
0715: // Desc: Use hit the "Change Device.." button. Display the dialog for the user
0716: //       to select a new device/mode, and call Change3DEnvironment to
0717: //       use the new device/mode.
0718: //-----------------------------------------------------------------------------
0719: VOID CAppForm::OnChangeDevice()
0720: {
0721:     Pause(true);
0722: 
0723:     UserSelectNewDevice();
0724: 
0725:     // Update UI
0726:     UpdateUIForDeviceCapabilites();
0727: 
0728:     Pause(false);
0729: }
0730: 
0731: 
0732: 
0733: 
0734: //-----------------------------------------------------------------------------
0735: // Name: AdjustWindowForChange()
0736: // Desc: Adjusts the window properties for windowed or fullscreen mode
0737: //-----------------------------------------------------------------------------
0738: HRESULT CAppForm::AdjustWindowForChange()
0739: {
0740:     if( m_bWindowed )
0741:     {
0742:         ::ShowWindow( m_hwndRenderFullScreen, SW_HIDE );
0743:         CD3DApplication::m_hWnd = m_hwndRenderWindow;
0744:     }
0745:     else
0746:     {
0747:         if( ::IsIconic( m_hwndRenderFullScreen ) )
0748:             ::ShowWindow( m_hwndRenderFullScreen, SW_RESTORE );
0749:         ::ShowWindow( m_hwndRenderFullScreen, SW_SHOW );
0750:         CD3DApplication::m_hWnd = m_hwndRenderFullScreen;
0751:     }
0752: 
0753:     return S_OK;
0754: }
0755: 
0756: 
0757: 
0758: 
0759: //-----------------------------------------------------------------------------
0760: // Name: FullScreenWndProc()
0761: // Desc: The WndProc funtion used when the app is in fullscreen mode. This is
0762: //       needed simply to trap the ESC key.
0763: //-----------------------------------------------------------------------------
0764: LRESULT CALLBACK FullScreenWndProc( HWND hWnd, UINT msg, WPARAM wParam,
0765:                                     LPARAM lParam )
0766: {
0767:     if( msg == WM_CLOSE )
0768:     {
0769:         // User wants to exit, so go back to windowed mode and exit for real
0770:         g_AppFormView->OnToggleFullScreen();
0771:         g_App.GetMainWnd()->PostMessage( WM_CLOSE, 0, 0 );
0772:     }
0773:     else if( msg == WM_SETCURSOR )
0774:     {
0775:         SetCursor( NULL );
0776:     }
0777:     else if( msg == WM_KEYUP && wParam == VK_ESCAPE )
0778:     {
0779:         // User wants to leave fullscreen mode
0780:         g_AppFormView->OnToggleFullScreen();
0781:     }
0782: 
0783:     return DefWindowProc( hWnd, msg, wParam, lParam );
0784: }
0785: 
0786: 
0787: 
0788: 
0789: //-----------------------------------------------------------------------------
0790: // Name: CheckForLostFullscreen()
0791: // Desc: If fullscreen and device was lost (probably due to alt-tab), 
0792: //       automatically switch to windowed mode
0793: //-----------------------------------------------------------------------------
0794: HRESULT CAppForm::CheckForLostFullscreen()
0795: {
0796:     HRESULT hr;
0797: 
0798:     if( m_bWindowed )
0799:         return S_OK;
0800: 
0801:     if( FAILED( hr = m_pd3dDevice->TestCooperativeLevel() ) )
0802:         ForceWindowed();
0803: 
0804:     return S_OK;
0805: }
0806: 
0807: 
0808: 
0809: 
0810: //-----------------------------------------------------------------------------
0811: // Name: UpdateUIForDeviceCapabilites()
0812: // Desc: Whenever we get a new device, call this function to enable/disable the
0813: //       appropiate UI controls to match the device's capabilities.
0814: //-----------------------------------------------------------------------------
0815: VOID CAppForm::UpdateUIForDeviceCapabilites()
0816: {
0817:     // TODO: Check the capabilities of the device and update the UI as needed
0818:     DWORD dwCaps = m_d3dCaps.RasterCaps;
0819:     UNREFERENCED_PARAMETER( dwCaps );
0820: }
0821: 
0822: 
0823: 
0824: 
0825: //-----------------------------------------------------------------------------
0826: // Name: OnInitialUpdate()
0827: // Desc: When the AppForm object is created, this function is called to
0828: //       initialize it. Here we getting access ptrs to some of the controls,
0829: //       and setting the initial state of some of them as well.
0830: //-----------------------------------------------------------------------------
0831: VOID CAppForm::OnInitialUpdate()
0832: {
0833:     // Update the UI
0834:     CFormView::OnInitialUpdate();
0835: 
0836:     // Get the top level parent hwnd
0837:     m_hWndTopLevelParent = GetTopLevelParent()->GetSafeHwnd();
0838: 
0839:     // Save static reference to the render window
0840:     m_hwndRenderWindow = GetDlgItem(IDC_RENDERVIEW)->GetSafeHwnd();
0841: 
0842:     // Register a class for a fullscreen window
0843:     WNDCLASS wndClass = { CS_HREDRAW | CS_VREDRAW, FullScreenWndProc, 0, 0, NULL,
0844:                           NULL, NULL, (HBRUSH)GetStockObject(WHITE_BRUSH), NULL,
0845:                           _T("Fullscreen Window") };
0846:     RegisterClass( &wndClass );
0847: 
0848:     // We create the fullscreen window (not visible) at startup, so it can
0849:     // be the focus window.  The focus window can only be set at CreateDevice
0850:     // time, not in a Reset, so ToggleFullscreen wouldn't work unless we have
0851:     // already set up the fullscreen focus window.
0852:     m_hwndRenderFullScreen = CreateWindow( _T("Fullscreen Window"), NULL,
0853:                                            WS_POPUP, CW_USEDEFAULT,
0854:                                            CW_USEDEFAULT, 100, 100,
0855:                                            m_hWndTopLevelParent, 0L, NULL, 0L );
0856: 
0857:     // Note that for the MFC samples, the device window and focus window
0858:     // are not the same.
0859:     CD3DApplication::m_hWnd = m_hwndRenderWindow;
0860:     CD3DApplication::m_hWndFocus = m_hwndRenderFullScreen;
0861:     CD3DApplication::Create( AfxGetInstanceHandle() );
0862: 
0863:     // TODO: Update the UI as needed
0864: }
0865: 
0866: 
0867: 
0868: 
0869: 
0870: void CAppForm::OnBnClickedButtonCheckDeviceFormat()
0871: {
0872:     this->UpdateData();
0873: 
0874:     // usage の決定
0875:     DWORD usage = 0;
0876:     if(m_bCheckRenderTarget)    usage |= D3DUSAGE_RENDERTARGET;
0877:     if(m_bCheckDepthStencil)    usage |= D3DUSAGE_DEPTHSTENCIL;
0878:     if(m_bCheckDynamic)         usage |= D3DUSAGE_DYNAMIC;
0879:     if(m_bCheckAutoGenMipmap)   usage |= D3DUSAGE_AUTOGENMIPMAP;
0880:     if(m_bCheckDMap)            usage |= D3DUSAGE_DMAP;
0881: 
0882:     // リソース タイプの決定
0883:     D3DRESOURCETYPE source_type = D3DRTYPE_TEXTURE;
0884:     switch(m_iResourceType){
0885:     case 0: source_type = D3DRTYPE_TEXTURE; break;
0886:     case 1: source_type = D3DRTYPE_VOLUMETEXTURE; break;
0887:     case 2: source_type = D3DRTYPE_CUBETEXTURE; break;
0888:     case 3: source_type = D3DRTYPE_SURFACE; break;
0889:     default: source_type = D3DRTYPE_TEXTURE; break;
0890:     }
0891:     
0892:     // チェック!!
0893:     D3DFORMAT adapterFormat = m_d3dSettings.DisplayMode().Format;
0894: 
0895:     char buf[256];
0896:     char *strResult = new char[m_nFormat*256];
0897:     strResult[0] = '\0';
0898: 
0899:     for(int i = 0; i<m_nFormat; i++){
0900:         HRESULT hr;
0901:         hr = m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
0902:                                         m_d3dCaps.DeviceType,
0903:                                         adapterFormat,
0904:                                         usage,
0905:                                         source_type,
0906:                                         this->m_Formats[i].val );
0907:         // 結果の解析
0908:         m_pbResult[i] = FALSE;
0909:         if(usage & D3DUSAGE_AUTOGENMIPMAP){
0910:             // こいつだけ返り値が特別
0911:             if(hr!=D3DOK_NOAUTOGEN){
0912:                 m_pbResult[i] = TRUE;
0913:             }
0914:         }else{
0915:             // 普通のやつ
0916:             if(SUCCEEDED(hr)){
0917:                 m_pbResult[i] = TRUE;
0918:             }
0919:         }
0920:         // 結果表示
0921:         wsprintf (buf, "%s %s\r\n", m_pbResult[i]?"":"×", this->m_Formats[i].text);
0922:         lstrcat(strResult, buf);
0923:     }
0924:     
0925:     CEdit *Edit = (CEdit *) GetDlgItem (IDC_EDIT_RESULT);
0926:     Edit->SetWindowText (strResult);
0927: 
0928:     SAFE_DELETE_ARRAY(strResult);
0929: }
0930: