home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / Source / GPCHAP23 / PROG23_3.CPP < prev    next >
C/C++ Source or Header  |  2002-05-01  |  11KB  |  458 lines

  1. // PROG23_3.CPP - evasion 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. // PROTOTYPES /////////////////////////////////////////////
  40.  
  41. // game console
  42. int Game_Init(void *parms=NULL);
  43. int Game_Shutdown(void *parms=NULL);
  44. int Game_Main(void *parms=NULL);
  45.  
  46. // GLOBALS ////////////////////////////////////////////////
  47.  
  48. HWND main_window_handle           = NULL; // save the window handle
  49. HINSTANCE main_instance           = NULL; // save the instance
  50. char buffer[80];                          // used to print text
  51.  
  52. BITMAP_IMAGE background_bmp;   // holds the background
  53.  
  54. BOB          bat,              // the ai bat bob
  55.              ghost;            // the player ghost bob
  56.  
  57. static int ghost_anim[] = {0,1,2,1}; // animation sequence for ghost
  58.  
  59. int bat_sound_id  = -1,        // sound of bat flapping wings
  60.     wind_sound_id = -1;        // the ambient wind
  61.  
  62. // FUNCTIONS //////////////////////////////////////////////
  63.  
  64. LRESULT CALLBACK WindowProc(HWND hwnd, 
  65.                             UINT msg, 
  66.                             WPARAM wparam, 
  67.                             LPARAM lparam)
  68. {
  69. // this is the main message handler of the system
  70. PAINTSTRUCT    ps;           // used in WM_PAINT
  71. HDC            hdc;       // handle to a device context
  72.  
  73. // what is the message 
  74. switch(msg)
  75.     {    
  76.     case WM_CREATE: 
  77.         {
  78.         // do initialization stuff here
  79.         return(0);
  80.         } break;
  81.  
  82.     case WM_PAINT:
  83.          {
  84.          // start painting
  85.          hdc = BeginPaint(hwnd,&ps);
  86.  
  87.          // end painting
  88.          EndPaint(hwnd,&ps);
  89.          return(0);
  90.         } break;
  91.  
  92.     case WM_DESTROY: 
  93.         {
  94.         // kill the application            
  95.         PostQuitMessage(0);
  96.         return(0);
  97.         } break;
  98.  
  99.     default:break;
  100.  
  101.     } // end switch
  102.  
  103. // process any messages that we didn't take care of 
  104. return (DefWindowProc(hwnd, msg, wparam, lparam));
  105.  
  106. } // end WinProc
  107.  
  108. // WINMAIN ////////////////////////////////////////////////
  109.  
  110. int WINAPI WinMain(    HINSTANCE hinstance,
  111.                     HINSTANCE hprevinstance,
  112.                     LPSTR lpcmdline,
  113.                     int ncmdshow)
  114. {
  115. // this is the winmain function
  116.  
  117. WNDCLASS winclass;    // this will hold the class we create
  118. HWND     hwnd;        // generic window handle
  119. MSG         msg;        // generic message
  120. HDC      hdc;       // generic dc
  121. PAINTSTRUCT ps;     // generic paintstruct
  122.  
  123. // first fill in the window class stucture
  124. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  125.                           CS_HREDRAW | CS_VREDRAW;
  126. winclass.lpfnWndProc    = WindowProc;
  127. winclass.cbClsExtra        = 0;
  128. winclass.cbWndExtra        = 0;
  129. winclass.hInstance        = hinstance;
  130. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  131. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  132. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  133. winclass.lpszMenuName    = NULL; 
  134. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  135.  
  136. // register the window class
  137. if (!RegisterClass(&winclass))
  138.     return(0);
  139.  
  140. // create the window, note the use of WS_POPUP
  141. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  142.                           "WinX Game Console",     // title
  143.                           WS_POPUP | WS_VISIBLE,
  144.                            0,0,       // x,y
  145.                           WINDOW_WIDTH,  // width
  146.                           WINDOW_HEIGHT, // height
  147.                           NULL,       // handle to parent 
  148.                           NULL,       // handle to menu
  149.                           hinstance,// instance
  150.                           NULL)))    // creation parms
  151. return(0);
  152.  
  153. // save the window handle and instance in a global
  154. main_window_handle = hwnd;
  155. main_instance      = hinstance;
  156.  
  157. // perform all game console specific initialization
  158. Game_Init();
  159.  
  160. // enter main event loop
  161. while(1)
  162.     {
  163.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  164.         { 
  165.         // test if this is a quit
  166.         if (msg.message == WM_QUIT)
  167.            break;
  168.     
  169.         // translate any accelerator keys
  170.         TranslateMessage(&msg);
  171.  
  172.         // send the message to the window proc
  173.         DispatchMessage(&msg);
  174.         } // end if
  175.     
  176.     // main game processing goes here
  177.     Game_Main();
  178.  
  179.     } // end while
  180.  
  181. // shutdown game and release all resources
  182. Game_Shutdown();
  183.  
  184. // return to Windows like this
  185. return(msg.wParam);
  186.  
  187. } // end WinMain
  188.  
  189. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  190.  
  191. int Game_Init(void *parms)
  192. {
  193. // this function is where you do all the initialization 
  194. // for your game
  195.  
  196. int index; // looping variable
  197.  
  198. // start up DirectDraw (replace the parms as you desire)
  199. DD_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
  200.  
  201. // load background image
  202. Load_Bitmap_File(&bitmap8bit, "GHOSTBACK.BMP");
  203. Create_Bitmap(&background_bmp,0,0,640,480);
  204. Load_Image_Bitmap(&background_bmp, &bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);
  205. Set_Palette(bitmap8bit.palette);
  206. Unload_Bitmap_File(&bitmap8bit);
  207.  
  208. // load the bat bitmaps
  209. Load_Bitmap_File(&bitmap8bit, "BATS8.BMP");
  210.  
  211. // create bat bob
  212. Create_BOB(&bat,320,200, 16,16, 5, BOB_ATTR_MULTI_FRAME | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY);
  213. Set_Anim_Speed_BOB(&bat, 2);
  214.  
  215. // load the bat in 
  216. for (index=0; index < 5; index++)
  217.     Load_Frame_BOB(&bat, &bitmap8bit, index, index, 0, BITMAP_EXTRACT_MODE_CELL);
  218.  
  219. // unload bat
  220. Unload_Bitmap_File(&bitmap8bit);
  221.  
  222. // load the ghost bitmaps
  223. Load_Bitmap_File(&bitmap8bit, "GHOSTS8.BMP");
  224.  
  225. // create ghost bob
  226. Create_BOB(&ghost,100,200, 64,100, 3, BOB_ATTR_MULTI_ANIM | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY);
  227. Set_Anim_Speed_BOB(&ghost, 10);
  228.  
  229. // load the ghost in 
  230. for (index=0; index < 3; index++)
  231.     Load_Frame_BOB(&ghost, &bitmap8bit, index, index, 0, BITMAP_EXTRACT_MODE_CELL);
  232.  
  233. // unload ghost
  234. Unload_Bitmap_File(&bitmap8bit);
  235.  
  236. // set animation for ghost
  237. Load_Animation_BOB(&ghost, 0, 4, ghost_anim);
  238.  
  239. // set the animation
  240. Set_Animation_BOB(&ghost,0);
  241.  
  242. // initialize directinput
  243. DInput_Init();
  244.  
  245. // acquire the keyboard only
  246. DI_Init_Keyboard();
  247.  
  248. // initilize DirectSound
  249. DSound_Init();
  250.  
  251. // load background sounds
  252. bat_sound_id = Load_WAV("BAT.WAV");
  253. wind_sound_id = Load_WAV("WIND.WAV");
  254.  
  255. // start the sounds
  256. Play_Sound(bat_sound_id, DSBPLAY_LOOPING);
  257. Play_Sound(wind_sound_id, DSBPLAY_LOOPING);
  258.  
  259. // set clipping rectangle to screen extents so objects dont
  260. // mess up at edges
  261. RECT screen_rect = {0,0,screen_width,screen_height};
  262. lpddclipper = DD_Attach_Clipper(lpddsback,1,&screen_rect);
  263.  
  264. // hide the mouse
  265. ShowCursor(FALSE);
  266.  
  267. // return success
  268. return(1);
  269.  
  270. } // end Game_Init
  271.  
  272. ///////////////////////////////////////////////////////////
  273.  
  274. int Game_Shutdown(void *parms)
  275. {
  276. // this function is where you shutdown your game and
  277. // release all resources that you allocated
  278.  
  279. // shut everything down
  280.  
  281. // kill all the bobs
  282. Destroy_BOBX(&bat);
  283. Destroy_BOBX(&ghost);
  284.  
  285. // shutdown directdraw last
  286. DD_Shutdown();
  287.  
  288. // now directsound
  289. Stop_All_Sounds();
  290. DSound_Shutdown();
  291.  
  292. // shut down directinput
  293. DInput_Shutdown();
  294.  
  295. // return success
  296. return(1);
  297. } // end Game_Shutdown
  298.  
  299.  
  300. //////////////////////////////////////////////////////////
  301.  
  302. void Bat_AI(void)
  303. {
  304. // this function performs the bat ai
  305.  
  306. // do evasion algorithm
  307.  
  308. // only evade if bat is within specified distance
  309. // otherwise bat will end up in corner
  310.  
  311. if ((abs(bat.x-ghost.x) + abs(bat.y-ghost.y))<250)
  312. {
  313. // first x-axis    
  314. if (ghost.x < bat.x)
  315.    bat.x+=2;
  316. else
  317. if (ghost.x > bat.x)
  318.    bat.x-=2;
  319.  
  320. // now y-axis
  321. if (ghost.y < bat.y)
  322.    bat.y+=2;
  323. else
  324. if (ghost.y > bat.y)
  325.    bat.y-=2;
  326. } // end if
  327. else
  328.     {
  329.     // just move back to center of screen
  330.     // first x-axis    
  331.     if (SCREEN_WIDTH/2 > bat.x)
  332.        bat.x+=1;
  333.     else
  334.     if (SCREEN_WIDTH/2 < bat.x)
  335.        bat.x-=1;
  336.     
  337.     // now y-axis
  338.     if (SCREEN_HEIGHT/2 > bat.y)
  339.        bat.y+=1;
  340.     else
  341.     if (SCREEN_HEIGHT/2 < bat.y)
  342.        bat.y-=1;
  343.  
  344.     } // end else
  345.  
  346. // check boundaries
  347. if (bat.x >= SCREEN_WIDTH)
  348.    bat.x = -bat.width;
  349. else
  350. if (bat.x < -bat.width)
  351.    bat.x = SCREEN_WIDTH;
  352.  
  353. if (bat.y >= SCREEN_HEIGHT)
  354.    bat.y = -bat.height;
  355. else
  356. if (bat.y < -bat.height)
  357.    bat.y = SCREEN_HEIGHT;
  358.  
  359. } // end Bat_AI
  360.  
  361. //////////////////////////////////////////////////////////
  362.  
  363. int Game_Main(void *parms)
  364. {
  365. // this is the workhorse of your game it will be called
  366. // continuously in real-time this is like main() in C
  367. // all the calls for you game go here!
  368.  
  369. int index; // looping var
  370.  
  371. // start the timing clock
  372. Start_Clock();
  373.  
  374. // clear the drawing surface
  375. DD_Fill_Surface(lpddsback, 0);
  376.  
  377. // lock back buffer and copy background into it
  378. DD_Lock_Back_Surface();
  379.  
  380. // draw background
  381. Draw_Bitmap(&background_bmp, back_buffer, back_lpitch,0);
  382.  
  383. // unlock back surface
  384. DD_Unlock_Back_Surface();
  385.  
  386. // read keyboard
  387. DI_Read_Keyboard();
  388.  
  389. // call the bat AI
  390. Bat_AI();
  391.  
  392. // the animate the bat
  393. Animate_BOB(&bat);
  394.  
  395. // draw the bat
  396. Draw_BOB(&bat, lpddsback);
  397.  
  398. // allow player to move
  399. if (keyboard_state[DIK_RIGHT])
  400.    ghost.x+=4;
  401. else
  402. if (keyboard_state[DIK_LEFT])
  403.    ghost.x-=4;
  404.  
  405. if (keyboard_state[DIK_UP])
  406.    ghost.y-=4;
  407. else
  408. if (keyboard_state[DIK_DOWN])
  409.    ghost.y+=4;
  410.  
  411. // test if player is off screen, if so wrap around
  412. if (ghost.x >= SCREEN_WIDTH)
  413.    ghost.x = -ghost.width;
  414. else
  415. if (ghost.x < -ghost.width)
  416.    ghost.x = SCREEN_WIDTH;
  417.  
  418. if (ghost.y >= SCREEN_HEIGHT)
  419.    ghost.y = -ghost.height;
  420. else
  421. if (ghost.y < -ghost.height)
  422.    ghost.y = SCREEN_HEIGHT;
  423.  
  424. // the animate the ghost
  425. Animate_BOB(&ghost);
  426.  
  427. // draw the ghost
  428. Draw_BOB(&ghost, lpddsback);
  429.  
  430. // draw title
  431. Draw_Text_GDI("GOING BATS! - Evasion Demo",10, 10,RGB(0,255,0), lpddsback);
  432.  
  433. // flip the surfaces
  434. DD_Flip();
  435.  
  436. // sync to 30ish fps
  437. Wait_Clock(30);
  438.  
  439. // check of user is trying to exit
  440. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  441.     {
  442.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  443.  
  444.     // stop all sounds
  445.     Stop_All_Sounds();
  446.  
  447.     // do a screen transition
  448.     Screen_Transitions(SCREEN_REDNESS,NULL,0);
  449.  
  450.     } // end if
  451.  
  452. // return success
  453. return(1);
  454.  
  455. } // end Game_Main
  456.  
  457. //////////////////////////////////////////////////////////
  458.