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

游戏

开发平台:

Visual C++

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