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

  1. // PROG23_1.CPP - fly brain demo
  2. // to compile make sure to include DDRAW.LIB, DSOUND.LIB,
  3. // DINPUT.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. // defines for flys
  40. #define MAX_FLYS        64
  41.  
  42. // bounding box for flys
  43. #define MIN_X_FLY       190 
  44. #define MAX_X_FLY       450
  45. #define MIN_Y_FLY       350
  46. #define MAX_Y_FLY       470
  47.  
  48. // PROTOTYPES /////////////////////////////////////////////
  49.  
  50. // game console
  51. int Game_Init(void *parms=NULL);
  52. int Game_Shutdown(void *parms=NULL);
  53. int Game_Main(void *parms=NULL);
  54.  
  55. // GLOBALS ////////////////////////////////////////////////
  56.  
  57. HWND main_window_handle           = NULL; // save the window handle
  58. HINSTANCE main_instance           = NULL; // save the instance
  59. char buffer[80];                          // used to print text
  60.  
  61. BITMAP_IMAGE background_bmp;   // holds the background
  62.  
  63. BOB flys[MAX_FLYS];            // the flys
  64.  
  65. int fly_sound_id = -1;         // sound id for fly sound
  66.  
  67. // FUNCTIONS //////////////////////////////////////////////
  68.  
  69. LRESULT CALLBACK WindowProc(HWND hwnd, 
  70.                             UINT msg, 
  71.                             WPARAM wparam, 
  72.                             LPARAM lparam)
  73. {
  74. // this is the main message handler of the system
  75. PAINTSTRUCT    ps;           // used in WM_PAINT
  76. HDC            hdc;       // handle to a device context
  77.  
  78. // what is the message 
  79. switch(msg)
  80.     {    
  81.     case WM_CREATE: 
  82.         {
  83.         // do initialization stuff here
  84.         return(0);
  85.         } break;
  86.  
  87.     case WM_PAINT:
  88.          {
  89.          // start painting
  90.          hdc = BeginPaint(hwnd,&ps);
  91.  
  92.          // end painting
  93.          EndPaint(hwnd,&ps);
  94.          return(0);
  95.         } break;
  96.  
  97.     case WM_DESTROY: 
  98.         {
  99.         // kill the application            
  100.         PostQuitMessage(0);
  101.         return(0);
  102.         } break;
  103.  
  104.     default:break;
  105.  
  106.     } // end switch
  107.  
  108. // process any messages that we didn't take care of 
  109. return (DefWindowProc(hwnd, msg, wparam, lparam));
  110.  
  111. } // end WinProc
  112.  
  113. // WINMAIN ////////////////////////////////////////////////
  114.  
  115. int WINAPI WinMain(    HINSTANCE hinstance,
  116.                     HINSTANCE hprevinstance,
  117.                     LPSTR lpcmdline,
  118.                     int ncmdshow)
  119. {
  120. // this is the winmain function
  121.  
  122. WNDCLASS winclass;    // this will hold the class we create
  123. HWND     hwnd;        // generic window handle
  124. MSG         msg;        // generic message
  125. HDC      hdc;       // generic dc
  126. PAINTSTRUCT ps;     // generic paintstruct
  127.  
  128. // first fill in the window class stucture
  129. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  130.                           CS_HREDRAW | CS_VREDRAW;
  131. winclass.lpfnWndProc    = WindowProc;
  132. winclass.cbClsExtra        = 0;
  133. winclass.cbWndExtra        = 0;
  134. winclass.hInstance        = hinstance;
  135. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  136. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  137. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  138. winclass.lpszMenuName    = NULL; 
  139. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  140.  
  141. // register the window class
  142. if (!RegisterClass(&winclass))
  143.     return(0);
  144.  
  145. // create the window, note the use of WS_POPUP
  146. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  147.                           "WinX Game Console",     // title
  148.                           WS_POPUP | WS_VISIBLE,
  149.                            0,0,       // x,y
  150.                           WINDOW_WIDTH,  // width
  151.                           WINDOW_HEIGHT, // height
  152.                           NULL,       // handle to parent 
  153.                           NULL,       // handle to menu
  154.                           hinstance,// instance
  155.                           NULL)))    // creation parms
  156. return(0);
  157.  
  158. // save the window handle and instance in a global
  159. main_window_handle = hwnd;
  160. main_instance      = hinstance;
  161.  
  162. // perform all game console specific initialization
  163. Game_Init();
  164.  
  165. // enter main event loop
  166. while(1)
  167.     {
  168.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  169.         { 
  170.         // test if this is a quit
  171.         if (msg.message == WM_QUIT)
  172.            break;
  173.     
  174.         // translate any accelerator keys
  175.         TranslateMessage(&msg);
  176.  
  177.         // send the message to the window proc
  178.         DispatchMessage(&msg);
  179.         } // end if
  180.     
  181.     // main game processing goes here
  182.     Game_Main();
  183.  
  184.     } // end while
  185.  
  186. // shutdown game and release all resources
  187. Game_Shutdown();
  188.  
  189. // return to Windows like this
  190. return(msg.wParam);
  191.  
  192. } // end WinMain
  193.  
  194. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  195.  
  196. int Game_Init(void *parms)
  197. {
  198. // this function is where you do all the initialization 
  199. // for your game
  200.  
  201. int index; // looping variable
  202.  
  203. // start up DirectDraw (replace the parms as you desire)
  204. DD_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
  205.  
  206. // load background image
  207. Load_Bitmap_File(&bitmap8bit, "FLYBACK.BMP");
  208. Create_Bitmap(&background_bmp,0,0,640,480);
  209. Load_Image_Bitmap(&background_bmp, &bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);
  210. Set_Palette(bitmap8bit.palette);
  211. Unload_Bitmap_File(&bitmap8bit);
  212.  
  213. // load the fly bitmaps
  214. Load_Bitmap_File(&bitmap8bit, "FLYS8.BMP");
  215.  
  216. // create master fly bob
  217. Create_BOB(&flys[0],320,200, 8,8, 4, BOB_ATTR_MULTI_FRAME | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY);
  218. Set_Anim_Speed_BOB(&flys[0], 1);
  219.  
  220. // load the fly in 
  221. for (index=0; index<4; index++)
  222.     Load_Frame_BOB(&flys[0], &bitmap8bit, index, index, 0, BITMAP_EXTRACT_MODE_CELL);
  223.  
  224. // unload flys
  225. Unload_Bitmap_File(&bitmap8bit);
  226.  
  227.  
  228. // now replicate flys
  229. for (index = 1; index<MAX_FLYS; index++)
  230.     Clone_BOBX(&flys[0], &flys[index]);
  231.  
  232. // now set all of their values
  233. for (index=0; index<MAX_FLYS; index++)
  234.     {
  235.     // set positions
  236.     Set_Pos_BOB(&flys[index], 320-32+rand()%64, 450-rand()%32);
  237.  
  238.     // set start frame randomly
  239.     flys[index].curr_frame = 0;
  240.  
  241.     } // end for index
  242.  
  243. // initilize DirectSound
  244. DSound_Init();
  245.  
  246. // load fly sound
  247. fly_sound_id = Load_WAV("FLYS.WAV");
  248.  
  249. // start the sound
  250. Play_Sound(fly_sound_id, DSBPLAY_LOOPING);
  251.  
  252. // return success
  253. return(1);
  254.  
  255. } // end Game_Init
  256.  
  257. ///////////////////////////////////////////////////////////
  258.  
  259. int Game_Shutdown(void *parms)
  260. {
  261. // this function is where you shutdown your game and
  262. // release all resources that you allocated
  263.  
  264. // shut everything down
  265.  
  266. // kill all the flys
  267. for (int index=0; index<MAX_FLYS; index++)
  268.     Destroy_BOBX(&flys[index]);
  269.  
  270. // shutdown directdraw last
  271. DD_Shutdown();
  272.  
  273. // now directsound
  274. Stop_All_Sounds();
  275. DSound_Shutdown();
  276.  
  277. // return success
  278. return(1);
  279. } // end Game_Shutdown
  280.  
  281. ///////////////////////////////////////////////////////////
  282.  
  283. void Flys_AI(void)
  284. {
  285. // this function performs the AI for the flys
  286.  
  287. for (int curr_fly=0; curr_fly<MAX_FLYS; curr_fly++)
  288.     {   
  289.     // test if fly directional counter <= 0
  290.     if (--flys[curr_fly].varsI[0] <= 0)
  291.         {
  292.         // select a new random directional velocity
  293.         flys[curr_fly].xv = -4 + rand()%9;
  294.         flys[curr_fly].yv = -4 + rand()%9;
  295.  
  296.         // set time for motion to occur
  297.         flys[curr_fly].varsI[0] = 2+rand()%8;
  298.  
  299.         } // end if
  300.  
  301.     // move the fly
  302.     Move_BOB(&flys[curr_fly]);
  303.  
  304.     // animate the fly
  305.     Animate_BOB(&flys[curr_fly]);
  306.  
  307.     // test if fly has left the fresh meat
  308.     if (flys[curr_fly].x > MAX_X_FLY) 
  309.        {
  310.        flys[curr_fly].xv=-flys[curr_fly].xv;
  311.        flys[curr_fly].x = MAX_X_FLY;
  312.        } // end if
  313.  
  314.     if (flys[curr_fly].x < MIN_X_FLY) 
  315.        {
  316.        flys[curr_fly].xv=-flys[curr_fly].xv;
  317.        flys[curr_fly].x = MIN_X_FLY;
  318.        } // end if
  319.  
  320.     if (flys[curr_fly].y > MAX_Y_FLY) 
  321.        {
  322.        flys[curr_fly].yv=-flys[curr_fly].yv;
  323.        flys[curr_fly].y = MAX_Y_FLY;
  324.        } // end if
  325.  
  326.     if (flys[curr_fly].y < MIN_Y_FLY) 
  327.        {
  328.        flys[curr_fly].yv=-flys[curr_fly].yv;
  329.        flys[curr_fly].y = MIN_Y_FLY;
  330.        } // end if
  331.  
  332.     } // end for curr_fly
  333.  
  334. } // end Flys_AI
  335.  
  336. //////////////////////////////////////////////////////////
  337.  
  338. int Game_Main(void *parms)
  339. {
  340. // this is the workhorse of your game it will be called
  341. // continuously in real-time this is like main() in C
  342. // all the calls for you game go here!
  343.  
  344. int index; // looping var
  345.  
  346. // check of user is trying to exit
  347. if (KEY_DOWN(VK_ESCAPE))
  348.     {
  349.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  350.  
  351.     // stop all sounds
  352.     Stop_All_Sounds();
  353.  
  354.     // do a screen transition
  355.     Screen_Transitions(SCREEN_REDNESS,NULL,0);
  356.  
  357.     } // end if
  358.  
  359. // start the timing clock
  360. Start_Clock();
  361.  
  362. // clear the drawing surface
  363. DD_Fill_Surface(lpddsback, 0);
  364.  
  365. // lock back buffer and copy background into it
  366. DD_Lock_Back_Surface();
  367.  
  368. // draw background
  369. Draw_Bitmap(&background_bmp, back_buffer, back_lpitch,0);
  370.  
  371. // unlock back surface
  372. DD_Unlock_Back_Surface();
  373.  
  374. // process the fly ai, move them buzz around dead bodies
  375. Flys_AI();
  376.  
  377. // draw the flys
  378. for (index=0; index < MAX_FLYS; index++)
  379.      Draw_BOB(&flys[index], lpddsback);
  380.  
  381. // flip the surfaces
  382. DD_Flip();
  383.  
  384. // sync to 30ish fps
  385. Wait_Clock(30);
  386.  
  387. // return success
  388. return(1);
  389.  
  390. } // end Game_Main
  391.  
  392. //////////////////////////////////////////////////////////
  393.