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

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