home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / directx2 / sdk / samples / rockem / winmain.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-28  |  10.6 KB  |  402 lines

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