home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / directx / pplane / winmain.cpp < prev    next >
C/C++ Source or Header  |  1997-07-14  |  14KB  |  517 lines

  1. /*
  2. **-----------------------------------------------------------------------------
  3. **  File:       WinMain.cpp
  4. **  Purpose:    D3D sample showing DrawPrimitive functionality 
  5. **  Notes:
  6. **
  7. **  Copyright (c) 1995 - 1997 by Microsoft, all rights reserved
  8. **-----------------------------------------------------------------------------
  9. */
  10.  
  11. /*
  12. **-----------------------------------------------------------------------------
  13. **  Include files
  14. **-----------------------------------------------------------------------------
  15. */
  16.  
  17. // Note:  Define INITGUID in one and only one file!!!
  18. #define  INITGUID    
  19. #include "Common.h"
  20. #include "Debug.h"
  21. #include "WinMain.h"
  22. #include "WinProc.h"
  23. #include "DrvMgr.h"
  24. #include "D3DWin.h"
  25. #include "D3DScene.h"
  26.  
  27.  
  28. /*
  29. **-----------------------------------------------------------------------------
  30. **  Global Variables
  31. **-----------------------------------------------------------------------------
  32. */
  33.  
  34. // Application variables
  35. HINSTANCE    g_hMainInstance        = NULL;
  36. HWND        g_hMainWindow       = NULL;
  37. HACCEL      g_hMainAccel        = NULL;
  38.  
  39. LPCTSTR     g_szMainName        = TEXT ("PPlane");
  40. LPCTSTR     g_szMainClass        = TEXT ("PPlaneClass");
  41. LPCTSTR     g_szMainTitle        = TEXT ("Paper Plane Sample");
  42. LPCTSTR     g_szPaused          = TEXT ("Paused");
  43.  
  44. INT         g_nExitCode         = 0L;
  45.  
  46.  
  47. //
  48. //    D3D variables
  49. //
  50.  
  51. // Driver Management (enumeration of drivers, modes, D3D devices)
  52. DDDrvMgr    g_DrvMgr;
  53.  
  54. // D3D Window management
  55. LPD3DWindow        g_lpd3dWin;
  56.  
  57. // D3D Sample Scene
  58. LPD3DScene        g_lpd3dScene;
  59.                                                     
  60.  
  61.  
  62. /*
  63. **-----------------------------------------------------------------------------
  64. **  Local Function Prototypes
  65. **-----------------------------------------------------------------------------
  66. */
  67.  
  68. HRESULT CreateD3DWindow (
  69.     DWORD        dwStyleEx,            /* In:  Standard CreateWindowEx parameters */
  70.     LPCTSTR        lpszClass, 
  71.     LPCTSTR        lpszName, 
  72.     DWORD        dwStyle,
  73.     int            x, 
  74.     int            y, 
  75.     int            nWidth, 
  76.     int            nHeight,
  77.     HWND        hParent, 
  78.     HMENU        hMenu,
  79.     HINSTANCE    hInstance, 
  80.     LPVOID        lpParam, 
  81.  
  82.     BOOL        fUseZBuffer,        /* In:  Use Z-buffer */
  83.     HWND *        lphWnd,                /* Out:    Window handle */
  84.     LPD3DWindow * lpd3dWnd);        /* Out: Created D3DWindow pointer */
  85.  
  86. HRESULT DestroyD3DWindow (void);
  87.  
  88. extern void GetDXVersion (LPDWORD pdwDXVersion, LPDWORD pdwDXPlatform);
  89.  
  90.  
  91. /*
  92. **-----------------------------------------------------------------------------
  93. **  Function Definitions
  94. **-----------------------------------------------------------------------------
  95. */
  96.  
  97. /*
  98. **-----------------------------------------------------------------------------
  99. **  Name:       InitMain
  100. **  Purpose:    Initialize Application
  101. **-----------------------------------------------------------------------------
  102. */
  103.  
  104. BOOL InitMain (void)
  105. {
  106.     DWORD dwDxVersion  = 0L;
  107.     DWORD dwDxPlatform = 0L;
  108.  
  109.     DPF (DEBUG_DETAILS, TEXT("InitMain"));
  110.  
  111.     // Get hInstance handle
  112.     g_hMainInstance = (HINSTANCE) GetModuleHandle (NULL);
  113.     if (NULL == g_hMainInstance)
  114.     {
  115.         // Error - GetModule Handle 
  116.  
  117.         DPF (DEBUG_CRITICAL, TEXT("InitMain - GetModuleHandle() failed."));
  118.         return FALSE;
  119.     }
  120.  
  121.     // Check for DX5.0 or greater
  122.     GetDXVersion (&dwDxVersion, &dwDxPlatform);
  123.     if (dwDxVersion < 0x500)
  124.     {
  125.         MessageBox (NULL, TEXT("This App requires DX 5.0 or greater\r\nin order to run."), TEXT("Error"), MB_OK);
  126.         return FALSE;
  127.     }
  128.  
  129.     // Init main window
  130.     if (! InitMainWindow ())
  131.         return FALSE;
  132.  
  133.     DPF (DEBUG_DETAILS, TEXT("InitMain - Success"));
  134.  
  135.     // Success
  136.     return TRUE;
  137. } // End InitMain
  138.  
  139.  
  140.  
  141. /*
  142. **-----------------------------------------------------------------------------
  143. **  Name:       FiniMain
  144. **  Purpose:    Cleanup Application
  145. **-----------------------------------------------------------------------------
  146. */
  147.  
  148. void FiniMain (void)
  149. {
  150.     DestroyD3DWindow ();
  151.     UnregisterClass (g_szMainClass, g_hMainInstance);
  152. } // End FiniMain
  153.   
  154.  
  155.  
  156. /*
  157. **-----------------------------------------------------------------------------
  158. **  Name:       RunMain
  159. **  Purpose:    Main Message Pump
  160. **-----------------------------------------------------------------------------
  161. */
  162.  
  163. void RunMain (void)
  164. {
  165.     MSG msg;
  166.  
  167.     DPF (DEBUG_DETAILS, TEXT("RunMain - Enter"));
  168.  
  169.     if ((! g_hMainInstance) || (! g_hMainWindow))
  170.     {
  171.         return;
  172.     }
  173.  
  174.     g_hMainAccel = LoadAccelerators (g_hMainInstance, MAKEINTRESOURCE (IDR_MAIN_ACCEL));
  175.   
  176.     //
  177.     //  The Main Message loop
  178.     //
  179.     //  Note:  In order to catch Idle behavior we use PeekMessage instead of GetMessage
  180.     //
  181.     while (TRUE)
  182.     {
  183.         while (TRUE)
  184.         {
  185.  
  186.             if (! PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  187.             {
  188.                 // App is Idle
  189.                 break;
  190.             }
  191.  
  192.             // Exit App ?!?
  193.             if (msg.message == WM_QUIT)
  194.             {
  195.                 DPF (DEBUG_DETAILS, TEXT("RunMain - Exit"));
  196.                 g_nExitCode = msg.wParam;
  197.                 return;
  198.             }
  199.  
  200.             if (g_hMainAccel)
  201.             {
  202.                 if (! TranslateAccelerator (g_hMainWindow, g_hMainAccel, &msg))
  203.                 {
  204.                     TranslateMessage (&msg);
  205.                     DispatchMessage (&msg);
  206.                 }
  207.             }
  208.             else
  209.             {
  210.                 TranslateMessage (&msg);
  211.                 DispatchMessage (&msg);
  212.             }
  213.         }
  214.       
  215.         // Do some Idle processing
  216.         OnIdle (g_hMainWindow);
  217.     } // End While
  218.  
  219. } // End RunMain
  220.  
  221.  
  222.   
  223. /*
  224. **-----------------------------------------------------------------------------
  225. **  Name:       WinMain
  226. **  Purpose:    Application EntryPoint
  227. **-----------------------------------------------------------------------------
  228. */
  229.  
  230. INT WINAPI WinMain(
  231.     HINSTANCE   hInstance,        // handle to current instance
  232.     HINSTANCE   hPrevInstance,    // handle to previous instance
  233.     LPSTR       lpCmdLine,        // pointer to command line
  234.     INT         nShowCmd)         // show state of window
  235. {
  236.     g_nExitCode = 0;
  237.     
  238.     DPF (DEBUG_DETAILS, TEXT("WinMain - Enter"));
  239.  
  240.     // Initialize App
  241.     if (! InitMain ())
  242.         return g_nExitCode;
  243.  
  244.     // Main Message Loop
  245.     RunMain ();
  246.  
  247.     // Cleanup App
  248.     FiniMain ();
  249.  
  250.     // Success
  251.     DPF (DEBUG_DETAILS, TEXT("WinMain - Exit, status = %08lX"), (DWORD)g_nExitCode);
  252.     return g_nExitCode;
  253. } // End WinMain
  254.  
  255.  
  256.  
  257.  /*
  258. **-----------------------------------------------------------------------------
  259. **  Name:       InitMainWindow
  260. **  Purpose:    
  261. **-----------------------------------------------------------------------------
  262. */
  263.  
  264. BOOL InitMainWindow (void)
  265. {
  266.     HRESULT        hResult;
  267.     WNDCLASS    wc;
  268.     HWND        hWnd;
  269.     RECT        rWin;
  270.     DWORD        dwStyle, dwStyleEx;
  271.     DWORD        dwWidth, dwHeight;
  272.  
  273.     DPF (DEBUG_DETAILS, TEXT("InitMainWindow - Enter"));
  274.  
  275.     // Register Main Window Class
  276.     wc.style            = CS_DBLCLKS; 
  277.     wc.lpfnWndProc      = (WNDPROC)D3DWindowProc;    // D3D Window proc
  278.     wc.cbClsExtra       = 0; 
  279.     wc.cbWndExtra       = 4;                        // Reserve space for lpd3dWindow pointer
  280.     wc.hInstance        = g_hMainInstance; 
  281.     wc.hIcon            = LoadIcon (g_hMainInstance, MAKEINTRESOURCE (IDI_MAIN_ICON));
  282.     wc.hCursor          = LoadCursor ((HINSTANCE)NULL, IDC_ARROW); 
  283.     wc.hbrBackground    = (HBRUSH)GetStockObject (WHITE_BRUSH);
  284.     wc.lpszMenuName     = MAKEINTRESOURCE (IDR_MAIN_MENU);
  285.     wc.lpszClassName    = g_szMainClass; 
  286.  
  287.     if (! RegisterClass (&wc))
  288.     {
  289.         // Error - Register class failed
  290.         DPF (DEBUG_CRITICAL, TEXT("InitMainWindow - RegisterClass failed."));
  291.         return FALSE;
  292.     }
  293.  
  294.     // Calculate Initial window size to 
  295.     // support client area of 256 x 256
  296.     rWin.left    = 0;
  297.     rWin.top    = 0;
  298.     rWin.right  = DEF_WIN_SIZE_X;
  299.     rWin.bottom    = DEF_WIN_SIZE_Y;
  300.  
  301.     // Setup a normal window with the following exceptions
  302.     //        - No Minimize button (not supported yet...)
  303.     //        - No Maximize button (not supported yet...)
  304.     dwStyleEx = WS_EX_OVERLAPPEDWINDOW;
  305.     dwStyle   = WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME | WS_SYSMENU;
  306.     AdjustWindowRectEx (&rWin, dwStyle, TRUE, dwStyleEx);
  307.  
  308.     dwWidth     = rWin.right - rWin.left;
  309.     dwHeight = rWin.bottom - rWin.top;
  310.  
  311.     //
  312.     // Create Main Window
  313.     //
  314.     hResult = CreateD3DWindow (dwStyleEx, 
  315.                                g_szMainClass,
  316.                                g_szMainTitle,
  317.                                dwStyle,
  318.                                CW_USEDEFAULT, CW_USEDEFAULT,
  319.                                dwWidth, dwHeight,
  320.                                NULL,
  321.                                NULL,
  322.                                g_hMainInstance,
  323.                                NULL,
  324.                                TRUE,    // Use Z-Buffer
  325.                                &hWnd,
  326.                                &g_lpd3dWin);
  327.     if (FAILED (hResult))
  328.     {
  329.         // Error - CreateWindow failed
  330.         DPF (DEBUG_CRITICAL, TEXT("InitMainWindow - CreateWindow failed."));
  331.         return FALSE;
  332.     }
  333.  
  334.     //
  335.     // Create our D3D scene
  336.     //
  337.  
  338.     g_lpd3dScene = new D3DScene;
  339.     if (! g_lpd3dScene)
  340.     {
  341.         // Error - Construct D3D Scene failed
  342.         DPF (DEBUG_CRITICAL, TEXT("InitMainWindow - Not enough memory for D3D scene."));
  343.         return FALSE;
  344.     }
  345.  
  346.     // Attach our D3DScene to the Window
  347.     hResult = g_lpd3dWin->AttachScene (g_lpd3dScene);
  348.     if (FAILED (hResult))
  349.     {
  350.         // Error - Attaching our D3D Scene to our D3D Window failed
  351.         DPF (DEBUG_CRITICAL, TEXT("InitMainWindow - AttachScene failed."));
  352.         return FALSE;
  353.     }
  354.  
  355.     g_hMainWindow = hWnd;
  356.  
  357.     // Success
  358.     DPF (DEBUG_DETAILS, TEXT("InitMainWindow - Success"));
  359.     return TRUE;
  360. } // End InitMainWindow
  361.  
  362.   
  363. /*
  364. **-----------------------------------------------------------------------------
  365. **  DD/D3D Function Definitions
  366. **-----------------------------------------------------------------------------
  367. */
  368.  
  369. /*
  370. **-----------------------------------------------------------------------------
  371. ** Name:    CreateD3DWindow
  372. ** Purpose: Creates a simple D3D window 
  373. ** Notes:   
  374. **-----------------------------------------------------------------------------
  375. */
  376.  
  377. HRESULT CreateD3DWindow (
  378.     DWORD        dwStyleEx,            /* In:  CreateWindowEx parameters */
  379.     LPCTSTR        lpszClass, 
  380.     LPCTSTR        lpszName, 
  381.     DWORD        dwStyle,
  382.     int            x, 
  383.     int            y, 
  384.     int            nWidth, 
  385.     int            nHeight,
  386.     HWND        hParent, 
  387.     HMENU        hMenu,
  388.     HINSTANCE    hInstance, 
  389.     LPVOID        lpParam, 
  390.  
  391.     BOOL        fUseZBuffer,        /* In:  Use Z-buffer */
  392.     HWND *        lphWindow,            /* Out:    Window handle */
  393.     LPD3DWindow * lpd3dWindow)        /* Out: Created D3DWindow pointer */
  394. {
  395.     HRESULT             hResult;
  396.     HWND                hWnd;
  397.     LPD3DWindow         lpd3dWin;
  398.  
  399.     // Check Parameters
  400.     if (! lpszClass)
  401.     {
  402.         REPORTERR (DDERR_INVALIDPARAMS);
  403.         return DDERR_INVALIDPARAMS;
  404.     }
  405.  
  406.     // Fill in defaults, if missing
  407.     if (! hInstance)
  408.         hInstance = GetModuleHandle (NULL);
  409.  
  410.     if (! hParent)
  411.         hParent = GetDesktopWindow ();
  412.  
  413.     // Create D3D Window 
  414.     lpd3dWin = new D3DWindow ();
  415.     if (! lpd3dWin)
  416.     {
  417.         // Error, not enough memory
  418.         REPORTERR (DDERR_OUTOFMEMORY);
  419.         return DDERR_OUTOFMEMORY;
  420.     }
  421.  
  422.     // Create Extended Window
  423.     hWnd = CreateWindowEx (
  424.                 dwStyleEx,                // extended window style
  425.                 lpszClass,                // window class
  426.                 lpszName,                // window name (title)
  427.                 dwStyle,                // window style
  428.                 x,                        // horizontal position of window
  429.                 y,                        // vertical position of window
  430.                 nWidth,                    // window width
  431.                 nHeight,                // window height
  432.                 hParent,                // handle to parent window
  433.                 hMenu,                  // handle to menu
  434.                 hInstance,              // handle to app instance
  435.                 lpParam);               // Extra Data
  436.     if (! hWnd)
  437.     {
  438.         // Error, unable to create window
  439.         delete lpd3dWin;
  440.         REPORTERR (DDERR_GENERIC);
  441.         return DDERR_GENERIC;
  442.     }
  443.  
  444.  
  445.     //
  446.     //  Gotcha:  You must Show the window before you switch to Fullscreen
  447.     //           Otherwise the following can happen.
  448.     //           1. SetCooperativeLevel (DDSCL_EXCLUSIVE) works
  449.     //             2. SetDisplayMode (w,h,bpp) loses the exclusive mode state
  450.     //                because the window is not fully initialized.
  451.     //                (DDraw gets confused by the responses it gets from the window)
  452.     //             3. CreateSurface (PRIMARY | FLIP) fails with DDERR_NOEXCLUSIVEMODE.
  453.     //                As the exlusive mode state has been lost
  454.     //
  455.  
  456.     // Show the Window and Paint it's contents
  457.     ShowWindow (hWnd, SW_SHOWDEFAULT);
  458.     UpdateWindow (hWnd);
  459.  
  460.     // Create D3DWindow from hWnd
  461.     hResult = lpd3dWin->Create (hWnd);
  462.     if (FAILED (hResult))
  463.     {
  464.         // Error, unable to create window
  465.         delete lpd3dWin;
  466.         DestroyWindow (hWnd);
  467.         return hResult;
  468.     }
  469.  
  470.     // Return results
  471.     if (lphWindow)
  472.         *lphWindow = hWnd;
  473.     if (lpd3dWindow)
  474.         *lpd3dWindow = lpd3dWin;
  475.  
  476.     // Success
  477.     return DD_OK;
  478. } // End CreateD3DWindow
  479.  
  480.  
  481.  
  482. /*
  483. **-----------------------------------------------------------------------------
  484. ** Name:    DestroyD3DWindow
  485. ** Purpose: Destroys a D3DWindow (window and D3DWindow object)
  486. **-----------------------------------------------------------------------------
  487. */
  488.  
  489. HRESULT DestroyD3DWindow (void)
  490. {
  491.     // Check Parameters
  492.     if ((g_hMainWindow) && (IsWindow (g_hMainWindow)))
  493.     {
  494.         DestroyWindow (g_hMainWindow);
  495.         g_hMainWindow = NULL;
  496.     }
  497.  
  498.     // Free memory associated with D3DWindow Object
  499.     if (g_lpd3dWin)
  500.     {
  501.         delete g_lpd3dWin;
  502.         g_lpd3dWin = NULL;
  503.     }
  504.  
  505.     // Sucess
  506.     return DD_OK;
  507. } // End DestroyD3DWindow
  508.  
  509.  
  510. /*
  511. **-----------------------------------------------------------------------------
  512. **  End of File
  513. **-----------------------------------------------------------------------------
  514. */
  515.  
  516.  
  517.