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

  1. // PROG23_2.CPP - tracking 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 tracking algorithm
  307.  
  308. // first x-axis    
  309. if (ghost.x > bat.x)
  310.    bat.x+=2;
  311. else
  312. if (ghost.x < bat.x)
  313.    bat.x-=2;
  314.  
  315. // now y-axis
  316. if (ghost.y > bat.y)
  317.    bat.y+=2;
  318. else
  319. if (ghost.y < bat.y)
  320.    bat.y-=2;
  321.  
  322. // check boundaries
  323. if (bat.x >= SCREEN_WIDTH)
  324.    bat.x = -bat.width;
  325. else
  326. if (bat.x < -bat.width)
  327.    bat.x = SCREEN_WIDTH;
  328.  
  329. if (bat.y >= SCREEN_HEIGHT)
  330.    bat.y = -bat.height;
  331. else
  332. if (bat.y < -bat.height)
  333.    bat.y = SCREEN_HEIGHT;
  334.  
  335. } // end Bat_AI
  336.  
  337. //////////////////////////////////////////////////////////
  338.  
  339. int Game_Main(void *parms)
  340. {
  341. // this is the workhorse of your game it will be called
  342. // continuously in real-time this is like main() in C
  343. // all the calls for you game go here!
  344.  
  345. int index; // looping var
  346.  
  347. // start the timing clock
  348. Start_Clock();
  349.  
  350. // clear the drawing surface
  351. DD_Fill_Surface(lpddsback, 0);
  352.  
  353. // lock back buffer and copy background into it
  354. DD_Lock_Back_Surface();
  355.  
  356. // draw background
  357. Draw_Bitmap(&background_bmp, back_buffer, back_lpitch,0);
  358.  
  359. // unlock back surface
  360. DD_Unlock_Back_Surface();
  361.  
  362. // read keyboard
  363. DI_Read_Keyboard();
  364.  
  365. // call the bat AI
  366. Bat_AI();
  367.  
  368. // the animate the bat
  369. Animate_BOB(&bat);
  370.  
  371. // draw the bat
  372. Draw_BOB(&bat, lpddsback);
  373.  
  374. // allow player to move
  375. if (keyboard_state[DIK_RIGHT])
  376.    ghost.x+=4;
  377. else
  378. if (keyboard_state[DIK_LEFT])
  379.    ghost.x-=4;
  380.  
  381. if (keyboard_state[DIK_UP])
  382.    ghost.y-=4;
  383. else
  384. if (keyboard_state[DIK_DOWN])
  385.    ghost.y+=4;
  386.  
  387. // test if player is off screen, if so wrap around
  388. if (ghost.x >= SCREEN_WIDTH)
  389.    ghost.x = -ghost.width;
  390. else
  391. if (ghost.x < -ghost.width)
  392.    ghost.x = SCREEN_WIDTH;
  393.  
  394. if (ghost.y >= SCREEN_HEIGHT)
  395.    ghost.y = -ghost.height;
  396. else
  397. if (ghost.y < -ghost.height)
  398.    ghost.y = SCREEN_HEIGHT;
  399.  
  400. // the animate the ghost
  401. Animate_BOB(&ghost);
  402.  
  403. // draw the ghost
  404. Draw_BOB(&ghost, lpddsback);
  405.  
  406. // draw title
  407. Draw_Text_GDI("GOING BATS! - Tracking Demo",10, 10,RGB(0,255,0), lpddsback);
  408.  
  409. // flip the surfaces
  410. DD_Flip();
  411.  
  412. // sync to 30ish fps
  413. Wait_Clock(30);
  414.  
  415. // check of user is trying to exit
  416. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  417.     {
  418.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  419.  
  420.     // stop all sounds
  421.     Stop_All_Sounds();
  422.  
  423.     // do a screen transition
  424.     Screen_Transitions(SCREEN_REDNESS,NULL,0);
  425.  
  426.     } // end if
  427.  
  428. // return success
  429. return(1);
  430.  
  431. } // end Game_Main
  432.  
  433. //////////////////////////////////////////////////////////
  434.