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

游戏

开发平台:

Visual C++

  1. // DEMOII8_6_8b.CPP - flat shading demo with z-sorting (8-bit version)
  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        64 // 96
  62. #define NUM_TANKS         32 // 24
  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_LIGHT_INDEX      3 // spot light index
  69. // PROTOTYPES /////////////////////////////////////////////
  70. // game console
  71. int Game_Init(void *parms=NULL);
  72. int Game_Shutdown(void *parms=NULL);
  73. int Game_Main(void *parms=NULL);
  74. // GLOBALS ////////////////////////////////////////////////
  75. HWND main_window_handle           = NULL; // save the window handle
  76. HINSTANCE main_instance           = NULL; // save the instance
  77. char buffer[2048];                        // used to print text
  78. // initialize camera position and direction
  79. POINT4D  cam_pos    = {0,40,0,1};
  80. POINT4D  cam_target = {0,0,0,1};
  81. VECTOR4D cam_dir    = {0,0,0,1};
  82. // all your initialization code goes here...
  83. VECTOR4D vscale={1.0,1.0,1.0,1}, 
  84.          vpos = {0,0,0,1}, 
  85.          vrot = {0,0,0,1};
  86. CAM4DV1        cam;       // the single camera
  87. OBJECT4DV1     obj_tower,    // used to hold the master tower
  88.                obj_tank,     // used to hold the master tank
  89.                obj_marker,   // the ground marker
  90.                obj_player;   // the player object             
  91. POINT4D        towers[NUM_TOWERS],
  92.                tanks[NUM_TANKS];
  93.                
  94. RENDERLIST4DV1 rend_list; // the render list
  95. // FUNCTIONS //////////////////////////////////////////////
  96. LRESULT CALLBACK WindowProc(HWND hwnd, 
  97.     UINT msg, 
  98.                             WPARAM wparam, 
  99.                             LPARAM lparam)
  100. {
  101. // this is the main message handler of the system
  102. PAINTSTRUCT ps;    // used in WM_PAINT
  103. HDC hdc;    // handle to a device context
  104. // what is the message 
  105. switch(msg)
  106. {
  107. case WM_CREATE: 
  108.         {
  109. // do initialization stuff here
  110. return(0);
  111. } break;
  112.     case WM_PAINT:
  113.          {
  114.          // start painting
  115.          hdc = BeginPaint(hwnd,&ps);
  116.          // end painting
  117.          EndPaint(hwnd,&ps);
  118.          return(0);
  119.         } break;
  120. case WM_DESTROY: 
  121. {
  122. // kill the application
  123. PostQuitMessage(0);
  124. return(0);
  125. } break;
  126. default:break;
  127.     } // end switch
  128. // process any messages that we didn't take care of 
  129. return (DefWindowProc(hwnd, msg, wparam, lparam));
  130. } // end WinProc
  131. // WINMAIN ////////////////////////////////////////////////
  132. int WINAPI WinMain( HINSTANCE hinstance,
  133. HINSTANCE hprevinstance,
  134. LPSTR lpcmdline,
  135. int ncmdshow)
  136. {
  137. // this is the winmain function
  138. WNDCLASS winclass; // this will hold the class we create
  139. HWND  hwnd; // generic window handle
  140. MSG  msg; // generic message
  141. HDC      hdc;       // generic dc
  142. PAINTSTRUCT ps;     // generic paintstruct
  143. // first fill in the window class stucture
  144. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  145.                           CS_HREDRAW | CS_VREDRAW;
  146. winclass.lpfnWndProc = WindowProc;
  147. winclass.cbClsExtra = 0;
  148. winclass.cbWndExtra = 0;
  149. winclass.hInstance = hinstance;
  150. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  151. winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  152. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  153. winclass.lpszMenuName = NULL; 
  154. winclass.lpszClassName = WINDOW_CLASS_NAME;
  155. // register the window class
  156. if (!RegisterClass(&winclass))
  157. return(0);
  158. // create the window, note the test to see if WINDOWED_APP is
  159. // true to select the appropriate window flags
  160. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  161.   WINDOW_TITLE,  // title
  162.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  163.     0,0,    // x,y
  164.   WINDOW_WIDTH,  // width
  165.                           WINDOW_HEIGHT, // height
  166.   NULL,    // handle to parent 
  167.   NULL,    // handle to menu
  168.   hinstance,// instance
  169.   NULL))) // creation parms
  170. return(0);
  171. // save the window handle and instance in a global
  172. main_window_handle = hwnd;
  173. main_instance      = hinstance;
  174. // resize the window so that client is really width x height
  175. if (WINDOWED_APP)
  176. {
  177. // now resize the window, so the client area is the actual size requested
  178. // since there may be borders and controls if this is going to be a windowed app
  179. // if the app is not windowed then it won't matter
  180. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  181. // make the call to adjust window_rect
  182. AdjustWindowRectEx(&window_rect,
  183.      GetWindowStyle(main_window_handle),
  184.      GetMenu(main_window_handle) != NULL,  
  185.      GetWindowExStyle(main_window_handle));
  186. // save the global client offsets, they are needed in DDraw_Flip()
  187. window_client_x0 = -window_rect.left;
  188. window_client_y0 = -window_rect.top;
  189. // now resize the window with a call to MoveWindow()
  190. MoveWindow(main_window_handle,
  191.            0, // x position
  192.            0, // y position
  193.            window_rect.right - window_rect.left, // width
  194.            window_rect.bottom - window_rect.top, // height
  195.            FALSE);
  196. // show the window, so there's no garbage on first render
  197. ShowWindow(main_window_handle, SW_SHOW);
  198. } // end if windowed
  199. // perform all game console specific initialization
  200. Game_Init();
  201. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  202. // if it causes your system to crash
  203. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  204. // enter main event loop
  205. while(1)
  206. {
  207. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  208. // test if this is a quit
  209.         if (msg.message == WM_QUIT)
  210.            break;
  211. // translate any accelerator keys
  212. TranslateMessage(&msg);
  213. // send the message to the window proc
  214. DispatchMessage(&msg);
  215. } // end if
  216.     
  217.     // main game processing goes here
  218.     Game_Main();
  219. } // end while
  220. // shutdown game and release all resources
  221. Game_Shutdown();
  222. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  223. // if it causes your system to crash
  224. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  225. // return to Windows like this
  226. return(msg.wParam);
  227. } // end WinMain
  228. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  229. int Game_Init(void *parms)
  230. {
  231. // this function is where you do all the initialization 
  232. // for your game
  233. int index; // looping var
  234. // start up DirectDraw (replace the parms as you desire)
  235. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  236. // initialize directinput
  237. DInput_Init();
  238. // acquire the keyboard 
  239. DInput_Init_Keyboard();
  240. // add calls to acquire other directinput devices here...
  241. // initialize directsound and directmusic
  242. DSound_Init();
  243. DMusic_Init();
  244. // hide the mouse
  245. if (!WINDOWED_APP)
  246.     ShowCursor(FALSE);
  247. // seed random number generator
  248. srand(Start_Clock());
  249. Open_Error_File("ERROR.TXT");
  250. // initialize math engine
  251. Build_Sin_Cos_Tables();
  252. // initialize the camera with 90 FOV, normalized coordinates
  253. Init_CAM4DV1(&cam,      // the camera object
  254.              CAM_MODEL_EULER, // the euler model
  255.              &cam_pos,  // initial camera position
  256.              &cam_dir,  // initial camera angles
  257.              &cam_target,      // no target
  258.              200.0,      // near and far clipping planes
  259.              12000.0,
  260.              120.0,      // field of view in degrees
  261.              WINDOW_WIDTH,   // size of final screen viewport
  262.              WINDOW_HEIGHT);
  263. // load the master tank object
  264. VECTOR4D_INITXYZ(&vscale,0.75,0.75,0.75);
  265. Load_OBJECT4DV1_PLG(&obj_tank, "tank3_8b.plg",&vscale, &vpos, &vrot);
  266. //Load_OBJECT4DV1_3DSASC(&obj_tank,"sphere01.asc",  
  267. //                       &vscale, &vpos, &vrot, 
  268. //                       VERTEX_FLAGS_INVERT_WINDING_ORDER | VERTEX_FLAGS_SWAP_YZ );
  269. // load player object for 3rd person view
  270. VECTOR4D_INITXYZ(&vscale,0.75,0.75,0.75);
  271. Load_OBJECT4DV1_PLG(&obj_player, "tank2_8b.plg",&vscale, &vpos, &vrot);
  272. //Load_OBJECT4DV1_3DSASC(&obj_player,"sphere01.asc",  
  273. //                       &vscale, &vpos, &vrot, 
  274. //                       VERTEX_FLAGS_INVERT_WINDING_ORDER | VERTEX_FLAGS_SWAP_YZ );
  275. // load the master tower object
  276. VECTOR4D_INITXYZ(&vscale,1.0, 2.0, 1.0);
  277. Load_OBJECT4DV1_PLG(&obj_tower, "tower1_8b.plg",&vscale, &vpos, &vrot);
  278. // load the master ground marker
  279. VECTOR4D_INITXYZ(&vscale,3.0,3.0,3.0);
  280. Load_OBJECT4DV1_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. RGBAV1 white, gray, black, red, green, blue;
  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, white, 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 slight_pos = {0,200,0,0};
  335. VECTOR4D slight_dir = {-1,0,-1,0};
  336. // spot light
  337. Init_Light_LIGHTV1(SPOT_LIGHT_INDEX,
  338.                    LIGHTV1_STATE_ON,         // turn the light on
  339.                    LIGHTV1_ATTR_SPOTLIGHT2,  // spot light type 2
  340.                    black, white, black,      // color for diffuse term only
  341.                    &slight_pos, &slight_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. // draw the sky
  401. Draw_Rectangle(0,0, WINDOW_WIDTH-1, WINDOW_HEIGHT/2, 166, lpddsback);
  402. //Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, rgblookup[RGB16Bit565(115,42,16)], lpddsback);
  403. //Draw_Rectangle(0,0, WINDOW_WIDTH-1, WINDOW_HEIGHT/2, rgblookup[RGB16Bit565(0,140,192)], lpddsback);
  404. //Draw_Rectangle(0,0, WINDOW_WIDTH-1, WINDOW_HEIGHT/2, RGB16Bit(0,140,192), lpddsback);
  405. // draw the ground
  406. Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, 28, lpddsback);
  407. //Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, rgblookup[RGB16Bit565(115,42,16)], lpddsback);
  408. //Draw_Rectangle(0,WINDOW_HEIGHT/2, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, RGB16Bit(103,62,3), lpddsback);
  409. // read keyboard and other devices here
  410. DInput_Read_Keyboard();
  411. // game logic here...
  412. // reset the render list
  413. Reset_RENDERLIST4DV1(&rend_list);
  414. // allow user to move camera
  415. // turbo
  416. if (keyboard_state[DIK_SPACE])
  417.     tank_speed = 5*TANK_SPEED;
  418. else
  419.     tank_speed = TANK_SPEED;
  420. // forward/backward
  421. if (keyboard_state[DIK_UP])
  422.    {
  423.    // move forward
  424.    cam.pos.x += tank_speed*Fast_Sin(cam.dir.y);
  425.    cam.pos.z += tank_speed*Fast_Cos(cam.dir.y);
  426.    } // end if
  427. if (keyboard_state[DIK_DOWN])
  428.    {
  429.    // move backward
  430.    cam.pos.x -= tank_speed*Fast_Sin(cam.dir.y);
  431.    cam.pos.z -= tank_speed*Fast_Cos(cam.dir.y);
  432.    } // end if
  433. // rotate
  434. if (keyboard_state[DIK_RIGHT])
  435.    {
  436.    cam.dir.y+=3;
  437.    
  438.    // add a little turn to object
  439.    if ((turning+=2) > 15)
  440.       turning=15;
  441.    } // end if
  442. if (keyboard_state[DIK_LEFT])
  443.    {
  444.    cam.dir.y-=3;
  445.    // add a little turn to object
  446.    if ((turning-=2) < -15)
  447.       turning=-15;
  448.    } // end if
  449. else // center heading again
  450.    {
  451.    if (turning > 0)
  452.        turning-=1;
  453.    else
  454.    if (turning < 0)
  455.        turning+=1;
  456.    } // end else
  457. // modes and lights
  458. // wireframe mode
  459. if (keyboard_state[DIK_W])
  460.    {
  461.    // toggle wireframe mode
  462.    wireframe_mode = -wireframe_mode;
  463.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  464.    } // end if
  465. // backface removal
  466. if (keyboard_state[DIK_B])
  467.    {
  468.    // toggle backface removal
  469.    backface_mode = -backface_mode;
  470.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  471.    } // end if
  472. // lighting
  473. if (keyboard_state[DIK_L])
  474.    {
  475.    // toggle lighting engine completely
  476.    lighting_mode = -lighting_mode;
  477.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  478.    } // end if
  479. // toggle ambient light
  480. if (keyboard_state[DIK_A])
  481.    {
  482.    // toggle ambient light
  483.    if (lights[AMBIENT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  484.       lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  485.    else
  486.       lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  487.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  488.    } // end if
  489. // toggle infinite light
  490. if (keyboard_state[DIK_I])
  491.    {
  492.    // toggle infinite light
  493.    if (lights[INFINITE_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  494.       lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  495.    else
  496.       lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  497.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  498.    } // end if
  499. // toggle point light
  500. if (keyboard_state[DIK_P])
  501.    {
  502.    // toggle point light
  503.    if (lights[POINT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  504.       lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  505.    else
  506.       lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  507.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  508.    } // end if
  509. // toggle spot light
  510. if (keyboard_state[DIK_S])
  511.    {
  512.    // toggle spot light
  513.    if (lights[SPOT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  514.       lights[SPOT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  515.    else
  516.       lights[SPOT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  517.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  518.    } // end if
  519. // help menu
  520. if (keyboard_state[DIK_H])
  521.    {
  522.    // toggle help menu 
  523.    help_mode = -help_mode;
  524.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  525.    } // end if
  526. // z-sorting
  527. if (keyboard_state[DIK_Z])
  528.    {
  529.    // toggle z sorting
  530.    zsort_mode = -zsort_mode;
  531.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  532.    } // end if
  533. static float plight_ang = 0, slight_ang = 0; // angles for light motion
  534. // move point light source in ellipse around game world
  535. lights[POINT_LIGHT_INDEX].pos.x = 4000*Fast_Cos(plight_ang);
  536. lights[POINT_LIGHT_INDEX].pos.y = 200;
  537. lights[POINT_LIGHT_INDEX].pos.z = 4000*Fast_Sin(plight_ang);
  538. if ((plight_ang+=3) > 360)
  539.     plight_ang = 0;
  540. // move spot light source in ellipse around game world
  541. lights[SPOT_LIGHT_INDEX].pos.x = 2000*Fast_Cos(slight_ang);
  542. lights[SPOT_LIGHT_INDEX].pos.y = 200;
  543. lights[SPOT_LIGHT_INDEX].pos.z = 2000*Fast_Sin(slight_ang);
  544. if ((slight_ang-=5) < 0) 
  545.     slight_ang = 360;
  546. // generate camera matrix
  547. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  548. // insert the player into the world
  549. // reset the object (this only matters for backface and object removal)
  550. Reset_OBJECT4DV1(&obj_player);
  551. // set position of tank
  552. obj_player.world_pos.x = cam.pos.x+300*Fast_Sin(cam.dir.y);
  553. obj_player.world_pos.y = cam.pos.y-70;
  554. obj_player.world_pos.z = cam.pos.z+300*Fast_Cos(cam.dir.y);
  555. // generate rotation matrix around y axis
  556. Build_XYZ_Rotation_MATRIX4X4(0, cam.dir.y+turning, 0, &mrot);
  557. // rotate the local coords of the object
  558. Transform_OBJECT4DV1(&obj_player, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  559. // perform world transform
  560. Model_To_World_OBJECT4DV1(&obj_player, TRANSFORM_TRANS_ONLY);
  561. // insert the object into render list
  562. Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_player,0, 0);
  563. //////////////////////////////////////////////////////////
  564. // insert the tanks in the world
  565. for (index = 0; index < NUM_TANKS; index++)
  566.     {
  567.     // reset the object (this only matters for backface and object removal)
  568.     Reset_OBJECT4DV1(&obj_tank);
  569.     // generate rotation matrix around y axis
  570.     Build_XYZ_Rotation_MATRIX4X4(0, tanks[index].w, 0, &mrot);
  571.     // rotate the local coords of the object
  572.     Transform_OBJECT4DV1(&obj_tank, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  573.     // set position of tank
  574.     obj_tank.world_pos.x = tanks[index].x;
  575.     obj_tank.world_pos.y = tanks[index].y;
  576.     obj_tank.world_pos.z = tanks[index].z;
  577.     // attempt to cull object   
  578.     if (!Cull_OBJECT4DV1(&obj_tank, &cam, CULL_OBJECT_XYZ_PLANES))
  579.        {
  580.        // if we get here then the object is visible at this world position
  581.        // so we can insert it into the rendering list
  582.        // perform local/model to world transform
  583.        Model_To_World_OBJECT4DV1(&obj_tank, TRANSFORM_TRANS_ONLY);
  584.        // insert the object into render list
  585.        Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_tank,0,0);
  586.        } // end if
  587.  
  588.     } // end for
  589. ////////////////////////////////////////////////////////
  590. // insert the towers in the world
  591. for (index = 0; index < NUM_TOWERS; index++)
  592.     {
  593.     // reset the object (this only matters for backface and object removal)
  594.     Reset_OBJECT4DV1(&obj_tower);
  595.     // set position of tower
  596.     obj_tower.world_pos.x = towers[index].x;
  597.     obj_tower.world_pos.y = towers[index].y;
  598.     obj_tower.world_pos.z = towers[index].z;
  599.     // attempt to cull object   
  600.     if (!Cull_OBJECT4DV1(&obj_tower, &cam, CULL_OBJECT_XYZ_PLANES))
  601.        {
  602.        // if we get here then the object is visible at this world position
  603.        // so we can insert it into the rendering list
  604.        // perform local/model to world transform
  605.        Model_To_World_OBJECT4DV1(&obj_tower);
  606.        // insert the object into render list
  607.        Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_tower,0,0);
  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.            // insert the object into render list
  632.            Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_marker,0,0);
  633.            } // end if
  634.         } // end for
  635. ////////////////////////////////////////////////////////////////////////
  636. // remove backfaces
  637. if (backface_mode==1)
  638.    Remove_Backfaces_RENDERLIST4DV1(&rend_list, &cam);
  639. // light scene all at once 
  640. if (lighting_mode==1)
  641.    Light_RENDERLIST4DV1_World(&rend_list, &cam, lights, 4);
  642. // apply world to camera transform
  643. World_To_Camera_RENDERLIST4DV1(&rend_list, &cam);
  644. // sort the polygon list (hurry up!)
  645. if (zsort_mode==1)
  646.    Sort_RENDERLIST4DV1(&rend_list,  SORT_POLYLIST_AVGZ);
  647. // apply camera to perspective transformation
  648. Camera_To_Perspective_RENDERLIST4DV1(&rend_list, &cam);
  649. // apply screen transform
  650. Perspective_To_Screen_RENDERLIST4DV1(&rend_list, &cam);
  651. sprintf(work_string,"pos:[%f, %f, %f] heading:[%f] elev:[%f], polys[%d]", 
  652.         cam.pos.x, cam.pos.y, cam.pos.z, cam.dir.y, cam.dir.x, debug_polys_rendered_per_frame); 
  653. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-20, RGB(0,255,0), lpddsback);
  654. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, Spot=%d | Zsort [%s], BckFceRM [%s]", 
  655.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  656.                                                                                  lights[AMBIENT_LIGHT_INDEX].state,
  657.                                                                                  lights[INFINITE_LIGHT_INDEX].state, 
  658.                                                                                  lights[POINT_LIGHT_INDEX].state,
  659.                                                                                  lights[SPOT_LIGHT_INDEX].state,
  660.                                                                                  ((zsort_mode == 1) ? "ON" : "OFF"),
  661.                                                                                  ((backface_mode == 1) ? "ON" : "OFF"));
  662. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34, RGB(0,255,0), lpddsback);
  663. // draw instructions
  664. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  665. // should we display help
  666. int text_y = 16;
  667. if (help_mode==1)
  668.     {
  669.     // draw help menu
  670.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  671.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  672.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  673.     Draw_Text_GDI("<S>..............Toggle spot light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  674.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  675.     Draw_Text_GDI("<B>..............Toggle backface removal.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  676.     Draw_Text_GDI("<RIGHT ARROW>....Rotate player right.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  677.     Draw_Text_GDI("<LEFT ARROW>.....Rotate player left.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  678.     Draw_Text_GDI("<UP ARROW>.......Move player forward.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  679.     Draw_Text_GDI("<DOWN ARROW>.....Move player backward.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  680.     Draw_Text_GDI("<SPACE BAR>......Turbo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  681.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  682.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  683.     } // end help
  684. // lock the back buffer
  685. DDraw_Lock_Back_Surface();
  686. // reset number of polys rendered
  687. debug_polys_rendered_per_frame = 0;
  688. // render the object
  689. if (wireframe_mode  == 1)
  690.    Draw_RENDERLIST4DV1_Wire(&rend_list, back_buffer, back_lpitch);
  691. else
  692.    Draw_RENDERLIST4DV1_Solid2(&rend_list, back_buffer, back_lpitch);
  693. // unlock the back buffer
  694. DDraw_Unlock_Back_Surface();
  695. // flip the surfaces
  696. DDraw_Flip();
  697. // sync to 30ish fps
  698. Wait_Clock(30);
  699. // check of user is trying to exit
  700. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  701.     {
  702.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  703.     } // end if
  704. // return success
  705. return(1);
  706.  
  707. } // end Game_Main
  708. //////////////////////////////////////////////////////////