home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / MFCTex / mfctex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  11.7 KB  |  354 lines

  1. //-----------------------------------------------------------------------------
  2. // File: MFCTex.cpp
  3. //
  4. // Desc: Main file for the D3D multitexture app that uses MFC.
  5. //
  6. // Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #include "stdafx.h"
  9. #include <windows.h>
  10. #include <windowsx.h>
  11. #include <stdio.h>
  12. #include <mmsystem.h>
  13. #include <D3DX8.h>
  14. #include "D3DApp.h"
  15. #include "D3DFont.h"
  16. #include "D3DUtil.h"
  17. #include "DXUtil.h"
  18. #include "MFCTex.h"
  19.  
  20.  
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // The MFC macros are all listed here
  25. //-----------------------------------------------------------------------------
  26. IMPLEMENT_DYNCREATE( CAppDoc,      CDocument )
  27. IMPLEMENT_DYNCREATE( CAppFrameWnd, CFrameWnd )
  28. IMPLEMENT_DYNCREATE( CAppForm,     CFormView )
  29.  
  30.  
  31. BEGIN_MESSAGE_MAP( CApp, CWinApp )
  32.     //{{AFX_MSG_MAP(CApp)
  33.     //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35.  
  36.  
  37. BEGIN_MESSAGE_MAP( CAppForm, CFormView )
  38.     //{{AFX_MSG_MAP(CAppForm)
  39.     ON_COMMAND( IDC_VIEWFULLSCREEN, OnToggleFullScreen )
  40.     ON_COMMAND( IDC_VIEWCODE,       OnViewCode )
  41.     ON_COMMAND( IDM_CHANGEDEVICE,   OnChangeDevice )
  42.     ON_BN_CLICKED( IDM_CHANGEDEVICE,  OnChangeDevice )
  43.     ON_EN_CHANGE(  IDC_TEX0_NAME,     OnChangeTex )
  44.     ON_EN_CHANGE(  IDC_TEX1_NAME,     OnChangeTex )
  45.     ON_EN_CHANGE(  IDC_TEX2_NAME,     OnChangeTex )
  46.     ON_BN_CLICKED( IDC_SELECTTEX0,    OnSelectTexture0Name )
  47.     ON_BN_CLICKED( IDC_SELECTTEX1,    OnSelectTexture1Name )
  48.     ON_BN_CLICKED( IDC_SELECTTEX2,    OnSelectTexture2Name )
  49.     ON_EN_CHANGE(  IDC_BLEND_FACTOR,  OnChangeBlendFactor )
  50.     ON_EN_CHANGE(  IDC_DIFFUSE_COLOR, OnChangeDiffuseColor )
  51.     ON_CBN_SELCHANGE( IDC_PRESET_EFFECTS, OnChangePresetEffects )
  52.     ON_CBN_SELCHANGE( IDC_TEX0_COLORARG1, OnChangeStageArgs )
  53.     ON_CBN_SELCHANGE( IDC_TEX0_COLOROP,   OnChangeStageArgs )
  54.     ON_CBN_SELCHANGE( IDC_TEX0_COLORARG2, OnChangeStageArgs )
  55.     ON_CBN_SELCHANGE( IDC_TEX0_ALPHAARG1, OnChangeStageArgs )
  56.     ON_CBN_SELCHANGE( IDC_TEX0_ALPHAOP,   OnChangeStageArgs )
  57.     ON_CBN_SELCHANGE( IDC_TEX0_ALPHAARG2, OnChangeStageArgs )
  58.     ON_CBN_SELCHANGE( IDC_TEX1_COLORARG1, OnChangeStageArgs )
  59.     ON_CBN_SELCHANGE( IDC_TEX1_COLOROP,   OnChangeStageArgs )
  60.     ON_CBN_SELCHANGE( IDC_TEX1_COLORARG2, OnChangeStageArgs )
  61.     ON_CBN_SELCHANGE( IDC_TEX1_ALPHAARG1, OnChangeStageArgs )
  62.     ON_CBN_SELCHANGE( IDC_TEX1_ALPHAOP,   OnChangeStageArgs )
  63.     ON_CBN_SELCHANGE( IDC_TEX1_ALPHAARG2, OnChangeStageArgs )
  64.     ON_CBN_SELCHANGE( IDC_TEX2_COLORARG1, OnChangeStageArgs )
  65.     ON_CBN_SELCHANGE( IDC_TEX2_COLOROP,   OnChangeStageArgs )
  66.     ON_CBN_SELCHANGE( IDC_TEX2_COLORARG2, OnChangeStageArgs )
  67.     ON_CBN_SELCHANGE( IDC_TEX2_ALPHAARG1, OnChangeStageArgs )
  68.     ON_CBN_SELCHANGE( IDC_TEX2_ALPHAOP,   OnChangeStageArgs )
  69.     ON_CBN_SELCHANGE( IDC_TEX2_ALPHAARG2, OnChangeStageArgs )
  70.     //}}AFX_MSG_MAP
  71. END_MESSAGE_MAP()
  72.  
  73.  
  74.  
  75.  
  76. BEGIN_MESSAGE_MAP(CAppDoc, CDocument)
  77.     //{{AFX_MSG_MAP(CAppDoc)
  78.         // NOTE - the ClassWizard will add and remove mapping macros here.
  79.         //    DO NOT EDIT what you see in these blocks of generated code!
  80.     //}}AFX_MSG_MAP
  81. END_MESSAGE_MAP()
  82.  
  83.  
  84.  
  85.  
  86. BEGIN_MESSAGE_MAP(CAppFrameWnd, CFrameWnd)
  87.     //{{AFX_MSG_MAP(CAppFrameWnd)
  88.     //}}AFX_MSG_MAP
  89. END_MESSAGE_MAP()
  90.  
  91.  
  92.  
  93.  
  94. //-----------------------------------------------------------------------------
  95. // Global data and objects
  96. //-----------------------------------------------------------------------------
  97. CApp          g_App;
  98. CAppForm*     g_AppFormView = NULL;
  99. TCHAR*        g_strAppTitle = _T("MFCTex: MFC Multitexture Sample");
  100.  
  101.  
  102.  
  103.  
  104. //-----------------------------------------------------------------------------
  105. // Name: FullScreenWndProc()
  106. // Desc: The WndProc funtion used when the app is in fullscreen mode. This is
  107. //       needed simply to trap the ESC key.
  108. //-----------------------------------------------------------------------------
  109. LRESULT CALLBACK FullScreenWndProc( HWND hWnd, UINT msg, WPARAM wParam,
  110.                                     LPARAM lParam )
  111. {
  112.     if( msg == WM_CLOSE )
  113.     {
  114.         // User wants to exit, so go back to windowed mode and exit for real
  115.         g_AppFormView->OnToggleFullScreen();
  116.         g_App.GetMainWnd()->PostMessage( WM_CLOSE, 0, 0 );
  117.     }
  118.  
  119.     if( msg == WM_SETCURSOR )
  120.     {
  121.         SetCursor( NULL );
  122.     }
  123.  
  124.     if( msg == WM_KEYUP && wParam == VK_ESCAPE )
  125.     {
  126.         // User wants to leave fullscreen mode
  127.         g_AppFormView->OnToggleFullScreen();
  128.     }
  129.  
  130.     return DefWindowProc( hWnd, msg, wParam, lParam );
  131. }
  132.  
  133.  
  134.  
  135.  
  136. //-----------------------------------------------------------------------------
  137. // Name: CheckForLostFullscreen()
  138. // Desc: If fullscreen and device was lost (probably due to alt-tab), 
  139. //       automatically switch to windowed mode
  140. //-----------------------------------------------------------------------------
  141. HRESULT CAppForm::CheckForLostFullscreen()
  142. {
  143.     HRESULT hr;
  144.  
  145.     if( m_bWindowed )
  146.         return S_OK;
  147.  
  148.     if( FAILED( hr = m_pd3dDevice->TestCooperativeLevel() ) )
  149.         ForceWindowed();
  150.  
  151.     return S_OK;
  152. }
  153.  
  154.  
  155.  
  156.  
  157. //-----------------------------------------------------------------------------
  158. // Name: PreCreateWindow()
  159. // Desc: Change the window style (so it cannot maximize or be sized) before
  160. //       the main frame window is created.
  161. //-----------------------------------------------------------------------------
  162. BOOL CAppFrameWnd::PreCreateWindow( CREATESTRUCT& cs )
  163. {
  164.     cs.style = WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX;
  165.     return CFrameWnd::PreCreateWindow( cs );
  166. }
  167.  
  168.  
  169.  
  170.  
  171. //-----------------------------------------------------------------------------
  172. // Name: InitInstance()
  173. // Desc: This is the main entry point for the application. The MFC window stuff
  174. //       is initialized here. See also the main initialization routine for the
  175. //       CAppForm class, which is called indirectly from here.
  176. //-----------------------------------------------------------------------------
  177. BOOL CApp::InitInstance()
  178. {
  179.     // Asscociate the MFC app with the frame window and doc/view classes
  180.     AddDocTemplate( new CSingleDocTemplate( IDR_MAINFRAME, 
  181.                                             RUNTIME_CLASS(CAppDoc),
  182.                                             RUNTIME_CLASS(CAppFrameWnd),
  183.                                             RUNTIME_CLASS(CAppForm) ) );
  184.  
  185.     // Dispatch commands specified on the command line (req'd by MFC). This
  186.     // also initializes the the CAppDoc, CAppFrameWnd, and CAppForm classes.
  187.     CCommandLineInfo cmdInfo;
  188.     ParseCommandLine( cmdInfo );
  189.     if( !ProcessShellCommand( cmdInfo ) )
  190.         return FALSE;
  191.  
  192.     if( !g_AppFormView->IsReady() )
  193.         return FALSE;
  194.  
  195.     g_AppFormView->GetParentFrame()->RecalcLayout();
  196.     g_AppFormView->ResizeParentToFit( FALSE ); 
  197.     
  198.     m_pMainWnd->SetWindowText( g_strAppTitle );
  199.     m_pMainWnd->UpdateWindow();
  200.  
  201.     return TRUE;
  202. }
  203.  
  204.  
  205.  
  206.  
  207. //-----------------------------------------------------------------------------
  208. // Name: OnIdle()
  209. // Desc: Uses idle time to render the 3D scene.
  210. //-----------------------------------------------------------------------------
  211. BOOL CApp::OnIdle( LONG )
  212. {
  213.     // Do not render if the app is minimized
  214.     if( m_pMainWnd->IsIconic() )
  215.         return FALSE;
  216.  
  217.     // Update and render a frame
  218.     if( g_AppFormView->IsReady() )
  219.     {
  220.         g_AppFormView->CheckForLostFullscreen();
  221.         g_AppFormView->RenderScene();
  222.     }
  223.  
  224.     // Keep requesting more idle time
  225.     return TRUE;
  226. }
  227.  
  228.  
  229.  
  230.  
  231. //-----------------------------------------------------------------------------
  232. // Name: CAppForm()
  233. // Desc: Constructor for the dialog resource form
  234. //-----------------------------------------------------------------------------
  235. CAppForm::CAppForm()
  236.          :CFormView( IDD_FORMVIEW )
  237. {
  238.     g_AppFormView       = this;
  239.     m_bActive           = FALSE;
  240.     m_bReady            = FALSE;
  241.     m_bWindowed         = TRUE;
  242.     
  243.     m_dwAdapter         = 0L;
  244.     m_pD3D              = NULL;
  245.     m_pd3dDevice        = NULL;
  246.     m_bUseDepthBuffer   = TRUE;
  247. }
  248.  
  249.  
  250.  
  251.  
  252. //-----------------------------------------------------------------------------
  253. // Name: ~CAppForm()
  254. // Desc: Destructor for the dialog resource form. Shuts down the app
  255. //-----------------------------------------------------------------------------
  256. CAppForm::~CAppForm()
  257. {
  258.     Cleanup3DEnvironment();
  259. }
  260.  
  261.  
  262.  
  263.  
  264. //-----------------------------------------------------------------------------
  265. // Name: OnInitialUpdate()
  266. // Desc: When the AppForm object is created, this function is called to
  267. //       initialize it. Here we getting access ptrs to some of the controls,
  268. //       and setting the initial state of some of them as well.
  269. //-----------------------------------------------------------------------------
  270. VOID CAppForm::OnInitialUpdate()
  271. {
  272.     // Update the UI
  273.     CFormView::OnInitialUpdate();
  274.     InitializeUIControls();
  275.  
  276.     // Save static reference to the render window
  277.     m_hwndRenderWindow = GetDlgItem(IDC_RENDERVIEW)->GetSafeHwnd();
  278.  
  279.     // Register a class for a fullscreen window
  280.     WNDCLASS wndClass = { CS_HREDRAW | CS_VREDRAW, FullScreenWndProc, 0, 0, NULL,
  281.                           NULL, NULL, (HBRUSH)GetStockObject(WHITE_BRUSH), NULL,
  282.                           _T("Fullscreen Window") };
  283.     RegisterClass( &wndClass );
  284.  
  285.     // We create the fullscreen window (not visible) at startup, so it can
  286.     // be the focus window.  The focus window can only be set at CreateDevice
  287.     // time, not in a Reset, so ToggleFullscreen wouldn't work unless we have
  288.     // already set up the fullscreen focus window.
  289.     m_hwndRenderFullScreen = CreateWindow( _T("Fullscreen Window"), NULL,
  290.                                            WS_POPUP, CW_USEDEFAULT,
  291.                                            CW_USEDEFAULT, 100, 100,
  292.                                            GetTopLevelParent()->GetSafeHwnd(), 0L, NULL, 0L );
  293.  
  294.     // Note that for the MFC samples, the device window and focus window
  295.     // are not the same.
  296.     CD3DApplication::m_hWnd = m_hwndRenderWindow;
  297.     CD3DApplication::m_hWndFocus = m_hwndRenderFullScreen;
  298.     CD3DApplication::Create( AfxGetInstanceHandle() );
  299. }
  300.  
  301.  
  302.  
  303.  
  304. //-----------------------------------------------------------------------------
  305. // Name: OnChangeDevice()
  306. // Desc: Use hit the "Change Device.." button. Display the dialog for the user
  307. //       to select a new device/mode, and call Change3DEnvironment to
  308. //       use the new device/mode.
  309. //-----------------------------------------------------------------------------
  310. VOID CAppForm::OnChangeDevice()
  311. {
  312.     UserSelectNewDevice();
  313. }
  314.  
  315.  
  316.  
  317.  
  318. //-----------------------------------------------------------------------------
  319. // Name: AdjustWindowForChange()
  320. // Desc: Adjusts the window properties for windowed or fullscreen mode
  321. //-----------------------------------------------------------------------------
  322. HRESULT CAppForm::AdjustWindowForChange()
  323. {
  324.     if( m_bWindowed )
  325.     {
  326.         ::ShowWindow( m_hwndRenderFullScreen, SW_HIDE );
  327.         CD3DApplication::m_hWnd = m_hwndRenderWindow;
  328.     }
  329.     else
  330.     {
  331.         if( ::IsIconic( m_hwndRenderFullScreen ) )
  332.             ::ShowWindow( m_hwndRenderFullScreen, SW_RESTORE );
  333.         ::ShowWindow( m_hwndRenderFullScreen, SW_SHOW );
  334.         CD3DApplication::m_hWnd = m_hwndRenderFullScreen;
  335.     }
  336.     return S_OK;
  337. }
  338.  
  339.  
  340.  
  341.  
  342. //-----------------------------------------------------------------------------
  343. // Name: OnToggleFullScreen()
  344. // Desc: Called when user toggles the fullscreen mode
  345. //-----------------------------------------------------------------------------
  346. void CAppForm::OnToggleFullScreen()
  347. {
  348.     ToggleFullscreen();
  349. }
  350.  
  351.  
  352.  
  353.  
  354.