afApplication.cpp
资源名称:AirForce.rar [点击查看]
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:29k
源码类别:
其他游戏
开发平台:
Visual C++
- //-----------------------------------------------------------------------------
- // File:afApplication.cpp
- //
- // Copyright (C) Microsoft Corporation. All Rights Reserved.
- //-----------------------------------------------------------------------------
- #pragma warning(disable:4305)
- #include "stdafx.h"
- #include "afApplication.h"
- #include "afResourceManager.h"
- #include "af3DObject.h"
- #include "afTerrain.h"
- #include "afTime.h"
- //-----------------------------------------------------------------------------
- // Globals
- //-----------------------------------------------------------------------------
- // This GUID must be unique for every game, and the same for
- // every instance of this app. // {769FCCA3-150E-4514-A9E2-E28449A7C401}
- // The GUID allows DirectInput to remember input settings
- GUID g_guidApp = { 0x769fcca3, 0x150e, 0x4514, { 0xa9, 0xe2, 0xe2, 0x84, 0x49, 0xa7, 0xc4, 0x01 } };
- afApplication* g_pApp = NULL;
- //-----------------------------------------------------------------------------
- // Name: WinMain()
- // Desc: Application entry point
- //-----------------------------------------------------------------------------
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow )
- {
- afApplication app;
- srand( timeGetTime() );
- app.m_hInst=hInstance;
- if( FAILED( app.Create( hInstance ) ) )
- return 0;
- return app.Run();
- }
- //-----------------------------------------------------------------------------
- // Name: CMyApplication()
- // Desc: Constructor
- //-----------------------------------------------------------------------------
- afApplication::afApplication()
- {
- g_pApp = this;
- m_fTime = 0.0f;
- m_strAppName = _T("AirCraft");
- m_hWndMain = NULL;
- m_pInput = NULL;
- m_pd3dDevice = NULL;
- m_pTerrain = NULL;
- m_dwScreenWidth = 1024;
- m_dwScreenHeight = 768;
- m_bFullScreen = FALSE;
- m_bIsActive = FALSE;
- m_bDisplayReady = FALSE;
- m_bMouseVisible = FALSE;
- m_dwAppState = APPSTATE_LOADSPLASH;
- m_fFPS = 0.0f;
- m_bPaused = FALSE;
- m_bDebugMode = FALSE;
- m_bWireMode = FALSE;
- }
- //-----------------------------------------------------------------------------
- // Name: Create()
- // Desc: Creates the window
- //-----------------------------------------------------------------------------
- HRESULT afApplication::Create( HINSTANCE hInstance )
- {
- // Register the window class
- WNDCLASS wndClass = { CS_DBLCLKS, StaticMsgProc, 0, 0, hInstance,
- 0,
- LoadCursor( NULL, IDC_ARROW ),
- (HBRUSH)GetStockObject( BLACK_BRUSH ),
- NULL, TEXT("Donuts4Class") };
- RegisterClass( &wndClass );
- // Create our main window
- m_hWndMain = CreateWindowEx( 0, TEXT("Donuts4Class"), m_strAppName,
- WS_POPUP|WS_CAPTION|WS_SYSMENU,
- 0, 0, 640, 480, NULL, NULL,
- hInstance, NULL );
- if( NULL == m_hWndMain )
- return E_FAIL;
- UpdateWindow( m_hWndMain );
- SetCursor( NULL );
- SetFocus( m_hWndMain );
- SetForegroundWindow( m_hWndMain );
- // Save window properties
- m_dwWindowStyle = GetWindowLong( m_hWndMain, GWL_STYLE );
- GetWindowRect( m_hWndMain, &m_rcWindowBounds );
- GetClientRect( m_hWndMain, &m_rcWindowClient );
- // Create the game objects (display objects, sounds, input devices,
- // menus, etc.)
- if( FAILED( OneTimeSceneInit( m_hWndMain ) ) )
- {
- DestroyWindow( m_hWndMain );
- return E_FAIL;
- }
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: Run()
- // Desc: Handles the message loop and calls UpdateScene() and Render() when
- // idle.
- //-----------------------------------------------------------------------------
- INT afApplication::Run()
- {
- // Now we're ready to recieve and process Windows messages.
- BOOL bGotMsg;
- MSG msg;
- PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );
- while( WM_QUIT != msg.message )
- {
- // Use PeekMessage() if the app is active, so we can use idle time to
- // render the scene. Else, use GetMessage() to avoid eating CPU time.
- if( m_bIsActive )
- bGotMsg = PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE );
- else
- bGotMsg = GetMessage( &msg, NULL, 0U, 0U );
- if( bGotMsg )
- {
- // Translate and dispatch the message
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- else
- {
- // Render a frame during idle time (no messages are waiting)
- if( m_bDisplayReady )
- {
- UpdateScene();
- RenderScene();
- }
- }
- }
- return (int)msg.wParam;
- }
- //-----------------------------------------------------------------------------
- // Name: StaticMsgProc()
- // Desc: Static msg handler which passes messages to the application class.
- //-----------------------------------------------------------------------------
- LRESULT CALLBACK StaticMsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- return g_pApp->MsgProc( hWnd, uMsg, wParam, lParam );
- }
- //-----------------------------------------------------------------------------
- // Name: MsgProc()
- // Desc: Callback for all Windows messages
- //-----------------------------------------------------------------------------
- LRESULT afApplication::MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
- {
- if(m_pInput)
- m_pInput->processWindowMsg(hWnd,msg,wParam,lParam);
- switch( msg )
- {
- case WM_ACTIVATEAPP:
- m_bIsActive = (BOOL)wParam;
- break;
- case WM_GETMINMAXINFO:
- ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 320;
- ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 200;
- break;
- case WM_SETCURSOR:
- if( !m_bMouseVisible )
- SetCursor( NULL );
- else
- SetCursor( LoadCursor( NULL, IDC_ARROW ) );
- return TRUE;
- case WM_SYSCOMMAND:
- // Prevent moving/sizing and power loss
- switch( wParam )
- {
- case SC_MOVE:
- case SC_SIZE:
- case SC_MAXIMIZE:
- case SC_KEYMENU:
- case SC_MONITORPOWER:
- return 1;
- }
- break;
- case WM_SYSKEYDOWN:
- // Handle Alt+Enter to do mode-switching
- if( VK_RETURN == wParam )
- {
- SwitchDisplayModes( !m_bFullScreen, m_dwScreenWidth,
- m_dwScreenHeight );
- }
- break;
- case WM_LBUTTONDOWN:
- case WM_RBUTTONDOWN:
- case WM_KEYDOWN:
- return 0;
- case WM_KEYUP:
- return 0;
- case WM_PAINT:
- if( m_bDisplayReady )
- {
- switch( m_dwAppState )
- {
- case APPSTATE_DISPLAYSPLASH:
- {
- RenderSplash();
- m_pd3dDevice->Present( 0, 0, 0, 0 );
- break;
- }
- case APPSTATE_ACTIVE:
- {
- RenderFrame();
- m_pd3dDevice->Present( 0, 0, 0, 0 );
- break;
- }
- }
- }
- break;
- case WM_DESTROY:
- FinalCleanup();
- PostQuitMessage( 0 );
- m_bDisplayReady = FALSE;
- break;
- }
- return DefWindowProc( hWnd, msg, wParam, lParam );
- }
- //-----------------------------------------------------------------------------
- // Name: OneTimeSceneInit()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT afApplication::OneTimeSceneInit( HWND hWnd )
- {
- afLog::init("log.txt");
- afSettings::init();
- m_pInput=new afInput();
- if(m_pInput)
- m_pInput->init(afInput::KEYBOARD|afInput::MOUSE,m_hInst,hWnd);
- m_bFullScreen=false; //FullScreen
- if(!m_bFullScreen )
- ShowWindow( m_hWndMain, SW_SHOW );
- // Initialize the display stuff
- HRESULT hr;
- if( FAILED( hr = InitDeviceObjects( hWnd ) ) )
- return hr;
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: InitDeviceObjects()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT afApplication::InitDeviceObjects( HWND hWnd )
- {
- HRESULT hr;
- // Construct a new display
- LPDIRECT3D9 pD3D = Direct3DCreate9( D3D_SDK_VERSION );
- if( NULL == pD3D )
- {
- CleanupAndDisplayError( AFERR_NODIRECT3D, NULL, NULL );
- return DXTRACE_ERR( TEXT("Direct3DCreate9"), E_FAIL );
- }
- // Get the current desktop format
- pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &m_DesktopMode );
- const D3DFORMAT fmtFullscreenArray[] =
- {
- D3DFMT_A8R8G8B8,
- D3DFMT_X8R8G8B8,
- D3DFMT_X1R5G5B5,
- D3DFMT_A1R5G5B5,
- D3DFMT_R5G6B5,
- };
- const INT numFullscreenFmts = sizeof(fmtFullscreenArray) / sizeof(fmtFullscreenArray[0]);
- INT iFmt;
- // Find a pixel format that will be good for fullscreen back buffers
- for( iFmt = 0; iFmt < numFullscreenFmts; iFmt++ )
- {
- if( SUCCEEDED( pD3D->CheckDeviceType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
- fmtFullscreenArray[iFmt], fmtFullscreenArray[iFmt], FALSE ) ) )
- {
- m_d3dfmtFullscreen = fmtFullscreenArray[iFmt];
- break;
- }
- }
- const D3DFORMAT fmtTextureArray[] =
- {
- D3DFMT_A8R8G8B8,
- D3DFMT_A4R4G4B4,
- D3DFMT_A1R5G5B5,
- };
- const INT numTextureFmts = sizeof(fmtTextureArray) / sizeof(fmtTextureArray[0]);
- // Find a format that is supported as a texture map for the current mode
- for( iFmt = 0; iFmt < numTextureFmts; iFmt++ )
- {
- if( SUCCEEDED( pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
- m_DesktopMode.Format, 0, D3DRTYPE_TEXTURE, fmtTextureArray[iFmt] ) ) )
- {
- m_d3dfmtTexture = fmtTextureArray[iFmt];
- break;
- }
- }
- // Set up presentation parameters for the display
- ZeroMemory( &m_d3dpp, sizeof(m_d3dpp) );
- m_d3dpp.Windowed = !m_bFullScreen;
- m_d3dpp.BackBufferCount = 1;
- m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- m_d3dpp.EnableAutoDepthStencil = TRUE;
- m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
- if( m_bFullScreen )
- {
- m_d3dpp.hDeviceWindow = hWnd;
- m_d3dpp.BackBufferWidth = m_dwScreenWidth;
- m_d3dpp.BackBufferHeight = m_dwScreenHeight;
- m_d3dpp.BackBufferFormat = m_d3dfmtFullscreen;
- }
- else
- {
- m_d3dpp.BackBufferFormat = m_DesktopMode.Format;
- }
- D3DDEVTYPE dwDevType = D3DDEVTYPE_HAL;
- D3DCAPS9 dcap;
- pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&dcap);
- DWORD dwBehaviorFlags =D3DCREATE_SOFTWARE_VERTEXPROCESSING;
- if(dcap.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
- dwBehaviorFlags =D3DCREATE_MIXED_VERTEXPROCESSING;
- // Create the device
- hr = pD3D->CreateDevice(D3DADAPTER_DEFAULT, dwDevType, hWnd,
- dwBehaviorFlags, &m_d3dpp, &m_pd3dDevice);
- pD3D->Release();
- if(FAILED(hr))
- {
- if(hr==D3DERR_INVALIDCALL)
- afLog::info("here");
- CleanupAndDisplayError( AFERR_NOD3DDEVICE, NULL, NULL );
- return DXTRACE_ERR( TEXT("pD3D->CreateDevice"), hr );
- }
- //Set Correct Device!!!
- af3DObject::pd3dDevice=m_pd3dDevice;
- afResourceManager::init();
- //******************************************************//
- //Load or Create resource here:Texture,Mesh,Vertex or Index buffer(must be in sysmem),allocate memory//
- m_pTerrain=new afTerrain();
- m_pTerrain->setMipmapFilter(afTextureStage::FILTER_POINT);
- m_pTerrain->setPVSName("mountain\heightmap");
- m_pTerrain->setNumPatches(16,16);
- m_pTerrain->setHeightMap(afResourceManager::getRawImage("mountain\heightmap",257,257, afImage::P8));
- afTexture* ColorTex,*AlphaTex;
- ColorTex=afResourceManager::getTexture("mountain\baseColor");
- AlphaTex=afResourceManager::getTexture("mountain\baseLight");
- if(ColorTex&&AlphaTex)
- m_pTerrain->addPass(ColorTex,32,32,AlphaTex);
- ColorTex=afResourceManager::getTexture("mountain\pass1Color");
- AlphaTex=afResourceManager::getTexture("mountain\pass1Light");
- if(ColorTex&&AlphaTex)
- m_pTerrain->addPass(ColorTex,15,15,AlphaTex);
- ColorTex=afResourceManager::getTexture("mountain\pass2Color");
- AlphaTex=afResourceManager::getTexture("mountain\pass2Light");
- if(ColorTex&&AlphaTex)
- m_pTerrain->addPass(ColorTex,10,10,AlphaTex);
- m_pTerrain->setPatchSize(4000,25000,4000);
- m_pTerrain->setMaxError(0.005);
- m_pTerrain->setMergePatches(true);
- // m_pTerrain->setDesiredFPS(40);
- m_pTerrain->build();
- // Create a vextex buffer for the viewport
- if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4*sizeof(SCREENVERTEX),
- D3DUSAGE_WRITEONLY, D3DFVF_SCREENVERTEX,
- D3DPOOL_MANAGED, &m_pViewportVB, NULL ) ) )
- return DXTRACE_ERR( TEXT("g_pd3dDevice->CreateVertexBuffer"), hr );
- if( FAILED( hr = RestoreDeviceObjects() ) )
- return DXTRACE_ERR( TEXT("RestoreDeviceObjects"), hr );
- // The display is now ready
- m_bDisplayReady = TRUE;
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: RestoreDeviceObjects()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT afApplication::RestoreDeviceObjects()
- {
- HWND hWnd = m_hWndMain;
- if( FALSE == m_bFullScreen )
- {
- // If we are still a WS_POPUP window we should convert to a normal app
- // window so we look like a windows app.
- DWORD dwStyle = GetWindowStyle( hWnd );
- dwStyle &= ~WS_POPUP;
- dwStyle |= WS_CAPTION | WS_MINIMIZEBOX;
- SetWindowLong( hWnd, GWL_STYLE, dwStyle );
- // Set window size
- RECT rc;
- SetRect( &rc, 0, 0, 640, 480 );
- SetWindowPos( hWnd, NULL, 0, 0, rc.right-rc.left, rc.bottom-rc.top,
- SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
- SetWindowPos( hWnd, HWND_NOTOPMOST, 0, 0, 0, 0,
- SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
- // Make sure our window does not hang outside of the work area
- RECT rcWork;
- SystemParametersInfo( SPI_GETWORKAREA, 0, &rcWork, 0 );
- GetWindowRect( hWnd, &rc );
- if( rc.left < rcWork.left ) rc.left = rcWork.left;
- if( rc.top < rcWork.top ) rc.top = rcWork.top;
- SetWindowPos( hWnd, NULL, rc.left, rc.top, 0, 0,
- SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
- }
- //****************************************************************************//
- //Prepare lose-easy resource:fill buffer;setup the transform;Setup some states//
- //****************************************************************************//
- // Get viewport dimensions
- D3DVIEWPORT9 vp;
- m_pd3dDevice->GetViewport(&vp);
- FLOAT sx = (FLOAT)vp.Width;
- FLOAT sy = (FLOAT)vp.Height;
- if( m_pViewportVB )
- {
- // Setup dimensions for the viewport covering square
- SCREENVERTEX* v;
- m_pViewportVB->Lock( 0, 0, (void**)&v, 0 );
- v[0].color = v[1].color = v[2].color = v[3].color = 0xFF0000FF;
- v[0].p = D3DXVECTOR4( 0,sy,0.0f,1.0f);
- v[0].tu = 0.0f; v[0].tv = 1.0f;
- v[1].p = D3DXVECTOR4( 0, 0,0.0f,1.0f);
- v[1].tu = 0.0f; v[1].tv = 0.0f;
- v[2].p = D3DXVECTOR4(sx,sy,0.0f,1.0f);
- v[2].tu = 1.0f; v[2].tv = 1.0f;
- v[3].p = D3DXVECTOR4(sx, 0,0.0f,1.0f);
- v[3].tu = 1.0f; v[3].tv = 0.0f;
- m_pViewportVB->Unlock();
- }
- if(m_pTerrain)
- m_pTerrain->restoreDeviceObjects();
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: UpdateScene()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT afApplication::UpdateScene()
- {
- // switch( m_dwAppState)
- FrameMove();
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: FrameMove()
- // Desc:
- //-----------------------------------------------------------------------------
- VOID afApplication::FrameMove()
- {
- m_pInput->update();
- if(m_pInput->isKeyDown(afInput::KEY_ESCAPE))
- {
- PostMessage(m_hWndMain,WM_CLOSE,0,0);
- return;
- }
- afTime::update();
- m_Camera.SetProjParams(4.0f/3.0f, 3.14/4, 50.0f, 40000.0f);
- D3DXVECTOR3 eyepos(00000,20000,15000),lookat(11000,20000,15000),up(0,1,0);
- m_Camera.SetViewParams(eyepos,lookat,up);
- m_Camera.SetUpdateVisibility(true);
- if(m_pTerrain)
- {
- m_pTerrain->update();
- }
- }
- //-----------------------------------------------------------------------------
- // Name: UpdateCullInfo()
- // Desc: Sets up the frustum planes, endpoints, and center for the frustum
- // defined by a given view matrix and projection matrix. This info will
- // be used when culling each object in CullObject().
- //-----------------------------------------------------------------------------
- VOID afApplication::UpdateCullInfo( CULLINFO* pCullInfo, D3DXMATRIXA16* pMatView,
- D3DXMATRIXA16* pMatProj )
- {
- D3DXMATRIXA16 mat;
- D3DXMatrixMultiply( &mat, pMatView, pMatProj );
- D3DXMatrixInverse( &mat, NULL, &mat );
- pCullInfo->vecFrustum[0] = D3DXVECTOR3(-1.0f, -1.0f, 0.0f); // xyz
- pCullInfo->vecFrustum[1] = D3DXVECTOR3( 1.0f, -1.0f, 0.0f); // Xyz
- pCullInfo->vecFrustum[2] = D3DXVECTOR3(-1.0f, 1.0f, 0.0f); // xYz
- pCullInfo->vecFrustum[3] = D3DXVECTOR3( 1.0f, 1.0f, 0.0f); // XYz
- pCullInfo->vecFrustum[4] = D3DXVECTOR3(-1.0f, -1.0f, 1.0f); // xyZ
- pCullInfo->vecFrustum[5] = D3DXVECTOR3( 1.0f, -1.0f, 1.0f); // XyZ
- pCullInfo->vecFrustum[6] = D3DXVECTOR3(-1.0f, 1.0f, 1.0f); // xYZ
- pCullInfo->vecFrustum[7] = D3DXVECTOR3( 1.0f, 1.0f, 1.0f); // XYZ
- for( INT i = 0; i < 8; i++ )
- D3DXVec3TransformCoord( &pCullInfo->vecFrustum[i], &pCullInfo->vecFrustum[i], &mat );
- D3DXPlaneFromPoints( &pCullInfo->planeFrustum[0], &pCullInfo->vecFrustum[0],
- &pCullInfo->vecFrustum[1], &pCullInfo->vecFrustum[2] ); // Near
- D3DXPlaneFromPoints( &pCullInfo->planeFrustum[1], &pCullInfo->vecFrustum[6],
- &pCullInfo->vecFrustum[7], &pCullInfo->vecFrustum[5] ); // Far
- D3DXPlaneFromPoints( &pCullInfo->planeFrustum[2], &pCullInfo->vecFrustum[2],
- &pCullInfo->vecFrustum[6], &pCullInfo->vecFrustum[4] ); // Left
- D3DXPlaneFromPoints( &pCullInfo->planeFrustum[3], &pCullInfo->vecFrustum[7],
- &pCullInfo->vecFrustum[3], &pCullInfo->vecFrustum[5] ); // Right
- D3DXPlaneFromPoints( &pCullInfo->planeFrustum[4], &pCullInfo->vecFrustum[2],
- &pCullInfo->vecFrustum[3], &pCullInfo->vecFrustum[6] ); // Top
- D3DXPlaneFromPoints( &pCullInfo->planeFrustum[5], &pCullInfo->vecFrustum[1],
- &pCullInfo->vecFrustum[0], &pCullInfo->vecFrustum[4] ); // Bottom
- }
- //-----------------------------------------------------------------------------
- // Name: RenderScene()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT afApplication::RenderScene()
- {
- HRESULT hr;
- // Test the cooperative level to see if it's okay to render
- if( FAILED( hr = m_pd3dDevice->TestCooperativeLevel() ) )
- {
- // If the device was lost, do not render until we get it back
- if( D3DERR_DEVICELOST == hr )
- return S_OK;
- // Check if the device needs to be resized.
- if( D3DERR_DEVICENOTRESET == hr )
- {
- m_bDisplayReady = FALSE;
- InvalidateDeviceObjects();
- // Resize the device
- if( SUCCEEDED(m_pd3dDevice->Reset( &m_d3dpp ) ) )
- {
- // Initialize the app's device-dependent objects
- if( SUCCEEDED( RestoreDeviceObjects() ) )
- {
- m_bDisplayReady = TRUE;
- return S_OK;
- }
- }
- PostMessage( m_hWndMain, WM_CLOSE, 0, 0 );
- }
- return hr;
- }
- RenderFrame();
- m_pd3dDevice->Present( 0, 0, 0, 0 );
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: RenderFrame()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT afApplication::RenderFrame()
- {
- m_pd3dDevice->SetTransform(D3DTS_VIEW,m_Camera.GetViewMatrix());
- m_pd3dDevice->SetTransform(D3DTS_PROJECTION,m_Camera.GetProjMatrix());
- // Begin the scene
- if( SUCCEEDED(m_pd3dDevice->BeginScene()))
- {
- // Clear the display
- m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(255,0,0,0), 1.0f, 0L );
- if(m_pTerrain)
- {
- m_pTerrain->render();
- }
- // m_pd3dDevice->SetFVF( D3DFVF_SCREENVERTEX );
- // m_pd3dDevice->SetStreamSource( 0, m_pViewportVB, 0, sizeof(SCREENVERTEX) );
- // m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,0, 2 );
- m_pd3dDevice->EndScene();
- }
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: InvalidateDeviceObjects()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT afApplication::InvalidateDeviceObjects()
- {
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: DeleteDeviceObjects()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT afApplication::DeleteDeviceObjects()
- {
- afLog::info("fps %f",afTime::getAverageFPS());
- SAFE_RELEASE(m_pViewportVB);
- SAFE_RELEASE(m_pd3dDevice);
- // SAFE_RELEASE(af3DObject::pd3dDevice);
- if(m_pTerrain)
- m_pTerrain->deleteDeviceObjects();
- SAFE_DELETE(m_pTerrain);
- afResourceManager::deinit();
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: FinalCleanup()
- // Desc: Cleanup everything
- //-----------------------------------------------------------------------------
- VOID afApplication::FinalCleanup()
- {
- if(m_pInput)
- {
- m_pInput->cleanup();
- SAFE_DELETE(m_pInput);
- }
- InvalidateDeviceObjects();
- DeleteDeviceObjects();
- afProfiler::logResult();
- }
- //-----------------------------------------------------------------------------
- // Name: DarkenScene()
- // Desc:
- //-----------------------------------------------------------------------------
- VOID afApplication::DarkenScene( FLOAT fAmount )
- {
- }
- //-----------------------------------------------------------------------------
- // Name: RenderSplash()
- // Desc:
- //-----------------------------------------------------------------------------
- VOID afApplication::RenderSplash()
- {
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- VOID afApplication::ConstructMenus()
- {
- return;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- VOID afApplication::DestroyMenus()
- {
- }
- //-----------------------------------------------------------------------------
- // Name: UpdateMenus()
- // Desc:
- //-----------------------------------------------------------------------------
- VOID afApplication::UpdateMenus()
- {
- }
- //-----------------------------------------------------------------------------
- // Name: SwitchDisplayModes()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT afApplication::SwitchDisplayModes( BOOL bFullScreen, DWORD dwWidth, DWORD dwHeight )
- {
- HRESULT hr;
- if( FALSE==m_bIsActive || FALSE==m_bDisplayReady )
- return S_OK;
- // Check to see if a change was actually requested
- if( bFullScreen )
- {
- if( m_dwScreenWidth==dwWidth && m_dwScreenHeight==dwHeight &&
- m_bFullScreen==bFullScreen )
- return S_OK;
- }
- else
- {
- // if( m_bFullScreen == FALSE )
- // return S_OK;
- }
- // Invalidate the old display objects
- m_bDisplayReady = FALSE;
- InvalidateDeviceObjects();
- if( bFullScreen )
- {
- // Set windowed-mode style
- SetWindowLong( m_hWndMain, GWL_STYLE, m_dwWindowStyle|WS_VISIBLE );
- }
- else
- {
- // Set fullscreen-mode style
- SetWindowLong( m_hWndMain, GWL_STYLE, WS_POPUP|WS_SYSMENU|WS_VISIBLE );
- }
- // Set up the new presentation paramters
- if( bFullScreen )
- {
- m_d3dpp.Windowed = FALSE;
- m_d3dpp.hDeviceWindow = m_hWndMain;
- m_d3dpp.BackBufferWidth = m_dwScreenWidth = dwWidth;
- m_d3dpp.BackBufferHeight = m_dwScreenHeight = dwHeight;
- m_d3dpp.BackBufferFormat = m_d3dfmtFullscreen;
- }
- else
- {
- m_d3dpp.Windowed = TRUE;
- m_d3dpp.hDeviceWindow = NULL;
- m_d3dpp.BackBufferWidth = 0L;
- m_d3dpp.BackBufferHeight = 0L;
- m_d3dpp.BackBufferFormat = m_DesktopMode.Format;
- }
- // Reset the device
- if( SUCCEEDED( hr=m_pd3dDevice->Reset( &m_d3dpp ) ) )
- {
- m_bFullScreen = bFullScreen;
- if( SUCCEEDED( hr = RestoreDeviceObjects() ) )
- {
- m_bDisplayReady = TRUE;
- return S_OK;
- }
- }
- // If we get here, a fatal error occurred
- PostMessage( m_hWndMain, WM_CLOSE, 0, 0 );
- return E_FAIL;
- }
- //-----------------------------------------------------------------------------
- // Name: CleanupAndDisplayError()
- // Desc:
- //-----------------------------------------------------------------------------
- VOID afApplication::CleanupAndDisplayError( DWORD dwError, TCHAR* strArg1, TCHAR* strArg2 )
- {
- TCHAR* strDbgOut = NULL;
- TCHAR* strMsgBox = NULL;
- // Cleanup the app
- InvalidateDeviceObjects();
- DeleteDeviceObjects();
- FinalCleanup();
- // Make the cursor visible
- SetCursor( LoadCursor( NULL, IDC_ARROW ) );
- m_bMouseVisible = TRUE;
- // Get the appropriate error strings
- switch( dwError )
- {
- case AFERR_NODIRECT3D:
- strDbgOut = _T("Could not create Direct3Dn");
- strMsgBox = _T("Could not create Direct3D.nn")
- _T("Please make sure you have the latest DirectXn")
- _T(".dlls installed on your system.");
- break;
- case AFERR_NOD3DDEVICE:
- strDbgOut = _T("Could not create a Direct3D devicen");
- strMsgBox = _T("Could not create a Direct3D device. Yourn")
- _T("graphics accelerator is not sufficient ton")
- _T("run this demo, or your desktop is usingn")
- _T("a color format that cannot be accelerated byn")
- _T("your graphics card (try 16-bit mode).");
- break;
- case AFERR_ARTLOADFAILED:
- strDbgOut = _T("Could not load game artn");
- strMsgBox = _T("Couldn't load game art %s in %s. ")
- _T("Either your graphics hardware does not have ")
- _T("sufficient resources, or the DirectX SDK was ")
- _T("not properly installed.");
- break;
- case AFERR_NOINPUT:
- strDbgOut = _T("Could not create input objectsn");
- strMsgBox = _T("Could not create input objects.");
- break;
- }
- // Output the error strings
- if( strDbgOut && strMsgBox )
- {
- OutputDebugString( strDbgOut );
- TCHAR strMsg[512];
- _sntprintf( strMsg, 512, strMsgBox, strArg1, strArg2 );
- strMsg[511]=0;
- MessageBox( m_hWndMain, strMsg, m_strAppName, MB_OK );
- }
- }