demoII3_6.cpp
上传用户:husern
上传日期:2018-01-20
资源大小:42486k
文件大小:16k
源码类别:

游戏

开发平台:

Visual C++

  1. // DEMOII3_6.CPP - Directinput joystick demo based on code
  2. // from volume I
  3. // READ THIS!
  4. // To compile make sure to include DDRAW.LIB, DSOUND.LIB,
  5. // DINPUT.LIB, DINPUT8.LIB, WINMM.LIB in the project link list, and of course 
  6. // the C++ source modules T3DLIB1.CPP,T3DLIB2.CPP, and T3DLIB3.CPP
  7. // and the headers T3DLIB1.H,T3DLIB2.H, and T3DLIB3.H must
  8. // be in the working directory of the compiler
  9. // INCLUDES ///////////////////////////////////////////////
  10. #define INITGUID       // make sure al the COM interfaces are available
  11.                        // instead of this you can include the .LIB file
  12.                        // DXGUID.LIB
  13. #define WIN32_LEAN_AND_MEAN  
  14. #include <windows.h>   // include important windows stuff
  15. #include <windowsx.h> 
  16. #include <mmsystem.h>
  17. #include <iostream.h> // include important C/C++ stuff
  18. #include <conio.h>
  19. #include <stdlib.h>
  20. #include <malloc.h>
  21. #include <memory.h>
  22. #include <string.h>
  23. #include <stdarg.h>
  24. #include <stdio.h> 
  25. #include <math.h>
  26. #include <io.h>
  27. #include <fcntl.h>
  28. #include <ddraw.h>  // directX includes
  29. #include <dsound.h>
  30. #include <dmksctrl.h>
  31. #include <dmusici.h>
  32. #include <dmusicc.h>
  33. #include <dmusicf.h>
  34. #include <dinput.h>
  35. #include "T3DLIB1.h" // game library includes
  36. #include "T3DLIB2.h"
  37. #include "T3DLIB3.h"
  38. // DEFINES ////////////////////////////////////////////////
  39. // defines for windows interface
  40. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  41. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  42. #define WINDOW_WIDTH      640   // size of window
  43. #define WINDOW_HEIGHT     480
  44. #define WINDOW_BPP        8    // bitdepth of window (8,16,24 etc.)
  45.                                 // note: if windowed and not
  46.                                 // fullscreen then bitdepth must
  47.                                 // be same as system bitdepth
  48.                                 // also if 8-bit the a pallete
  49.                                 // is created and attached
  50. #define WINDOWED_APP      0     // 0 not windowed, 1 windowed
  51. // PROTOTYPES /////////////////////////////////////////////
  52. // game console
  53. int Game_Init(void *parms=NULL);
  54. int Game_Shutdown(void *parms=NULL);
  55. int Game_Main(void *parms=NULL);
  56. int Start_Missile(void);
  57. int Move_Missile(void);
  58. int Draw_Missile(void);
  59. // GLOBALS ////////////////////////////////////////////////
  60. HWND main_window_handle           = NULL; // save the window handle
  61. HINSTANCE main_instance           = NULL; // save the instance
  62. char buffer[256];                          // used to print text
  63. // demo globals
  64. BITMAP_IMAGE playfield;       // used to hold playfield
  65. BITMAP_IMAGE mushrooms[4];    // holds mushrooms
  66. BOB          blaster;         // holds bug blaster
  67. int blaster_anim[5] = {0,1,2,1,0};  // blinking animation
  68. // lets use a line segment for the missle
  69. int missile_x,              // position of missle
  70.     missile_y,            
  71.     missile_state;          // state of missle 0 off, 1 on
  72. // FUNCTIONS //////////////////////////////////////////////
  73. LRESULT CALLBACK WindowProc(HWND hwnd, 
  74.     UINT msg, 
  75.                             WPARAM wparam, 
  76.                             LPARAM lparam)
  77. {
  78. // this is the main message handler of the system
  79. PAINTSTRUCT ps;    // used in WM_PAINT
  80. HDC hdc;    // handle to a device context
  81. // what is the message 
  82. switch(msg)
  83. {
  84. case WM_CREATE: 
  85.         {
  86. // do initialization stuff here
  87. return(0);
  88. } break;
  89.     case WM_PAINT:
  90.          {
  91.          // start painting
  92.          hdc = BeginPaint(hwnd,&ps);
  93.          // end painting
  94.          EndPaint(hwnd,&ps);
  95.          return(0);
  96.         } break;
  97. case WM_DESTROY: 
  98. {
  99. // kill the application
  100. PostQuitMessage(0);
  101. return(0);
  102. } break;
  103. default:break;
  104.     } // end switch
  105. // process any messages that we didn't take care of 
  106. return (DefWindowProc(hwnd, msg, wparam, lparam));
  107. } // end WinProc
  108. // WINMAIN ////////////////////////////////////////////////
  109. int WINAPI WinMain( HINSTANCE hinstance,
  110. HINSTANCE hprevinstance,
  111. LPSTR lpcmdline,
  112. int ncmdshow)
  113. {
  114. // this is the winmain function
  115. WNDCLASS winclass; // this will hold the class we create
  116. HWND  hwnd; // generic window handle
  117. MSG  msg; // generic message
  118. HDC      hdc;       // generic dc
  119. PAINTSTRUCT ps;     // generic paintstruct
  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. // register the window class
  133. if (!RegisterClass(&winclass))
  134. return(0);
  135. // create the window, note the test to see if WINDOWED_APP is
  136. // true to select the appropriate window flags
  137. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  138.   WINDOW_TITLE,  // title
  139.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  140.     0,0,    // x,y
  141.   WINDOW_WIDTH,  // width
  142.                           WINDOW_HEIGHT, // height
  143.   NULL,    // handle to parent 
  144.   NULL,    // handle to menu
  145.   hinstance,// instance
  146.   NULL))) // creation parms
  147. return(0);
  148. // save the window handle and instance in a global
  149. main_window_handle = hwnd;
  150. main_instance      = hinstance;
  151. // resize the window so that client is really width x height
  152. if (WINDOWED_APP)
  153. {
  154. // now resize the window, so the client area is the actual size requested
  155. // since there may be borders and controls if this is going to be a windowed app
  156. // if the app is not windowed then it won't matter
  157. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  158. // make the call to adjust window_rect
  159. AdjustWindowRectEx(&window_rect,
  160.      GetWindowStyle(main_window_handle),
  161.      GetMenu(main_window_handle) != NULL, 
  162.      GetWindowExStyle(main_window_handle));
  163. // save the global client offsets, they are needed in DDraw_Flip()
  164. window_client_x0 = -window_rect.left;
  165. window_client_y0 = -window_rect.top;
  166. // now resize the window with a call to MoveWindow()
  167. MoveWindow(main_window_handle,
  168.            0, // x position
  169.            0, // y position
  170.            window_rect.right - window_rect.left, // width
  171.            window_rect.bottom - window_rect.top, // height
  172.            FALSE);
  173. // show the window, so there's no garbage on first render
  174. ShowWindow(main_window_handle, SW_SHOW);
  175. } // end if windowed
  176. // perform all game console specific initialization
  177. Game_Init();
  178. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  179. // if it causes your system to crash
  180. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  181. // enter main event loop
  182. while(1)
  183. {
  184. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  185. // test if this is a quit
  186.         if (msg.message == WM_QUIT)
  187.            break;
  188. // translate any accelerator keys
  189. TranslateMessage(&msg);
  190. // send the message to the window proc
  191. DispatchMessage(&msg);
  192. } // end if
  193.     
  194.     // main game processing goes here
  195.     Game_Main();
  196. } // end while
  197. // shutdown game and release all resources
  198. Game_Shutdown();
  199. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  200. // if it causes your system to crash
  201. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  202. // return to Windows like this
  203. return(msg.wParam);
  204. } // end WinMain
  205. /////////////////////////////////////////////////////////
  206. int Start_Missile(void)
  207. {
  208. // this function starts the missle, if it is currently off
  209. if (missile_state==0)
  210.     {
  211.     // enable missile
  212.     missile_state = 1;
  213.     // computer position of missile
  214.     missile_x = blaster.x + 16;
  215.     missile_y = blaster.y-4;
  216.     // return success
  217.     return(1);
  218.     } // end if
  219. // couldn't start missile
  220. return(0);
  221. } // end Start_Missile
  222. ///////////////////////////////////////////////////////////
  223. int Move_Missile(void)
  224. {
  225. // this function moves the missle 
  226. // test if missile is alive
  227. if (missile_state==1)
  228.    {
  229.    // move the missile upward
  230.    if ((missile_y-=10) < 0)
  231.       {
  232.       missile_state = 0;
  233.       return(1);
  234.       } // end if
  235.    // lock secondary buffer
  236.    DDraw_Lock_Back_Surface();
  237.    // add missile collision here
  238.    // unlock surface
  239.    DDraw_Unlock_Back_Surface();
  240.    // return success
  241.    return(1);
  242.    } // end if
  243. // return failure
  244. return(0);
  245. } // end Move_Missle
  246. ///////////////////////////////////////////////////////////
  247. int Draw_Missile(void)
  248. {
  249. // this function draws the missile 
  250. // test if missile is alive
  251. if (missile_state==1)
  252.    {
  253.    // lock secondary buffer
  254.    DDraw_Lock_Back_Surface();
  255.    // draw the missile in green
  256.    Draw_Clip_Line(missile_x, missile_y, 
  257.                   missile_x, missile_y+6,
  258.                   250,back_buffer, back_lpitch);
  259.    // unlock surface
  260.    DDraw_Unlock_Back_Surface();
  261.    // return success
  262.    return(1);
  263.    } // end if
  264. // return failure
  265. return(0);
  266. } // end Draw_Missle
  267. ///////////////////////////////////////////////////////////
  268. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  269. int Game_Init(void *parms)
  270. {
  271. // this function is where you do all the initialization 
  272. // for your game
  273. int index; // looping var
  274. // start up DirectDraw (replace the parms as you desire)
  275. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  276. // initialize directinput
  277. DInput_Init();
  278. // acquire the keyboard 
  279. DInput_Init_Keyboard();
  280. // initialize the joystick
  281. DInput_Init_Joystick(-24,24,-24,24);
  282. // initialize directsound and directmusic
  283. DSound_Init();
  284. DMusic_Init();
  285. // hide the mouse
  286. ShowCursor(FALSE);
  287. // seed random number generator
  288. srand(Start_Clock());
  289. // load the background
  290. Load_Bitmap_File(&bitmap8bit, "MUSH.BMP");
  291. // set the palette to background image palette
  292. Set_Palette(bitmap8bit.palette);
  293. // load in the four frames of the mushroom
  294. for (index=0; index<4; index++)
  295.     {
  296.     // create mushroom bitmaps
  297.     Create_Bitmap(&mushrooms[index],0,0,32,32);
  298.     Load_Image_Bitmap(&mushrooms[index],&bitmap8bit,index,0,BITMAP_EXTRACT_MODE_CELL);  
  299.     } // end for index
  300. // now create the bug blaster bob
  301. Create_BOB(&blaster,0,0,32,32,3,
  302.            BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_ANIM | BOB_ATTR_ANIM_ONE_SHOT,
  303.            DDSCAPS_SYSTEMMEMORY);
  304. // load in the four frames of the mushroom
  305. for (index=0; index<3; index++)
  306.      Load_Frame_BOB(&blaster,&bitmap8bit,index,index,1,BITMAP_EXTRACT_MODE_CELL);  
  307. // unload the bitmap file
  308. Unload_Bitmap_File(&bitmap8bit);
  309. // set the animation sequences for bug blaster
  310. Load_Animation_BOB(&blaster,0,5,blaster_anim);
  311. // set up stating state of bug blaster
  312. Set_Pos_BOB(&blaster,320, 400);
  313. Set_Anim_Speed_BOB(&blaster,3);
  314. // set clipping rectangle to screen extents so objects dont
  315. // mess up at edges
  316. RECT screen_rect = {0,0,screen_width,screen_height};
  317. lpddclipper = DDraw_Attach_Clipper(lpddsback,1,&screen_rect);
  318. // create mushroom playfield bitmap
  319. Create_Bitmap(&playfield,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
  320. playfield.attr |= BITMAP_ATTR_LOADED;
  321. // fill in the background
  322. Load_Bitmap_File(&bitmap8bit, "GRASS.BMP");
  323. // load the grass bitmap image
  324. Load_Image_Bitmap(&playfield,&bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);
  325. Unload_Bitmap_File(&bitmap8bit);
  326. // create the random mushroom patch
  327. for (index=0; index<50; index++)
  328.     {
  329.     // select a mushroom
  330.     int mush = rand()%4;
  331.     // set mushroom to random position
  332.     mushrooms[mush].x = rand()%(SCREEN_WIDTH-32);
  333.     mushrooms[mush].y = rand()%(SCREEN_HEIGHT-128);
  334.     // now draw the mushroom into playfield
  335.     Draw_Bitmap(&mushrooms[mush], playfield.buffer, playfield.width,1);
  336.     } // end for
  337. // return success
  338. return(1);
  339. } // end Game_Init
  340. ///////////////////////////////////////////////////////////
  341. int Game_Shutdown(void *parms)
  342. {
  343. // this function is where you shutdown your game and
  344. // release all resources that you allocated
  345. // shut everything down
  346. // release all your resources created for the game here....
  347. // kill the bug blaster
  348. Destroy_BOB(&blaster);
  349. // kill the mushroom maker
  350. for (int index=0; index<4; index++)
  351.     Destroy_Bitmap(&mushrooms[index]);
  352. // kill the playfield bitmap
  353. Destroy_Bitmap(&playfield);
  354. // now directsound
  355. DSound_Stop_All_Sounds();
  356. DSound_Delete_All_Sounds();
  357. DSound_Shutdown();
  358. // directmusic
  359. DMusic_Delete_All_MIDI();
  360. DMusic_Shutdown();
  361. // shut down directinput
  362. DInput_Release_Keyboard();
  363. DInput_Release_Joystick();
  364. DInput_Shutdown();
  365. // shutdown directdraw last
  366. DDraw_Shutdown();
  367. // return success
  368. return(1);
  369. } // end Game_Shutdown
  370. //////////////////////////////////////////////////////////
  371. int Game_Main(void *parms)
  372. {
  373. // this is the workhorse of your game it will be called
  374. // continuously in real-time this is like main() in C
  375. // all the calls for you game go here!
  376. int          index;             // looping var
  377. int          dx,dy;             // general deltas used in collision detection
  378. // start the timing clock
  379. Start_Clock();
  380. // clear the drawing surface
  381. DDraw_Fill_Surface(lpddsback, 0);
  382. // get the joystick data
  383. DInput_Read_Joystick();
  384. // lock the back buffer
  385. DDraw_Lock_Back_Surface();
  386. // draw the background reactor image
  387. Draw_Bitmap(&playfield, back_buffer, back_lpitch, 0);
  388. // unlock the back buffer
  389. DDraw_Unlock_Back_Surface();
  390. // is the player moving?
  391. blaster.x+=joy_state.lX;
  392. blaster.y+=joy_state.lY;
  393. // test bounds
  394. if (blaster.x > SCREEN_WIDTH-32)
  395.     blaster.x = SCREEN_WIDTH-32;
  396. else
  397. if (blaster.x < 0)
  398.     blaster.x = 0;
  399. if (blaster.y > SCREEN_HEIGHT-32)
  400.     blaster.y = SCREEN_HEIGHT-32;
  401. else
  402. if (blaster.y < SCREEN_HEIGHT-128)
  403.     blaster.y = SCREEN_HEIGHT-128;
  404. // is player firing?
  405. if (joy_state.rgbButtons[0])
  406.    Start_Missile();
  407. // move and draw missle
  408. Move_Missile();
  409. Draw_Missile();
  410. // is it time to blink eyes
  411. if ((rand()%100)==50)
  412.    Set_Animation_BOB(&blaster,0);
  413. // draw blaster
  414. Animate_BOB(&blaster);
  415. Draw_BOB(&blaster,lpddsback);
  416. // draw some text
  417. Draw_Text_GDI("Make My Centipede!",0,0,RGB(255,255,255),lpddsback);
  418. // display joystick and buttons 0-7
  419. sprintf(buffer,"Joystick Stats: X-Axis=%d, Y-Axis=%d, buttons(%d,%d,%d,%d,%d,%d,%d,%d)",
  420.                                                                       joy_state.lX,joy_state.lY,
  421.                                                                       joy_state.rgbButtons[0],
  422.                                                                       joy_state.rgbButtons[1],
  423.                                                                       joy_state.rgbButtons[2],
  424.                                                                       joy_state.rgbButtons[3],
  425.                                                                       joy_state.rgbButtons[4],
  426.                                                                       joy_state.rgbButtons[5],
  427.                                                                       joy_state.rgbButtons[6],
  428.                                                                       joy_state.rgbButtons[7]);
  429. Draw_Text_GDI(buffer,0,SCREEN_HEIGHT-20,RGB(255,255,50),lpddsback);
  430. // print out name of joystick
  431. sprintf(buffer, "Joystick Name & Vendor: %s",joyname);
  432. Draw_Text_GDI(buffer,0,SCREEN_HEIGHT-40,RGB(255,255,50),lpddsback);
  433. // flip the surfaces
  434. DDraw_Flip();
  435. // sync to 30 fps
  436. Wait_Clock(30);
  437. // check of user is trying to exit
  438. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  439.     {
  440.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  441.     } // end if
  442. // return success
  443. return(1);
  444.  
  445. } // end Game_Main
  446. //////////////////////////////////////////////////////////