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

游戏

开发平台:

Visual C++

  1. // DEMOII7_3.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      640   // size of window
  43. #define WINDOW_HEIGHT     480
  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      1     // 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. // GLOBALS ////////////////////////////////////////////////
  57. HWND main_window_handle           = NULL; // save the window handle
  58. HINSTANCE main_instance           = NULL; // save the instance
  59. char buffer[256];                         // used to print text
  60. // initialize camera position and direction
  61. POINT4D  cam_pos = {0,0,0,1};
  62. VECTOR4D cam_dir = {0,0,0,1};
  63. // all your initialization code goes here...
  64. VECTOR4D vscale={5.0,5.0,5.0,1}, vpos = {0,0,0,1}, vrot = {0,0,0,1};
  65. CAM4DV1    cam;     // the single camera
  66. OBJECT4DV1 obj;     // used to hold our cube mesh                   
  67. // FUNCTIONS //////////////////////////////////////////////
  68. LRESULT CALLBACK WindowProc(HWND hwnd, 
  69.     UINT msg, 
  70.                             WPARAM wparam, 
  71.                             LPARAM lparam)
  72. {
  73. // this is the main message handler of the system
  74. PAINTSTRUCT ps;    // used in WM_PAINT
  75. HDC hdc;    // handle to a device context
  76. // what is the message 
  77. switch(msg)
  78. {
  79. case WM_CREATE: 
  80.         {
  81. // do initialization stuff here
  82. return(0);
  83. } break;
  84.     case WM_PAINT:
  85.          {
  86.          // start painting
  87.          hdc = BeginPaint(hwnd,&ps);
  88.          // end painting
  89.          EndPaint(hwnd,&ps);
  90.          return(0);
  91.         } break;
  92. case WM_DESTROY: 
  93. {
  94. // kill the application
  95. PostQuitMessage(0);
  96. return(0);
  97. } break;
  98. default:break;
  99.     } // end switch
  100. // process any messages that we didn't take care of 
  101. return (DefWindowProc(hwnd, msg, wparam, lparam));
  102. } // end WinProc
  103. // WINMAIN ////////////////////////////////////////////////
  104. int WINAPI WinMain( HINSTANCE hinstance,
  105. HINSTANCE hprevinstance,
  106. LPSTR lpcmdline,
  107. int ncmdshow)
  108. {
  109. // this is the winmain function
  110. WNDCLASS winclass; // this will hold the class we create
  111. HWND  hwnd; // generic window handle
  112. MSG  msg; // generic message
  113. HDC      hdc;       // generic dc
  114. PAINTSTRUCT ps;     // generic paintstruct
  115. // first fill in the window class stucture
  116. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  117.                           CS_HREDRAW | CS_VREDRAW;
  118. winclass.lpfnWndProc = WindowProc;
  119. winclass.cbClsExtra = 0;
  120. winclass.cbWndExtra = 0;
  121. winclass.hInstance = hinstance;
  122. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  123. winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  124. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  125. winclass.lpszMenuName = NULL; 
  126. winclass.lpszClassName = WINDOW_CLASS_NAME;
  127. // register the window class
  128. if (!RegisterClass(&winclass))
  129. return(0);
  130. // create the window, note the test to see if WINDOWED_APP is
  131. // true to select the appropriate window flags
  132. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  133.   WINDOW_TITLE,  // title
  134.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  135.     0,0,    // x,y
  136.   WINDOW_WIDTH,  // width
  137.                           WINDOW_HEIGHT, // height
  138.   NULL,    // handle to parent 
  139.   NULL,    // handle to menu
  140.   hinstance,// instance
  141.   NULL))) // creation parms
  142. return(0);
  143. // save the window handle and instance in a global
  144. main_window_handle = hwnd;
  145. main_instance      = hinstance;
  146. // resize the window so that client is really width x height
  147. if (WINDOWED_APP)
  148. {
  149. // now resize the window, so the client area is the actual size requested
  150. // since there may be borders and controls if this is going to be a windowed app
  151. // if the app is not windowed then it won't matter
  152. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  153. // make the call to adjust window_rect
  154. AdjustWindowRectEx(&window_rect,
  155.      GetWindowStyle(main_window_handle),
  156.      GetMenu(main_window_handle) != NULL,
  157.      GetWindowExStyle(main_window_handle));
  158. // save the global client offsets, they are needed in DDraw_Flip()
  159. window_client_x0 = -window_rect.left;
  160. window_client_y0 = -window_rect.top;
  161. // now resize the window with a call to MoveWindow()
  162. MoveWindow(main_window_handle,
  163.            0, // x position
  164.            0, // y position
  165.            window_rect.right - window_rect.left, // width
  166.            window_rect.bottom - window_rect.top, // height
  167.            FALSE);
  168. // show the window, so there's no garbage on first render
  169. ShowWindow(main_window_handle, SW_SHOW);
  170. } // end if windowed
  171. // perform all game console specific initialization
  172. Game_Init();
  173. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  174. // if it causes your system to crash
  175. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  176. // enter main event loop
  177. while(1)
  178. {
  179. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  180. // test if this is a quit
  181.         if (msg.message == WM_QUIT)
  182.            break;
  183. // translate any accelerator keys
  184. TranslateMessage(&msg);
  185. // send the message to the window proc
  186. DispatchMessage(&msg);
  187. } // end if
  188.     
  189.     // main game processing goes here
  190.     Game_Main();
  191. } // end while
  192. // shutdown game and release all resources
  193. Game_Shutdown();
  194. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  195. // if it causes your system to crash
  196. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  197. // return to Windows like this
  198. return(msg.wParam);
  199. } // end WinMain
  200. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  201. int Game_Init(void *parms)
  202. {
  203. // this function is where you do all the initialization 
  204. // for your game
  205. // start up DirectDraw (replace the parms as you desire)
  206. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  207. // initialize directinput
  208. DInput_Init();
  209. // acquire the keyboard 
  210. DInput_Init_Keyboard();
  211. // add calls to acquire other directinput devices here...
  212. // initialize directsound and directmusic
  213. DSound_Init();
  214. DMusic_Init();
  215. // hide the mouse
  216. if (!WINDOWED_APP)
  217.    ShowCursor(FALSE);
  218. // seed random number generator
  219. srand(Start_Clock());
  220. Open_Error_File("ERROR.TXT");
  221. // initialize math engine
  222. Build_Sin_Cos_Tables();
  223. // initialize the camera with 90 FOV, normalized coordinates
  224. Init_CAM4DV1(&cam,      // the camera object
  225.              CAM_MODEL_EULER, // the euler model
  226.              &cam_pos,  // initial camera position
  227.              &cam_dir,  // initial camera angles
  228.              NULL,      // no target
  229.              50.0,      // near and far clipping planes
  230.              500.0,
  231.              90.0,      // field of view in degrees
  232.              WINDOW_WIDTH,   // size of final screen viewport
  233.              WINDOW_HEIGHT);
  234. // load the cube
  235. Load_OBJECT4DV1_PLG(&obj, "cube2.plg",&vscale, &vpos, &vrot);
  236. // set the position of the cube in the world
  237. obj.world_pos.x = 0;
  238. obj.world_pos.y = 0;
  239. obj.world_pos.z = 100;
  240. // return success
  241. return(1);
  242. } // end Game_Init
  243. ///////////////////////////////////////////////////////////
  244. int Game_Shutdown(void *parms)
  245. {
  246. // this function is where you shutdown your game and
  247. // release all resources that you allocated
  248. // shut everything down
  249. // release all your resources created for the game here....
  250. // now directsound
  251. DSound_Stop_All_Sounds();
  252. DSound_Delete_All_Sounds();
  253. DSound_Shutdown();
  254. // directmusic
  255. DMusic_Delete_All_MIDI();
  256. DMusic_Shutdown();
  257. // shut down directinput
  258. DInput_Release_Keyboard();
  259. DInput_Shutdown();
  260. // shutdown directdraw last
  261. DDraw_Shutdown();
  262. Close_Error_File();
  263. // return success
  264. return(1);
  265. } // end Game_Shutdown
  266. //////////////////////////////////////////////////////////
  267. int Game_Main(void *parms)
  268. {
  269. // this is the workhorse of your game it will be called
  270. // continuously in real-time this is like main() in C
  271. // all the calls for you game go here!
  272. static MATRIX4X4 mrot; // general rotation matrix
  273. static float x_ang = 0, y_ang = 2, z_ang = 0;
  274. int index; // looping var
  275. // start the timing clock
  276. Start_Clock();
  277. // clear the drawing surface 
  278. DDraw_Fill_Surface(lpddsback, 0);
  279. // read keyboard and other devices here
  280. DInput_Read_Keyboard();
  281. // game logic here...
  282. // reset the object (this only matters for backface and object removal)
  283. Reset_OBJECT4DV1(&obj);
  284. // reset angles
  285. x_ang = z_ang = 0;
  286. // is user trying to rotate
  287. if (KEY_DOWN(VK_DOWN))
  288.     x_ang = 1;
  289. else
  290. if (KEY_DOWN(VK_UP))
  291.     x_ang = -1; 
  292. // generate rotation matrix around y axis
  293. Build_XYZ_Rotation_MATRIX4X4(x_ang, y_ang, z_ang, &mrot);
  294. // rotate the local coords of single polygon in renderlist
  295. Transform_OBJECT4DV1(&obj, &mrot, TRANSFORM_LOCAL_ONLY,1);
  296. // perform local/model to world transform
  297. Model_To_World_OBJECT4DV1(&obj);
  298. // generate camera matrix
  299. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  300. // remove backfaces
  301. Remove_Backfaces_OBJECT4DV1(&obj, &cam);
  302. // apply world to camera transform
  303. World_To_Camera_OBJECT4DV1(&obj, &cam);
  304. // apply camera to perspective transformation
  305. Camera_To_Perspective_OBJECT4DV1(&obj, &cam);
  306. // apply screen transform
  307. Perspective_To_Screen_OBJECT4DV1(&obj, &cam);
  308. // draw instructions
  309. Draw_Text_GDI("Press ESC to exit. Use UP ARROW and DOWN ARROW to rotate.", 0, 0, RGB(0,255,0), lpddsback);
  310. // lock the back buffer
  311. DDraw_Lock_Back_Surface();
  312. // render the object
  313. Draw_OBJECT4DV1_Wire16(&obj, back_buffer, back_lpitch);
  314. // unlock the back buffer
  315. DDraw_Unlock_Back_Surface();
  316. // flip the surfaces
  317. DDraw_Flip();
  318. // sync to 30ish fps
  319. Wait_Clock(30);
  320. // check of user is trying to exit
  321. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  322.     {
  323.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  324.     } // end if
  325. // return success
  326. return(1);
  327.  
  328. } // end Game_Main
  329. //////////////////////////////////////////////////////////