afApplication.cpp
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:29k
源码类别:

其他游戏

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // File:afApplication.cpp
  3. //
  4. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  5. //-----------------------------------------------------------------------------
  6. #pragma warning(disable:4305)
  7. #include "stdafx.h"
  8. #include "afApplication.h"
  9. #include "afResourceManager.h"
  10. #include "af3DObject.h"
  11. #include "afTerrain.h"
  12. #include "afTime.h"
  13. //-----------------------------------------------------------------------------
  14. // Globals
  15. //-----------------------------------------------------------------------------
  16. // This GUID must be unique for every game, and the same for 
  17. // every instance of this app.  // {769FCCA3-150E-4514-A9E2-E28449A7C401}
  18. // The GUID allows DirectInput to remember input settings
  19. GUID g_guidApp = { 0x769fcca3, 0x150e, 0x4514, { 0xa9, 0xe2, 0xe2, 0x84, 0x49, 0xa7, 0xc4, 0x01 } };
  20. afApplication*    g_pApp       = NULL; 
  21. //-----------------------------------------------------------------------------
  22. // Name: WinMain()
  23. // Desc: Application entry point
  24. //-----------------------------------------------------------------------------
  25. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow )
  26. {
  27.     afApplication app;
  28.     srand( timeGetTime() );
  29.     app.m_hInst=hInstance;
  30.     if( FAILED( app.Create( hInstance ) ) )
  31.         return 0;
  32.     return app.Run();
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Name: CMyApplication()
  36. // Desc: Constructor
  37. //-----------------------------------------------------------------------------
  38. afApplication::afApplication()
  39. {
  40.     g_pApp                  = this;
  41.     m_fTime                 = 0.0f;
  42.     m_strAppName            = _T("AirCraft");
  43.     m_hWndMain              = NULL;
  44. m_pInput = NULL;
  45. m_pd3dDevice = NULL;
  46. m_pTerrain = NULL;
  47.     m_dwScreenWidth         = 1024;   
  48.     m_dwScreenHeight        = 768;
  49.     m_bFullScreen           = FALSE; 
  50.     m_bIsActive             = FALSE; 
  51.     m_bDisplayReady         = FALSE; 
  52.     m_bMouseVisible         = FALSE;  
  53.     m_dwAppState            = APPSTATE_LOADSPLASH;            
  54.     m_fFPS                  = 0.0f; 
  55.     m_bPaused               = FALSE;    
  56.     m_bDebugMode            = FALSE;
  57.     m_bWireMode             = FALSE;   
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Name: Create()
  61. // Desc: Creates the window
  62. //-----------------------------------------------------------------------------
  63. HRESULT afApplication::Create( HINSTANCE hInstance )
  64. {
  65.     // Register the window class
  66.     WNDCLASS wndClass = { CS_DBLCLKS, StaticMsgProc, 0, 0, hInstance,
  67.                           0,
  68.                           LoadCursor( NULL, IDC_ARROW ),
  69.                           (HBRUSH)GetStockObject( BLACK_BRUSH ),
  70.                           NULL, TEXT("Donuts4Class") };
  71.     RegisterClass( &wndClass );
  72.     
  73.     // Create our main window
  74.     m_hWndMain = CreateWindowEx( 0, TEXT("Donuts4Class"), m_strAppName,
  75.                                  WS_POPUP|WS_CAPTION|WS_SYSMENU,
  76.                                  0, 0, 640, 480, NULL, NULL,
  77.                                  hInstance, NULL );
  78.     if( NULL == m_hWndMain )
  79.         return E_FAIL;
  80.     UpdateWindow( m_hWndMain );
  81.     SetCursor( NULL );
  82.     SetFocus( m_hWndMain );
  83.     SetForegroundWindow( m_hWndMain );
  84.     // Save window properties
  85.     m_dwWindowStyle = GetWindowLong( m_hWndMain, GWL_STYLE );
  86.     GetWindowRect( m_hWndMain, &m_rcWindowBounds );
  87.     GetClientRect( m_hWndMain, &m_rcWindowClient );
  88.     // Create the game objects (display objects, sounds, input devices,
  89.     // menus, etc.)
  90.     if( FAILED( OneTimeSceneInit( m_hWndMain ) ) )
  91.     {
  92.         DestroyWindow( m_hWndMain );
  93.         return E_FAIL;
  94.     }
  95.     return S_OK;
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Name: Run()
  99. // Desc: Handles the message loop and calls UpdateScene() and Render() when
  100. //       idle.
  101. //-----------------------------------------------------------------------------
  102. INT afApplication::Run()
  103. {
  104.     // Now we're ready to recieve and process Windows messages.
  105.     BOOL bGotMsg;
  106.     MSG  msg;
  107.     PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );
  108.     while( WM_QUIT != msg.message  )
  109.     {
  110.         // Use PeekMessage() if the app is active, so we can use idle time to
  111.         // render the scene. Else, use GetMessage() to avoid eating CPU time.
  112.         if( m_bIsActive )
  113.             bGotMsg = PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE );
  114.         else
  115.             bGotMsg = GetMessage( &msg, NULL, 0U, 0U );
  116.         if( bGotMsg )
  117.         {
  118.             // Translate and dispatch the message
  119.             TranslateMessage( &msg );
  120.             DispatchMessage( &msg );
  121.         }
  122.         else
  123.         {
  124.             // Render a frame during idle time (no messages are waiting)
  125.             if( m_bDisplayReady )
  126.             {                
  127.                 UpdateScene();
  128.                 RenderScene();                
  129.             }
  130.         }
  131.     }
  132.     return (int)msg.wParam;
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Name: StaticMsgProc()
  136. // Desc: Static msg handler which passes messages to the application class.
  137. //-----------------------------------------------------------------------------
  138. LRESULT CALLBACK StaticMsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  139. {
  140.     return g_pApp->MsgProc( hWnd, uMsg, wParam, lParam );
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Name: MsgProc()
  144. // Desc: Callback for all Windows messages
  145. //-----------------------------------------------------------------------------
  146. LRESULT afApplication::MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  147. {
  148. if(m_pInput)
  149. m_pInput->processWindowMsg(hWnd,msg,wParam,lParam);
  150.     switch( msg )
  151.     {
  152.         case WM_ACTIVATEAPP:
  153.             m_bIsActive = (BOOL)wParam;            
  154.             break;
  155.         case WM_GETMINMAXINFO:
  156.             ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 320;
  157.             ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 200;
  158.             break;
  159.         case WM_SETCURSOR:
  160.             if( !m_bMouseVisible )
  161.                 SetCursor( NULL );
  162.             else
  163.                 SetCursor( LoadCursor( NULL, IDC_ARROW ) );
  164.             return TRUE;
  165.         case WM_SYSCOMMAND:
  166.             // Prevent moving/sizing and power loss
  167.             switch( wParam )
  168.             {
  169.                 case SC_MOVE:
  170.                 case SC_SIZE:
  171.                 case SC_MAXIMIZE:
  172.                 case SC_KEYMENU:
  173.                 case SC_MONITORPOWER:
  174.                         return 1;
  175.             }
  176.             break;
  177.         case WM_SYSKEYDOWN:
  178.             // Handle Alt+Enter to do mode-switching
  179.             if( VK_RETURN == wParam )
  180.             {
  181.                 SwitchDisplayModes( !m_bFullScreen, m_dwScreenWidth,
  182.                                     m_dwScreenHeight );
  183.             }
  184.             break;
  185.         case WM_LBUTTONDOWN:
  186.         case WM_RBUTTONDOWN:
  187.         case WM_KEYDOWN:           
  188.               return 0;
  189.         case WM_KEYUP:            
  190.             return 0;
  191.         case WM_PAINT:
  192.             if( m_bDisplayReady )
  193.             {
  194.                 switch( m_dwAppState )
  195.                 {
  196.                     case APPSTATE_DISPLAYSPLASH:
  197.                     {
  198.                         RenderSplash();
  199.                         m_pd3dDevice->Present( 0, 0, 0, 0 );
  200.                         break;
  201.                     }
  202.                     case APPSTATE_ACTIVE:
  203.                     {
  204.                         RenderFrame();
  205.                         m_pd3dDevice->Present( 0, 0, 0, 0 );
  206.                         break;
  207.                     }
  208.                 }
  209.             }
  210.             break;
  211.         case WM_DESTROY:           
  212.             FinalCleanup();
  213.             PostQuitMessage( 0 );
  214.             m_bDisplayReady = FALSE;
  215.             break;
  216.     }
  217.     return DefWindowProc( hWnd, msg, wParam, lParam );
  218. }
  219. //-----------------------------------------------------------------------------
  220. // Name: OneTimeSceneInit()
  221. // Desc:
  222. //-----------------------------------------------------------------------------
  223. HRESULT afApplication::OneTimeSceneInit( HWND hWnd )
  224. {
  225. afLog::init("log.txt");
  226. afSettings::init();
  227. m_pInput=new afInput();
  228. if(m_pInput)
  229. m_pInput->init(afInput::KEYBOARD|afInput::MOUSE,m_hInst,hWnd);
  230. m_bFullScreen=false; //FullScreen
  231. if(!m_bFullScreen )
  232.         ShowWindow( m_hWndMain, SW_SHOW );
  233. // Initialize the display stuff
  234. HRESULT hr;
  235.     if( FAILED( hr = InitDeviceObjects( hWnd ) ) )
  236.         return hr;
  237.      return S_OK;
  238. }
  239. //-----------------------------------------------------------------------------
  240. // Name: InitDeviceObjects()
  241. // Desc:
  242. //-----------------------------------------------------------------------------
  243. HRESULT afApplication::InitDeviceObjects( HWND hWnd )
  244. {
  245. HRESULT hr;
  246.     // Construct a new display
  247.     LPDIRECT3D9 pD3D = Direct3DCreate9( D3D_SDK_VERSION );
  248.     if( NULL == pD3D )
  249.     {
  250.         CleanupAndDisplayError( AFERR_NODIRECT3D, NULL, NULL );
  251.         return DXTRACE_ERR( TEXT("Direct3DCreate9"), E_FAIL );
  252.     }
  253.     // Get the current desktop format
  254.     pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &m_DesktopMode );
  255.     const D3DFORMAT fmtFullscreenArray[] = 
  256.     {
  257.         D3DFMT_A8R8G8B8,
  258.         D3DFMT_X8R8G8B8,
  259.         D3DFMT_X1R5G5B5,
  260.         D3DFMT_A1R5G5B5,
  261.         D3DFMT_R5G6B5,
  262.     };
  263.     const INT numFullscreenFmts = sizeof(fmtFullscreenArray) / sizeof(fmtFullscreenArray[0]);
  264.     INT iFmt;
  265.     // Find a pixel format that will be good for fullscreen back buffers
  266.     for( iFmt = 0; iFmt < numFullscreenFmts; iFmt++ )
  267.     {
  268.         if( SUCCEEDED( pD3D->CheckDeviceType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 
  269.             fmtFullscreenArray[iFmt], fmtFullscreenArray[iFmt], FALSE ) ) )
  270.         {
  271.             m_d3dfmtFullscreen = fmtFullscreenArray[iFmt];
  272.             break;
  273.         }
  274.     }
  275.     const D3DFORMAT fmtTextureArray[] = 
  276.     {
  277.         D3DFMT_A8R8G8B8,
  278.         D3DFMT_A4R4G4B4,
  279.         D3DFMT_A1R5G5B5,
  280.     };
  281.     const INT numTextureFmts = sizeof(fmtTextureArray) / sizeof(fmtTextureArray[0]);
  282.     // Find a format that is supported as a texture map for the current mode
  283.     for( iFmt = 0; iFmt < numTextureFmts; iFmt++ )
  284.     {
  285.         if( SUCCEEDED( pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 
  286.             m_DesktopMode.Format, 0, D3DRTYPE_TEXTURE, fmtTextureArray[iFmt] ) ) )
  287.         {
  288.             m_d3dfmtTexture = fmtTextureArray[iFmt];
  289.             break;
  290.         }
  291.     }
  292.     // Set up presentation parameters for the display
  293.     ZeroMemory( &m_d3dpp, sizeof(m_d3dpp) );
  294.     m_d3dpp.Windowed         = !m_bFullScreen;
  295.     m_d3dpp.BackBufferCount  = 1;
  296.     m_d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
  297.     m_d3dpp.EnableAutoDepthStencil = TRUE;
  298.     m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  299.     m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  300.     if( m_bFullScreen )
  301.     {
  302.         m_d3dpp.hDeviceWindow    = hWnd;
  303.         m_d3dpp.BackBufferWidth  = m_dwScreenWidth;
  304.         m_d3dpp.BackBufferHeight = m_dwScreenHeight;
  305.         m_d3dpp.BackBufferFormat = m_d3dfmtFullscreen;
  306.     }
  307.     else
  308.     {
  309.         m_d3dpp.BackBufferFormat = m_DesktopMode.Format;
  310.     }
  311.     D3DDEVTYPE dwDevType = D3DDEVTYPE_HAL; 
  312. D3DCAPS9 dcap;
  313. pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&dcap);
  314. DWORD dwBehaviorFlags =D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  315. if(dcap.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  316.     dwBehaviorFlags =D3DCREATE_MIXED_VERTEXPROCESSING;
  317.     // Create the device
  318.     hr = pD3D->CreateDevice(D3DADAPTER_DEFAULT, dwDevType, hWnd,
  319.                              dwBehaviorFlags, &m_d3dpp, &m_pd3dDevice);
  320.     pD3D->Release();
  321.     if(FAILED(hr))
  322.     {
  323. if(hr==D3DERR_INVALIDCALL)
  324. afLog::info("here");
  325.         CleanupAndDisplayError( AFERR_NOD3DDEVICE, NULL, NULL );
  326.         return DXTRACE_ERR( TEXT("pD3D->CreateDevice"), hr );
  327.     }
  328. //Set Correct Device!!!
  329. af3DObject::pd3dDevice=m_pd3dDevice;
  330. afResourceManager::init();
  331.    //******************************************************//
  332.    //Load or Create resource here:Texture,Mesh,Vertex or Index buffer(must be in sysmem),allocate memory//
  333.     m_pTerrain=new afTerrain();
  334. m_pTerrain->setMipmapFilter(afTextureStage::FILTER_POINT);
  335. m_pTerrain->setPVSName("mountain\heightmap");
  336. m_pTerrain->setNumPatches(16,16);
  337. m_pTerrain->setHeightMap(afResourceManager::getRawImage("mountain\heightmap",257,257, afImage::P8));
  338. afTexture* ColorTex,*AlphaTex;
  339. ColorTex=afResourceManager::getTexture("mountain\baseColor");
  340. AlphaTex=afResourceManager::getTexture("mountain\baseLight");
  341. if(ColorTex&&AlphaTex)
  342. m_pTerrain->addPass(ColorTex,32,32,AlphaTex);
  343. ColorTex=afResourceManager::getTexture("mountain\pass1Color");
  344. AlphaTex=afResourceManager::getTexture("mountain\pass1Light");
  345. if(ColorTex&&AlphaTex)
  346. m_pTerrain->addPass(ColorTex,15,15,AlphaTex);
  347. ColorTex=afResourceManager::getTexture("mountain\pass2Color");
  348. AlphaTex=afResourceManager::getTexture("mountain\pass2Light");
  349. if(ColorTex&&AlphaTex)
  350. m_pTerrain->addPass(ColorTex,10,10,AlphaTex);
  351. m_pTerrain->setPatchSize(4000,25000,4000);
  352. m_pTerrain->setMaxError(0.005);
  353. m_pTerrain->setMergePatches(true);
  354. // m_pTerrain->setDesiredFPS(40);
  355. m_pTerrain->build();
  356.     // Create a vextex buffer for the viewport
  357.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4*sizeof(SCREENVERTEX),
  358.                                        D3DUSAGE_WRITEONLY, D3DFVF_SCREENVERTEX,
  359.                                        D3DPOOL_MANAGED, &m_pViewportVB, NULL ) ) )
  360.         return DXTRACE_ERR( TEXT("g_pd3dDevice->CreateVertexBuffer"), hr );
  361.    
  362. if( FAILED( hr = RestoreDeviceObjects() ) )
  363.         return DXTRACE_ERR( TEXT("RestoreDeviceObjects"), hr );
  364.     // The display is now ready
  365.     m_bDisplayReady = TRUE;
  366.     return S_OK;   
  367. }
  368. //-----------------------------------------------------------------------------
  369. // Name: RestoreDeviceObjects()
  370. // Desc:
  371. //-----------------------------------------------------------------------------
  372. HRESULT afApplication::RestoreDeviceObjects()
  373. {  
  374. HWND hWnd = m_hWndMain;
  375.     if( FALSE == m_bFullScreen )
  376.     {
  377.         // If we are still a WS_POPUP window we should convert to a normal app
  378.         // window so we look like a windows app.
  379.         DWORD dwStyle  = GetWindowStyle( hWnd );
  380.         dwStyle &= ~WS_POPUP;
  381.         dwStyle |= WS_CAPTION | WS_MINIMIZEBOX;
  382.         SetWindowLong( hWnd, GWL_STYLE, dwStyle );
  383.         // Set window size
  384.         RECT rc;
  385.         SetRect( &rc, 0, 0, 640, 480 );
  386.         SetWindowPos( hWnd, NULL, 0, 0, rc.right-rc.left, rc.bottom-rc.top,
  387.                       SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
  388.         SetWindowPos( hWnd, HWND_NOTOPMOST, 0, 0, 0, 0,
  389.                       SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
  390.         //  Make sure our window does not hang outside of the work area
  391.         RECT rcWork;
  392.         SystemParametersInfo( SPI_GETWORKAREA, 0, &rcWork, 0 );
  393.         GetWindowRect( hWnd, &rc );
  394.         if( rc.left < rcWork.left ) rc.left = rcWork.left;
  395.         if( rc.top  < rcWork.top )  rc.top  = rcWork.top;
  396.         SetWindowPos( hWnd, NULL, rc.left, rc.top, 0, 0,
  397.                       SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
  398.     }
  399. //****************************************************************************//
  400. //Prepare lose-easy resource:fill buffer;setup the transform;Setup some states//
  401. //****************************************************************************//
  402. // Get viewport dimensions
  403.     D3DVIEWPORT9 vp;
  404.     m_pd3dDevice->GetViewport(&vp);
  405.     FLOAT sx = (FLOAT)vp.Width;
  406.     FLOAT sy = (FLOAT)vp.Height;
  407.     if( m_pViewportVB )
  408.     {
  409.         // Setup dimensions for the viewport covering square
  410.         SCREENVERTEX* v;
  411.         m_pViewportVB->Lock( 0, 0, (void**)&v, 0 );
  412.         v[0].color = v[1].color = v[2].color = v[3].color = 0xFF0000FF;
  413.         v[0].p = D3DXVECTOR4( 0,sy,0.0f,1.0f);
  414.         v[0].tu = 0.0f;  v[0].tv = 1.0f; 
  415.         v[1].p = D3DXVECTOR4( 0, 0,0.0f,1.0f);
  416.         v[1].tu = 0.0f;  v[1].tv = 0.0f; 
  417.         v[2].p = D3DXVECTOR4(sx,sy,0.0f,1.0f);
  418.         v[2].tu = 1.0f;  v[2].tv = 1.0f; 
  419.         v[3].p = D3DXVECTOR4(sx, 0,0.0f,1.0f);
  420.         v[3].tu = 1.0f;  v[3].tv = 0.0f; 
  421.         m_pViewportVB->Unlock();
  422.     }
  423.      
  424. if(m_pTerrain)
  425. m_pTerrain->restoreDeviceObjects();    
  426.     return S_OK;
  427. }
  428. //-----------------------------------------------------------------------------
  429. // Name: UpdateScene()
  430. // Desc:
  431. //-----------------------------------------------------------------------------
  432. HRESULT afApplication::UpdateScene()
  433. {
  434.    // switch( m_dwAppState)  
  435.     FrameMove();
  436.     return S_OK;
  437. }
  438. //-----------------------------------------------------------------------------
  439. // Name: FrameMove()
  440. // Desc:
  441. //-----------------------------------------------------------------------------
  442. VOID afApplication::FrameMove()
  443. {
  444.     m_pInput->update();
  445. if(m_pInput->isKeyDown(afInput::KEY_ESCAPE))
  446. {
  447. PostMessage(m_hWndMain,WM_CLOSE,0,0);
  448. return;
  449. }
  450. afTime::update();
  451. m_Camera.SetProjParams(4.0f/3.0f, 3.14/4, 50.0f, 40000.0f);
  452. D3DXVECTOR3 eyepos(00000,20000,15000),lookat(11000,20000,15000),up(0,1,0);
  453. m_Camera.SetViewParams(eyepos,lookat,up);
  454. m_Camera.SetUpdateVisibility(true);
  455. if(m_pTerrain)
  456. {
  457. m_pTerrain->update();
  458. }
  459. }
  460. //-----------------------------------------------------------------------------
  461. // Name: UpdateCullInfo()
  462. // Desc: Sets up the frustum planes, endpoints, and center for the frustum
  463. //       defined by a given view matrix and projection matrix.  This info will 
  464. //       be used when culling each object in CullObject().
  465. //-----------------------------------------------------------------------------
  466. VOID afApplication::UpdateCullInfo( CULLINFO* pCullInfo, D3DXMATRIXA16* pMatView, 
  467.                                      D3DXMATRIXA16* pMatProj )
  468. {
  469.     D3DXMATRIXA16 mat;
  470.     D3DXMatrixMultiply( &mat, pMatView, pMatProj );
  471.     D3DXMatrixInverse( &mat, NULL, &mat );
  472.     pCullInfo->vecFrustum[0] = D3DXVECTOR3(-1.0f, -1.0f,  0.0f); // xyz
  473.     pCullInfo->vecFrustum[1] = D3DXVECTOR3( 1.0f, -1.0f,  0.0f); // Xyz
  474.     pCullInfo->vecFrustum[2] = D3DXVECTOR3(-1.0f,  1.0f,  0.0f); // xYz
  475.     pCullInfo->vecFrustum[3] = D3DXVECTOR3( 1.0f,  1.0f,  0.0f); // XYz
  476.     pCullInfo->vecFrustum[4] = D3DXVECTOR3(-1.0f, -1.0f,  1.0f); // xyZ
  477.     pCullInfo->vecFrustum[5] = D3DXVECTOR3( 1.0f, -1.0f,  1.0f); // XyZ
  478.     pCullInfo->vecFrustum[6] = D3DXVECTOR3(-1.0f,  1.0f,  1.0f); // xYZ
  479.     pCullInfo->vecFrustum[7] = D3DXVECTOR3( 1.0f,  1.0f,  1.0f); // XYZ
  480.     for( INT i = 0; i < 8; i++ )
  481.         D3DXVec3TransformCoord( &pCullInfo->vecFrustum[i], &pCullInfo->vecFrustum[i], &mat );
  482.     D3DXPlaneFromPoints( &pCullInfo->planeFrustum[0], &pCullInfo->vecFrustum[0], 
  483.         &pCullInfo->vecFrustum[1], &pCullInfo->vecFrustum[2] ); // Near
  484.     D3DXPlaneFromPoints( &pCullInfo->planeFrustum[1], &pCullInfo->vecFrustum[6], 
  485.         &pCullInfo->vecFrustum[7], &pCullInfo->vecFrustum[5] ); // Far
  486.     D3DXPlaneFromPoints( &pCullInfo->planeFrustum[2], &pCullInfo->vecFrustum[2], 
  487.         &pCullInfo->vecFrustum[6], &pCullInfo->vecFrustum[4] ); // Left
  488.     D3DXPlaneFromPoints( &pCullInfo->planeFrustum[3], &pCullInfo->vecFrustum[7], 
  489.         &pCullInfo->vecFrustum[3], &pCullInfo->vecFrustum[5] ); // Right
  490.     D3DXPlaneFromPoints( &pCullInfo->planeFrustum[4], &pCullInfo->vecFrustum[2], 
  491.         &pCullInfo->vecFrustum[3], &pCullInfo->vecFrustum[6] ); // Top
  492.     D3DXPlaneFromPoints( &pCullInfo->planeFrustum[5], &pCullInfo->vecFrustum[1], 
  493.         &pCullInfo->vecFrustum[0], &pCullInfo->vecFrustum[4] ); // Bottom
  494. }
  495. //-----------------------------------------------------------------------------
  496. // Name: RenderScene()
  497. // Desc:
  498. //-----------------------------------------------------------------------------
  499. HRESULT afApplication::RenderScene()
  500. {
  501.     HRESULT hr;    
  502.     // Test the cooperative level to see if it's okay to render
  503.    if( FAILED( hr = m_pd3dDevice->TestCooperativeLevel() ) )
  504.     {
  505.         // If the device was lost, do not render until we get it back
  506.         if( D3DERR_DEVICELOST == hr )
  507.             return S_OK;
  508.         // Check if the device needs to be resized.
  509.         if( D3DERR_DEVICENOTRESET == hr )
  510.         {
  511.             m_bDisplayReady = FALSE;
  512.             InvalidateDeviceObjects();
  513.             // Resize the device
  514.             if( SUCCEEDED(m_pd3dDevice->Reset( &m_d3dpp ) ) )
  515.             {
  516.                 // Initialize the app's device-dependent objects
  517.                 if( SUCCEEDED( RestoreDeviceObjects() ) )
  518.                 {
  519.                     m_bDisplayReady = TRUE;
  520.                     return S_OK;
  521.                 }
  522.             }
  523.             PostMessage( m_hWndMain, WM_CLOSE, 0, 0 );
  524.         }
  525.         return hr;
  526.     } 
  527. RenderFrame();
  528.     m_pd3dDevice->Present( 0, 0, 0, 0 );
  529.     return S_OK;
  530. }
  531. //-----------------------------------------------------------------------------
  532. // Name: RenderFrame()
  533. // Desc:
  534. //-----------------------------------------------------------------------------
  535. HRESULT afApplication::RenderFrame()
  536. {
  537. m_pd3dDevice->SetTransform(D3DTS_VIEW,m_Camera.GetViewMatrix());
  538. m_pd3dDevice->SetTransform(D3DTS_PROJECTION,m_Camera.GetProjMatrix());
  539. // Begin the scene
  540.     if( SUCCEEDED(m_pd3dDevice->BeginScene()))
  541.     {
  542.         // Clear the display       
  543.         m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(255,0,0,0), 1.0f, 0L );
  544.         if(m_pTerrain)
  545. {
  546. m_pTerrain->render();
  547. }
  548. // m_pd3dDevice->SetFVF( D3DFVF_SCREENVERTEX );
  549.     //  m_pd3dDevice->SetStreamSource( 0, m_pViewportVB, 0, sizeof(SCREENVERTEX) );
  550.     //  m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,0, 2 );
  551.         m_pd3dDevice->EndScene();
  552.     }  
  553.     return S_OK;
  554. }
  555. //-----------------------------------------------------------------------------
  556. // Name: InvalidateDeviceObjects()
  557. // Desc:
  558. //-----------------------------------------------------------------------------
  559. HRESULT afApplication::InvalidateDeviceObjects()
  560. {   
  561. return S_OK;
  562. }
  563. //-----------------------------------------------------------------------------
  564. // Name: DeleteDeviceObjects()
  565. // Desc:
  566. //-----------------------------------------------------------------------------
  567. HRESULT afApplication::DeleteDeviceObjects()
  568. {    
  569. afLog::info("fps %f",afTime::getAverageFPS());
  570. SAFE_RELEASE(m_pViewportVB);
  571. SAFE_RELEASE(m_pd3dDevice);
  572. // SAFE_RELEASE(af3DObject::pd3dDevice);
  573. if(m_pTerrain)
  574. m_pTerrain->deleteDeviceObjects();
  575. SAFE_DELETE(m_pTerrain);
  576. afResourceManager::deinit();
  577.     return S_OK;
  578. }
  579. //-----------------------------------------------------------------------------
  580. // Name: FinalCleanup()
  581. // Desc: Cleanup everything
  582. //-----------------------------------------------------------------------------
  583. VOID afApplication::FinalCleanup()
  584. {
  585.    if(m_pInput)
  586.    {
  587.    m_pInput->cleanup();
  588.    SAFE_DELETE(m_pInput);
  589.    }
  590.    InvalidateDeviceObjects();
  591.    DeleteDeviceObjects(); 
  592.    afProfiler::logResult();
  593. }
  594. //-----------------------------------------------------------------------------
  595. // Name: DarkenScene()
  596. // Desc:
  597. //-----------------------------------------------------------------------------
  598. VOID afApplication::DarkenScene( FLOAT fAmount )
  599. {
  600.     
  601. }
  602. //-----------------------------------------------------------------------------
  603. // Name: RenderSplash()
  604. // Desc:
  605. //-----------------------------------------------------------------------------
  606. VOID afApplication::RenderSplash()
  607. {
  608.     
  609. }
  610. //-----------------------------------------------------------------------------
  611. // Name:
  612. // Desc:
  613. //-----------------------------------------------------------------------------
  614. VOID afApplication::ConstructMenus()
  615. {    
  616.     return;
  617. }
  618. //-----------------------------------------------------------------------------
  619. // Name:
  620. // Desc:
  621. //-----------------------------------------------------------------------------
  622. VOID afApplication::DestroyMenus()
  623. {
  624.     
  625. }
  626. //-----------------------------------------------------------------------------
  627. // Name: UpdateMenus()
  628. // Desc:
  629. //-----------------------------------------------------------------------------
  630. VOID afApplication::UpdateMenus()
  631. {
  632.     
  633. }
  634. //-----------------------------------------------------------------------------
  635. // Name: SwitchDisplayModes()
  636. // Desc:
  637. //-----------------------------------------------------------------------------
  638. HRESULT afApplication::SwitchDisplayModes( BOOL bFullScreen, DWORD dwWidth, DWORD dwHeight )
  639. HRESULT hr;
  640.     if( FALSE==m_bIsActive || FALSE==m_bDisplayReady )
  641.         return S_OK;
  642.     // Check to see if a change was actually requested
  643.     if( bFullScreen )
  644.     {
  645.         if( m_dwScreenWidth==dwWidth && m_dwScreenHeight==dwHeight &&
  646.             m_bFullScreen==bFullScreen )
  647.             return S_OK;
  648.     }
  649.     else
  650.     {
  651. //        if( m_bFullScreen == FALSE )
  652. //            return S_OK;
  653.     }
  654.     
  655.     // Invalidate the old display objects
  656.     m_bDisplayReady = FALSE;
  657.     InvalidateDeviceObjects();
  658.     if( bFullScreen )
  659.     {
  660.         // Set windowed-mode style
  661.         SetWindowLong( m_hWndMain, GWL_STYLE, m_dwWindowStyle|WS_VISIBLE );
  662.     }
  663.     else
  664.     {
  665.         // Set fullscreen-mode style
  666.         SetWindowLong( m_hWndMain, GWL_STYLE, WS_POPUP|WS_SYSMENU|WS_VISIBLE );
  667.     }
  668.     
  669.     // Set up the new presentation paramters
  670.     if( bFullScreen )
  671.     {
  672.         m_d3dpp.Windowed         = FALSE;
  673.         m_d3dpp.hDeviceWindow    = m_hWndMain;
  674.         m_d3dpp.BackBufferWidth  = m_dwScreenWidth  = dwWidth;
  675.         m_d3dpp.BackBufferHeight = m_dwScreenHeight = dwHeight;
  676.         m_d3dpp.BackBufferFormat = m_d3dfmtFullscreen;
  677.     }
  678.     else
  679.     {
  680.         m_d3dpp.Windowed         = TRUE;
  681.         m_d3dpp.hDeviceWindow    = NULL;
  682.         m_d3dpp.BackBufferWidth  = 0L;
  683.         m_d3dpp.BackBufferHeight = 0L;
  684.         m_d3dpp.BackBufferFormat = m_DesktopMode.Format;
  685.     }
  686.     // Reset the device
  687.     if( SUCCEEDED( hr=m_pd3dDevice->Reset( &m_d3dpp ) ) )
  688.     {
  689.         m_bFullScreen   = bFullScreen;
  690.         if( SUCCEEDED( hr = RestoreDeviceObjects() ) )
  691.         {
  692.             m_bDisplayReady = TRUE;
  693.             return S_OK;
  694.         }
  695.     }
  696.     // If we get here, a fatal error occurred
  697.     PostMessage( m_hWndMain, WM_CLOSE, 0, 0 );
  698.     return E_FAIL;
  699. }
  700. //-----------------------------------------------------------------------------
  701. // Name: CleanupAndDisplayError()
  702. // Desc:
  703. //-----------------------------------------------------------------------------
  704. VOID afApplication::CleanupAndDisplayError( DWORD dwError, TCHAR* strArg1, TCHAR* strArg2 )
  705. {
  706.     TCHAR* strDbgOut = NULL;
  707.     TCHAR* strMsgBox = NULL;
  708.     // Cleanup the app
  709.     InvalidateDeviceObjects();
  710.     DeleteDeviceObjects();
  711.     FinalCleanup();
  712.     // Make the cursor visible
  713.     SetCursor( LoadCursor( NULL, IDC_ARROW ) );
  714.     m_bMouseVisible = TRUE;
  715.     // Get the appropriate error strings
  716.     switch( dwError )
  717.     {
  718.         case AFERR_NODIRECT3D:
  719.             strDbgOut = _T("Could not create Direct3Dn");
  720.             strMsgBox = _T("Could not create Direct3D.nn")
  721.                         _T("Please make sure you have the latest DirectXn")
  722.                         _T(".dlls installed on your system.");
  723.             break;
  724.         case AFERR_NOD3DDEVICE:
  725.             strDbgOut = _T("Could not create a Direct3D devicen");
  726.             strMsgBox = _T("Could not create a Direct3D device. Yourn")
  727.                         _T("graphics accelerator is not sufficient ton")
  728.                         _T("run this demo, or your desktop is usingn")
  729.                         _T("a color format that cannot be accelerated byn")
  730.                         _T("your graphics card (try 16-bit mode).");
  731.             break;
  732.         case AFERR_ARTLOADFAILED:            
  733.             strDbgOut = _T("Could not load game artn");
  734.             strMsgBox = _T("Couldn't load game art %s in %s. ")
  735.                         _T("Either your graphics hardware does not have ")
  736.                         _T("sufficient resources, or the DirectX SDK was ")
  737.                         _T("not properly installed.");
  738.             break;
  739.         case AFERR_NOINPUT:
  740.             strDbgOut = _T("Could not create input objectsn");
  741.             strMsgBox = _T("Could not create input objects.");
  742.             break;
  743.     }
  744.     // Output the error strings
  745.     if( strDbgOut && strMsgBox )
  746.     {
  747.         OutputDebugString( strDbgOut );
  748.         TCHAR strMsg[512];
  749.         _sntprintf( strMsg, 512, strMsgBox, strArg1, strArg2 );
  750.         strMsg[511]=0;
  751.         MessageBox( m_hWndMain, strMsg, m_strAppName, MB_OK );
  752.     }
  753. }