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

游戏

开发平台:

Visual C++

  1. // DEMOII8_5.CPP - flat shading demo
  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      800 // size of window
  45. #define WINDOW_HEIGHT     600
  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      1     // 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);                   // spotlight powerfactor only
  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. char work_string[256]; // temp string
  393. int index; // looping var
  394. // start the timing clock
  395. Start_Clock();
  396. // clear the drawing surface 
  397. DDraw_Fill_Surface(lpddsback, 0);
  398. // draw the sky
  399. //Draw_Rectangle(0,0, WINDOW_WIDTH-1, WINDOW_HEIGHT/2, 166, lpddsback);
  400. //Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, rgblookup[RGB16Bit565(115,42,16)], lpddsback);
  401. //Draw_Rectangle(0,0, WINDOW_WIDTH-1, WINDOW_HEIGHT/2, rgblookup[RGB16Bit565(0,140,192)], lpddsback);
  402. Draw_Rectangle(0,0, WINDOW_WIDTH-1, WINDOW_HEIGHT/2, RGB16Bit(0,140,192), lpddsback);
  403. // draw the ground
  404. //Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, 28, lpddsback);
  405. //Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, rgblookup[RGB16Bit565(115,42,16)], lpddsback);
  406. Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, RGB16Bit(103,62,3), lpddsback);
  407. // read keyboard and other devices here
  408. DInput_Read_Keyboard();
  409. // game logic here...
  410. // reset the render list
  411. Reset_RENDERLIST4DV1(&rend_list);
  412. // allow user to move camera
  413. // turbo
  414. if (keyboard_state[DIK_SPACE])
  415.     tank_speed = 5*TANK_SPEED;
  416. else
  417.     tank_speed = TANK_SPEED;
  418. // forward/backward
  419. if (keyboard_state[DIK_UP])
  420.    {
  421.    // move forward
  422.    cam.pos.x += tank_speed*Fast_Sin(cam.dir.y);
  423.    cam.pos.z += tank_speed*Fast_Cos(cam.dir.y);
  424.    } // end if
  425. if (keyboard_state[DIK_DOWN])
  426.    {
  427.    // move backward
  428.    cam.pos.x -= tank_speed*Fast_Sin(cam.dir.y);
  429.    cam.pos.z -= tank_speed*Fast_Cos(cam.dir.y);
  430.    } // end if
  431. // rotate
  432. if (keyboard_state[DIK_RIGHT])
  433.    {
  434.    cam.dir.y+=3;
  435.    
  436.    // add a little turn to object
  437.    if ((turning+=2) > 15)
  438.       turning=15;
  439.    } // end if
  440. if (keyboard_state[DIK_LEFT])
  441.    {
  442.    cam.dir.y-=3;
  443.    // add a little turn to object
  444.    if ((turning-=2) < -15)
  445.       turning=-15;
  446.    } // end if
  447. else // center heading again
  448.    {
  449.    if (turning > 0)
  450.        turning-=1;
  451.    else
  452.    if (turning < 0)
  453.        turning+=1;
  454.    } // end else
  455. // modes and lights
  456. // wireframe mode
  457. if (keyboard_state[DIK_W])
  458.    {
  459.    // toggle wireframe mode
  460.    wireframe_mode = -wireframe_mode;
  461.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  462.    } // end if
  463. // backface removal
  464. if (keyboard_state[DIK_B])
  465.    {
  466.    // toggle backface removal
  467.    backface_mode = -backface_mode;
  468.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  469.    } // end if
  470. // lighting
  471. if (keyboard_state[DIK_L])
  472.    {
  473.    // toggle lighting engine completely
  474.    lighting_mode = -lighting_mode;
  475.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  476.    } // end if
  477. // toggle ambient light
  478. if (keyboard_state[DIK_A])
  479.    {
  480.    // toggle ambient light
  481.    if (lights[AMBIENT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  482.       lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  483.    else
  484.       lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  485.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  486.    } // end if
  487. // toggle infinite light
  488. if (keyboard_state[DIK_I])
  489.    {
  490.    // toggle ambient light
  491.    if (lights[INFINITE_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  492.       lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  493.    else
  494.       lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  495.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  496.    } // end if
  497. // toggle point light
  498. if (keyboard_state[DIK_P])
  499.    {
  500.    // toggle point light
  501.    if (lights[POINT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  502.       lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  503.    else
  504.       lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  505.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  506.    } // end if
  507. // toggle spot light
  508. if (keyboard_state[DIK_S])
  509.    {
  510.    // toggle spot light
  511.    if (lights[SPOT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  512.       lights[SPOT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  513.    else
  514.       lights[SPOT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  515.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  516.    } // end if
  517. // help menu
  518. if (keyboard_state[DIK_H])
  519.    {
  520.    // toggle help menu 
  521.    help_mode = -help_mode;
  522.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  523.    } // end if
  524. static float plight_ang = 0, slight_ang = 0; // angles for light motion
  525. // move point light source in ellipse around game world
  526. lights[POINT_LIGHT_INDEX].pos.x = 4000*Fast_Cos(plight_ang);
  527. lights[POINT_LIGHT_INDEX].pos.y = 200;
  528. lights[POINT_LIGHT_INDEX].pos.z = 4000*Fast_Sin(plight_ang);
  529. if ((plight_ang+=3) > 360)
  530.     plight_ang = 0;
  531. // move spot light source in ellipse around game world
  532. lights[SPOT_LIGHT_INDEX].pos.x = 2000*Fast_Cos(slight_ang);
  533. lights[SPOT_LIGHT_INDEX].pos.y = 200;
  534. lights[SPOT_LIGHT_INDEX].pos.z = 2000*Fast_Sin(slight_ang);
  535. if ((slight_ang-=5) < 0)
  536.     slight_ang = 360;
  537. // generate camera matrix
  538. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  539. // insert the player into the world
  540. // reset the object (this only matters for backface and object removal)
  541. Reset_OBJECT4DV1(&obj_player);
  542. // set position of tank
  543. obj_player.world_pos.x = cam.pos.x+300*Fast_Sin(cam.dir.y);
  544. obj_player.world_pos.y = cam.pos.y-70;
  545. obj_player.world_pos.z = cam.pos.z+300*Fast_Cos(cam.dir.y);
  546. // generate rotation matrix around y axis
  547. Build_XYZ_Rotation_MATRIX4X4(0, cam.dir.y+turning, 0, &mrot);
  548. // rotate the local coords of the object
  549. Transform_OBJECT4DV1(&obj_player, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  550. // perform world transform
  551. Model_To_World_OBJECT4DV1(&obj_player, TRANSFORM_TRANS_ONLY);
  552. // perform lighting
  553. if (lighting_mode==1)
  554.    Light_OBJECT4DV1_World16(&obj_player, &cam, lights, 4);   
  555. // insert the object into render list
  556. Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_player,0, lighting_mode);
  557. //////////////////////////////////////////////////////////
  558. // insert the tanks in the world
  559. for (index = 0; index < NUM_TANKS; index++)
  560.     {
  561.     // reset the object (this only matters for backface and object removal)
  562.     Reset_OBJECT4DV1(&obj_tank);
  563.     // generate rotation matrix around y axis
  564.     Build_XYZ_Rotation_MATRIX4X4(0, tanks[index].w, 0, &mrot);
  565.     // rotate the local coords of the object
  566.     Transform_OBJECT4DV1(&obj_tank, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  567.     // set position of tank
  568.     obj_tank.world_pos.x = tanks[index].x;
  569.     obj_tank.world_pos.y = tanks[index].y;
  570.     obj_tank.world_pos.z = tanks[index].z;
  571.     // attempt to cull object   
  572.     if (!Cull_OBJECT4DV1(&obj_tank, &cam, CULL_OBJECT_XYZ_PLANES))
  573.        {
  574.        // if we get here then the object is visible at this world position
  575.        // so we can insert it into the rendering list
  576.        // perform local/model to world transform
  577.        Model_To_World_OBJECT4DV1(&obj_tank, TRANSFORM_TRANS_ONLY);
  578.        // perform lighting
  579.        if (lighting_mode==1)
  580.           Light_OBJECT4DV1_World16(&obj_tank, &cam, lights, 4);   
  581.        // insert the object into render list
  582.        Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_tank,0, lighting_mode);
  583.        } // end if
  584.  
  585.     } // end for
  586. ////////////////////////////////////////////////////////
  587. // insert the towers in the world
  588. for (index = 0; index < NUM_TOWERS; index++)
  589.     {
  590.     // reset the object (this only matters for backface and object removal)
  591.     Reset_OBJECT4DV1(&obj_tower);
  592.     // set position of tower
  593.     obj_tower.world_pos.x = towers[index].x;
  594.     obj_tower.world_pos.y = towers[index].y;
  595.     obj_tower.world_pos.z = towers[index].z;
  596.     // attempt to cull object   
  597.     if (!Cull_OBJECT4DV1(&obj_tower, &cam, CULL_OBJECT_XYZ_PLANES))
  598.        {
  599.        // if we get here then the object is visible at this world position
  600.        // so we can insert it into the rendering list
  601.        // perform local/model to world transform
  602.        Model_To_World_OBJECT4DV1(&obj_tower);
  603.        // perform lighting
  604.        if (lighting_mode==1)
  605.           Light_OBJECT4DV1_World16(&obj_tower, &cam, lights, 4);   
  606.        // insert the object into render list
  607.        Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_tower,0,lighting_mode);
  608.        } // end if
  609.  
  610.     } // end for
  611. ///////////////////////////////////////////////////////////////
  612. // seed number generator so that modulation of markers is always the same
  613. srand(13);
  614. // insert the ground markers into the world
  615. for (int index_x = 0; index_x < NUM_POINTS_X; index_x++)
  616.     for (int index_z = 0; index_z < NUM_POINTS_Z; index_z++)
  617.         {
  618.         // reset the object (this only matters for backface and object removal)
  619.         Reset_OBJECT4DV1(&obj_marker);
  620.         // set position of tower
  621.         obj_marker.world_pos.x = RAND_RANGE(-100,100)-UNIVERSE_RADIUS+index_x*POINT_SIZE;
  622.         obj_marker.world_pos.y = obj_marker.max_radius;
  623.         obj_marker.world_pos.z = RAND_RANGE(-100,100)-UNIVERSE_RADIUS+index_z*POINT_SIZE;
  624.         // attempt to cull object   
  625.         if (!Cull_OBJECT4DV1(&obj_marker, &cam, CULL_OBJECT_XYZ_PLANES))
  626.            {
  627.            // if we get here then the object is visible at this world position
  628.            // so we can insert it into the rendering list
  629.            // perform local/model to world transform
  630.            Model_To_World_OBJECT4DV1(&obj_marker);
  631.            // perform lighting
  632.            if (lighting_mode==1)
  633.               Light_OBJECT4DV1_World16(&obj_marker, &cam, lights, 4);   
  634.            // insert the object into render list
  635.            Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_marker,0,lighting_mode);
  636.            } // end if
  637.         } // end for
  638. ////////////////////////////////////////////////////////////////////////
  639. // remove backfaces
  640. if (backface_mode==1)
  641.    Remove_Backfaces_RENDERLIST4DV1(&rend_list, &cam);
  642. // apply world to camera transform
  643. World_To_Camera_RENDERLIST4DV1(&rend_list, &cam);
  644. // apply camera to perspective transformation
  645. Camera_To_Perspective_RENDERLIST4DV1(&rend_list, &cam);
  646. // apply screen transform
  647. Perspective_To_Screen_RENDERLIST4DV1(&rend_list, &cam);
  648. sprintf(work_string,"pos:[%f, %f, %f] heading:[%f] elev:[%f], polys[%d]", 
  649.         cam.pos.x, cam.pos.y, cam.pos.z, cam.dir.y, cam.dir.x, debug_polys_rendered_per_frame); 
  650. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-20, RGB(0,255,0), lpddsback);
  651. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, Spot=%d | BckFceRM [%s]", 
  652.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  653.                                                                                  lights[AMBIENT_LIGHT_INDEX].state,
  654.                                                                                  lights[INFINITE_LIGHT_INDEX].state, 
  655.                                                                                  lights[POINT_LIGHT_INDEX].state,
  656.                                                                                  lights[SPOT_LIGHT_INDEX].state,
  657.                                                                                  ((backface_mode == 1) ? "ON" : "OFF"));
  658. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34, RGB(0,255,0), lpddsback);
  659. // draw instructions
  660. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  661. // should we display help
  662. int text_y = 16;
  663. if (help_mode==1)
  664.     {
  665.     // draw help menu
  666.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  667.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  668.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  669.     Draw_Text_GDI("<S>..............Toggle spot light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  670.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  671.     Draw_Text_GDI("<B>..............Toggle backface removal.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  672.     Draw_Text_GDI("<RIGHT ARROW>....Rotate player right.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  673.     Draw_Text_GDI("<LEFT ARROW>.....Rotate player left.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  674.     Draw_Text_GDI("<UP ARROW>.......Move player forward.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  675.     Draw_Text_GDI("<DOWN ARROW>.....Move player backward.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  676.     Draw_Text_GDI("<SPACE BAR>......Turbo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  677.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  678.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  679.     } // end help
  680. // lock the back buffer
  681. DDraw_Lock_Back_Surface();
  682. // reset number of polys rendered
  683. debug_polys_rendered_per_frame = 0;
  684. // render the object
  685. if (wireframe_mode  == 1)
  686.    Draw_RENDERLIST4DV1_Wire16(&rend_list, back_buffer, back_lpitch);
  687. else
  688.    Draw_RENDERLIST4DV1_Solid16(&rend_list, back_buffer, back_lpitch);
  689. // unlock the back buffer
  690. DDraw_Unlock_Back_Surface();
  691. // flip the surfaces
  692. DDraw_Flip();
  693. // sync to 30ish fps
  694. Wait_Clock(30);
  695. // check of user is trying to exit
  696. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  697.     {
  698.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  699.     } // end if
  700. // return success
  701. return(1);
  702.  
  703. } // end Game_Main
  704. //////////////////////////////////////////////////////////