gamemenu.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:5k
源码类别:

游戏

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // File: GameMenu.cpp
  3. //
  4. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  5. //-----------------------------------------------------------------------------
  6. #include "stdafx.h"
  7. //-----------------------------------------------------------------------------
  8. // Name: 
  9. // Desc: 
  10. //-----------------------------------------------------------------------------
  11. CMenuItem::CMenuItem( TCHAR* strNewLabel, DWORD dwNewID )
  12. {
  13.     _tcscpy( strLabel, strNewLabel );
  14.     dwID           = dwNewID;
  15.     pParent        = NULL;
  16.     dwNumChildren  = 0L;
  17.     dwSelectedMenu = 0L;
  18. }
  19. //-----------------------------------------------------------------------------
  20. // Name: 
  21. // Desc: 
  22. //-----------------------------------------------------------------------------
  23. CMenuItem::~CMenuItem()
  24. {
  25.     while( dwNumChildren )
  26.         delete pChild[--dwNumChildren];
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Name: 
  30. // Desc: 
  31. //-----------------------------------------------------------------------------
  32. CMenuItem* CMenuItem::Add( CMenuItem* pNewChild )
  33. {
  34.     pChild[dwNumChildren++] = pNewChild;
  35.     pNewChild->pParent = this;
  36.     return pNewChild;
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Name: Render()
  40. // Desc: 
  41. //-----------------------------------------------------------------------------
  42. HRESULT CMenuItem::Render( CD3DFont* pFont )
  43. {
  44.     // Check parameters
  45.     if( NULL == pFont )
  46.         return E_INVALIDARG;
  47.     // Save current matrices
  48.     D3DXMATRIX matViewSaved, matProjSaved;
  49.     g_pd3dDevice->GetTransform( D3DTS_VIEW,       &matViewSaved );
  50.     g_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProjSaved );
  51.     // Setup new view and proj matrices for head-on viewing
  52.     D3DXMATRIX matView, matProj;
  53.     D3DXVECTOR3 vAt = D3DXVECTOR3(0.0f,0.0f,-30.0f);
  54.     D3DXVECTOR3 vLookAt = D3DXVECTOR3(0.0f,0.0f,0.0f);
  55.     D3DXVECTOR3 vUp = D3DXVECTOR3(0.0f,1.0f,0.0f);
  56.     
  57.     D3DXMatrixLookAtLH( &matView, &vAt, &vLookAt, &vUp );
  58.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
  59.     g_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  60.     g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  61.     // Establish colors for selected vs. normal menu items
  62.     D3DMATERIAL9 mtrlNormal, mtrlSelected, mtrlTitle;
  63.     ZeroMemory( &mtrlTitle, sizeof(D3DMATERIAL9) );
  64.     mtrlTitle.Diffuse.r = mtrlTitle.Ambient.r = 1.0f;
  65.     mtrlTitle.Diffuse.g = mtrlTitle.Ambient.g = 0.0f;
  66.     mtrlTitle.Diffuse.b = mtrlTitle.Ambient.b = 0.0f;
  67.     mtrlTitle.Diffuse.a = mtrlTitle.Ambient.a = 1.0f;
  68.     ZeroMemory( &mtrlNormal, sizeof(D3DMATERIAL9) );
  69.     mtrlNormal.Diffuse.r = mtrlNormal.Ambient.r = 1.0f;
  70.     mtrlNormal.Diffuse.g = mtrlNormal.Ambient.g = 1.0f;
  71.     mtrlNormal.Diffuse.b = mtrlNormal.Ambient.b = 1.0f;
  72.     mtrlNormal.Diffuse.a = mtrlNormal.Ambient.a = 0.5f;
  73.     ZeroMemory( &mtrlSelected, sizeof(D3DMATERIAL9) );
  74.     mtrlSelected.Diffuse.r = mtrlSelected.Ambient.r = 1.0f;
  75.     mtrlSelected.Diffuse.g = mtrlSelected.Ambient.g = 1.0f;
  76.     mtrlSelected.Diffuse.b = mtrlSelected.Ambient.b = 0.0f;
  77.     mtrlSelected.Diffuse.a = mtrlSelected.Ambient.a = 1.0f;
  78.     g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
  79.     g_pd3dDevice->SetRenderState( D3DRS_AMBIENT,  0xffffffff );
  80.     // Translate the menuitem into place
  81.     D3DXMATRIX matWorld;
  82.     D3DXMatrixScaling( &matWorld, 0.4f, 0.4f, 0.4f );
  83.     matWorld._42 = (dwNumChildren*1.0f) + 2.0f;
  84.     g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  85.     g_pd3dDevice->SetMaterial( &mtrlTitle );
  86.     // Render the menuitem's label
  87.     pFont->Render3DText( strLabel, D3DFONT_CENTERED_X|D3DFONT_TWOSIDED );
  88.     // Loop through and render all menuitem lables
  89.     for( DWORD i=0; i<dwNumChildren; i++ )
  90.     {
  91.         D3DXMATRIX matWorld;
  92.         D3DXMatrixScaling( &matWorld, 0.3f, 0.3f, 0.3f );
  93.         g_pd3dDevice->SetMaterial( &mtrlNormal );
  94.         // Give a different effect for selected items
  95.         if( dwSelectedMenu == i )
  96.         {
  97.             D3DXMATRIX matRotate;
  98.             D3DXMatrixRotationY( &matRotate, (D3DX_PI/3)*sinf(timeGetTime()/200.0f) );
  99.             D3DXMatrixMultiply( &matWorld, &matWorld, &matRotate );
  100.             g_pd3dDevice->SetMaterial( &mtrlSelected );
  101.         }
  102.         // Translate the menuitem into place
  103.         matWorld._42 = (dwNumChildren*1.0f) - (i*2.0f);
  104.         g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  105.         // Render the menuitem's label
  106.         pFont->Render3DText( pChild[i]->strLabel, 
  107.                              D3DFONT_CENTERED_X|D3DFONT_TWOSIDED );
  108.     }
  109.     // Restore matrices
  110.     g_pd3dDevice->SetTransform( D3DTS_VIEW,       &matViewSaved );
  111.     g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProjSaved );
  112.     return S_OK;
  113. }