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

游戏

开发平台:

Visual C++

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