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 / rockem / winmain.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-10  |  10.7 KB  |  402 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1997 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File: winmain.cpp
  6.  *
  7.  ***************************************************************************/
  8.  
  9. // Defines....
  10. #define CLASS_NAME                              "RockEm3D_Class"
  11.  
  12. // Includes....
  13. #include "windows.h"
  14. #include "resource.h"
  15. #include "winmain.h"
  16.  
  17. #include "directx.h"
  18. #include "rm.h"
  19.  
  20. #include "control.h"
  21.  
  22. #include "midi.h"
  23.  
  24. // Globals....
  25. HINSTANCE       g_hInst = NULL;
  26. HWND            g_hWnd  = NULL;
  27.  
  28. BOOL            g_bActive       = FALSE;
  29. BOOL            g_bFirstActive  = TRUE;
  30. BOOL            g_bErrorOccured = FALSE;
  31. char            g_sError[2048];
  32. BOOL            g_bShowStats    = FALSE;
  33. BOOL            g_bMusicPaused  = FALSE;
  34. BOOL            g_bSoundPaused  = FALSE;
  35.  
  36. // Externals....
  37. extern DWORD    g_dwCurrMode; // Defined in DIRECTX.CPP
  38.  
  39. //------------------------------------------------------------------
  40. // 
  41. // Function     : RegError()
  42. //
  43. // Purpose      : Registers an error
  44. //
  45. //------------------------------------------------------------------
  46.  
  47. void RegError(char *sErr)
  48. {
  49.     sprintf(g_sError, "%s\r\n", sErr);
  50.     OutputDebugString(g_sError);
  51.     g_bErrorOccured = TRUE;
  52. }
  53.  
  54.  
  55. //------------------------------------------------------------------
  56. // 
  57. // Function     : InitClass()
  58. //
  59. // Purpose      : Initialises and registers window class
  60. //
  61. //------------------------------------------------------------------
  62.  
  63. BOOL InitClass(HINSTANCE hInst)
  64. {
  65.     WNDCLASS    wndClass;
  66.  
  67.     // Fill out WNDCLASS info
  68.     wndClass.style              = CS_HREDRAW | CS_VREDRAW;
  69.     wndClass.lpfnWndProc        = WndProc;
  70.     wndClass.cbClsExtra         = 0;
  71.     wndClass.cbWndExtra         = 0;
  72.     wndClass.hInstance          = hInst;
  73.     wndClass.hIcon              = LoadIcon(hInst, "ROCKEM3D");
  74.     wndClass.hCursor            = LoadCursor(NULL, IDC_ARROW);
  75.     wndClass.hbrBackground      = (HBRUSH) GetStockObject(BLACK_BRUSH);
  76.     wndClass.lpszMenuName       = NULL;
  77.     wndClass.lpszClassName      = CLASS_NAME;
  78.     
  79.     if (!RegisterClass(&wndClass)) return FALSE;
  80.  
  81.     // Everything's perfect
  82.     return TRUE;
  83. }
  84.  
  85. //------------------------------------------------------------------
  86. // 
  87. // Function     : InitWindow()
  88. //
  89. // Purpose      : Initialises and creates the main window
  90. //
  91. //------------------------------------------------------------------
  92.  
  93. BOOL InitWindow(HINSTANCE hInst, int nCmdShow)
  94. {
  95.     // Create a window
  96.     g_hWnd = CreateWindowEx(WS_EX_APPWINDOW,
  97.                             CLASS_NAME, 
  98.                             "RockEm3D Demo",
  99.                             WS_POPUP | WS_SYSMENU,
  100.                             0, 0,
  101.                             GetSystemMetrics(SM_CXSCREEN),
  102.                             GetSystemMetrics(SM_CYSCREEN),
  103.                             NULL,
  104.                             NULL,
  105.                             hInst,
  106.                             NULL);
  107.  
  108.     // Return false if window creation failed
  109.     if (!g_hWnd) return FALSE;
  110.     
  111.     // Show the window
  112.     ShowWindow(g_hWnd, SW_SHOWNORMAL);
  113.  
  114.     // Update the window
  115.     UpdateWindow(g_hWnd);
  116.     
  117.     // Everything's perfect
  118.     return TRUE;
  119. }
  120.  
  121. //------------------------------------------------------------------
  122. // 
  123. // Function     : WndProc()
  124. //
  125. // Purpose      : Windows procedure to handle messages
  126. //
  127. //------------------------------------------------------------------
  128.  
  129. long FAR PASCAL WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  130. {
  131.     // D3DVECTOR used for 3D position of sound
  132.     static D3DVECTOR d3dvPos= {D3DVAL(0), D3DVAL(0), D3DVAL(0)};
  133.     
  134.     // Handle messages
  135.     switch (message)
  136.     {
  137.         case WM_KEYDOWN:
  138.         {
  139.             switch (wParam)
  140.             {
  141.                 case VK_ESCAPE:
  142.                 {                                                       
  143.                     // Time to quit....
  144.                     PostMessage(g_hWnd, WM_CLOSE ,0 ,0);
  145.                 }
  146.                 break;
  147.  
  148.                 case VK_F5:
  149.                 {
  150.                     // Toggle stats
  151.                     g_bShowStats = !g_bShowStats;
  152.                 }
  153.                 break;
  154.  
  155.                 case 'M':
  156.                 {
  157.                     // Toggle music
  158.                     if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
  159.                     {
  160.                         if (!g_bMusicPaused)
  161.                         {
  162.                             PauseMidi();
  163.                         }
  164.                         else
  165.                         {
  166.                             ResumeMidi();
  167.                         }
  168.                         g_bMusicPaused = !g_bMusicPaused;
  169.                     }
  170.                 }
  171.                 break;
  172.                 
  173.                 case 'S':
  174.                 {
  175.                     // Toggle sound
  176.                     if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
  177.                     {
  178.                         g_bSoundPaused = !g_bSoundPaused;
  179.  
  180.                         if (g_bSoundPaused)
  181.                         {
  182.                             // Kill all the sound effects
  183.                             StopAllSounds();
  184.                         }
  185.                         else
  186.                         {
  187.                             // Start the crowd noise looping
  188.                          PlaySoundDS(CROWD_LOOP,d3dvPos, DSBPLAY_LOOPING);
  189.                         }
  190.                     }
  191.                 }
  192.                 break;
  193.  
  194.                 case VK_F6 :
  195.                 {
  196.                     // Go up a video mode
  197.                     if(!EnterNextVideoMode()) 
  198.                         PostMessage(g_hWnd, WM_CLOSE ,0 ,0);
  199.                 }
  200.                 break;
  201.  
  202.                 case VK_F7 :
  203.                 {
  204.                     // Go down a video mode
  205.                     if(!EnterPrevVideoMode())
  206.                         PostMessage(g_hWnd, WM_CLOSE ,0 ,0);
  207.                 }
  208.                 break;
  209.  
  210.                 case VK_END :
  211.                 {
  212.                     // Go to highest video mode
  213.                     if(!EnterLowestVideoMode())
  214.                         PostMessage(g_hWnd, WM_CLOSE ,0 ,0);
  215.                 }
  216.                 break;
  217.  
  218.                 case VK_HOME :
  219.                 {
  220.                     // Go to lowest video mode
  221.                     if(!EnterHighestVideoMode()) 
  222.                         PostMessage(g_hWnd, WM_CLOSE ,0 ,0);
  223.                 }
  224.                 break;
  225.             }
  226.         }
  227.         break;
  228.  
  229.         case WM_SYSCOMMAND:
  230.         {
  231.             switch (wParam)
  232.             {
  233.                 // Trap ALT so it doesn't pause the app
  234.                 case SC_KEYMENU :
  235.                 {
  236.                     return 0;
  237.                 }
  238.                 break;
  239.             }
  240.         }       
  241.         
  242.         case WM_ACTIVATEAPP:
  243.         {
  244.             // Determine whether app is being activated or not
  245.             g_bActive = (BOOL)wParam ? TRUE : FALSE;
  246.  
  247.             if (g_bActive)
  248.             {
  249.                 while (ShowCursor(FALSE) > 0) { };
  250.                 if (!g_bMusicPaused) ResumeMidi();
  251.             }
  252.             else
  253.             {
  254.                 ShowCursor(TRUE);
  255.                 PauseMidi();
  256.             }
  257.         }
  258.         break;
  259.         
  260.         case WM_CLOSE:
  261.         {                       
  262.             DestroyWindow(g_hWnd);
  263.         }
  264.         break;
  265.  
  266.         case WM_DESTROY:
  267.         {                       
  268.             // Stop midi music
  269.             StopMidi();
  270.  
  271.             // Destroy scene
  272.             TermScene();
  273.             
  274.             // Terminate all the DirectX objects, surfaces devices etc
  275.             TermDirectX();
  276.  
  277.             // Show the mouse
  278.             ShowCursor(TRUE);
  279.  
  280.             // Time to leave this mortal coil
  281.             PostQuitMessage(0);
  282.         }
  283.         break;
  284.         
  285.         case MCI_NOTIFY:
  286.         {
  287.             if (wParam == MCI_NOTIFY_SUCCESSFUL)
  288.             {
  289.                 ReplayMidi();
  290.             }
  291.         }
  292.         break;
  293.     }
  294.     
  295.     return DefWindowProc(hWnd, message, wParam, lParam);
  296. }
  297.  
  298. //------------------------------------------------------------------
  299. // 
  300. // Function     : WinMain()
  301. //
  302. // Purpose      : Entry point to application
  303. //
  304. //------------------------------------------------------------------
  305.  
  306. int FAR PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
  307. {
  308.     MSG msg;
  309.  
  310.     // Set global handle
  311.     g_hInst = hInst;    
  312.  
  313.     // Initialise window class
  314.     if (!InitClass(hInst)) return 1;
  315.  
  316.     // Initialise window
  317.     if (!InitWindow(hInst, nCmdShow)) return 1;
  318.  
  319.     // Initialise DirectX objects (Termination is handled in WM_DESTROY)
  320.     if (!InitDirectX())
  321.     {
  322.             DestroyWindow(g_hWnd);
  323.             return 1;
  324.     }
  325.  
  326.     // Show the splash screen
  327.     DoSplashScreen(2000);
  328.  
  329.     // Load the scene
  330.     if (!InitScene()) 
  331.     {
  332.             DestroyWindow(g_hWnd);
  333.             return 1;
  334.     }
  335.  
  336.     // Release the splash screen
  337.     ReleaseSplashScreen();
  338.  
  339.     // Set DirectDraw exclusive mode here so that the splash could stay
  340.     // up during initialisation if we are using a different DirectDraw device
  341.     // that could not support 640x480x8 for hardware rendering.
  342.     if (!SetDirectDrawExclusiveMode())
  343.     {
  344.             RegError("Could not set exclusive mode!");
  345.             return FALSE;
  346.     }
  347.  
  348.     // Hide the mouse
  349.     ShowCursor(FALSE);
  350.  
  351.     // Enter video mode set in g_dwCurMode
  352.     if (!EnterVideoMode(g_dwCurrMode))
  353.     {
  354.             DestroyWindow(g_hWnd);
  355.             return 1;
  356.     }
  357.  
  358.     // Load accelerators
  359.     HACCEL hAccel = LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_ACCEL));
  360.  
  361.     // Start the music!
  362.     PlayMidi("RockEm3D.mid");
  363.  
  364.     // Pump messages via a PeekMessage loop
  365.     while (TRUE)
  366.     {
  367.         while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  368.         {
  369.             if (msg.message == WM_QUIT)
  370.             {                                                           
  371.                 PostQuitMessage(msg.wParam);
  372.                 return 1;
  373.             }
  374.  
  375.             if (hAccel && (msg.hwnd == g_hWnd))
  376.             {
  377.                 TranslateAccelerator(g_hWnd, hAccel, &msg);
  378.             }
  379.  
  380.             TranslateMessage(&msg);
  381.             DispatchMessage(&msg);
  382.         }
  383.  
  384.         // Perform any neccessary updating during idle time
  385.         if (g_bActive)
  386.         {
  387.             // Update everything
  388.             CheckInputAndUpdate();
  389.             
  390.             // Render the current scene
  391.             if (!RenderScene())
  392.             {
  393.                 DestroyWindow(g_hWnd);
  394.                 return 1;
  395.             }
  396.         }
  397.     }
  398.  
  399.     // Exit WinMain and terminate the app....
  400.     return msg.wParam;
  401. }
  402.