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

游戏

开发平台:

Visual C++

  1. // DEMOII7_5.CPP
  2. // READ THIS!
  3. // To compile make sure to include DDRAW.LIB, DSOUND.LIB,
  4. // DINPUT.LIB, DINPUT8.LIB, WINMM.LIB in the project link list, and of course 
  5. // the C++ source modules T3DLIB1-5.CPP and the headers T3DLIB1-5.H
  6. // be in the working directory of the compiler
  7. // INCLUDES ///////////////////////////////////////////////
  8. #define INITGUID       // make sure al the COM interfaces are available
  9.                        // instead of this you can include the .LIB file
  10.                        // DXGUID.LIB
  11. #define WIN32_LEAN_AND_MEAN  
  12. #include <windows.h>   // include important windows stuff
  13. #include <windowsx.h> 
  14. #include <mmsystem.h>
  15. #include <iostream.h> // include important C/C++ stuff
  16. #include <conio.h>
  17. #include <stdlib.h>
  18. #include <malloc.h>
  19. #include <memory.h>
  20. #include <string.h>
  21. #include <stdarg.h>
  22. #include <stdio.h> 
  23. #include <math.h>
  24. #include <io.h>
  25. #include <fcntl.h>
  26. #include <ddraw.h>  // directX includes
  27. #include <dsound.h>
  28. #include <dmksctrl.h>
  29. #include <dmusici.h>
  30. #include <dmusicc.h>
  31. #include <dmusicf.h>
  32. #include <dinput.h>
  33. #include "T3DLIB1.h" // game library includes
  34. #include "T3DLIB2.h"
  35. #include "T3DLIB3.h"
  36. #include "T3DLIB4.h"
  37. #include "T3DLIB5.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      800  // size of window
  43. #define WINDOW_HEIGHT     600
  44. #define WINDOW_BPP        16    // 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. // object defines
  52. #define NUM_OBJECTS     16
  53. #define OBJECT_SPACING  250
  54. // PROTOTYPES /////////////////////////////////////////////
  55. // game console
  56. int Game_Init(void *parms=NULL);
  57. int Game_Shutdown(void *parms=NULL);
  58. int Game_Main(void *parms=NULL);
  59. // GLOBALS ////////////////////////////////////////////////
  60. HWND main_window_handle           = NULL; // save the window handle
  61. HINSTANCE main_instance           = NULL; // save the instance
  62. char buffer[2048];                        // used to print text
  63. // initialize camera position and direction
  64. POINT4D  cam_pos = {0,0,0,1};
  65. POINT4D  cam_target = {0,0,0,1};
  66. VECTOR4D cam_dir = {0,0,0,1};
  67. // all your initialization code goes here...
  68. VECTOR4D vscale={1.0,1.0,1.0,1}, 
  69.          vpos = {0,0,0,1}, 
  70.          vrot = {0,0,0,1};
  71. CAM4DV1        cam;       // the single camera
  72. OBJECT4DV1     obj;       // used to hold our cube mesh                   
  73. RENDERLIST4DV1 rend_list; // the render list
  74. // FUNCTIONS //////////////////////////////////////////////
  75. LRESULT CALLBACK WindowProc(HWND hwnd, 
  76.     UINT msg, 
  77.                             WPARAM wparam, 
  78.                             LPARAM lparam)
  79. {
  80. // this is the main message handler of the system
  81. PAINTSTRUCT ps;    // used in WM_PAINT
  82. HDC hdc;    // handle to a device context
  83. // what is the message 
  84. switch(msg)
  85. {
  86. case WM_CREATE: 
  87.         {
  88. // do initialization stuff here
  89. return(0);
  90. } break;
  91.     case WM_PAINT:
  92.          {
  93.          // start painting
  94.          hdc = BeginPaint(hwnd,&ps);
  95.          // end painting
  96.          EndPaint(hwnd,&ps);
  97.          return(0);
  98.         } break;
  99. case WM_DESTROY: 
  100. {
  101. // kill the application
  102. PostQuitMessage(0);
  103. return(0);
  104. } break;
  105. default:break;
  106.     } // end switch
  107. // process any messages that we didn't take care of 
  108. return (DefWindowProc(hwnd, msg, wparam, lparam));
  109. } // end WinProc
  110. // WINMAIN ////////////////////////////////////////////////
  111. int WINAPI WinMain( HINSTANCE hinstance,
  112. HINSTANCE hprevinstance,
  113. LPSTR lpcmdline,
  114. int ncmdshow)
  115. {
  116. // this is the winmain function
  117. WNDCLASS winclass; // this will hold the class we create
  118. HWND  hwnd; // generic window handle
  119. MSG  msg; // generic message
  120. HDC      hdc;       // generic dc
  121. PAINTSTRUCT ps;     // generic paintstruct
  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. // register the window class
  135. if (!RegisterClass(&winclass))
  136. return(0);
  137. // create the window, note the test to see if WINDOWED_APP is
  138. // true to select the appropriate window flags
  139. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  140.   WINDOW_TITLE,  // title
  141.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  142.     0,0,    // x,y
  143.   WINDOW_WIDTH,  // width
  144.                           WINDOW_HEIGHT, // height
  145.   NULL,    // handle to parent 
  146.   NULL,    // handle to menu
  147.   hinstance,// instance
  148.   NULL))) // creation parms
  149. return(0);
  150. // save the window handle and instance in a global
  151. main_window_handle = hwnd;
  152. main_instance      = hinstance;
  153. // resize the window so that client is really width x height
  154. if (WINDOWED_APP)
  155. {
  156. // now resize the window, so the client area is the actual size requested
  157. // since there may be borders and controls if this is going to be a windowed app
  158. // if the app is not windowed then it won't matter
  159. RECT window_rect = {0,0,WINDOW_WIDTH,WINDOW_HEIGHT};
  160. // make the call to adjust window_rect
  161. AdjustWindowRectEx(&window_rect,
  162.      GetWindowStyle(main_window_handle),
  163.      GetMenu(main_window_handle) != NULL,
  164.      GetWindowExStyle(main_window_handle));
  165. // save the global client offsets, they are needed in DDraw_Flip()
  166. window_client_x0 = -window_rect.left;
  167. window_client_y0 = -window_rect.top;
  168. // now resize the window with a call to MoveWindow()
  169. MoveWindow(main_window_handle,
  170.            0, // x position
  171.            0, // y position
  172.            window_rect.right - window_rect.left, // width
  173.            window_rect.bottom - window_rect.top, // height
  174.            FALSE);
  175. // show the window, so there's no garbage on first render
  176. ShowWindow(main_window_handle, SW_SHOW);
  177. } // end if windowed
  178. // perform all game console specific initialization
  179. Game_Init();
  180. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  181. // if it causes your system to crash
  182. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  183. // enter main event loop
  184. while(1)
  185. {
  186. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  187. // test if this is a quit
  188.         if (msg.message == WM_QUIT)
  189.            break;
  190. // translate any accelerator keys
  191. TranslateMessage(&msg);
  192. // send the message to the window proc
  193. DispatchMessage(&msg);
  194. } // end if
  195.     
  196.     // main game processing goes here
  197.     Game_Main();
  198. } // end while
  199. // shutdown game and release all resources
  200. Game_Shutdown();
  201. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  202. // if it causes your system to crash
  203. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  204. // return to Windows like this
  205. return(msg.wParam);
  206. } // end WinMain
  207. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  208. int Game_Init(void *parms)
  209. {
  210. // this function is where you do all the initialization 
  211. // for your game
  212. // start up DirectDraw (replace the parms as you desire)
  213. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  214. // initialize directinput
  215. DInput_Init();
  216. // acquire the keyboard 
  217. DInput_Init_Keyboard();
  218. // add calls to acquire other directinput devices here...
  219. // initialize directsound and directmusic
  220. DSound_Init();
  221. DMusic_Init();
  222. // hide the mouse
  223. if (!WINDOWED_APP)
  224.    ShowCursor(FALSE);
  225. // seed random number generator
  226. srand(Start_Clock());
  227. Open_Error_File("ERROR.TXT");
  228. // initialize math engine
  229. Build_Sin_Cos_Tables();
  230. // initialize the camera with 90 FOV, normalized coordinates
  231. Init_CAM4DV1(&cam,      // the camera object
  232.              CAM_MODEL_UVN, // the euler model
  233.              &cam_pos,  // initial camera position
  234.              &cam_dir,  // initial camera angles
  235.              &cam_target,      // no target
  236.              50.0,      // near and far clipping planes
  237.              8000.0,
  238.              90.0,      // field of view in degrees
  239.              WINDOW_WIDTH,   // size of final screen viewport
  240.              WINDOW_HEIGHT);
  241. // load the object
  242. Load_OBJECT4DV1_PLG(&obj, "tank1.plg",&vscale, &vpos, &vrot);
  243. // set the default position of the object in the world
  244. obj.world_pos.x = 0;
  245. obj.world_pos.y = 0;
  246. obj.world_pos.z = 0;
  247. // return success
  248. return(1);
  249. } // end Game_Init
  250. ///////////////////////////////////////////////////////////
  251. int Game_Shutdown(void *parms)
  252. {
  253. // this function is where you shutdown your game and
  254. // release all resources that you allocated
  255. // shut everything down
  256. // release all your resources created for the game here....
  257. // now directsound
  258. DSound_Stop_All_Sounds();
  259. DSound_Delete_All_Sounds();
  260. DSound_Shutdown();
  261. // directmusic
  262. DMusic_Delete_All_MIDI();
  263. DMusic_Shutdown();
  264. // shut down directinput
  265. DInput_Release_Keyboard();
  266. // shutdown directinput
  267. DInput_Shutdown();
  268. // shutdown directdraw last
  269. DDraw_Shutdown();
  270. Close_Error_File();
  271. // return success
  272. return(1);
  273. } // end Game_Shutdown
  274. //////////////////////////////////////////////////////////
  275. int Game_Main(void *parms)
  276. {
  277. // this is the workhorse of your game it will be called
  278. // continuously in real-time this is like main() in C
  279. // all the calls for you game go here!
  280. static MATRIX4X4 mrot;   // general rotation matrix
  281. static float x_ang = 0,  // object rotation angles
  282.              y_ang = 5, 
  283.              z_ang = 0;
  284. // these are used to create a circling camera
  285. static float view_angle = 0; 
  286. static float camera_distance = 1750;
  287. static VECTOR4D pos = {0,0,0,0};
  288. char work_string[256]; // temp string
  289. int index; // looping var
  290. // start the timing clock
  291. Start_Clock();
  292. // clear the drawing surface 
  293. DDraw_Fill_Surface(lpddsback, 0);
  294. // read keyboard and other devices here
  295. DInput_Read_Keyboard();
  296. // game logic here...
  297. // reset the render list
  298. Reset_RENDERLIST4DV1(&rend_list);
  299. // reset angles
  300. x_ang = 0;
  301. y_ang = 1;
  302. z_ang = 0;
  303. // generate rotation matrix around y axis
  304. Build_XYZ_Rotation_MATRIX4X4(x_ang, y_ang, z_ang, &mrot);
  305. // rotate the local coords of the object
  306. Transform_OBJECT4DV1(&obj, &mrot, TRANSFORM_LOCAL_ONLY,1);
  307. // compute new camera position, rides on a circular path 
  308. // with a sinudoid modulation
  309. cam.pos.x = camera_distance*Fast_Cos(view_angle);
  310. cam.pos.y = camera_distance*Fast_Sin(view_angle);
  311. cam.pos.z = 2*camera_distance*Fast_Sin(view_angle);
  312. // advance angle to circle around
  313. if ((view_angle+=1)>=360)
  314.     view_angle = 0;
  315. // generate camera matrix
  316. Build_CAM4DV1_Matrix_UVN(&cam, UVN_MODE_SIMPLE);
  317. // now cull the current object
  318. strcpy(buffer,"Objects Culled: ");
  319. for (int x=-NUM_OBJECTS/2; x < NUM_OBJECTS/2; x++)
  320.     for (int z=-NUM_OBJECTS/2; z < NUM_OBJECTS/2; z++)
  321.     {
  322.     // reset the object (this only matters for backface and object removal)
  323.     Reset_OBJECT4DV1(&obj);
  324.     // set position of object
  325.     obj.world_pos.x = x*OBJECT_SPACING+OBJECT_SPACING/2;
  326.     obj.world_pos.y = 0;
  327.     obj.world_pos.z = z*OBJECT_SPACING+OBJECT_SPACING/2;
  328.     // attempt to cull object   
  329.     if (!Cull_OBJECT4DV1(&obj, &cam, CULL_OBJECT_XYZ_PLANES))
  330.        {
  331.        // if we get here then the object is visible at this world position
  332.        // so we can insert it into the rendering list
  333.        // perform local/model to world transform
  334.        Model_To_World_OBJECT4DV1(&obj);
  335.        // insert the object into render list
  336.        Insert_OBJECT4DV1_RENDERLIST4DV1(&rend_list, &obj);
  337.        } // end if
  338.     else
  339.        {
  340.        sprintf(work_string, "[%d, %d] ", x,z);
  341.        strcat(buffer, work_string);
  342.        }
  343.  
  344.     } // end for
  345. Draw_Text_GDI(buffer, 0, WINDOW_HEIGHT-20, RGB(0,255,0), lpddsback);
  346. // Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  347. // remove backfaces
  348. Remove_Backfaces_RENDERLIST4DV1(&rend_list, &cam);
  349. // apply world to camera transform
  350. World_To_Camera_RENDERLIST4DV1(&rend_list, &cam);
  351. // apply camera to perspective transformation
  352. Camera_To_Perspective_RENDERLIST4DV1(&rend_list, &cam);
  353. // apply screen transform
  354. Perspective_To_Screen_RENDERLIST4DV1(&rend_list, &cam);
  355. // draw instructions
  356. Draw_Text_GDI("Press ESC to exit.", 0, 0, RGB(0,255,0), lpddsback);
  357. // lock the back buffer
  358. DDraw_Lock_Back_Surface();
  359. // render the object
  360. Draw_RENDERLIST4DV1_Wire16(&rend_list, back_buffer, back_lpitch);
  361. // unlock the back buffer
  362. DDraw_Unlock_Back_Surface();
  363. // flip the surfaces
  364. DDraw_Flip();
  365. // sync to 30ish fps
  366. Wait_Clock(30);
  367. // check of user is trying to exit
  368. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  369.     {
  370.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  371.     } // end if
  372. // return success
  373. return(1);
  374.  
  375. } // end Game_Main
  376. //////////////////////////////////////////////////////////