home *** CD-ROM | disk | FTP | other *** search
/ NEXT Generation 27 / NEXT27.iso / pc / demos / emperor / dx3.exe / SDK / SAMPLES / ROCKEM / WINMAIN.CPP < prev    next >
C/C++ Source or Header  |  1996-08-28  |  11KB  |  409 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.         // D3DVECTOR used for 3D position of sound
  131.         static D3DVECTOR d3dvPos= {D3DVAL(0), D3DVAL(0), D3DVAL(0)};
  132.         
  133.     // Handle messages
  134.     switch (message)
  135.     {
  136.         case WM_KEYDOWN:
  137.         {
  138.             switch (wParam)
  139.             {
  140.                 case VK_ESCAPE:
  141.                 {                                                       
  142.                     // Time to quit....
  143.                     PostMessage(g_hWnd, WM_CLOSE ,0 ,0);
  144.                 }
  145.                 break;
  146.  
  147.                 case VK_F5:
  148.                 {
  149.                     // Toggle stats
  150.                     g_bShowStats = !g_bShowStats;
  151.                 }
  152.                 break;
  153.  
  154.                 case 'M':
  155.                 {
  156.                     // Toggle music
  157.                     if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
  158.                     {
  159.                         if (!g_bMusicPaused)
  160.                         {
  161.                             PauseMidi();
  162.                         }
  163.                         else
  164.                         {
  165.                             ResumeMidi();
  166.                         }
  167.                         g_bMusicPaused = !g_bMusicPaused;
  168.                     }
  169.                 }
  170.                 break;
  171.                 
  172.                 case 'S':
  173.                 {
  174.                     // Toggle sound
  175.                     if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
  176.                     {
  177.                         g_bSoundPaused = !g_bSoundPaused;
  178.  
  179.                         if (g_bSoundPaused)
  180.                         {
  181.                             // Kill all the sound effects
  182.                             StopAllSounds();
  183.                         }
  184.                         else
  185.                         {
  186.                             // Start the crowd noise looping
  187.                          PlaySoundDS(CROWD_LOOP,d3dvPos, DSBPLAY_LOOPING);
  188.                         }
  189.                     }
  190.                 }
  191.                 break;
  192.  
  193.                 case VK_F6 :
  194.                 {
  195.                     // Go up a video mode
  196.                     if(!EnterNextVideoMode()) 
  197.                                                 PostMessage(g_hWnd, WM_CLOSE ,0 ,0);
  198.                 }
  199.                 break;
  200.  
  201.                 case VK_F7 :
  202.                 {
  203.                     // Go down a video mode
  204.                     if(!EnterPrevVideoMode())
  205.                                                 PostMessage(g_hWnd, WM_CLOSE ,0 ,0);
  206.                 }
  207.                 break;
  208.  
  209.                 case VK_END :
  210.                 {
  211.                     // Go to highest video mode
  212.                     if(!EnterLowestVideoMode())
  213.                                                 PostMessage(g_hWnd, WM_CLOSE ,0 ,0);
  214.                 }
  215.                 break;
  216.  
  217.                 case VK_HOME :
  218.                 {
  219.                     // Go to lowest video mode
  220.                     if(!EnterHighestVideoMode()) 
  221.                                                 PostMessage(g_hWnd, WM_CLOSE ,0 ,0);
  222.                 }
  223.                 break;
  224.             }
  225.         }
  226.         break;
  227.  
  228.         case WM_SYSCOMMAND:
  229.         {
  230.             switch (wParam)
  231.             {
  232.                 // Trap ALT so it doesn't pause the app
  233.                 case SC_KEYMENU :
  234.                 {
  235.                     return 0;
  236.                 }
  237.                 break;
  238.             }
  239.         }       
  240.         
  241.         case WM_ACTIVATEAPP:
  242.         {
  243.             // Determine whether app is being activated or not
  244.             g_bActive = (BOOL)wParam ? TRUE : FALSE;
  245.  
  246.             if (g_bActive)
  247.             {
  248.                 while (ShowCursor(FALSE) > 0) { };
  249.                 if (!g_bMusicPaused) ResumeMidi();
  250.             }
  251.             else
  252.             {
  253.                 ShowCursor(TRUE);
  254.                 PauseMidi();
  255.             }
  256.         }
  257.         break;
  258.         
  259.         case WM_CLOSE:
  260.         {                       
  261.             DestroyWindow(g_hWnd);
  262.         }
  263.         break;
  264.  
  265.         case WM_DESTROY:
  266.         {                       
  267.             // Stop midi music
  268.             StopMidi();
  269.  
  270.             // Destroy scene
  271.             TermScene();
  272.             
  273.             // Terminate all the DirectX objects, surfaces devices etc
  274.             TermDirectX();
  275.  
  276.             // Show the mouse
  277.             ShowCursor(TRUE);
  278.  
  279.             // If an error occured, show it
  280.             if (g_bErrorOccured)
  281.             {
  282.                 MessageBeep(0);
  283.                 MessageBox(NULL, g_sError, "Error!", MB_OK);
  284.             }
  285.                     
  286.             // Time to leave this mortal coil
  287.             PostQuitMessage(0);
  288.         }
  289.         break;
  290.         
  291.         case MCI_NOTIFY:
  292.         {
  293.             if (wParam == MCI_NOTIFY_SUCCESSFUL)
  294.             {
  295.                 ReplayMidi();
  296.             }
  297.         }
  298.         break;
  299.     }
  300.     
  301.     return DefWindowProc(hWnd, message, wParam, lParam);
  302. }
  303.  
  304. //------------------------------------------------------------------
  305. // 
  306. // Function     : WinMain()
  307. //
  308. // Purpose      : Entry point to application
  309. //
  310. //------------------------------------------------------------------
  311.  
  312. int FAR PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
  313. {
  314.     MSG msg;
  315.  
  316.     // Set global handle
  317.     g_hInst = hInst;    
  318.  
  319.     // Initialise window class
  320.     if (!InitClass(hInst)) return 1;
  321.  
  322.     // Initialise window
  323.     if (!InitWindow(hInst, nCmdShow)) return 1;
  324.  
  325.     // Initialise DirectX objects (Termination is handled in WM_DESTROY)
  326.     if (!InitDirectX())
  327.     {
  328.             DestroyWindow(g_hWnd);
  329.             return 1;
  330.     }
  331.  
  332.     // Show the splash screen
  333.     DoSplashScreen(2000);
  334.  
  335.     // Load the scene
  336.     if (!InitScene()) 
  337.     {
  338.             DestroyWindow(g_hWnd);
  339.             return 1;
  340.     }
  341.  
  342.     // Release the splash screen
  343.     ReleaseSplashScreen();
  344.  
  345.     // Set DirectDraw exclusive mode here so that the splash could stay
  346.     // up during initialisation if we are using a different DirectDraw device
  347.     // that could not support 640x480x8 for hardware rendering.
  348.     if (!SetDirectDrawExclusiveMode())
  349.     {
  350.             RegError("Could not set exclusive mode!");
  351.             return FALSE;
  352.     }
  353.  
  354.     // Hide the mouse
  355.     ShowCursor(FALSE);
  356.  
  357.     // Enter video mode set in g_dwCurMode
  358.     if (!EnterVideoMode(g_dwCurrMode))
  359.     {
  360.             DestroyWindow(g_hWnd);
  361.             return 1;
  362.     }
  363.  
  364.     // Load accelerators
  365.     HACCEL hAccel = LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_ACCEL));
  366.  
  367.     // Start the music!
  368.     PlayMidi("RockEm3D.mid");
  369.  
  370.     // Pump messages via a PeekMessage loop
  371.     while (TRUE)
  372.     {
  373.         while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  374.         {
  375.             if (msg.message == WM_QUIT)
  376.             {                                                           
  377.                 PostQuitMessage(msg.wParam);
  378.                 return 1;
  379.             }
  380.  
  381.             if (hAccel && (msg.hwnd == g_hWnd))
  382.             {
  383.                 TranslateAccelerator(g_hWnd, hAccel, &msg);
  384.             }
  385.  
  386.             TranslateMessage(&msg);
  387.             DispatchMessage(&msg);
  388.         }
  389.  
  390.         // Perform any neccessary updating during idle time
  391.         if (g_bActive)
  392.         {
  393.             // Update everything
  394.             CheckInputAndUpdate();
  395.             
  396.             // Render the current scene
  397.             if (!RenderScene())
  398.             {
  399.                 DestroyWindow(g_hWnd);
  400.                 return 1;
  401.             }
  402.         }
  403.     }
  404.  
  405.     // Exit WinMain and terminate the app....
  406.     return msg.wParam;
  407. }
  408.  
  409.