home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / Source / GPCHAP06 / PROG6_3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-25  |  4.8 KB  |  181 lines

  1. // PROG6_3.CPP - basic sound demo
  2.  
  3. // INCLUDES ///////////////////////////////////////////////
  4. #define WIN32_LEAN_AND_MEAN  
  5.  
  6. #include <windows.h>  
  7. #include <windowsx.h> 
  8. #include <mmsystem.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include "PROG6_3.H" // include menu header
  13.  
  14. // DEFINES ////////////////////////////////////////////////
  15.  
  16. // defines for windows 
  17. #define WINDOW_CLASS_NAME "WINCLASS1"
  18. #define WINDOW_WIDTH  320
  19. #define WINDOW_HEIGHT 200
  20.  
  21. // GLOBALS ////////////////////////////////////////////////
  22. HWND main_window_handle = NULL; // save the window handle
  23. char buffer[80];                // used to print text
  24.  
  25. // FUNCTIONS //////////////////////////////////////////////
  26. LRESULT CALLBACK WindowProc(HWND hwnd, 
  27.                             UINT msg, 
  28.                             WPARAM wparam, 
  29.                             LPARAM lparam)
  30. {
  31. // this is the main message handler of the system
  32. PAINTSTRUCT    ps;           // used in WM_PAINT
  33. HDC            hdc;       // handle to a device context
  34.  
  35. // what is the message 
  36. switch(msg)
  37.     {    
  38.     case WM_CREATE: 
  39.         {
  40.         // do initialization stuff here
  41.         return(0);
  42.         } break;
  43.  
  44.     // process menu messages
  45.     case WM_COMMAND:
  46.          {
  47.          // what menu item?
  48.          switch(wparam)
  49.                {
  50.                case ID_SMENU_FILE_EXIT:
  51.                     { 
  52.                     // terminate application
  53.                     PostMessage(hwnd,WM_DESTROY,0,0);
  54.                     } break;
  55.                case ID_SMENU_PLAY_LASER:
  56.                     {
  57.                     PlaySound("LASER.WAV",NULL,SND_FILENAME | SND_ASYNC);
  58.                     } break;
  59.                case ID_SMENU_PLAY_EXPL:
  60.                     { 
  61.                     PlaySound("EXPL.WAV",NULL,SND_FILENAME | SND_ASYNC);
  62.                     } break;
  63.                case ID_SMENU_PLAY_GROWL:
  64.                     {
  65.                     PlaySound("GROWL.WAV",NULL,SND_FILENAME | SND_ASYNC);
  66.                     } break;
  67.                case ID_SMENU_PLAY_DOOR:
  68.                     {
  69.                     PlaySound("DOOR.WAV",NULL,SND_FILENAME | SND_ASYNC);
  70.                     } break;
  71.            
  72.                default: break;
  73.                } // end switch
  74.    
  75.          // inform windows we handled message
  76.          return(0);
  77.          } break;
  78.  
  79.     case WM_PAINT:
  80.          {
  81.          // start painting
  82.          hdc = BeginPaint(hwnd,&ps);
  83.  
  84.          // end painting
  85.          EndPaint(hwnd,&ps);
  86.          return(0);
  87.         } break;
  88.  
  89.     case WM_DESTROY: 
  90.         {
  91.         // kill the application            
  92.         PostQuitMessage(0);
  93.         return(0);
  94.         } break;
  95.  
  96.     default:break;
  97.  
  98.     } // end switch
  99.  
  100. // process any messages that we didn't take care of 
  101. return (DefWindowProc(hwnd, msg, wparam, lparam));
  102.  
  103. } // end WinProc
  104.  
  105. // WINMAIN ////////////////////////////////////////////////
  106. int WINAPI WinMain(    HINSTANCE hinstance,
  107.                     HINSTANCE hprevinstance,
  108.                     LPSTR lpcmdline,
  109.                     int ncmdshow)
  110. {
  111.  
  112. WNDCLASS winclass;    // this will hold the class we create
  113. HWND     hwnd;        // generic window handle
  114. MSG         msg;        // generic message
  115. HDC      hdc;       // generic dc
  116. PAINTSTRUCT ps;     // generic paintstruct
  117.  
  118. // first fill in the window class stucture
  119. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  120.                           CS_HREDRAW | CS_VREDRAW;
  121. winclass.lpfnWndProc    = WindowProc;
  122. winclass.cbClsExtra        = 0;
  123. winclass.cbWndExtra        = 0;
  124. winclass.hInstance        = hinstance;
  125. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  126. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  127. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  128. winclass.lpszMenuName    = NULL; 
  129. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  130.  
  131. // register the window class
  132. if (!RegisterClass(&winclass))
  133.     return(0);
  134.  
  135. // create the window
  136. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  137.                           "Playsound Demo",     // title
  138.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  139.                            0,0,       // x,y
  140.                           WINDOW_WIDTH,  // width
  141.                           WINDOW_HEIGHT, // height
  142.                           NULL,       // handle to parent 
  143.                           NULL,       // handle to menu
  144.                           hinstance,// instance
  145.                           NULL)))    // creation parms
  146. return(0);
  147.  
  148. // save the window handle in a global
  149. main_window_handle = hwnd;
  150.  
  151. // load the menu in and attach it (method 3)
  152. SetMenu(hwnd,LoadMenu(hinstance, "SMENU"));
  153.  
  154. // enter main event loop
  155. while(1)
  156.     {
  157.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  158.         { 
  159.         // test if this is a quit
  160.         if (msg.message == WM_QUIT)
  161.            break;
  162.     
  163.         // translate any accelerator keys
  164.         TranslateMessage(&msg);
  165.  
  166.         // send the message to the window proc
  167.         DispatchMessage(&msg);
  168.         } // end if
  169.     
  170.     // main game processing goes here
  171.  
  172.     } // end while
  173.  
  174. // return to Windows like this
  175. return(msg.wParam);
  176.  
  177. } // end WinMain
  178.  
  179. ///////////////////////////////////////////////////////////
  180.  
  181.