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

游戏

开发平台:

Visual C++

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