home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / directx / dxf / samples / multimedia / direct3d / text3d / text3d.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  12.1 KB  |  351 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Text3D.cpp
  3. //
  4. // Desc: Example code showing how to do text in a Direct3D scene.
  5. //
  6. // Copyright (c) 1997-2000 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #include <windows.h>
  10. #include <commdlg.h>
  11. #include <math.h>
  12. #include <tchar.h>
  13. #include <stdio.h>
  14. #include <D3DX8.h>
  15. #include "D3DApp.h"
  16. #include "D3DFont.h"
  17. #include "D3DUtil.h"
  18. #include "DXUtil.h"
  19. #include "resource.h"
  20.  
  21.  
  22.  
  23.  
  24. //-----------------------------------------------------------------------------
  25. // Name: class CMyD3DApplication
  26. // Desc: Application class. The base class (CD3DApplication) provides the 
  27. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  28. //       adds functionality specific to this sample program.
  29. //-----------------------------------------------------------------------------
  30. class CMyD3DApplication : public CD3DApplication
  31. {
  32.     CD3DFont*     m_pFont;
  33.     CD3DFont*     m_pStatsFont;
  34.     TCHAR         m_strFont[100];
  35.     DWORD         m_dwFontSize;
  36.  
  37.     D3DXMATRIX    m_matObj1;
  38.     D3DXMATRIX    m_matObj2;
  39.     D3DXMATRIX    m_matObj3;
  40.     D3DXMATRIX    m_matObj4;
  41.     D3DXMATRIX    m_matObj5;
  42.  
  43. protected:
  44.     HRESULT OneTimeSceneInit();
  45.     HRESULT InitDeviceObjects();
  46.     HRESULT RestoreDeviceObjects();
  47.     HRESULT InvalidateDeviceObjects();
  48.     HRESULT DeleteDeviceObjects();
  49.     HRESULT Render();
  50.     HRESULT FrameMove();
  51.     HRESULT FinalCleanup();
  52.  
  53. public:
  54.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  55.     CMyD3DApplication();
  56. };
  57.  
  58.  
  59.  
  60.  
  61. //-----------------------------------------------------------------------------
  62. // Name: WinMain()
  63. // Desc: Entry point to the program. Initializes everything, and goes into a
  64. //       message-processing loop. Idle time is used to render the scene.
  65. //-----------------------------------------------------------------------------
  66. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  67. {
  68.     CMyD3DApplication d3dApp;
  69.  
  70.     if( FAILED( d3dApp.Create( hInst ) ) )
  71.         return 0;
  72.  
  73.     return d3dApp.Run();
  74. }
  75.  
  76.  
  77.  
  78.  
  79. //-----------------------------------------------------------------------------
  80. // Name: CMyD3DApplication()
  81. // Desc: Application constructor. Sets attributes for the app.
  82. //-----------------------------------------------------------------------------
  83. CMyD3DApplication::CMyD3DApplication()
  84. {
  85.     m_strWindowTitle    = _T("Text3D: Text in a 3D scene");
  86.     m_bUseDepthBuffer   = FALSE;
  87.  
  88.     // Create fonts
  89.     lstrcpy( m_strFont, _T("Arial") );
  90.     m_dwFontSize = 18;
  91.     m_pFont      = new CD3DFont( m_strFont, m_dwFontSize );
  92.     m_pStatsFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  93. }
  94.  
  95.  
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Name: OneTimeSceneInit()
  100. // Desc: Called during initial app startup, this function performs all the
  101. //       permanent initialization.
  102. //-----------------------------------------------------------------------------
  103. HRESULT CMyD3DApplication::OneTimeSceneInit()
  104. {
  105.     return S_OK;
  106. }
  107.  
  108.  
  109.  
  110.  
  111. //-----------------------------------------------------------------------------
  112. // Name: FrameMove()
  113. // Desc: Called once per frame, the call is the entry point for animating
  114. //       the scene.
  115. //-----------------------------------------------------------------------------
  116. HRESULT CMyD3DApplication::FrameMove()
  117. {
  118.     // Setup five rotation matrices (for rotating text strings)
  119.     D3DXMatrixRotationAxis( &m_matObj1, &D3DXVECTOR3(1.0f,2.0f,0.0f), m_fTime/1.1f  );
  120.     D3DXMatrixRotationAxis( &m_matObj2, &D3DXVECTOR3(2.0f,1.0f,0.0f), m_fTime/1.2f  );
  121.     D3DXMatrixRotationAxis( &m_matObj3, &D3DXVECTOR3(1.0f,0.0f,2.0f), m_fTime/1.3f  );
  122.     D3DXMatrixRotationAxis( &m_matObj4, &D3DXVECTOR3(2.0f,0.0f,1.0f), m_fTime/1.4f  );
  123.     D3DXMatrixRotationAxis( &m_matObj5, &D3DXVECTOR3(0.0f,1.0f,2.0f), m_fTime/1.5f  );
  124.  
  125.     // Add some translational values to the matrices
  126.     m_matObj1._41 = 1.0f;   m_matObj1._42 = 3.0f;   m_matObj1._43 = 0.0f; 
  127.     m_matObj2._41 = 2.0f;   m_matObj2._42 =-2.0f;   m_matObj2._43 = 0.0f; 
  128.     m_matObj3._41 = 3.0f;   m_matObj3._42 = 1.0f;   m_matObj3._43 = 0.0f; 
  129.     m_matObj4._41 =-1.0f;   m_matObj4._42 =-3.0f;   m_matObj4._43 = 0.0f;
  130.     m_matObj5._41 =-2.0f;   m_matObj5._42 = 2.0f;   m_matObj5._43 = 0.0f; 
  131.  
  132.     return S_OK;
  133. }
  134.  
  135.  
  136.  
  137.  
  138. //-----------------------------------------------------------------------------
  139. // Name: Render()
  140. // Desc: Called once per frame, the call is the entry point for 3d
  141. //       rendering. This function sets up render states, clears the
  142. //       viewport, and renders the scene.
  143. //-----------------------------------------------------------------------------
  144. HRESULT CMyD3DApplication::Render()
  145. {
  146.     D3DMATERIAL8 mtrl;
  147.  
  148.     // Clear the viewport
  149.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, 0x00000000, 1.0f, 0L );
  150.  
  151.     // Begin the scene 
  152.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  153.     {
  154.         // Draw the 5 spinning, colored 3D text objects
  155.         D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
  156.         m_pd3dDevice->SetMaterial( &mtrl );
  157.         m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matObj1 );
  158.         m_pFont->Render3DText( _T("Red Text"), D3DFONT_CENTERED|D3DFONT_TWOSIDED|D3DFONT_FILTERED );
  159.  
  160.         D3DUtil_InitMaterial( mtrl, 0.0f, 1.0f, 0.0f );
  161.         m_pd3dDevice->SetMaterial( &mtrl );
  162.         m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matObj2 );
  163.         m_pFont->Render3DText( _T("Green Text"), D3DFONT_CENTERED|D3DFONT_TWOSIDED|D3DFONT_FILTERED );
  164.  
  165.         D3DUtil_InitMaterial( mtrl, 0.0f, 0.0f, 1.0f );
  166.         m_pd3dDevice->SetMaterial( &mtrl );
  167.         m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matObj3 );
  168.         m_pFont->Render3DText( _T("Blue Text"), D3DFONT_CENTERED|D3DFONT_TWOSIDED|D3DFONT_FILTERED );
  169.  
  170.         D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 1.0f );
  171.         m_pd3dDevice->SetMaterial( &mtrl );
  172.         m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matObj4 );
  173.         m_pFont->Render3DText( _T("Purple Text"), D3DFONT_CENTERED|D3DFONT_TWOSIDED|D3DFONT_FILTERED );
  174.  
  175.         D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 0.0f );
  176.         m_pd3dDevice->SetMaterial( &mtrl );
  177.         m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matObj5 );
  178.         m_pFont->Render3DText( _T("Yellow Text"), D3DFONT_CENTERED|D3DFONT_TWOSIDED|D3DFONT_FILTERED );
  179.  
  180.         // Draw some 2D text
  181.         m_pFont->DrawText( 2, 40, 0xffffffff, _T("2D Text") );
  182.  
  183.         // Show frame rate
  184.         m_pStatsFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  185.         m_pStatsFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  186.  
  187.         // End the scene.
  188.         m_pd3dDevice->EndScene();
  189.     }
  190.  
  191.     return S_OK;
  192. }
  193.  
  194.  
  195.  
  196.  
  197. //-----------------------------------------------------------------------------
  198. // Name: InitDeviceObjects()
  199. // Desc: Initialize scene objects.
  200. //-----------------------------------------------------------------------------
  201. HRESULT CMyD3DApplication::InitDeviceObjects()
  202. {
  203.     // Restore the fonts
  204.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  205.     m_pStatsFont->InitDeviceObjects( m_pd3dDevice );
  206.  
  207.     return S_OK;
  208. }
  209.  
  210.  
  211.  
  212.  
  213. //-----------------------------------------------------------------------------
  214. // Name: RestoreDeviceObjects()
  215. // Desc: Initialize scene objects.
  216. //-----------------------------------------------------------------------------
  217. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  218. {
  219.     // Restore the fonts
  220.     m_pFont->RestoreDeviceObjects();
  221.     m_pStatsFont->RestoreDeviceObjects();
  222.  
  223.     // Restore the textures
  224.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  225.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  226.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
  227.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  228.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  229.     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
  230.     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );
  231.     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,  0x00ffffff );
  232.  
  233.     // Set the transform matrices
  234.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f,-5.0f,-10.0f );
  235.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f,  0.0f );
  236.     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f,  0.0f );
  237.     D3DXMATRIX  matWorld, matView, matProj;
  238.  
  239.     D3DXMatrixIdentity( &matWorld );
  240.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  241.     FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
  242.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
  243.  
  244.     m_pd3dDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  245.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  246.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  247.  
  248.     return S_OK;
  249. }
  250.  
  251.  
  252.  
  253.  
  254. //-----------------------------------------------------------------------------
  255. // Name: InvalidateDeviceObjects()
  256. // Desc: Called when the app is exiting, or the device is being changed,
  257. //       this function deletes any device dependent objects.
  258. //-----------------------------------------------------------------------------
  259. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  260. {
  261.     m_pFont->InvalidateDeviceObjects();
  262.     m_pStatsFont->InvalidateDeviceObjects();
  263.  
  264.     return S_OK;
  265. }
  266.  
  267.  
  268.  
  269.  
  270. //-----------------------------------------------------------------------------
  271. // Name: DeleteDeviceObjects()
  272. // Desc: Called when the app is exiting, or the device is being changed,
  273. //       this function deletes any device dependent objects.
  274. //-----------------------------------------------------------------------------
  275. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  276. {
  277.     m_pFont->DeleteDeviceObjects();
  278.     m_pStatsFont->DeleteDeviceObjects();
  279.     return S_OK;
  280. }
  281.  
  282.  
  283.  
  284.  
  285. //-----------------------------------------------------------------------------
  286. // Name: FinalCleanup()
  287. // Desc: Called before the app exits, this function gives the app the chance
  288. //       to cleanup after itself.
  289. //-----------------------------------------------------------------------------
  290. HRESULT CMyD3DApplication::FinalCleanup()
  291. {
  292.     SAFE_DELETE( m_pFont );
  293.     SAFE_DELETE( m_pStatsFont );
  294.  
  295.     return S_OK;
  296. }
  297.  
  298.  
  299.  
  300.  
  301. //-----------------------------------------------------------------------------
  302. // Name: MsgProc()
  303. // Desc: Message proc function to handle key and menu input
  304. //-----------------------------------------------------------------------------
  305. LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  306.                                     LPARAM lParam )
  307. {
  308.     if( WM_COMMAND == uMsg )
  309.     {
  310.         switch( LOWORD(wParam) )
  311.         {
  312.             case IDM_FONT:
  313.             {
  314.                 HDC hdc;
  315.                 LONG lHeight;
  316.                 hdc = GetDC( hWnd );
  317.                 lHeight = -MulDiv( m_dwFontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72 );
  318.                 ReleaseDC( hWnd, hdc );
  319.                 hdc = NULL;
  320.  
  321.                 LOGFONT lf;
  322.                 lstrcpy( lf.lfFaceName, m_strFont );
  323.                 lf.lfHeight = lHeight;
  324.  
  325.                 CHOOSEFONT cf;
  326.                 ZeroMemory( &cf, sizeof(cf) );
  327.                 cf.lStructSize = sizeof(cf);
  328.                 cf.hwndOwner   = m_hWnd;
  329.                 cf.lpLogFont   = &lf;
  330.                 cf.Flags       = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
  331.                 
  332.                 if( ChooseFont( &cf ) )
  333.                 {
  334.                     SAFE_DELETE( m_pFont );
  335.                     lstrcpy( m_strFont, lf.lfFaceName );
  336.                     m_dwFontSize = cf.iPointSize / 10;
  337.                     m_pFont = new CD3DFont( m_strFont, m_dwFontSize );
  338.                     m_pFont->InitDeviceObjects( m_pd3dDevice );
  339.                     m_pFont->RestoreDeviceObjects();
  340.                 }
  341.                 break;
  342.             }
  343.         }
  344.     }
  345.  
  346.     return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
  347. }
  348.  
  349.  
  350.  
  351.