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

  1. // PROG17_3.CPP - 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       10   // 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    = 8;
  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(&bitmap8bit, "PTABLE8.BMP");
  215. Create_Bitmap(&background_bmp,0,0,640,480);
  216. Load_Image_Bitmap(&background_bmp, &bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);
  217. Set_Palette(bitmap8bit.palette);
  218. Unload_Bitmap_File(&bitmap8bit);
  219.  
  220. // load the bitmaps
  221. Load_Bitmap_File(&bitmap8bit, "BALLS8.BMP");
  222.  
  223. // create master ball
  224. Create_BOB(&balls[0],0,0,20,20,6,BOB_ATTR_MULTI_FRAME | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY);
  225.  
  226. // load the imagery in
  227. for (index=0; index < 6; index++)
  228.     Load_Frame_BOB(&balls[0], &bitmap8bit, index, index,0,BITMAP_EXTRACT_MODE_CELL);
  229.  
  230. // create all the clones
  231. for (index=1; index < NUM_BALLS; index++)
  232.     Clone_BOBX(&balls[0], &balls[index]);
  233.  
  234. // now set the initial conditions of all the balls
  235. for (index=0; index < NUM_BALLS; index++)
  236.     {
  237.     // set position randomly
  238.     balls[index].x = 320 - 50 + rand()%100;
  239.     balls[index].y = 240 - 100 + rand()%200;
  240.  
  241.     // set initial velocity
  242.     balls[index].xv = -6 + rand()%13;
  243.     balls[index].yv = -6 + rand()%13;
  244.  
  245.     // set ball
  246.     balls[index].curr_frame = rand()%6;
  247.  
  248.     } // end for index
  249.  
  250. // unload bitmap image
  251. Unload_Bitmap_File(&bitmap8bit);
  252.  
  253. // initialize directinput
  254. DInput_Init();
  255.  
  256. // acquire the keyboard only
  257. DI_Init_Keyboard();
  258.  
  259. // initilize DirectSound
  260. DSound_Init();
  261.  
  262. // load background sounds
  263. ball_ids[0] = Load_WAV("PBALL.WAV");
  264.  
  265. // clone sounds
  266. for (index=1; index<8; index++)
  267.     ball_ids[index] = Replicate_Sound(ball_ids[0]);
  268.  
  269. // start the sounds
  270. //Play_Sound(sound_id, DSBPLAY_LOOPING);
  271.  
  272. // set clipping rectangle to screen extents so objects dont
  273. // mess up at edges
  274. RECT screen_rect = {0,0,screen_width,screen_height};
  275. lpddclipper = DD_Attach_Clipper(lpddsback,1,&screen_rect);
  276.  
  277. // hide the mouse
  278. ShowCursor(FALSE);
  279.  
  280. // return success
  281. return(1);
  282.  
  283. } // end Game_Init
  284.  
  285. ///////////////////////////////////////////////////////////
  286.  
  287. int Game_Shutdown(void *parms)
  288. {
  289. // this function is where you shutdown your game and
  290. // release all resources that you allocated
  291.  
  292. // shut everything down
  293.  
  294. // kill all the bobs
  295. for (int index=0; index<NUM_BALLS; index++)
  296.     Destroy_BOBX(&balls[index]);
  297.  
  298. // shutdown directdraw last
  299. DD_Shutdown();
  300.  
  301. // now directsound
  302. Stop_All_Sounds();
  303. DSound_Shutdown();
  304.  
  305. // shut down directinput
  306. DInput_Shutdown();
  307.  
  308. // return success
  309. return(1);
  310.  
  311. } // end Game_Shutdown
  312.  
  313. //////////////////////////////////////////////////////////
  314.  
  315. int Game_Main(void *parms)
  316. {
  317. // this is the workhorse of your game it will be called
  318. // continuously in real-time this is like main() in C
  319. // all the calls for you game go here!
  320.  
  321. int index; // looping var
  322.  
  323. // start the timing clock
  324. Start_Clock();
  325.  
  326. // clear the drawing surface
  327. DD_Fill_Surface(lpddsback, 0);
  328.  
  329. // lock back buffer and copy background into it
  330. DD_Lock_Back_Surface();
  331.  
  332. // draw background
  333. Draw_Bitmap(&background_bmp, back_buffer, back_lpitch,0);
  334.  
  335. // unlock back surface
  336. DD_Unlock_Back_Surface();
  337.  
  338. // read keyboard
  339. DI_Read_Keyboard();
  340.  
  341. // move all the balls
  342. for (index=0; index<NUM_BALLS; index++)
  343.     {
  344.     // move the ball
  345.     Move_BOB(&balls[index]);
  346.     
  347.     // test for collision with table edges
  348.     if ((balls[index].x+balls[index].width >= TABLE_MAX_X) || 
  349.         (balls[index].x <= TABLE_MIN_X))
  350.         {
  351.         // invert velocity
  352.         balls[index].xv=-balls[index].xv;
  353.  
  354.         // start a hit sound
  355.         // if ((rand()%3)==0)
  356.         for (int sound_index=0; sound_index < 8; sound_index++)
  357.             {
  358.             // test if this sound is playing
  359.             if (Status_Sound(ball_ids[sound_index])==0)
  360.                {
  361.                Play_Sound(ball_ids[sound_index]);
  362.                break;
  363.                } // end if
  364.             } // end for 
  365.  
  366.         } // end if
  367.  
  368.     if ((balls[index].y+balls[index].height >= TABLE_MAX_Y) || 
  369.         (balls[index].y <= TABLE_MIN_Y))
  370.         {
  371.         // invert velocity
  372.         balls[index].yv=-balls[index].yv;
  373.  
  374.         // start a hit sound
  375.         // if ((rand()%3)==0)
  376.         for (int sound_index=0; sound_index < 8; sound_index++)
  377.             {
  378.             // test if this sound is playing
  379.             if (Status_Sound(ball_ids[sound_index])==0)
  380.                {
  381.                Play_Sound(ball_ids[sound_index]);
  382.                break;
  383.                } // end if
  384.             } // end for 
  385.  
  386.         } // end if
  387.  
  388.     } // end for index
  389.  
  390. // draw the balls
  391. for (index=0; index < NUM_BALLS; index++)
  392.     Draw_BOB(&balls[index], lpddsback);
  393.  
  394. // draw the title
  395. Draw_Text_GDI("ELASTIC COLLISION DEMO",10, 10,RGB(0,255,255), lpddsback);
  396.  
  397. // flip the surfaces
  398. DD_Flip();
  399.  
  400. // sync to 30 fps = 1/30sec = 33 ms
  401. Wait_Clock(33);
  402.  
  403. // check of user is trying to exit
  404. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  405.     {
  406.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  407.  
  408.     // stop all sounds
  409.     Stop_All_Sounds();
  410.  
  411.     // do a screen transition
  412.     Screen_Transitions(SCREEN_GREENNESS,NULL,0);
  413.     } // end if
  414.  
  415. // return success
  416. return(1);
  417.  
  418. } // end Game_Main
  419.  
  420. //////////////////////////////////////////////////////////