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

  1. // PROG17_1_16b.CPP - 16-bit 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 = 800;
  201. screen_height = 600;
  202. screen_bpp    = 16;
  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(&bitmap16bit, "SKY16.BMP");
  209. Create_Bitmap16(&background_bmp,0,0,800,600);
  210. Load_Image_Bitmap16(&background_bmp, &bitmap16bit,0,0,BITMAP_EXTRACT_MODE_ABS);
  211. Unload_Bitmap_File(&bitmap16bit);
  212.  
  213. // load the bitmaps
  214. Load_Bitmap_File(&bitmap16bit, "CHOP16.BMP");
  215.  
  216. // create bob
  217. Create_BOB16(&chopper[0],0,120,200,64,3,BOB_ATTR_MULTI_FRAME | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY);
  218.  
  219. // set animation speed
  220. Set_Anim_Speed_BOB16(&chopper[0], 1);
  221.  
  222. // set motion speed
  223. Set_Vel_BOB16(&chopper[0],2+rand()%6,0);
  224.  
  225. // set position
  226. Set_Pos_BOB16(&chopper[0], rand()%screen_width, rand()%screen_height);
  227.  
  228. // load the bob in 
  229. for (index=0; index < 3; index++)
  230.     Load_Frame_BOB16(&chopper[0], &bitmap16bit, index, 0, index, BITMAP_EXTRACT_MODE_CELL);
  231.  
  232. // now make the remaining 15 clones
  233. for (index=1; index < NUM_CHOPPERS; index++)
  234.     {
  235.     // clone the bob
  236.     Clone_BOBX(&chopper[0], &chopper[index]);
  237.  
  238.     // set new position and velocity
  239.     Set_Vel_BOB16(&chopper[index],2+rand()%6,0);
  240.     Set_Pos_BOB16(&chopper[index], rand()%screen_width, rand()%screen_height);
  241.  
  242.     // now here the tricky part, alter the size of the bob, but make sure
  243.     // to keep the aspect ratio the same, so it doesn't look scrunched
  244.     float width  = 200 - 10 * index;
  245.     float height = (float)width*((float)64/(float)200);
  246.  
  247.     // store new width and height in varsIable cache positions 0,1
  248.     chopper[index].varsI[0] = width;
  249.     chopper[index].varsI[1] = height;
  250.  
  251.     } // end for index
  252.  
  253. // unload bitmap image
  254. Unload_Bitmap_File(&bitmap16bit);
  255.  
  256. // initialize directinput
  257. DInput_Init();
  258.  
  259. // acquire the keyboard only
  260. DI_Init_Keyboard();
  261.  
  262. // initilize DirectSound
  263. DSound_Init();
  264.  
  265. // load background sounds
  266. sound_id = Load_WAV("CHOP.WAV");
  267.  
  268. // start the sounds
  269. Play_Sound(sound_id, DSBPLAY_LOOPING);
  270.  
  271. // set clipping rectangle to screen extents so objects dont
  272. // mess up at edges
  273. RECT screen_rect = {0,0,screen_width,screen_height};
  274. lpddclipper = DD_Attach_Clipper(lpddsback,1,&screen_rect);
  275.  
  276. // hide the mouse
  277. ShowCursor(FALSE);
  278.  
  279. // return success
  280. return(1);
  281.  
  282. } // end Game_Init
  283.  
  284. ///////////////////////////////////////////////////////////
  285.  
  286. int Game_Shutdown(void *parms)
  287. {
  288. // this function is where you shutdown your game and
  289. // release all resources that you allocated
  290.  
  291. // shut everything down
  292.  
  293. // kill all the bobs
  294. for (int index=0; index < NUM_CHOPPERS; index++)
  295.     Destroy_BOBX(&chopper[index]);
  296.  
  297. // shutdown directdraw last
  298. DD_Shutdown();
  299.  
  300. // now directsound
  301. Stop_All_Sounds();
  302. DSound_Shutdown();
  303.  
  304. // shut down directinput
  305. DInput_Shutdown();
  306.  
  307. // return success
  308. return(1);
  309.  
  310. } // end Game_Shutdown
  311.  
  312. //////////////////////////////////////////////////////////
  313.  
  314. int Game_Main(void *parms)
  315. {
  316. // this is the workhorse of your game it will be called
  317. // continuously in real-time this is like main() in C
  318. // all the calls for you game go here!
  319. int index; // looping var
  320.  
  321. // start the timing clock
  322. Start_Clock();
  323.  
  324. // clear the drawing surface
  325. DD_Fill_Surface(lpddsback, 0);
  326.  
  327. // lock back buffer and copy background into it
  328. DD_Lock_Back_Surface();
  329.  
  330. // draw background
  331. Draw_Bitmap16(&background_bmp, back_buffer, back_lpitch,0);
  332.  
  333. // unlock back surface
  334. DD_Unlock_Back_Surface();
  335.  
  336. // read keyboard
  337. DI_Read_Keyboard();
  338.  
  339. // move and animate the fleet
  340. for (index=0; index<NUM_CHOPPERS; index++)
  341.     {
  342.     // move the chopper
  343.     Move_BOB16(&chopper[index]);
  344.  
  345.     // test for off the screen wrap around
  346.     if (chopper[index].x > screen_width)
  347.        chopper[index].x = -chopper[index].width;
  348.  
  349.     // animate the bat
  350.     Animate_BOB16(&chopper[index]);
  351.  
  352.     } // end for index
  353.  
  354. // if (keyboard_state[DIK_RIGHT])
  355.  
  356. // draw the choppers
  357. for (index=NUM_CHOPPERS-1; index>=0; index--)
  358.     Draw_Scaled_BOB16(&chopper[index], chopper[index].varsI[0], chopper[index].varsI[1],lpddsback);
  359.  
  360. // draw the title
  361. Draw_Text_GDI("CONSTANT VELOCITY DEMO",10, 10,RGB(0,0,0), lpddsback);
  362.  
  363. // flip the surfaces
  364. DD_Flip();
  365.  
  366. // sync to 30 fps = 1/30sec = 33 ms
  367. Wait_Clock(33);
  368.  
  369. // check of user is trying to exit
  370. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  371.     {
  372.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  373.  
  374.     // stop all sounds
  375.     Stop_All_Sounds();
  376.  
  377.     } // end if
  378.  
  379. // return success
  380. return(1);
  381.  
  382. } // end Game_Main
  383.  
  384. //////////////////////////////////////////////////////////