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

游戏

开发平台:

Visual C++

  1. // DEMOII8_6.CPP - flat shading demo with z-sorting
  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-6.CPP and the headers T3DLIB1-6.H
  6. // be in the working directory of the compiler
  7. // INCLUDES ///////////////////////////////////////////////
  8. #define DEBUG_ON
  9. #define INITGUID       // make sure al the COM interfaces are available
  10.                        // instead of this you can include the .LIB file
  11.                        // DXGUID.LIB
  12. #define WIN32_LEAN_AND_MEAN  
  13. #include <windows.h>   // include important windows stuff
  14. #include <windowsx.h> 
  15. #include <mmsystem.h>
  16. #include <iostream.h> // include important C/C++ stuff
  17. #include <conio.h>
  18. #include <stdlib.h> 
  19. #include <malloc.h>
  20. #include <memory.h>
  21. #include <string.h>
  22. #include <stdarg.h>
  23. #include <stdio.h> 
  24. #include <math.h>
  25. #include <io.h>
  26. #include <fcntl.h>
  27. #include <ddraw.h>  // directX includes
  28. #include <dsound.h>
  29. #include <dmksctrl.h>
  30. #include <dmusici.h>
  31. #include <dmusicc.h>
  32. #include <dmusicf.h>
  33. #include <dinput.h>
  34. #include "T3DLIB1.h" // game library includes
  35. #include "T3DLIB2.h"
  36. #include "T3DLIB3.h"
  37. #include "T3DLIB4.h"
  38. #include "T3DLIB5.h"
  39. #include "T3DLIB6.h"
  40. // DEFINES ////////////////////////////////////////////////
  41. // defines for windows interface
  42. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  43. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  44. #define WINDOW_WIDTH      1024 // size of window
  45. #define WINDOW_HEIGHT     768
  46. #define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
  47.                                 // note: if windowed and not
  48.                                 // fullscreen then bitdepth must
  49.                                 // be same as system bitdepth
  50.                                 // also if 8-bit the a pallete
  51.                                 // is created and attached
  52. #define WINDOWED_APP      0     // 0 not windowed, 1 windowed
  53. // defines for the game universe 
  54. #define UNIVERSE_RADIUS   4000
  55. #define POINT_SIZE        100
  56. #define NUM_POINTS_X      (2*UNIVERSE_RADIUS/POINT_SIZE)
  57. #define NUM_POINTS_Z      (2*UNIVERSE_RADIUS/POINT_SIZE)
  58. #define NUM_POINTS        (NUM_POINTS_X*NUM_POINTS_Z)
  59. // defines for objects
  60. #define NUM_TOWERS        64 // 96
  61. #define NUM_TANKS         32 // 24
  62. #define TANK_SPEED        15
  63. // create some constants for ease of access
  64. #define AMBIENT_LIGHT_INDEX   0 // ambient light index
  65. #define INFINITE_LIGHT_INDEX  1 // infinite light index
  66. #define POINT_LIGHT_INDEX     2 // point light index
  67. #define SPOT_LIGHT_INDEX      3 // spot light index
  68. // PROTOTYPES /////////////////////////////////////////////
  69. // game console
  70. int Game_Init(void *parms=NULL);
  71. int Game_Shutdown(void *parms=NULL);
  72. int Game_Main(void *parms=NULL);
  73. // GLOBALS ////////////////////////////////////////////////
  74. HWND main_window_handle           = NULL; // save the window handle
  75. HINSTANCE main_instance           = NULL; // save the instance
  76. char buffer[2048];                        // used to print text
  77. // initialize camera position and direction
  78. POINT4D  cam_pos    = {0,40,0,1};
  79. POINT4D  cam_target = {0,0,0,1};
  80. VECTOR4D cam_dir    = {0,0,0,1};
  81. // all your initialization code goes here...
  82. VECTOR4D vscale={1.0,1.0,1.0,1}, 
  83.          vpos = {0,0,0,1}, 
  84.          vrot = {0,0,0,1};
  85. CAM4DV1        cam;       // the single camera
  86. OBJECT4DV1     obj_tower,    // used to hold the master tower
  87.                obj_tank,     // used to hold the master tank
  88.                obj_marker,   // the ground marker
  89.                obj_player;   // the player object             
  90. POINT4D        towers[NUM_TOWERS],
  91.                tanks[NUM_TANKS];
  92.                
  93. RENDERLIST4DV1 rend_list; // the render list
  94. // FUNCTIONS //////////////////////////////////////////////
  95. LRESULT CALLBACK WindowProc(HWND hwnd, 
  96.     UINT msg, 
  97.                             WPARAM wparam, 
  98.                             LPARAM lparam)
  99. {
  100. // this is the main message handler of the system
  101. PAINTSTRUCT ps;    // used in WM_PAINT
  102. HDC hdc;    // handle to a device context
  103. // what is the message 
  104. switch(msg)
  105. {
  106. case WM_CREATE: 
  107.         {
  108. // do initialization stuff here
  109. return(0);
  110. } break;
  111.     case WM_PAINT:
  112.          {
  113.          // start painting
  114.          hdc = BeginPaint(hwnd,&ps);
  115.          // end painting
  116.          EndPaint(hwnd,&ps);
  117.          return(0);
  118.         } break;
  119. case WM_DESTROY: 
  120. {
  121. // kill the application
  122. PostQuitMessage(0);
  123. return(0);
  124. } break;
  125. default:break;
  126.     } // end switch
  127. // process any messages that we didn't take care of 
  128. return (DefWindowProc(hwnd, msg, wparam, lparam));
  129. } // end WinProc
  130. // WINMAIN ////////////////////////////////////////////////
  131. int WINAPI WinMain( HINSTANCE hinstance,
  132. HINSTANCE hprevinstance,
  133. LPSTR lpcmdline,
  134. int ncmdshow)
  135. {
  136. // this is the winmain function
  137. WNDCLASS winclass; // this will hold the class we create
  138. HWND  hwnd; // generic window handle
  139. MSG  msg; // generic message
  140. HDC      hdc;       // generic dc
  141. PAINTSTRUCT ps;     // generic paintstruct
  142. // first fill in the window class stucture
  143. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  144.                           CS_HREDRAW | CS_VREDRAW;
  145. winclass.lpfnWndProc = WindowProc;
  146. winclass.cbClsExtra = 0;
  147. winclass.cbWndExtra = 0;
  148. winclass.hInstance = hinstance;
  149. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  150. winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  151. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  152. winclass.lpszMenuName = NULL; 
  153. winclass.lpszClassName = WINDOW_CLASS_NAME;
  154. // register the window class
  155. if (!RegisterClass(&winclass))
  156. return(0);
  157. // create the window, note the test to see if WINDOWED_APP is
  158. // true to select the appropriate window flags
  159. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  160.   WINDOW_TITLE,  // title
  161.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  162.     0,0,    // x,y
  163.   WINDOW_WIDTH,  // width
  164.                           WINDOW_HEIGHT, // height
  165.   NULL,    // handle to parent 
  166.   NULL,    // handle to menu
  167.   hinstance,// instance
  168.   NULL))) // creation parms
  169. return(0);
  170. // save the window handle and instance in a global
  171. main_window_handle = hwnd;
  172. main_instance      = hinstance;
  173. // resize the window so that client is really width x height
  174. if (WINDOWED_APP)
  175. {
  176. // now resize the window, so the client area is the actual size requested
  177. // since there may be borders and controls if this is going to be a windowed app
  178. // if the app is not windowed then it won't matter
  179. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  180. // make the call to adjust window_rect
  181. AdjustWindowRectEx(&window_rect,
  182.      GetWindowStyle(main_window_handle),
  183.      GetMenu(main_window_handle) != NULL,  
  184.      GetWindowExStyle(main_window_handle));
  185. // save the global client offsets, they are needed in DDraw_Flip()
  186. window_client_x0 = -window_rect.left;
  187. window_client_y0 = -window_rect.top;
  188. // now resize the window with a call to MoveWindow()
  189. MoveWindow(main_window_handle,
  190.            0, // x position
  191.            0, // y position
  192.            window_rect.right - window_rect.left, // width
  193.            window_rect.bottom - window_rect.top, // height
  194.            FALSE);
  195. // show the window, so there's no garbage on first render
  196. ShowWindow(main_window_handle, SW_SHOW);
  197. } // end if windowed
  198. // perform all game console specific initialization
  199. Game_Init();
  200. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  201. // if it causes your system to crash
  202. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  203. // enter main event loop
  204. while(1)
  205. {
  206. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  207. // test if this is a quit
  208.         if (msg.message == WM_QUIT)
  209.            break;
  210. // translate any accelerator keys
  211. TranslateMessage(&msg);
  212. // send the message to the window proc
  213. DispatchMessage(&msg);
  214. } // end if
  215.     
  216.     // main game processing goes here
  217.     Game_Main();
  218. } // end while
  219. // shutdown game and release all resources
  220. Game_Shutdown();
  221. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  222. // if it causes your system to crash
  223. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  224. // return to Windows like this
  225. return(msg.wParam);
  226. } // end WinMain
  227. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  228. int Game_Init(void *parms)
  229. {
  230. // this function is where you do all the initialization 
  231. // for your game
  232. int index; // looping var
  233. // start up DirectDraw (replace the parms as you desire)
  234. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  235. // initialize directinput
  236. DInput_Init();
  237. // acquire the keyboard 
  238. DInput_Init_Keyboard();
  239. // add calls to acquire other directinput devices here...
  240. // initialize directsound and directmusic
  241. DSound_Init();
  242. DMusic_Init();
  243. // hide the mouse
  244. if (!WINDOWED_APP)
  245.     ShowCursor(FALSE);
  246. // seed random number generator
  247. srand(Start_Clock());
  248. Open_Error_File("ERROR.TXT");
  249. // initialize math engine
  250. Build_Sin_Cos_Tables();
  251. // initialize the camera with 90 FOV, normalized coordinates
  252. Init_CAM4DV1(&cam,      // the camera object
  253.              CAM_MODEL_EULER, // the euler model
  254.              &cam_pos,  // initial camera position
  255.              &cam_dir,  // initial camera angles
  256.              &cam_target,      // no target
  257.              200.0,      // near and far clipping planes
  258.              12000.0,
  259.              120.0,      // field of view in degrees
  260.              WINDOW_WIDTH,   // size of final screen viewport
  261.              WINDOW_HEIGHT);
  262. // load the master tank object
  263. VECTOR4D_INITXYZ(&vscale,0.75,0.75,0.75);
  264. Load_OBJECT4DV1_PLG(&obj_tank, "tank3.plg",&vscale, &vpos, &vrot);
  265. //Load_OBJECT4DV1_3DSASC(&obj_tank,"sphere01.asc",  
  266. //                       &vscale, &vpos, &vrot, 
  267. //                       VERTEX_FLAGS_INVERT_WINDING_ORDER | VERTEX_FLAGS_SWAP_YZ );
  268. // load player object for 3rd person view
  269. VECTOR4D_INITXYZ(&vscale,0.75,0.75,0.75);
  270. Load_OBJECT4DV1_PLG(&obj_player, "tank2.plg",&vscale, &vpos, &vrot);
  271. //Load_OBJECT4DV1_3DSASC(&obj_player,"sphere01.asc",  
  272. //                       &vscale, &vpos, &vrot, 
  273. //                       VERTEX_FLAGS_INVERT_WINDING_ORDER | VERTEX_FLAGS_SWAP_YZ );
  274. // load the master tower object
  275. VECTOR4D_INITXYZ(&vscale,1.0, 2.0, 1.0);
  276. Load_OBJECT4DV1_PLG(&obj_tower, "tower1.plg",&vscale, &vpos, &vrot);
  277. // load the master ground marker
  278. VECTOR4D_INITXYZ(&vscale,3.0,3.0,3.0);
  279. Load_OBJECT4DV1_PLG(&obj_marker, "marker1.plg",&vscale, &vpos, &vrot);
  280. // position the tanks
  281. for (index = 0; index < NUM_TANKS; index++)
  282.     {
  283.     // randomly position the tanks
  284.     tanks[index].x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS);
  285.     tanks[index].y = 0; // obj_tank.max_radius;
  286.     tanks[index].z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS);
  287.     tanks[index].w = RAND_RANGE(0,360);
  288.     } // end for
  289. // position the towers
  290. for (index = 0; index < NUM_TOWERS; index++)
  291.     {
  292.     // randomly position the tower
  293.     towers[index].x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS);
  294.     towers[index].y = 0; // obj_tower.max_radius;
  295.     towers[index].z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS);
  296.     } // end for
  297. // set up lights
  298. Reset_Lights_LIGHTV1();
  299. // create some working colors
  300. RGBAV1 white, gray, black, red, green, blue;
  301. white.rgba = _RGBA32BIT(255,255,255,0);
  302. gray.rgba  = _RGBA32BIT(100,100,100,0);
  303. black.rgba = _RGBA32BIT(0,0,0,0);
  304. red.rgba   = _RGBA32BIT(255,0,0,0);
  305. green.rgba = _RGBA32BIT(0,255,0,0);
  306. blue.rgba  = _RGBA32BIT(0,0,255,0);
  307. // ambient light
  308. Init_Light_LIGHTV1(AMBIENT_LIGHT_INDEX,   
  309.                    LIGHTV1_STATE_ON,      // turn the light on
  310.                    LIGHTV1_ATTR_AMBIENT,  // ambient light type
  311.                    gray, black, black,    // color for ambient term only
  312.                    NULL, NULL,            // no need for pos or dir
  313.                    0,0,0,                 // no need for attenuation
  314.                    0,0,0);                // spotlight info NA
  315. VECTOR4D dlight_dir = {-1,0,-1,0};
  316. // directional light
  317. Init_Light_LIGHTV1(INFINITE_LIGHT_INDEX,  
  318.                    LIGHTV1_STATE_ON,      // turn the light on
  319.                    LIGHTV1_ATTR_INFINITE, // infinite light type
  320.                    black, gray, black,    // color for diffuse term only
  321.                    NULL, &dlight_dir,     // need direction only
  322.                    0,0,0,                 // no need for attenuation
  323.                    0,0,0);                // spotlight info NA
  324. VECTOR4D plight_pos = {0,200,0,0};
  325. // point light
  326. Init_Light_LIGHTV1(POINT_LIGHT_INDEX,
  327.                    LIGHTV1_STATE_ON,      // turn the light on
  328.                    LIGHTV1_ATTR_POINT,    // pointlight type
  329.                    black, green, black,   // color for diffuse term only
  330.                    &plight_pos, NULL,     // need pos only
  331.                    0,.001,0,              // linear attenuation only
  332.                    0,0,1);                // spotlight info NA
  333. VECTOR4D slight_pos = {0,200,0,0};
  334. VECTOR4D slight_dir = {-1,0,-1,0};
  335. // spot light
  336. Init_Light_LIGHTV1(SPOT_LIGHT_INDEX,
  337.                    LIGHTV1_STATE_ON,         // turn the light on
  338.                    LIGHTV1_ATTR_SPOTLIGHT2,  // spot light type 2
  339.                    black, red, black,      // color for diffuse term only
  340.                    &slight_pos, &slight_dir, // need pos only
  341.                    0,.001,0,                 // linear attenuation only
  342.                    0,0,1);    
  343. // create lookup for lighting engine
  344. RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565,  // format we want to build table for
  345.                                   palette,             // source palette
  346.                                   rgblookup);          // lookup table
  347. // return success
  348. return(1);
  349. } // end Game_Init
  350. ///////////////////////////////////////////////////////////
  351. int Game_Shutdown(void *parms)
  352. {
  353. // this function is where you shutdown your game and
  354. // release all resources that you allocated
  355. // shut everything down
  356. // release all your resources created for the game here....
  357. // now directsound
  358. DSound_Stop_All_Sounds();
  359. DSound_Delete_All_Sounds();
  360. DSound_Shutdown();
  361. // directmusic
  362. DMusic_Delete_All_MIDI();
  363. DMusic_Shutdown();
  364. // shut down directinput
  365. DInput_Release_Keyboard();
  366. // shutdown directinput
  367. DInput_Shutdown();
  368. // shutdown directdraw last
  369. DDraw_Shutdown();
  370. Close_Error_File();
  371. // return success
  372. return(1);
  373. } // end Game_Shutdown
  374. //////////////////////////////////////////////////////////
  375. int Game_Main(void *parms)
  376. {
  377. // this is the workhorse of your game it will be called
  378. // continuously in real-time this is like main() in C
  379. // all the calls for you game go here!
  380. static MATRIX4X4 mrot;   // general rotation matrix
  381. // these are used to create a circling camera
  382. static float view_angle = 0; 
  383. static float camera_distance = 6000;
  384. static VECTOR4D pos = {0,0,0,0};
  385. static float tank_speed;
  386. static float turning = 0;
  387. // state variables for different rendering modes and help
  388. static int wireframe_mode = -1;
  389. static int backface_mode  = 1;
  390. static int lighting_mode  = 1;
  391. static int help_mode      = 1;
  392. static int zsort_mode     = 1;
  393. char work_string[256]; // temp string
  394. int index; // looping var
  395. // start the timing clock
  396. Start_Clock();
  397. // clear the drawing surface 
  398. DDraw_Fill_Surface(lpddsback, 0);
  399. // draw the sky
  400. //Draw_Rectangle(0,0, WINDOW_WIDTH-1, WINDOW_HEIGHT/2, 166, lpddsback);
  401. //Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, rgblookup[RGB16Bit565(115,42,16)], lpddsback);
  402. //Draw_Rectangle(0,0, WINDOW_WIDTH-1, WINDOW_HEIGHT/2, rgblookup[RGB16Bit565(0,140,192)], lpddsback);
  403. Draw_Rectangle(0,0, WINDOW_WIDTH-1, WINDOW_HEIGHT/2, RGB16Bit(0,140,192), lpddsback);
  404. // draw the ground
  405. //Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, 28, lpddsback);
  406. //Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, rgblookup[RGB16Bit565(115,42,16)], lpddsback);
  407. Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, RGB16Bit(103,62,3), lpddsback);
  408. // read keyboard and other devices here
  409. DInput_Read_Keyboard();
  410. // game logic here...
  411. // reset the render list
  412. Reset_RENDERLIST4DV1(&rend_list);
  413. // allow user to move camera
  414. // turbo
  415. if (keyboard_state[DIK_SPACE])
  416.     tank_speed = 5*TANK_SPEED;
  417. else
  418.     tank_speed = TANK_SPEED;
  419. // forward/backward
  420. if (keyboard_state[DIK_UP])
  421.    {
  422.    // move forward
  423.    cam.pos.x += tank_speed*Fast_Sin(cam.dir.y);
  424.    cam.pos.z += tank_speed*Fast_Cos(cam.dir.y);
  425.    } // end if
  426. if (keyboard_state[DIK_DOWN])
  427.    {
  428.    // move backward
  429.    cam.pos.x -= tank_speed*Fast_Sin(cam.dir.y);
  430.    cam.pos.z -= tank_speed*Fast_Cos(cam.dir.y);
  431.    } // end if
  432. // rotate
  433. if (keyboard_state[DIK_RIGHT])
  434.    {
  435.    cam.dir.y+=3;
  436.    
  437.    // add a little turn to object
  438.    if ((turning+=2) > 15)
  439.       turning=15;
  440.    } // end if
  441. if (keyboard_state[DIK_LEFT])
  442.    {
  443.    cam.dir.y-=3;
  444.    // add a little turn to object
  445.    if ((turning-=2) < -15)
  446.       turning=-15;
  447.    } // end if
  448. else // center heading again
  449.    {
  450.    if (turning > 0)
  451.        turning-=1;
  452.    else
  453.    if (turning < 0)
  454.        turning+=1;
  455.    } // end else
  456. // modes and lights
  457. // wireframe mode
  458. if (keyboard_state[DIK_W])
  459.    {
  460.    // toggle wireframe mode
  461.    wireframe_mode = -wireframe_mode;
  462.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  463.    } // end if
  464. // backface removal
  465. if (keyboard_state[DIK_B])
  466.    {
  467.    // toggle backface removal
  468.    backface_mode = -backface_mode;
  469.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  470.    } // end if
  471. // lighting
  472. if (keyboard_state[DIK_L])
  473.    {
  474.    // toggle lighting engine completely
  475.    lighting_mode = -lighting_mode;
  476.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  477.    } // end if
  478. // toggle ambient light
  479. if (keyboard_state[DIK_A])
  480.    {
  481.    // toggle ambient light
  482.    if (lights[AMBIENT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  483.       lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  484.    else
  485.       lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  486.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  487.    } // end if
  488. // toggle infinite light
  489. if (keyboard_state[DIK_I])
  490.    {
  491.    // toggle ambient light
  492.    if (lights[INFINITE_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  493.       lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  494.    else
  495.       lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  496.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  497.    } // end if
  498. // toggle point light
  499. if (keyboard_state[DIK_P])
  500.    {
  501.    // toggle point light
  502.    if (lights[POINT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  503.       lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  504.    else
  505.       lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  506.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  507.    } // end if
  508. // toggle spot light
  509. if (keyboard_state[DIK_S])
  510.    {
  511.    // toggle spot light
  512.    if (lights[SPOT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  513.       lights[SPOT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  514.    else
  515.       lights[SPOT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  516.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  517.    } // end if
  518. // help menu
  519. if (keyboard_state[DIK_H])
  520.    {
  521.    // toggle help menu 
  522.    help_mode = -help_mode;
  523.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  524.    } // end if
  525. // z-sorting
  526. if (keyboard_state[DIK_Z])
  527.    {
  528.    // toggle z sorting
  529.    zsort_mode = -zsort_mode;
  530.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  531.    } // end if
  532. static float plight_ang = 0, slight_ang = 0; // angles for light motion
  533. // move point light source in ellipse around game world
  534. lights[POINT_LIGHT_INDEX].pos.x = 4000*Fast_Cos(plight_ang);
  535. lights[POINT_LIGHT_INDEX].pos.y = 200;
  536. lights[POINT_LIGHT_INDEX].pos.z = 4000*Fast_Sin(plight_ang);
  537. if ((plight_ang+=3) > 360)
  538.     plight_ang = 0;
  539. // move spot light source in ellipse around game world
  540. lights[SPOT_LIGHT_INDEX].pos.x = 2000*Fast_Cos(slight_ang);
  541. lights[SPOT_LIGHT_INDEX].pos.y = 200;
  542. lights[SPOT_LIGHT_INDEX].pos.z = 2000*Fast_Sin(slight_ang);
  543. if ((slight_ang-=5) < 0)
  544.     slight_ang = 360;
  545. // generate camera matrix
  546. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  547. // insert the player into the world
  548. // reset the object (this only matters for backface and object removal)
  549. Reset_OBJECT4DV1(&obj_player);
  550. // set position of tank
  551. obj_player.world_pos.x = cam.pos.x+300*Fast_Sin(cam.dir.y);
  552. obj_player.world_pos.y = cam.pos.y-70;
  553. obj_player.world_pos.z = cam.pos.z+300*Fast_Cos(cam.dir.y);
  554. // generate rotation matrix around y axis
  555. Build_XYZ_Rotation_MATRIX4X4(0, cam.dir.y+turning, 0, &mrot);
  556. // rotate the local coords of the object
  557. Transform_OBJECT4DV1(&obj_player, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  558. // perform world transform
  559. Model_To_World_OBJECT4DV1(&obj_player, TRANSFORM_TRANS_ONLY);
  560. // insert the object into render list
  561. Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_player,0, 0);
  562. //////////////////////////////////////////////////////////
  563. // insert the tanks in the world
  564. for (index = 0; index < NUM_TANKS; index++)
  565.     {
  566.     // reset the object (this only matters for backface and object removal)
  567.     Reset_OBJECT4DV1(&obj_tank);
  568.     // generate rotation matrix around y axis
  569.     Build_XYZ_Rotation_MATRIX4X4(0, tanks[index].w, 0, &mrot);
  570.     // rotate the local coords of the object
  571.     Transform_OBJECT4DV1(&obj_tank, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  572.     // set position of tank
  573.     obj_tank.world_pos.x = tanks[index].x;
  574.     obj_tank.world_pos.y = tanks[index].y;
  575.     obj_tank.world_pos.z = tanks[index].z;
  576.     // attempt to cull object   
  577.     if (!Cull_OBJECT4DV1(&obj_tank, &cam, CULL_OBJECT_XYZ_PLANES))
  578.        {
  579.        // if we get here then the object is visible at this world position
  580.        // so we can insert it into the rendering list
  581.        // perform local/model to world transform
  582.        Model_To_World_OBJECT4DV1(&obj_tank, TRANSFORM_TRANS_ONLY);
  583.        // insert the object into render list
  584.        Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_tank,0, 0);
  585.        } // end if
  586.  
  587.     } // end for
  588. ////////////////////////////////////////////////////////
  589. // insert the towers in the world
  590. for (index = 0; index < NUM_TOWERS; index++)
  591.     {
  592.     // reset the object (this only matters for backface and object removal)
  593.     Reset_OBJECT4DV1(&obj_tower);
  594.     // set position of tower
  595.     obj_tower.world_pos.x = towers[index].x;
  596.     obj_tower.world_pos.y = towers[index].y;
  597.     obj_tower.world_pos.z = towers[index].z;
  598.     // attempt to cull object   
  599.     if (!Cull_OBJECT4DV1(&obj_tower, &cam, CULL_OBJECT_XYZ_PLANES))
  600.        {
  601.        // if we get here then the object is visible at this world position
  602.        // so we can insert it into the rendering list
  603.        // perform local/model to world transform
  604.        Model_To_World_OBJECT4DV1(&obj_tower);
  605.        // insert the object into render list
  606.        Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_tower,0, 0);
  607.        } // end if
  608.  
  609.     } // end for
  610. ///////////////////////////////////////////////////////////////
  611. // seed number generator so that modulation of markers is always the same
  612. srand(13);
  613. // insert the ground markers into the world
  614. for (int index_x = 0; index_x < NUM_POINTS_X; index_x++)
  615.     for (int index_z = 0; index_z < NUM_POINTS_Z; index_z++)
  616.         {
  617.         // reset the object (this only matters for backface and object removal)
  618.         Reset_OBJECT4DV1(&obj_marker);
  619.         // set position of tower
  620.         obj_marker.world_pos.x = RAND_RANGE(-100,100)-UNIVERSE_RADIUS+index_x*POINT_SIZE;
  621.         obj_marker.world_pos.y = obj_marker.max_radius;
  622.         obj_marker.world_pos.z = RAND_RANGE(-100,100)-UNIVERSE_RADIUS+index_z*POINT_SIZE;
  623.         // attempt to cull object   
  624.         if (!Cull_OBJECT4DV1(&obj_marker, &cam, CULL_OBJECT_XYZ_PLANES))
  625.            {
  626.            // if we get here then the object is visible at this world position
  627.            // so we can insert it into the rendering list
  628.            // perform local/model to world transform
  629.            Model_To_World_OBJECT4DV1(&obj_marker);
  630.            // insert the object into render list
  631.            Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_marker,0, 0);
  632.            } // end if
  633.         } // end for
  634. ////////////////////////////////////////////////////////////////////////
  635. // remove backfaces
  636. if (backface_mode==1)
  637.    Remove_Backfaces_RENDERLIST4DV1(&rend_list, &cam);
  638. // light scene all at once 
  639. if (lighting_mode==1)
  640.    Light_RENDERLIST4DV1_World16(&rend_list, &cam, lights, 4);
  641. // apply world to camera transform
  642. World_To_Camera_RENDERLIST4DV1(&rend_list, &cam);
  643. // sort the polygon list (hurry up!)
  644. if (zsort_mode == 1)
  645.    Sort_RENDERLIST4DV1(&rend_list,  SORT_POLYLIST_AVGZ);
  646. // apply camera to perspective transformation
  647. Camera_To_Perspective_RENDERLIST4DV1(&rend_list, &cam);
  648. // apply screen transform
  649. Perspective_To_Screen_RENDERLIST4DV1(&rend_list, &cam);
  650. sprintf(work_string,"pos:[%f, %f, %f] heading:[%f] elev:[%f], polys[%d]", 
  651.         cam.pos.x, cam.pos.y, cam.pos.z, cam.dir.y, cam.dir.x, debug_polys_rendered_per_frame); 
  652. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-20, RGB(0,255,0), lpddsback);
  653. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, Spot=%d | Zsort [%s], BckFceRM [%s]", 
  654.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  655.                                                                                  lights[AMBIENT_LIGHT_INDEX].state,
  656.                                                                                  lights[INFINITE_LIGHT_INDEX].state, 
  657.                                                                                  lights[POINT_LIGHT_INDEX].state,
  658.                                                                                  lights[SPOT_LIGHT_INDEX].state,
  659.                                                                                  ((zsort_mode == 1) ? "ON" : "OFF"),
  660.                                                                                  ((backface_mode == 1) ? "ON" : "OFF"));
  661. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34, RGB(0,255,0), lpddsback);
  662. // draw instructions
  663. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  664. // should we display help
  665. int text_y = 16;
  666. if (help_mode==1)
  667.     {
  668.     // draw help menu
  669.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  670.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  671.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  672.     Draw_Text_GDI("<S>..............Toggle spot light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  673.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  674.     Draw_Text_GDI("<B>..............Toggle backface removal.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  675.     Draw_Text_GDI("<RIGHT ARROW>....Rotate player right.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  676.     Draw_Text_GDI("<LEFT ARROW>.....Rotate player left.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  677.     Draw_Text_GDI("<UP ARROW>.......Move player forward.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  678.     Draw_Text_GDI("<DOWN ARROW>.....Move player backward.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  679.     Draw_Text_GDI("<SPACE BAR>......Turbo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  680.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  681.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  682.     } // end help
  683. // lock the back buffer
  684. DDraw_Lock_Back_Surface();
  685. // reset number of polys rendered
  686. debug_polys_rendered_per_frame = 0;
  687. // render the object
  688. if (wireframe_mode  == 1)
  689.    Draw_RENDERLIST4DV1_Wire16(&rend_list, back_buffer, back_lpitch);
  690. else
  691.    Draw_RENDERLIST4DV1_Solid16(&rend_list, back_buffer, back_lpitch);
  692. // unlock the back buffer
  693. DDraw_Unlock_Back_Surface();
  694. // flip the surfaces
  695. DDraw_Flip();
  696. // sync to 30ish fps
  697. Wait_Clock(30);
  698. // check of user is trying to exit
  699. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  700.     {
  701.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  702.     } // end if
  703. // return success
  704. return(1);
  705.  
  706. } // end Game_Main
  707. //////////////////////////////////////////////////////////