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

游戏

开发平台:

Visual C++

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