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

游戏

开发平台:

Visual C++

  1. // DEMOII12_9.CPP - Bilinear interpolation demo 
  2. // READ THIS!
  3. // To compile make sure to include DDRAW.LIB, DSOUND.LIB,
  4. // DINPUT.LIB, DINPUT8.LIB, WINMM.LIB in the project link list, and of course 
  5. // the C++ source modules T3DLIB1-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 SPOT_LIGHT1_INDEX     4 // point light index
  63. #define SPOT_LIGHT2_INDEX     3 // spot light index
  64. #define CAM_DECEL            (.25)   // deceleration/friction
  65. #define MAX_SPEED             20
  66. #define NUM_OBJECTS           4      // number of objects system loads
  67. #define NUM_SCENE_OBJECTS     500    // number of scenery objects 
  68. #define UNIVERSE_RADIUS       2000   // size of universe
  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,0,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,150,1}, 
  85.          vrot = {0,0,0,1};
  86. CAM4DV1         cam;                    // the single camera
  87. OBJECT4DV2_PTR  obj_work;               // pointer to active working object
  88. OBJECT4DV2      obj_array[NUM_OBJECTS], // array of objects 
  89.                 obj_scene;              // general scenery object
  90.                
  91. // filenames of objects to load
  92. char *object_filenames[NUM_OBJECTS] = { 
  93.                                         "cube_constant_textured_01.cob",
  94.                                         "cube_constant_textured_02.cob",
  95.                                         "fire_constant_cube01.cob",
  96.                                         "borg_cube_constant01.cob",
  97.                                       };
  98. int curr_object = 0;                  // currently active object index
  99. POINT4D         scene_objects[NUM_SCENE_OBJECTS]; // positions of scenery objects
  100. RENDERLIST4DV2  rend_list;      // the render list
  101. RGBAV1 white, gray, black, red, green, blue; // general colors
  102. // physical model defines
  103. float cam_speed  = 0;       // speed of the camera/jeep
  104. ZBUFFERV1 zbuffer;          // out little z buffer!
  105. RENDERCONTEXTV1 rc;         // the rendering context
  106. BOB background;             // the background image
  107. // FUNCTIONS //////////////////////////////////////////////
  108. LRESULT CALLBACK WindowProc(HWND hwnd, 
  109.     UINT msg, 
  110.                             WPARAM wparam, 
  111.                             LPARAM lparam)
  112. {
  113. // this is the main message handler of the system
  114. PAINTSTRUCT ps;    // used in WM_PAINT
  115. HDC hdc;    // handle to a device context
  116. // what is the message 
  117. switch(msg)
  118. {
  119. case WM_CREATE: 
  120.         {
  121. // do initialization stuff here
  122. return(0);
  123. } break;
  124.     case WM_PAINT:
  125.          {
  126.          // start painting
  127.          hdc = BeginPaint(hwnd,&ps);
  128.          // end painting
  129.          EndPaint(hwnd,&ps);
  130.          return(0);
  131.         } break;
  132. case WM_DESTROY: 
  133. {
  134. // kill the application
  135. PostQuitMessage(0);
  136. return(0);
  137. } break;
  138. default:break;
  139.     } // end switch
  140. // process any messages that we didn't take care of 
  141. return (DefWindowProc(hwnd, msg, wparam, lparam));
  142. } // end WinProc
  143. // WINMAIN ////////////////////////////////////////////////
  144. int WINAPI WinMain( HINSTANCE hinstance,
  145. HINSTANCE hprevinstance,
  146. LPSTR lpcmdline,
  147. int ncmdshow)
  148. {
  149. // this is the winmain function
  150. WNDCLASS winclass; // this will hold the class we create
  151. HWND  hwnd; // generic window handle
  152. MSG  msg; // generic message
  153. HDC      hdc;       // generic dc
  154. PAINTSTRUCT ps;     // generic paintstruct
  155. // first fill in the window class stucture
  156. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  157.                           CS_HREDRAW | CS_VREDRAW;
  158. winclass.lpfnWndProc = WindowProc;
  159. winclass.cbClsExtra = 0;
  160. winclass.cbWndExtra = 0;
  161. winclass.hInstance = hinstance;
  162. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  163. winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  164. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  165. winclass.lpszMenuName = NULL; 
  166. winclass.lpszClassName = WINDOW_CLASS_NAME;
  167. // register the window class
  168. if (!RegisterClass(&winclass))
  169. return(0);
  170. // create the window, note the test to see if WINDOWED_APP is
  171. // true to select the appropriate window flags
  172. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  173.   WINDOW_TITLE,  // title
  174.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  175.     0,0,    // x,y
  176.   WINDOW_WIDTH,  // width
  177.                           WINDOW_HEIGHT, // height
  178.   NULL,    // handle to parent 
  179.   NULL,    // handle to menu
  180.   hinstance,// instance
  181.   NULL))) // creation parms
  182. return(0);
  183. // save the window handle and instance in a global
  184. main_window_handle = hwnd;
  185. main_instance      = hinstance;
  186. // resize the window so that client is really width x height
  187. if (WINDOWED_APP)
  188. {
  189. // now resize the window, so the client area is the actual size requested
  190. // since there may be borders and controls if this is going to be a windowed app
  191. // if the app is not windowed then it won't matter
  192. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  193. // make the call to adjust window_rect
  194. AdjustWindowRectEx(&window_rect,
  195.      GetWindowStyle(main_window_handle),
  196.      GetMenu(main_window_handle) != NULL,  
  197.      GetWindowExStyle(main_window_handle));
  198. // save the global client offsets, they are needed in DDraw_Flip()
  199. window_client_x0 = -window_rect.left;
  200. window_client_y0 = -window_rect.top;
  201. // now resize the window with a call to MoveWindow()
  202. MoveWindow(main_window_handle,
  203.            0,                                    // x position
  204.            0,                                    // y position
  205.            window_rect.right - window_rect.left, // width
  206.            window_rect.bottom - window_rect.top, // height
  207.            FALSE);
  208. // show the window, so there's no garbage on first render
  209. ShowWindow(main_window_handle, SW_SHOW);
  210. } // end if windowed
  211. // perform all game console specific initialization
  212. Game_Init();
  213. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  214. // if it causes your system to crash
  215. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  216. // enter main event loop
  217. while(1)
  218. {
  219. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  220. // test if this is a quit
  221.         if (msg.message == WM_QUIT)
  222.            break;
  223. // translate any accelerator keys
  224. TranslateMessage(&msg);
  225. // send the message to the window proc
  226. DispatchMessage(&msg);
  227. } // end if
  228.     
  229.     // main game processing goes here
  230.     Game_Main();
  231. } // end while
  232. // shutdown game and release all resources
  233. Game_Shutdown();
  234. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  235. // if it causes your system to crash
  236. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  237. // return to Windows like this
  238. return(msg.wParam);
  239. } // end WinMain
  240. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  241. int Game_Init(void *parms)
  242. {
  243. // this function is where you do all the initialization 
  244. // for your game
  245. int index; // looping var
  246. // start up DirectDraw (replace the parms as you desire)
  247. DDraw_Init2(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP,1);
  248. // initialize directinput
  249. DInput_Init();
  250. // acquire the keyboard 
  251. DInput_Init_Keyboard();
  252. // add calls to acquire other directinput devices here...
  253. // initialize directsound and directmusic
  254. DSound_Init();
  255. DMusic_Init();
  256. // hide the mouse
  257. if (!WINDOWED_APP)
  258.     ShowCursor(FALSE);
  259. // seed random number generator
  260. srand(Start_Clock()); 
  261. Open_Error_File("ERROR.TXT");
  262. // initialize math engine
  263. Build_Sin_Cos_Tables();
  264. // initialize the camera with 90 FOV, normalized coordinates
  265. Init_CAM4DV1(&cam,      // the camera object
  266.              CAM_MODEL_EULER, // the euler model
  267.              &cam_pos,  // initial camera position
  268.              &cam_dir,  // initial camera angles
  269.              &cam_target,      // no target
  270.              10.0,        // near and far clipping planes
  271.              12000.0,
  272.              120.0,      // field of view in degrees
  273.              WINDOW_WIDTH,   // size of final screen viewport
  274.              WINDOW_HEIGHT);
  275.  
  276. // set a scaling vector
  277. VECTOR4D_INITXYZ(&vscale,20.00,20.00,20.00); 
  278. // load all the objects in
  279. for (int index_obj=0; index_obj < NUM_OBJECTS; index_obj++)
  280.     {
  281.     Load_OBJECT4DV2_COB2(&obj_array[index_obj], object_filenames[index_obj],  
  282.                         &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ 
  283.                                                 | VERTEX_FLAGS_TRANSFORM_LOCAL 
  284.                                                // | VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD
  285.                                                | VERTEX_FLAGS_INVERT_TEXTURE_V
  286.                                                ,0 );
  287.     } // end for index_obj
  288. // set current object
  289. curr_object = 0;
  290. obj_work = &obj_array[curr_object];
  291. // position the scenery objects randomly
  292. for (index = 0; index < NUM_SCENE_OBJECTS; index++)
  293.     {
  294.     // randomly position object
  295.     scene_objects[index].x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS);
  296.     scene_objects[index].y = RAND_RANGE(-200, 200);
  297.     scene_objects[index].z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS);
  298.     } // end for
  299. // set up lights
  300. Reset_Lights_LIGHTV2(lights2, MAX_LIGHTS);
  301. // create some working colors
  302. white.rgba = _RGBA32BIT(255,255,255,0);
  303. gray.rgba  = _RGBA32BIT(150,150,150,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_LIGHTV2(lights2,               // array of lights to work with
  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 = {-1,0,-1,1}; 
  318. // directional light
  319. Init_Light_LIGHTV2(lights2,               // array of lights to work with
  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,               // array of lights to work with
  330.                    POINT_LIGHT_INDEX,
  331.                    LIGHTV2_STATE_ON,      // turn the light on
  332.                    LIGHTV2_ATTR_POINT,    // pointlight type
  333.                    black, green, black,    // color for diffuse term only
  334.                    &plight_pos, NULL,     // need pos only
  335.                    0,.002,0,              // linear attenuation only
  336.                    0,0,1);                // spotlight info NA
  337. VECTOR4D slight2_pos = {0,1000,0,1};
  338. VECTOR4D slight2_dir = {-1,0,-1,1};
  339. // spot light2
  340. Init_Light_LIGHTV2(lights2,                  // array of lights to work with
  341.                    SPOT_LIGHT2_INDEX,
  342.                    LIGHTV2_STATE_ON,         // turn the light on
  343.                    LIGHTV2_ATTR_SPOTLIGHT2,  // spot light type 2
  344.                    black, red, black,        // color for diffuse term only
  345.                    &slight2_pos, &slight2_dir, // need pos only
  346.                    0,.001,0,                 // linear attenuation only
  347.                    0,0,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. // create the alpha lookup table
  358. RGB_Alpha_Table_Builder(NUM_ALPHA_LEVELS, rgb_alpha_table);
  359. // load in the background
  360. Create_BOB(&background, 0,0,800,600,1, BOB_ATTR_VISIBLE | BOB_ATTR_SINGLE_FRAME, DDSCAPS_SYSTEMMEMORY, 0, 16); 
  361. Load_Bitmap_File(&bitmap16bit, "nebblue01.bmp");
  362. Load_Frame_BOB16(&background, &bitmap16bit,0,0,0,BITMAP_EXTRACT_MODE_ABS);
  363. Unload_Bitmap_File(&bitmap16bit);
  364. // return success
  365. return(1);
  366. } // end Game_Init
  367. ///////////////////////////////////////////////////////////
  368. int Game_Shutdown(void *parms)
  369. {
  370. // this function is where you shutdown your game and
  371. // release all resources that you allocated
  372. // shut everything down
  373. // release all your resources created for the game here....
  374. // now directsound
  375. DSound_Stop_All_Sounds();
  376. DSound_Delete_All_Sounds();
  377. DSound_Shutdown();
  378. // directmusic
  379. DMusic_Delete_All_MIDI();
  380. DMusic_Shutdown();
  381. // shut down directinput
  382. DInput_Release_Keyboard();
  383. // shutdown directinput
  384. DInput_Shutdown();
  385. // shutdown directdraw last
  386. DDraw_Shutdown();
  387. Close_Error_File();
  388. Delete_Zbuffer(&zbuffer);
  389. // return success
  390. return(1);
  391. } // end Game_Shutdown
  392. //////////////////////////////////////////////////////////
  393. int Game_Main(void *parms)
  394. {
  395. // this is the workhorse of your game it will be called
  396. // continuously in real-time this is like main() in C
  397. // all the calls for you game go here!
  398. static MATRIX4X4 mrot;   // general rotation matrix
  399. static float plight_ang = 0, 
  400.              slight_ang = 0; // angles for light motion
  401. // use these to rotate objects
  402. static float x_ang = 0, y_ang = 0, z_ang = 0;
  403. // state variables for different rendering modes and help
  404. static int wireframe_mode = 1;
  405. static int backface_mode  = 1;
  406. static int lighting_mode  = 1;
  407. static int help_mode      = 1;
  408. static int zsort_mode     = 1;
  409. static int x_clip_mode    = 1;
  410. static int y_clip_mode    = 1;
  411. static int z_clip_mode    = 1;
  412. static int bilinear_mode  = 1;
  413. char work_string[256]; // temp string
  414. int index; // looping var
  415. // start the timing clock
  416. Start_Clock();
  417. // clear the drawing surface 
  418. //DDraw_Fill_Surface(lpddsback, 0);
  419. // draw the sky
  420. //Draw_Rectangle(0,0, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(250,190,80), lpddsback);
  421. lpddsback->Blt(NULL, background.images[0], NULL, DDBLT_WAIT, NULL); 
  422. // draw the ground
  423. //Draw_Rectangle(0,WINDOW_HEIGHT*.5, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(190,190,230), lpddsback);
  424. // read keyboard and other devices here
  425. DInput_Read_Keyboard();
  426. // game logic here...
  427. // reset the render list
  428. Reset_RENDERLIST4DV2(&rend_list);
  429. // modes and lights
  430. // wireframe mode
  431. if (keyboard_state[DIK_W]) 
  432.    {
  433.    // toggle wireframe mode
  434.    if (++wireframe_mode > 1)
  435.        wireframe_mode=0;
  436.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  437.    } // end if
  438. // lighting
  439. if (keyboard_state[DIK_L])
  440.    {
  441.    // toggle lighting engine completely
  442.    lighting_mode = -lighting_mode;
  443.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  444.    } // end if
  445. // toggle ambient light
  446. if (keyboard_state[DIK_A])
  447.    {
  448.    // toggle ambient light
  449.    if (lights2[AMBIENT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  450.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  451.    else
  452.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  453.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  454.    } // end if
  455. // toggle infinite light
  456. if (keyboard_state[DIK_I])
  457.    {
  458.    // toggle ambient light
  459.    if (lights2[INFINITE_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  460.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  461.    else
  462.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  463.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  464.    } // end if
  465. // toggle point light
  466. if (keyboard_state[DIK_P])
  467.    {
  468.    // toggle point light
  469.    if (lights2[POINT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  470.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  471.    else
  472.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  473.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  474.    } // end if
  475. // toggle spot light
  476. if (keyboard_state[DIK_S])
  477.    {
  478.    // toggle spot light
  479.    if (lights2[SPOT_LIGHT2_INDEX].state == LIGHTV2_STATE_ON)
  480.       lights2[SPOT_LIGHT2_INDEX].state = LIGHTV2_STATE_OFF;
  481.    else
  482.       lights2[SPOT_LIGHT2_INDEX].state = LIGHTV2_STATE_ON;
  483.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  484.    } // end if
  485. // help menu
  486. if (keyboard_state[DIK_H])
  487.    {
  488.    // toggle help menu 
  489.    help_mode = -help_mode;
  490.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  491.    } // end if
  492. // z-sorting
  493. if (keyboard_state[DIK_S])
  494.    {
  495.    // toggle z sorting
  496.    zsort_mode = -zsort_mode;
  497.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  498.    } // end if
  499. // forward/backward
  500. if (keyboard_state[DIK_UP])
  501.    {
  502.    // move forward
  503.    if ( (cam_speed+=1) > MAX_SPEED) cam_speed = MAX_SPEED;
  504.    } // end if
  505. else
  506. if (keyboard_state[DIK_DOWN])
  507.    {
  508.    // move backward
  509.    if ((cam_speed-=1) < -MAX_SPEED) cam_speed = -MAX_SPEED;
  510.    } // end if
  511. // rotate around y axis or yaw
  512. if (keyboard_state[DIK_RIGHT])
  513.    {
  514.    cam.dir.y+=5;
  515.    } // end if
  516. if (keyboard_state[DIK_LEFT])
  517.    {
  518.    cam.dir.y-=5;
  519.    } // end if
  520. // move to next object
  521. if (keyboard_state[DIK_N])
  522.    {
  523.    if (++curr_object >= NUM_OBJECTS)
  524.       curr_object = 0;
  525.  
  526.    // update pointer
  527.    obj_work = &obj_array[curr_object];
  528.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  529.    } // end if
  530. // toggle bilinear mode
  531. if (keyboard_state[DIK_B])
  532.    {
  533.    bilinear_mode = -bilinear_mode;
  534.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  535.    } // end if
  536. // decelerate camera
  537. if (cam_speed > (CAM_DECEL) ) cam_speed-=CAM_DECEL;
  538. else
  539. if (cam_speed < (-CAM_DECEL) ) cam_speed+=CAM_DECEL;
  540. else
  541.    cam_speed = 0;
  542. // move camera
  543. cam.pos.x += cam_speed*Fast_Sin(cam.dir.y);
  544. cam.pos.z += cam_speed*Fast_Cos(cam.dir.y);
  545. // move point light source in ellipse around game world
  546. lights2[POINT_LIGHT_INDEX].pos.x = 1000*Fast_Cos(plight_ang);
  547. lights2[POINT_LIGHT_INDEX].pos.y = 100;
  548. lights2[POINT_LIGHT_INDEX].pos.z = 1000*Fast_Sin(plight_ang);
  549. if ((plight_ang+=3) > 360)
  550.     plight_ang = 0;
  551. // move spot light source in ellipse around game world
  552. lights2[SPOT_LIGHT2_INDEX].pos.x = 1000*Fast_Cos(slight_ang);
  553. lights2[SPOT_LIGHT2_INDEX].pos.y = 200;
  554. lights2[SPOT_LIGHT2_INDEX].pos.z = 1000*Fast_Sin(slight_ang);
  555. if ((slight_ang-=5) < 0)
  556.     slight_ang = 360;
  557. // generate camera matrix
  558. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  559. ////////////////////////////////////////////////////////
  560. // insert the scenery into universe
  561. for (index = 0; index < NUM_SCENE_OBJECTS; index++)
  562.     {
  563.     // reset the object (this only matters for backface and object removal)
  564.     Reset_OBJECT4DV2(obj_work);
  565.     // set position of tower
  566.     obj_work->world_pos.x = scene_objects[index].x;
  567.     obj_work->world_pos.y = scene_objects[index].y;
  568.     obj_work->world_pos.z = scene_objects[index].z;
  569.     // attempt to cull object   
  570.     if (!Cull_OBJECT4DV2(obj_work, &cam, CULL_OBJECT_XYZ_PLANES))
  571.        {
  572.        MAT_IDENTITY_4X4(&mrot);
  573.  
  574.        // rotate the local coords of the object
  575.        Transform_OBJECT4DV2(obj_work, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  576.        // perform world transform
  577.        Model_To_World_OBJECT4DV2(obj_work, TRANSFORM_TRANS_ONLY);
  578.        // insert the object into render list
  579.        Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list,obj_work,0);
  580.        } // end if
  581.  
  582.     } // end for
  583. // reset number of polys rendered
  584. debug_polys_rendered_per_frame = 0;
  585. debug_polys_lit_per_frame = 0;
  586. // remove backfaces
  587. if (backface_mode==1)
  588.    Remove_Backfaces_RENDERLIST4DV2(&rend_list, &cam);
  589. // apply world to camera transform
  590. World_To_Camera_RENDERLIST4DV2(&rend_list, &cam);
  591. // clip the polygons themselves now
  592. Clip_Polys_RENDERLIST4DV2(&rend_list, &cam, ((x_clip_mode == 1) ? CLIP_POLY_X_PLANE : 0) | 
  593.                                             ((y_clip_mode == 1) ? CLIP_POLY_Y_PLANE : 0) | 
  594.                                             ((z_clip_mode == 1) ? CLIP_POLY_Z_PLANE : 0) );
  595. // light scene all at once 
  596. if (lighting_mode==1)
  597.    {
  598.    Transform_LIGHTSV2(lights2, 4, &cam.mcam, TRANSFORM_LOCAL_TO_TRANS);
  599.    Light_RENDERLIST4DV2_World2_16(&rend_list, &cam, lights2, 4);
  600.    } // end if
  601. // sort the polygon list (hurry up!)
  602. if (zsort_mode == 1)
  603.    Sort_RENDERLIST4DV2(&rend_list,  SORT_POLYLIST_AVGZ);
  604. // apply camera to perspective transformation
  605. Camera_To_Perspective_RENDERLIST4DV2(&rend_list, &cam);
  606. // apply screen transform
  607. Perspective_To_Screen_RENDERLIST4DV2(&rend_list, &cam);
  608. // lock the back buffer
  609. DDraw_Lock_Back_Surface();
  610. // reset number of polys rendered
  611. debug_polys_rendered_per_frame = 0;
  612. // render the renderinglist
  613. if (wireframe_mode  == 0)
  614.    Draw_RENDERLIST4DV2_Wire16(&rend_list, back_buffer, back_lpitch);
  615. else
  616. if (wireframe_mode  == 1)
  617.    {
  618.    // initialize zbuffer to 16000 fixed point
  619.    Clear_Zbuffer(&zbuffer, (000 << FIXP16_SHIFT));
  620.    // set up rendering context
  621.    rc.attr         = RENDER_ATTR_INVZBUFFER  
  622.                     // | RENDER_ATTR_ALPHA  
  623.                     // | RENDER_ATTR_MIPMAP  
  624.                      | (bilinear_mode==1 ? RENDER_ATTR_BILERP : 0)
  625.                      | RENDER_ATTR_TEXTURE_PERSPECTIVE_AFFINE;
  626.    
  627.    rc.video_buffer   = back_buffer;
  628.    rc.lpitch         = back_lpitch;
  629.    rc.mip_dist       = 3500;
  630.    rc.zbuffer        = (UCHAR *)zbuffer.zbuffer;
  631.    rc.zpitch         = WINDOW_WIDTH*4;
  632.    rc.rend_list      = &rend_list;
  633.    rc.texture_dist   = 0;
  634.    rc.alpha_override = 5;
  635.    // render scene
  636.    Draw_RENDERLIST4DV2_RENDERCONTEXTV1_16(&rc);
  637.    
  638.    } // end if
  639. // unlock the back buffer
  640. DDraw_Unlock_Back_Surface();
  641. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, Spot=%d, Zsort [%s], Bilinear Filtering [%s]", 
  642.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  643.                                                                                  lights[AMBIENT_LIGHT_INDEX].state,
  644.                                                                                  lights[INFINITE_LIGHT_INDEX].state, 
  645.                                                                                  lights[POINT_LIGHT_INDEX].state,
  646.                                                                                  lights[SPOT_LIGHT2_INDEX].state,
  647.                                                                                  ((zsort_mode == 1) ? "ON" : "OFF"),
  648.                                                                                  ((bilinear_mode == 1) ? "ON" : "OFF")
  649.                                                                                  );
  650. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16, RGB(0,255,0), lpddsback);
  651. // draw instructions
  652. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  653. // should we display help
  654. int text_y = 16;
  655. if (help_mode==1)
  656.     {
  657.     // draw help menu
  658.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  659.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  660.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  661.     Draw_Text_GDI("<S>..............Toggle spot light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  662.     Draw_Text_GDI("<N>..............Next object.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  663.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  664.     Draw_Text_GDI("<S>..............Toggle Z sorting.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  665.     Draw_Text_GDI("<B>..............Toggle Bilinear Filtering.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  666.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  667.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  668.     } // end help
  669. sprintf(work_string,"Polys Rendered: %d, Polys lit: %d", debug_polys_rendered_per_frame, debug_polys_lit_per_frame);
  670. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16-16, RGB(0,255,0), lpddsback);
  671. sprintf(work_string,"CAM [%5.2f, %5.2f, %5.2f]",  cam.pos.x, cam.pos.y, cam.pos.z);
  672. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16-16-16, RGB(0,255,0), lpddsback);
  673. // flip the surfaces
  674. DDraw_Flip2();
  675. // sync to 30ish fps
  676. Wait_Clock(30);
  677. // check of user is trying to exit
  678. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  679.     {
  680.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  681.     } // end if
  682. // return success
  683. return(1);
  684.  
  685. } // end Game_Main
  686. //////////////////////////////////////////////////////////