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

游戏

开发平台:

Visual C++

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