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

  1. // PROG17_1.CPP - constant velocity 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_CHOPPERS    16   // number of choppers in simulation
  40.  
  41. // PROTOTYPES /////////////////////////////////////////////
  42.  
  43. // game console
  44. int Game_Init(void *parms=NULL);
  45. int Game_Shutdown(void *parms=NULL);
  46. int Game_Main(void *parms=NULL);
  47.  
  48. // GLOBALS ////////////////////////////////////////////////
  49.  
  50. HWND main_window_handle           = NULL; // save the window handle
  51. HINSTANCE main_instance           = NULL; // save the instance
  52. char buffer[80];                          // used to print text
  53.  
  54. BITMAP_IMAGE background_bmp;   // holds the background
  55. BOB          chopper[16];      // the fleet
  56.  
  57. int sound_id = -1;             // general sound
  58.  
  59. // FUNCTIONS //////////////////////////////////////////////
  60.  
  61. LRESULT CALLBACK WindowProc(HWND hwnd, 
  62.                             UINT msg, 
  63.                             WPARAM wparam, 
  64.                             LPARAM lparam)
  65. {
  66. // this is the main message handler of the system
  67. PAINTSTRUCT    ps;           // used in WM_PAINT
  68. HDC            hdc;       // handle to a device context
  69.  
  70. // what is the message 
  71. switch(msg)
  72.     {    
  73.     case WM_CREATE: 
  74.         {
  75.         // do initialization stuff here
  76.         return(0);
  77.         } break;
  78.  
  79.     case WM_PAINT:
  80.          {
  81.          // start painting
  82.          hdc = BeginPaint(hwnd,&ps);
  83.  
  84.          // end painting
  85.          EndPaint(hwnd,&ps);
  86.          return(0);
  87.         } break;
  88.  
  89.     case WM_DESTROY: 
  90.         {
  91.         // kill the application            
  92.         PostQuitMessage(0);
  93.         return(0);
  94.         } break;
  95.  
  96.     default:break;
  97.  
  98.     } // end switch
  99.  
  100. // process any messages that we didn't take care of 
  101. return (DefWindowProc(hwnd, msg, wparam, lparam));
  102.  
  103. } // end WinProc
  104.  
  105. // WINMAIN ////////////////////////////////////////////////
  106.  
  107. int WINAPI WinMain(    HINSTANCE hinstance,
  108.                     HINSTANCE hprevinstance,
  109.                     LPSTR lpcmdline,
  110.                     int ncmdshow)
  111. {
  112. // this is the winmain function
  113.  
  114. WNDCLASS winclass;    // this will hold the class we create
  115. HWND     hwnd;        // generic window handle
  116. MSG         msg;        // generic message
  117. HDC      hdc;       // generic dc
  118. PAINTSTRUCT ps;     // generic paintstruct
  119.  
  120. // first fill in the window class stucture
  121. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  122.                           CS_HREDRAW | CS_VREDRAW;
  123. winclass.lpfnWndProc    = WindowProc;
  124. winclass.cbClsExtra        = 0;
  125. winclass.cbWndExtra        = 0;
  126. winclass.hInstance        = hinstance;
  127. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  128. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  129. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  130. winclass.lpszMenuName    = NULL; 
  131. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  132.  
  133. // register the window class
  134. if (!RegisterClass(&winclass))
  135.     return(0);
  136.  
  137. // create the window, note the use of WS_POPUP
  138. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  139.                           "WinX Game Console",     // title
  140.                           WS_POPUP | WS_VISIBLE,
  141.                            0,0,       // x,y
  142.                           WINDOW_WIDTH,  // width
  143.                           WINDOW_HEIGHT, // height
  144.                           NULL,       // handle to parent 
  145.                           NULL,       // handle to menu
  146.                           hinstance,// instance
  147.                           NULL)))    // creation parms
  148. return(0);
  149.  
  150. // save the window handle and instance in a global
  151. main_window_handle = hwnd;
  152. main_instance      = hinstance;
  153.  
  154. // perform all game console specific initialization
  155. Game_Init();
  156.  
  157. // enter main event loop
  158. while(1)
  159.     {
  160.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  161.         { 
  162.         // test if this is a quit
  163.         if (msg.message == WM_QUIT)
  164.            break;
  165.     
  166.         // translate any accelerator keys
  167.         TranslateMessage(&msg);
  168.  
  169.         // send the message to the window proc
  170.         DispatchMessage(&msg);
  171.         } // end if
  172.     
  173.     // main game processing goes here
  174.     Game_Main();
  175.  
  176.     } // end while
  177.  
  178. // shutdown game and release all resources
  179. Game_Shutdown();
  180.  
  181. // return to Windows like this
  182. return(msg.wParam);
  183.  
  184. } // end WinMain
  185.  
  186. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  187.  
  188. int Game_Init(void *parms)
  189. {
  190. // this function is where you do all the initialization 
  191. // for your game
  192.  
  193. int index; // looping varsIable
  194.  
  195. char filename[80]; // used to build up filenames
  196.  
  197. // seed random number generate
  198. srand(Start_Clock());
  199.  
  200. screen_width = 640;
  201. screen_height = 480;
  202. screen_bpp    = 8;
  203.  
  204. // start up DirectDraw (replace the parms as you desire)
  205. DD_Init(screen_width, screen_height, screen_bpp);
  206.  
  207. // load background image
  208. Load_Bitmap_File(&bitmap8bit, "SKY8.BMP");
  209. Create_Bitmap(&background_bmp,0,0,640,480);
  210. Load_Image_Bitmap(&background_bmp, &bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);
  211. Set_Palette(bitmap8bit.palette);
  212. Unload_Bitmap_File(&bitmap8bit);
  213.  
  214. // load the bitmaps
  215. Load_Bitmap_File(&bitmap8bit, "CHOP8.BMP");
  216.  
  217. // create bob
  218. Create_BOB(&chopper[0],0,120,200,64,3,BOB_ATTR_MULTI_FRAME | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY);
  219.  
  220. // set animation speed
  221. Set_Anim_Speed_BOB(&chopper[0], 1);
  222.  
  223. // set motion speed
  224. Set_Vel_BOB(&chopper[0],2+rand()%6,0);
  225.  
  226. // set position
  227. Set_Pos_BOB(&chopper[0], rand()%screen_width, rand()%screen_height);
  228.  
  229. // load the bob in 
  230. for (index=0; index < 3; index++)
  231.     Load_Frame_BOB(&chopper[0], &bitmap8bit, index, 0, index, BITMAP_EXTRACT_MODE_CELL);
  232.  
  233. // now make the remaining 15 clones
  234. for (index=1; index < NUM_CHOPPERS; index++)
  235.     {
  236.     // clone the bob
  237.     Clone_BOBX(&chopper[0], &chopper[index]);
  238.  
  239.     // set new position and velocity
  240.     Set_Vel_BOB(&chopper[index],2+rand()%6,0);
  241.     Set_Pos_BOB(&chopper[index], rand()%screen_width, rand()%screen_height);
  242.  
  243.     // now here the tricky part, alter the size of the bob, but make sure
  244.     // to keep the aspect ratio the same, so it doesn't look scrunched
  245.     float width  = 200 - 10 * index;
  246.     float height = (float)width*((float)64/(float)200);
  247.  
  248.     // store new width and height in varsIable cache positions 0,1
  249.     chopper[index].varsI[0] = width;
  250.     chopper[index].varsI[1] = height;
  251.  
  252.     } // end for index
  253.  
  254. // unload bitmap image
  255. Unload_Bitmap_File(&bitmap8bit);
  256.  
  257. // initialize directinput
  258. DInput_Init();
  259.  
  260. // acquire the keyboard only
  261. DI_Init_Keyboard();
  262.  
  263. // initilize DirectSound
  264. DSound_Init();
  265.  
  266. // load background sounds
  267. sound_id = Load_WAV("CHOP.WAV");
  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_CHOPPERS; index++)
  296.     Destroy_BOBX(&chopper[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. 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_Bitmap(&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 and animate the fleet
  341. for (index=0; index<NUM_CHOPPERS; index++)
  342.     {
  343.     // move the chopper
  344.     Move_BOB(&chopper[index]);
  345.  
  346.     // test for off the screen wrap around
  347.     if (chopper[index].x > screen_width)
  348.        chopper[index].x = -chopper[index].width;
  349.  
  350.     // animate the bat
  351.     Animate_BOB(&chopper[index]);
  352.  
  353.     } // end for index
  354.  
  355. // if (keyboard_state[DIK_RIGHT])
  356.  
  357. // draw the choppers
  358. for (index=NUM_CHOPPERS-1; index>=0; index--)
  359.     Draw_Scaled_BOB(&chopper[index], chopper[index].varsI[0], chopper[index].varsI[1],lpddsback);
  360.  
  361. // draw the title
  362. Draw_Text_GDI("CONSTANT VELOCITY DEMO",10, 10,RGB(0,200,200), lpddsback);
  363.  
  364. // flip the surfaces
  365. DD_Flip();
  366.  
  367. // sync to 30 fps = 1/30sec = 33 ms
  368. Wait_Clock(33);
  369.  
  370. // check of user is trying to exit
  371. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  372.     {
  373.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  374.  
  375.     // stop all sounds
  376.     Stop_All_Sounds();
  377.  
  378.     // do a screen transition
  379.     Screen_Transitions(SCREEN_DARKNESS,NULL,0);
  380.     } // end if
  381.  
  382. // return success
  383. return(1);
  384.  
  385. } // end Game_Main
  386.  
  387. //////////////////////////////////////////////////////////