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

  1. // PROG17_2.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    = 8;
  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(&bitmap8bit, "GANTRY8.BMP");
  211. Create_Bitmap(&background_bmp,0,0,640,480);
  212. Load_Image_Bitmap(&background_bmp, &bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);
  213. Set_Palette(bitmap8bit.palette);
  214. Unload_Bitmap_File(&bitmap8bit);
  215.  
  216. // load the bitmaps
  217. Load_Bitmap_File(&bitmap8bit, "ROCKET8.BMP");
  218.  
  219. // create bob
  220. Create_BOB(&rocket,312,420,24,80,3,BOB_ATTR_MULTI_FRAME | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY);
  221.  
  222. // set animation speed
  223. Set_Anim_Speed_BOB(&rocket, 2);
  224.  
  225. // load the bob in 
  226. for (index=0; index < 3; index++)
  227.     Load_Frame_BOB(&rocket, &bitmap8bit, index, index,0,BITMAP_EXTRACT_MODE_CELL);
  228.  
  229. // set animation state to on pad
  230. rocket.anim_state = ROCKET_STATE_ON_PAD;
  231.  
  232. // use varsI[0] to hold acceleration mode, set to on
  233. rocket.varsI[0] = 1; // -1 is off
  234.  
  235. // use varsF[0,1] to hold the velocity and acceleration
  236. rocket.varsF[0] = 0; // initial velocity
  237. rocket.varsF[1] = 0.2; // initial acceleration 0.2 pixel/frame^2
  238.  
  239. // unload bitmap image
  240. Unload_Bitmap_File(&bitmap8bit);
  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. sound_id = Load_WAV("ROCKET.WAV");
  253.  
  254. // start the sounds
  255. //Play_Sound(sound_id, DSBPLAY_LOOPING);
  256.  
  257. // set clipping rectangle to screen extents so objects dont
  258. // mess up at edges
  259. RECT screen_rect = {0,0,screen_width,screen_height};
  260. lpddclipper = DD_Attach_Clipper(lpddsback,1,&screen_rect);
  261.  
  262. // hide the mouse
  263. ShowCursor(FALSE);
  264.  
  265. // return success
  266. return(1);
  267.  
  268. } // end Game_Init
  269.  
  270. ///////////////////////////////////////////////////////////
  271.  
  272. int Game_Shutdown(void *parms)
  273. {
  274. // this function is where you shutdown your game and
  275. // release all resources that you allocated
  276.  
  277. // shut everything down
  278.  
  279. // kill all the bobs
  280. Destroy_BOBX(&rocket);
  281.  
  282. // shutdown directdraw last
  283. DD_Shutdown();
  284.  
  285. // now directsound
  286. Stop_All_Sounds();
  287. DSound_Shutdown();
  288.  
  289. // shut down directinput
  290. DInput_Shutdown();
  291.  
  292. // return success
  293. return(1);
  294.  
  295. } // end Game_Shutdown
  296.  
  297. //////////////////////////////////////////////////////////
  298.  
  299. int Game_Main(void *parms)
  300. {
  301. // this is the workhorse of your game it will be called
  302. // continuously in real-time this is like main() in C
  303. // all the calls for you game go here!
  304.  
  305. int index; // looping var
  306.  
  307. static int debounce_a = 0; // used to debounce acceleration toggle key
  308.  
  309. // start the timing clock
  310. Start_Clock();
  311.  
  312. // clear the drawing surface
  313. DD_Fill_Surface(lpddsback, 0);
  314.  
  315. // lock back buffer and copy background into it
  316. DD_Lock_Back_Surface();
  317.  
  318. // draw background
  319. Draw_Bitmap(&background_bmp, back_buffer, back_lpitch,0);
  320.  
  321. // unlock back surface
  322. DD_Unlock_Back_Surface();
  323.  
  324. // read keyboard
  325. DI_Read_Keyboard();
  326.  
  327.  
  328. // test if user is toggling acceleration control
  329. if (keyboard_state[DIK_A] && !debounce_a)
  330.    {
  331.    // toggle acceleration
  332.    rocket.varsI[0] = -rocket.varsI[0];
  333.    
  334.    // debounce key, so it doesn't toggle 60 times!
  335.    debounce_a = 1;
  336.  
  337.    } // end if
  338.  
  339. if (!keyboard_state[DIK_A])
  340.    debounce_a = 0;
  341.  
  342. // test if user is changing acceleration
  343. if (keyboard_state[DIK_DOWN])
  344.    {
  345.    // decrease acceleration factor   
  346.    if ((rocket.varsF[1]-=0.1) < 0)
  347.         rocket.varsF[1] = 0.1;
  348.    } // end if
  349. else
  350. if (keyboard_state[DIK_UP])
  351.    {
  352.    // increase acceleration factor   
  353.    rocket.varsF[1]+=0.1;
  354.    } // end if
  355.  
  356. // test if player is firing rocket
  357. if (keyboard_state[DIK_SPACE] && rocket.anim_state == ROCKET_STATE_ON_PAD)
  358.     {
  359.     // fire the rocket
  360.     rocket.anim_state = ROCKET_STATE_IN_FLIGHT;
  361.  
  362.     // start sound
  363.     Play_Sound(sound_id, 0);
  364.  
  365.     } // end if
  366.  
  367. // test state of rocket
  368. if (rocket.anim_state == ROCKET_STATE_ON_PAD)
  369.    {
  370.    // make sure there is no animation and frame is 0
  371.    rocket.curr_frame = 0;
  372.  
  373.    } // end if
  374. else // rocket is in flight
  375.     {
  376.     // animate the rocket
  377.     Animate_BOB(&rocket);
  378.  
  379.     // move the rocket
  380.    
  381.     // update the position with the current velocity
  382.     rocket.y-=(int)(rocket.varsF[0]+0.5);
  383.  
  384.     // update velocity with acceleration (if acceleration is on)
  385.     if (rocket.varsI[0]==1)
  386.        rocket.varsF[0]+=rocket.varsF[1];
  387.  
  388.     // test if the rocket has moved off the screen
  389.     if (rocket.y < -4*rocket.height)
  390.        {
  391.        // reset everything
  392.        rocket.anim_state = ROCKET_STATE_ON_PAD;
  393.        rocket.curr_frame = 0;
  394.        rocket.varsF[0]   = 0;
  395.  
  396.        // reset position
  397.        Set_Pos_BOB(&rocket, 312, 420);
  398.  
  399.        // turn sound off
  400.        Stop_All_Sounds();
  401.  
  402.        } // end if
  403.  
  404.     } // end else
  405.  
  406. // draw the rocket
  407. Draw_BOB(&rocket, lpddsback);
  408.  
  409. // draw the title
  410. Draw_Text_GDI("ACCELERATION DEMO - Space to Launch, Arrows to Change Acceleration.",10, 10,RGB(0,255,255), lpddsback);
  411.  
  412. // draw information
  413.  
  414. // first acceleration mode
  415. if (rocket.varsI[0]==1)
  416.     sprintf(buffer, "Acceleration is ON");
  417. else
  418.     sprintf(buffer, "Acceleration is OFF");
  419.  
  420. Draw_Text_GDI(buffer,10,25,RGB(0,255,0), lpddsback);
  421.  
  422. // now current velocity
  423. sprintf(buffer, "Velocity = %f pixels/frame",rocket.varsF[0]);
  424. Draw_Text_GDI(buffer,10,40,RGB(0,255,0), lpddsback);
  425.  
  426. // and acceleration
  427. sprintf(buffer, "Acceleration = %f pixels/frame^2",rocket.varsF[1]);
  428. Draw_Text_GDI(buffer,10,55,RGB(0,255,0), lpddsback);
  429.  
  430. // flip the surfaces
  431. DD_Flip();
  432.  
  433. // sync to 30 fps = 1/30sec = 33 ms
  434. Wait_Clock(33);
  435.  
  436. // check of user is trying to exit
  437. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  438.     {
  439.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  440.  
  441.     // stop all sounds
  442.     Stop_All_Sounds();
  443.  
  444.     // do a screen transition
  445.     Screen_Transitions(SCREEN_BLUENESS,NULL,0);
  446.     } // end if
  447.  
  448. // return success
  449. return(1);
  450.  
  451. } // end Game_Main
  452.  
  453. //////////////////////////////////////////////////////////