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

  1. // PROG13_1.CPP - Creating a DirectSound object
  2. // note that you don't need DirectDraw, just a window
  3.  
  4. // INCLUDES ///////////////////////////////////////////////
  5. #define WIN32_LEAN_AND_MEAN  
  6. #define INITGUID
  7.  
  8. #include <windows.h>   // include important windows stuff
  9. #include <windowsx.h> 
  10. #include <mmsystem.h>
  11. #include <iostream.h> // include important C/C++ stuff
  12. #include <conio.h>
  13. #include <stdlib.h>
  14. #include <malloc.h>
  15. #include <memory.h>
  16. #include <string.h>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <io.h>
  21. #include <fcntl.h>
  22.  
  23. #include <ddraw.h>  // directX includes
  24. #include <dsound.h>
  25.  
  26. // DEFINES ////////////////////////////////////////////////
  27.  
  28. // defines for windows 
  29. #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
  30.  
  31. #define WINDOW_WIDTH  320              // size of window
  32. #define WINDOW_HEIGHT 240
  33. #define SCREEN_WIDTH  640              // size of screen
  34. #define SCREEN_HEIGHT 480
  35. #define SCREEN_BPP    8                // bits per pixel
  36.  
  37. // MACROS /////////////////////////////////////////////////
  38.  
  39. // these read the keyboard asynchronously
  40. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  41. #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  42.  
  43. // TYPES //////////////////////////////////////////////////
  44.  
  45. typedef unsigned short USHORT;
  46. typedef unsigned short WORD;
  47. typedef unsigned char  UCHAR;
  48. typedef unsigned char  BYTE;
  49.  
  50. // PROTOTYPES /////////////////////////////////////////////
  51.  
  52. int Game_Init(void *parms=NULL);
  53. int Game_Shutdown(void *parms=NULL);
  54. int Game_Main(void *parms=NULL);
  55.  
  56. // GLOBALS ////////////////////////////////////////////////
  57.  
  58. HWND main_window_handle = NULL; // save the window handle
  59. HINSTANCE main_instance = NULL; // save the instance
  60. char buffer[80];                // used to print text
  61.  
  62. LPDIRECTSOUND  lpds;            // directsoudn object
  63. HRESULT        ddrval;          // result back from dd calls
  64.  
  65.               
  66. // FUNCTIONS //////////////////////////////////////////////
  67.  
  68. LRESULT CALLBACK WindowProc(HWND hwnd, 
  69.                             UINT msg,
  70.                             WPARAM wparam, 
  71.                             LPARAM lparam)
  72. {
  73. // this is the main message handler of the system
  74. PAINTSTRUCT    ps;           // used in WM_PAINT
  75. HDC             hdc;       // handle to a device context
  76.  
  77. // what is the message 
  78. switch(msg)
  79.     {    
  80.     case WM_CREATE: 
  81.         {
  82.         // do initialization stuff here
  83.         return(0);
  84.         } break;
  85.  
  86.     case WM_PAINT:
  87.          {
  88.          // start painting
  89.          hdc = BeginPaint(hwnd,&ps);
  90.  
  91.          // end painting
  92.          EndPaint(hwnd,&ps);
  93.          return(0);
  94.         } break;
  95.  
  96.     case WM_DESTROY: 
  97.         {
  98.         // kill the application            
  99.         PostQuitMessage(0);
  100.         return(0);
  101.         } break;
  102.  
  103.     default:break;
  104.  
  105.     } // end switch
  106.  
  107. // process any messages that we didn't take care of 
  108. return (DefWindowProc(hwnd, msg, wparam, lparam));
  109.  
  110. } // end WinProc
  111.  
  112. // WINMAIN ////////////////////////////////////////////////
  113.  
  114. int WINAPI WinMain(    HINSTANCE hinstance,
  115.                     HINSTANCE hprevinstance,
  116.                     LPSTR lpcmdline,
  117.                     int ncmdshow)
  118. {
  119.  
  120. WNDCLASS winclass;    // this will hold the class we create
  121. HWND     hwnd;        // generic window handle
  122. MSG      msg;           // generic message
  123. HDC      hdc;       // generic dc
  124. PAINTSTRUCT ps;     // generic paintstruct
  125.  
  126. // first fill in the window class stucture
  127. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  128.                           CS_HREDRAW | CS_VREDRAW;
  129. winclass.lpfnWndProc    = WindowProc;
  130. winclass.cbClsExtra    = 0;
  131. winclass.cbWndExtra    = 0;
  132. winclass.hInstance     = hinstance;
  133. winclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  134. winclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  135. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  136. winclass.lpszMenuName    = NULL; 
  137. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  138.  
  139. // register the window class
  140. if (!RegisterClass(&winclass))
  141.     return(0);
  142.  
  143. // create the window, note the use of WS_POPUP
  144. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  145.                           "WinX Game Console - DirectSound",     // title
  146.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  147.                           0,0,     // x,y
  148.                           WINDOW_WIDTH,  // width
  149.                           WINDOW_HEIGHT, // height
  150.                           NULL,    // handle to parent
  151.                           NULL,    // handle to menu
  152.                           hinstance,// instance
  153.                           NULL)))       // creation parms
  154. return(0);
  155.  
  156. // save the window handle and instance in a global
  157. main_window_handle = hwnd;
  158. main_instance      = hinstance;
  159.  
  160. // perform all game console specific initialization
  161. Game_Init();
  162.  
  163. // enter main event loop
  164. while(1)
  165.     {
  166.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  167.         { 
  168.         // test if this is a quit
  169.         if (msg.message == WM_QUIT)
  170.            break;
  171.     
  172.         // translate any accelerator keys
  173.         TranslateMessage(&msg);
  174.  
  175.         // send the message to the window proc
  176.         DispatchMessage(&msg);
  177.         } // end if
  178.     
  179.     // main game processing goes here
  180.     Game_Main();
  181.  
  182.     } // end while
  183.  
  184. // shutdown game and release all resources
  185. Game_Shutdown();
  186.  
  187. // return to Windows like this
  188. return(msg.wParam);
  189.  
  190. } // end WinMain
  191.  
  192. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  193.  
  194. int Game_Init(void *parms)
  195. {
  196. // this function is where you do all the initialization 
  197. // for your game
  198.  
  199. // create directsound object and test for error
  200. if (DirectSoundCreate(NULL,&lpds,NULL)!=DD_OK)
  201.    return(0);
  202.  
  203. // set cooperation level to normal priority
  204. if (lpds->SetCooperativeLevel(main_window_handle,DSSCL_NORMAL)!=DS_OK)
  205.     return(0);
  206.  
  207. // return success
  208. return(1);
  209.  
  210. } // end Game_Init
  211.  
  212. ///////////////////////////////////////////////////////////
  213.  
  214. int Game_Shutdown(void *parms)
  215. {
  216. // this function is where you shutdown your game and
  217. // release all resources that you allocated
  218.  
  219. // release the directsoundobject
  220. if (lpds!=NULL)
  221.    lpds->Release();
  222.  
  223.  
  224. // return success
  225. return(1);
  226. } // end Game_Shutdown
  227.  
  228. ///////////////////////////////////////////////////////////
  229.  
  230. int Game_Main(void *parms)
  231. {
  232. // this is the workhorse of your game it will be called
  233. // continuously in real-time this is like main() in C
  234. // all the calls for you game go here!
  235.  
  236. // check of user is trying to exit
  237. if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))
  238.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  239.  
  240. // return success
  241. return(1);
  242. } // end Game_Main
  243.  
  244. ///////////////////////////////////////////////////////////
  245.  
  246.