home *** CD-ROM | disk | FTP | other *** search
/ Beginning Direct3D Game Programming / Direct3D.iso / directx / dxf / samples / multimedia / direct3d / mfctex / mfctex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  11.3 KB  |  335 lines

  1. //-----------------------------------------------------------------------------
  2. // File: MFCTex.cpp
  3. //
  4. // Desc: Main file for the D3D multitexture app that uses MFC.
  5. //
  6. // Copyright (c) 1997-2000 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. //-----------------------------------------------------------------------------
  77. // Global data and objects
  78. //-----------------------------------------------------------------------------
  79. CApp          g_App;
  80. CAppForm*     g_AppFormView = NULL;
  81. TCHAR*        g_strAppTitle = _T("MFCTex: MFC Multitexture Sample");
  82.  
  83.  
  84.  
  85.  
  86. //-----------------------------------------------------------------------------
  87. // Name: FullScreenWndProc()
  88. // Desc: The WndProc funtion used when the app is in fullscreen mode. This is
  89. //       needed simply to trap the ESC key.
  90. //-----------------------------------------------------------------------------
  91. LRESULT CALLBACK FullScreenWndProc( HWND hWnd, UINT msg, WPARAM wParam,
  92.                                     LPARAM lParam )
  93. {
  94.     if( msg == WM_CLOSE )
  95.     {
  96.         // User wants to exit, so go back to windowed mode and exit for real
  97.         g_AppFormView->OnToggleFullScreen();
  98.         g_App.GetMainWnd()->PostMessage( WM_CLOSE, 0, 0 );
  99.     }
  100.  
  101.     if( msg == WM_SETCURSOR )
  102.     {
  103.         SetCursor( NULL );
  104.     }
  105.  
  106.     if( msg == WM_KEYUP && wParam == VK_ESCAPE )
  107.     {
  108.         // User wants to leave fullscreen mode
  109.         g_AppFormView->OnToggleFullScreen();
  110.     }
  111.  
  112.     return DefWindowProc( hWnd, msg, wParam, lParam );
  113. }
  114.  
  115.  
  116.  
  117.  
  118. //-----------------------------------------------------------------------------
  119. // Name: CheckForLostFullscreen()
  120. // Desc: If fullscreen and device was lost (probably due to alt-tab), 
  121. //       automatically switch to windowed mode
  122. //-----------------------------------------------------------------------------
  123. HRESULT CAppForm::CheckForLostFullscreen()
  124. {
  125.     HRESULT hr;
  126.  
  127.     if( m_bWindowed )
  128.         return S_OK;
  129.  
  130.     if( FAILED( hr = m_pd3dDevice->TestCooperativeLevel() ) )
  131.         ForceWindowed();
  132.  
  133.     return S_OK;
  134. }
  135.  
  136.  
  137.  
  138.  
  139. //-----------------------------------------------------------------------------
  140. // Name: PreCreateWindow()
  141. // Desc: Change the window style (so it cannot maximize or be sized) before
  142. //       the main frame window is created.
  143. //-----------------------------------------------------------------------------
  144. BOOL CAppFrameWnd::PreCreateWindow( CREATESTRUCT& cs )
  145. {
  146.     cs.style = WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX;
  147.     return CFrameWnd::PreCreateWindow( cs );
  148. }
  149.  
  150.  
  151.  
  152.  
  153. //-----------------------------------------------------------------------------
  154. // Name: InitInstance()
  155. // Desc: This is the main entry point for the application. The MFC window stuff
  156. //       is initialized here. See also the main initialization routine for the
  157. //       CAppForm class, which is called indirectly from here.
  158. //-----------------------------------------------------------------------------
  159. BOOL CApp::InitInstance()
  160. {
  161.     // Asscociate the MFC app with the frame window and doc/view classes
  162.     AddDocTemplate( new CSingleDocTemplate( IDR_MAINFRAME, 
  163.                                             RUNTIME_CLASS(CAppDoc),
  164.                                             RUNTIME_CLASS(CAppFrameWnd),
  165.                                             RUNTIME_CLASS(CAppForm) ) );
  166.  
  167.     // Dispatch commands specified on the command line (req'd by MFC). This
  168.     // also initializes the the CAppDoc, CAppFrameWnd, and CAppForm classes.
  169.     CCommandLineInfo cmdInfo;
  170.     ParseCommandLine( cmdInfo );
  171.     if( !ProcessShellCommand( cmdInfo ) )
  172.         return FALSE;
  173.  
  174.     if( !g_AppFormView->IsReady() )
  175.         return FALSE;
  176.  
  177.     // The formview window has been initialized, so show and update it.
  178.     m_pMainWnd->MoveWindow( 0, 0, 640, 350, TRUE );
  179.     m_pMainWnd->SetWindowText( g_strAppTitle );
  180.     m_pMainWnd->UpdateWindow();
  181.  
  182.     return TRUE;
  183. }
  184.  
  185.  
  186.  
  187.  
  188. //-----------------------------------------------------------------------------
  189. // Name: OnIdle()
  190. // Desc: Uses idle time to render the 3D scene.
  191. //-----------------------------------------------------------------------------
  192. BOOL CApp::OnIdle( LONG )
  193. {
  194.     // Do not render if the app is minimized
  195.     if( m_pMainWnd->IsIconic() )
  196.         return FALSE;
  197.  
  198.     // Update and render a frame
  199.     if( g_AppFormView->IsReady() )
  200.     {
  201.         g_AppFormView->CheckForLostFullscreen();
  202.         g_AppFormView->RenderScene();
  203.     }
  204.  
  205.     // Keep requesting more idle time
  206.     return TRUE;
  207. }
  208.  
  209.  
  210.  
  211.  
  212. //-----------------------------------------------------------------------------
  213. // Name: CAppForm()
  214. // Desc: Constructor for the dialog resource form
  215. //-----------------------------------------------------------------------------
  216. CAppForm::CAppForm()
  217.          :CFormView( IDD_FORMVIEW )
  218. {
  219.     g_AppFormView       = this;
  220.     m_bActive           = FALSE;
  221.     m_bReady            = FALSE;
  222.     m_bWindowed         = TRUE;
  223.     
  224.     m_dwAdapter         = 0L;
  225.     m_pD3D              = NULL;
  226.     m_pd3dDevice        = NULL;
  227.     m_bUseDepthBuffer   = TRUE;
  228. }
  229.  
  230.  
  231.  
  232.  
  233. //-----------------------------------------------------------------------------
  234. // Name: ~CAppForm()
  235. // Desc: Destructor for the dialog resource form. Shuts down the app
  236. //-----------------------------------------------------------------------------
  237. CAppForm::~CAppForm()
  238. {
  239.     Cleanup3DEnvironment();
  240. }
  241.  
  242.  
  243.  
  244.  
  245. //-----------------------------------------------------------------------------
  246. // Name: OnInitialUpdate()
  247. // Desc: When the AppForm object is created, this function is called to
  248. //       initialize it. Here we getting access ptrs to some of the controls,
  249. //       and setting the initial state of some of them as well.
  250. //-----------------------------------------------------------------------------
  251. VOID CAppForm::OnInitialUpdate()
  252. {
  253.     // Update the UI
  254.     CFormView::OnInitialUpdate();
  255.     InitializeUIControls();
  256.  
  257.     // Save static reference to the render window
  258.     m_hwndRenderWindow = GetDlgItem(IDC_RENDERVIEW)->GetSafeHwnd();
  259.  
  260.     // Register a class for a fullscreen window
  261.     WNDCLASS wndClass = { CS_HREDRAW | CS_VREDRAW, FullScreenWndProc, 0, 0, NULL,
  262.                           NULL, NULL, (HBRUSH)GetStockObject(WHITE_BRUSH), NULL,
  263.                           _T("Fullscreen Window") };
  264.     RegisterClass( &wndClass );
  265.  
  266.     // We create the fullscreen window (not visible) at startup, so it can
  267.     // be the focus window.  The focus window can only be set at CreateDevice
  268.     // time, not in a Reset, so ToggleFullscreen wouldn't work unless we have
  269.     // already set up the fullscreen focus window.
  270.     m_hwndRenderFullScreen = CreateWindow( _T("Fullscreen Window"), NULL,
  271.                                            WS_POPUP, CW_USEDEFAULT,
  272.                                            CW_USEDEFAULT, 100, 100,
  273.                                            GetTopLevelParent()->GetSafeHwnd(), 0L, NULL, 0L );
  274.  
  275.     // Note that for the MFC samples, the device window and focus window
  276.     // are not the same.
  277.     CD3DApplication::m_hWnd = m_hwndRenderWindow;
  278.     CD3DApplication::m_hWndFocus = m_hwndRenderFullScreen;
  279.     CD3DApplication::Create( AfxGetInstanceHandle() );
  280. }
  281.  
  282.  
  283.  
  284.  
  285. //-----------------------------------------------------------------------------
  286. // Name: OnChangeDevice()
  287. // Desc: Use hit the "Change Device.." button. Display the dialog for the user
  288. //       to select a new device/mode, and call Change3DEnvironment to
  289. //       use the new device/mode.
  290. //-----------------------------------------------------------------------------
  291. VOID CAppForm::OnChangeDevice()
  292. {
  293.     UserSelectNewDevice();
  294. }
  295.  
  296.  
  297.  
  298.  
  299. //-----------------------------------------------------------------------------
  300. // Name: AdjustWindowForChange()
  301. // Desc: Adjusts the window properties for windowed or fullscreen mode
  302. //-----------------------------------------------------------------------------
  303. HRESULT CAppForm::AdjustWindowForChange()
  304. {
  305.     if( m_bWindowed )
  306.     {
  307.         ::ShowWindow( m_hwndRenderFullScreen, SW_HIDE );
  308.         CD3DApplication::m_hWnd = m_hwndRenderWindow;
  309.     }
  310.     else
  311.     {
  312.         if( ::IsIconic( m_hwndRenderFullScreen ) )
  313.             ::ShowWindow( m_hwndRenderFullScreen, SW_RESTORE );
  314.         ::ShowWindow( m_hwndRenderFullScreen, SW_SHOW );
  315.         CD3DApplication::m_hWnd = m_hwndRenderFullScreen;
  316.     }
  317.     return S_OK;
  318. }
  319.  
  320.  
  321.  
  322.  
  323. //-----------------------------------------------------------------------------
  324. // Name: OnToggleFullScreen()
  325. // Desc: Called when user toggles the fullscreen mode
  326. //-----------------------------------------------------------------------------
  327. void CAppForm::OnToggleFullScreen()
  328. {
  329.     ToggleFullscreen();
  330. }
  331.  
  332.  
  333.  
  334.  
  335.