home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / Source / GPCHAP17 / Prog17_3_16b.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-01  |  10.3 KB  |  416 lines

  1. // PROG17_3_16b.CPP - 16-bit version simple collision demo
  2. // to compile make sure to include DDRAW.LIB, DSOUND.LIB,
  3. // DINPUT.LIB, DINPUT8.LIB, WINMM.LIB, and of course GPDUMB I and II files.
  4.  
  5. // INCLUDES ///////////////////////////////////////////////
  6.  
  7. #define WIN32_LEAN_AND_MEAN  
  8. #define INITGUID
  9.  
  10. #include <windows.h>   // include important windows stuff
  11. #include <windowsx.h> 
  12. #include <mmsystem.h>
  13. #include <iostream.h> // include important C/C++ stuff
  14. #include <conio.h>
  15. #include <stdlib.h>
  16. #include <malloc.h>
  17. #include <memory.h>
  18. #include <string.h>
  19. #include <stdarg.h>
  20. #include <stdio.h> 
  21. #include <math.h>
  22. #include <io.h>
  23. #include <fcntl.h>
  24.  
  25. #include <ddraw.h>  // directX includes
  26. #include <dsound.h>
  27. #include <dinput.h>
  28. #include "gpdumb1.h" // game library includes
  29. #include "gpdumb2.h"
  30.  
  31. // DEFINES ////////////////////////////////////////////////
  32.  
  33. // defines for windows 
  34. #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
  35.  
  36. #define WINDOW_WIDTH    320   // size of window
  37. #define WINDOW_HEIGHT   240
  38.  
  39. #define NUM_BALLS       20   // number of pool balls
  40.  
  41. // extents of table
  42. #define TABLE_MAX_X     468
  43. #define TABLE_MIN_X     176
  44. #define TABLE_MAX_Y     448
  45. #define TABLE_MIN_Y     44
  46.  
  47. // PROTOTYPES /////////////////////////////////////////////
  48.  
  49. // game console
  50. int Game_Init(void *parms=NULL);
  51. int Game_Shutdown(void *parms=NULL);
  52. int Game_Main(void *parms=NULL);
  53.  
  54. // GLOBALS ////////////////////////////////////////////////
  55.  
  56. HWND main_window_handle   = NULL; // save the window handle
  57. HINSTANCE main_instance   = NULL; // save the instance
  58. char buffer[80];                          // used to print text
  59.  
  60. BITMAP_IMAGE background_bmp;   // holds the background
  61. BOB          balls[NUM_BALLS]; // the balls
  62.  
  63. int ball_ids[8];               // sound ids for balls
  64.  
  65. // FUNCTIONS //////////////////////////////////////////////
  66.  
  67. LRESULT CALLBACK WindowProc(HWND hwnd, 
  68.                             UINT msg, 
  69.                             WPARAM wparam, 
  70.                             LPARAM lparam)
  71. {
  72. // this is the main message handler of the system
  73. PAINTSTRUCT    ps;           // used in WM_PAINT
  74. HDC            hdc;       // handle to a device context
  75.  
  76. // what is the message 
  77. switch(msg)
  78.     {    
  79.     case WM_CREATE: 
  80.         {
  81.         // do initialization stuff here
  82.         return(0);
  83.         } break;
  84.  
  85.     case WM_PAINT:
  86.          {
  87.          // start painting
  88.          hdc = BeginPaint(hwnd,&ps);
  89.  
  90.          // end painting
  91.          EndPaint(hwnd,&ps);
  92.          return(0);
  93.         } break;
  94.  
  95.     case WM_DESTROY: 
  96.         {
  97.         // kill the application            
  98.         PostQuitMessage(0);
  99.         return(0);
  100.         } break;
  101.  
  102.     default:break;
  103.  
  104.     } // end switch
  105.  
  106. // process any messages that we didn't take care of 
  107. return (DefWindowProc(hwnd, msg, wparam, lparam));
  108.  
  109. } // end WinProc
  110.  
  111. // WINMAIN ////////////////////////////////////////////////
  112.  
  113. int WINAPI WinMain(    HINSTANCE hinstance,
  114.                     HINSTANCE hprevinstance,
  115.                     LPSTR lpcmdline,
  116.                     int ncmdshow)
  117. {
  118. // this is the winmain function
  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",     // title
  146.                           WS_POPUP | 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. int index; // looping varsIable
  200.  
  201. char filename[80]; // used to build up filenames
  202.  
  203. // seed random number generate
  204. srand(Start_Clock());
  205.  
  206. screen_width  = 640;
  207. screen_height = 480;
  208. screen_bpp    = 16;
  209.  
  210. // start up DirectDraw (replace the parms as you desire)
  211. DD_Init(screen_width, screen_height, screen_bpp);
  212.  
  213. // load background image
  214. Load_Bitmap_File(&bitmap16bit, "PTABLE16.BMP");
  215. Create_Bitmap16(&background_bmp,0,0,640,480);
  216. Load_Image_Bitmap16(&background_bmp, &bitmap16bit,0,0,BITMAP_EXTRACT_MODE_ABS);
  217. Unload_Bitmap_File(&bitmap16bit);
  218.  
  219. // load the bitmaps
  220. Load_Bitmap_File(&bitmap16bit, "BALLS16.BMP");
  221.  
  222. // create master ball
  223. Create_BOB16(&balls[0],0,0,20,20,6,BOB_ATTR_MULTI_FRAME | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY);
  224.  
  225. // load the imagery in
  226. for (index=0; index < 6; index++)
  227.     Load_Frame_BOB16(&balls[0], &bitmap16bit, index, index,0,BITMAP_EXTRACT_MODE_CELL);
  228.  
  229. // create all the clones
  230. for (index=1; index < NUM_BALLS; index++)
  231.     Clone_BOBX(&balls[0], &balls[index]);
  232.  
  233. // now set the initial conditions of all the balls
  234. for (index=0; index < NUM_BALLS; index++)
  235.     {
  236.     // set position randomly
  237.     balls[index].x = 320 - 50 + rand()%100;
  238.     balls[index].y = 240 - 100 + rand()%200;
  239.  
  240.     // set initial velocity
  241.     balls[index].xv = -6 + rand()%13;
  242.     balls[index].yv = -6 + rand()%13;
  243.  
  244.     // set ball
  245.     balls[index].curr_frame = rand()%6;
  246.  
  247.     } // end for index
  248.  
  249. // unload bitmap image
  250. Unload_Bitmap_File(&bitmap16bit);
  251.  
  252. // initialize directinput
  253. DInput_Init();
  254.  
  255. // acquire the keyboard only
  256. DI_Init_Keyboard();
  257.  
  258. // initilize DirectSound
  259. DSound_Init();
  260.  
  261. // load background sounds
  262. ball_ids[0] = Load_WAV("PBALL.WAV");
  263.  
  264. // clone sounds
  265. for (index=1; index<8; index++)
  266.     ball_ids[index] = Replicate_Sound(ball_ids[0]);
  267.  
  268. // start the sounds
  269. //Play_Sound(sound_id, DSBPLAY_LOOPING);
  270.  
  271. // set clipping rectangle to screen extents so objects dont
  272. // mess up at edges
  273. RECT screen_rect = {0,0,screen_width,screen_height};
  274. lpddclipper = DD_Attach_Clipper(lpddsback,1,&screen_rect);
  275.  
  276. // hide the mouse
  277. ShowCursor(FALSE);
  278.  
  279. // return success
  280. return(1);
  281.  
  282. } // end Game_Init
  283.  
  284. ///////////////////////////////////////////////////////////
  285.  
  286. int Game_Shutdown(void *parms)
  287. {
  288. // this function is where you shutdown your game and
  289. // release all resources that you allocated
  290.  
  291. // shut everything down
  292.  
  293. // kill all the bobs
  294. for (int index=0; index<NUM_BALLS; index++)
  295.     Destroy_BOBX(&balls[index]);
  296.  
  297. // shutdown directdraw last
  298. DD_Shutdown();
  299.  
  300. // now directsound
  301. Stop_All_Sounds();
  302. DSound_Shutdown();
  303.  
  304. // shut down directinput
  305. DInput_Shutdown();
  306.  
  307. // return success
  308. return(1);
  309.  
  310. } // end Game_Shutdown
  311.  
  312. //////////////////////////////////////////////////////////
  313.  
  314. int Game_Main(void *parms)
  315. {
  316. // this is the workhorse of your game it will be called
  317. // continuously in real-time this is like main() in C
  318. // all the calls for you game go here!
  319.  
  320. int index; // looping var
  321.  
  322. // start the timing clock
  323. Start_Clock();
  324.  
  325. // clear the drawing surface
  326. DD_Fill_Surface(lpddsback, 0);
  327.  
  328. // lock back buffer and copy background into it
  329. DD_Lock_Back_Surface();
  330.  
  331. // draw background
  332. Draw_Bitmap16(&background_bmp, back_buffer, back_lpitch,0);
  333.  
  334. // unlock back surface
  335. DD_Unlock_Back_Surface();
  336.  
  337. // read keyboard
  338. DI_Read_Keyboard();
  339.  
  340. // move all the balls
  341. for (index=0; index<NUM_BALLS; index++)
  342.     {
  343.     // move the ball
  344.     Move_BOB16(&balls[index]);
  345.     
  346.     // test for collision with table edges
  347.     if ((balls[index].x+balls[index].width >= TABLE_MAX_X) || 
  348.         (balls[index].x <= TABLE_MIN_X))
  349.         {
  350.         // invert velocity
  351.         balls[index].xv=-balls[index].xv;
  352.  
  353.         // start a hit sound
  354.         // if ((rand()%3)==0)
  355.         for (int sound_index=0; sound_index < 8; sound_index++)
  356.             {
  357.             // test if this sound is playing
  358.             if (Status_Sound(ball_ids[sound_index])==0)
  359.                {
  360.                Play_Sound(ball_ids[sound_index]);
  361.                break;
  362.                } // end if
  363.             } // end for 
  364.  
  365.         } // end if
  366.  
  367.     if ((balls[index].y+balls[index].height >= TABLE_MAX_Y) || 
  368.         (balls[index].y <= TABLE_MIN_Y))
  369.         {
  370.         // invert velocity
  371.         balls[index].yv=-balls[index].yv;
  372.  
  373.         // start a hit sound
  374.         // if ((rand()%3)==0)
  375.         for (int sound_index=0; sound_index < 8; sound_index++)
  376.             {
  377.             // test if this sound is playing
  378.             if (Status_Sound(ball_ids[sound_index])==0)
  379.                {
  380.                Play_Sound(ball_ids[sound_index]);
  381.                break;
  382.                } // end if
  383.             } // end for 
  384.  
  385.         } // end if
  386.  
  387.     } // end for index
  388.  
  389. // draw the balls
  390. for (index=0; index < NUM_BALLS; index++)
  391.     Draw_BOB16(&balls[index], lpddsback);
  392.  
  393. // draw the title
  394. Draw_Text_GDI("ELASTIC COLLISION DEMO",10, 10,RGB(255,255,255), lpddsback);
  395.  
  396. // flip the surfaces
  397. DD_Flip();
  398.  
  399. // sync to 30 fps = 1/30sec = 33 ms
  400. Wait_Clock(33);
  401.  
  402. // check of user is trying to exit
  403. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  404.     {
  405.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  406.  
  407.     // stop all sounds
  408.     Stop_All_Sounds();
  409.     } // end if
  410.  
  411. // return success
  412. return(1);
  413.  
  414. } // end Game_Main
  415.  
  416. //////////////////////////////////////////////////////////