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

游戏

开发平台:

Visual C++

  1. // DEMOII12_11.CPP - Mipmapping 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-0.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.                                                ,1 );
  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 bilinear_mode  = 1;
  410. static float mipdistance    = 2500;
  411. static int mip_mode       = 1;
  412. char work_string[256]; // temp string
  413. int index; // looping var
  414. // start the timing clock
  415. Start_Clock();
  416. // clear the drawing surface 
  417. //DDraw_Fill_Surface(lpddsback, 0);
  418. // draw the sky
  419. //Draw_Rectangle(0,0, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(250,190,80), lpddsback);
  420. lpddsback->Blt(NULL, background.images[0], NULL, DDBLT_WAIT, NULL); 
  421. // draw the ground
  422. //Draw_Rectangle(0,WINDOW_HEIGHT*.5, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(190,190,230), lpddsback);
  423. // read keyboard and other devices here
  424. DInput_Read_Keyboard();
  425. // game logic here...
  426. // reset the render list
  427. Reset_RENDERLIST4DV2(&rend_list);
  428. // modes and lights
  429. // wireframe mode
  430. if (keyboard_state[DIK_W]) 
  431.    {
  432.    // toggle wireframe mode
  433.    if (++wireframe_mode > 1)
  434.        wireframe_mode=0;
  435.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  436.    } // end if
  437. // lighting
  438. if (keyboard_state[DIK_L])
  439.    {
  440.    // toggle lighting engine completely
  441.    lighting_mode = -lighting_mode;
  442.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  443.    } // end if
  444. // toggle ambient light
  445. if (keyboard_state[DIK_A])
  446.    {
  447.    // toggle ambient light
  448.    if (lights2[AMBIENT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  449.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  450.    else
  451.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  452.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  453.    } // end if
  454. // toggle infinite light
  455. if (keyboard_state[DIK_I])
  456.    {
  457.    // toggle ambient light
  458.    if (lights2[INFINITE_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  459.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  460.    else
  461.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  462.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  463.    } // end if
  464. // toggle point light
  465. if (keyboard_state[DIK_P])
  466.    {
  467.    // toggle point light
  468.    if (lights2[POINT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  469.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  470.    else
  471.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  472.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  473.    } // end if
  474. // toggle spot light
  475. if (keyboard_state[DIK_S])
  476.    {
  477.    // toggle spot light
  478.    if (lights2[SPOT_LIGHT2_INDEX].state == LIGHTV2_STATE_ON)
  479.       lights2[SPOT_LIGHT2_INDEX].state = LIGHTV2_STATE_OFF;
  480.    else
  481.       lights2[SPOT_LIGHT2_INDEX].state = LIGHTV2_STATE_ON;
  482.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  483.    } // end if
  484. // z-sorting
  485. if (keyboard_state[DIK_S])
  486.    {
  487.    // toggle z sorting
  488.    zsort_mode = -zsort_mode;
  489.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  490.    } // end if
  491. // help menu
  492. if (keyboard_state[DIK_H])
  493.    {
  494.    // toggle help menu 
  495.    help_mode = -help_mode;
  496.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  497.    } // end if
  498. // forward/backward
  499. if (keyboard_state[DIK_UP])
  500.    {
  501.    // move forward
  502.    if ( (cam_speed+=1) > MAX_SPEED) cam_speed = MAX_SPEED;
  503.    } // end if
  504. else
  505. if (keyboard_state[DIK_DOWN])
  506.    {
  507.    // move backward
  508.    if ((cam_speed-=1) < -MAX_SPEED) cam_speed = -MAX_SPEED;
  509.    } // end if
  510. // rotate around y axis or yaw
  511. if (keyboard_state[DIK_RIGHT])
  512.    {
  513.    cam.dir.y+=5;
  514.    } // end if
  515. if (keyboard_state[DIK_LEFT])
  516.    {
  517.    cam.dir.y-=5;
  518.    } // end if
  519. // move to next object
  520. if (keyboard_state[DIK_N])
  521.    {
  522.    if (++curr_object >= NUM_OBJECTS)
  523.       curr_object = 0;
  524.  
  525.    // update pointer
  526.    obj_work = &obj_array[curr_object];
  527.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  528.    } // end if
  529. // toggle bilinear mode
  530. if (keyboard_state[DIK_B])
  531.    {
  532.    bilinear_mode = -bilinear_mode;
  533.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  534.    } // end if
  535. // toggle mipmap mode
  536. if (keyboard_state[DIK_M])
  537.    {
  538.    mip_mode = -mip_mode;
  539.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  540.    } // end if
  541. // decrease mip range
  542. if (keyboard_state[DIK_1])
  543.    {
  544.    if ((mipdistance-=10) < 0) mipdistance = 0;
  545.    Wait_Clock(1); // wait, so keyboard doesn't bounce
  546.    } // end if
  547. // increase mip range
  548. if (keyboard_state[DIK_2])
  549.    {
  550.    mipdistance+=10;
  551.    Wait_Clock(1); // wait, so keyboard doesn't bounce
  552.    } // end if
  553. // decelerate camera
  554. if (cam_speed > (CAM_DECEL) ) cam_speed-=CAM_DECEL;
  555. else
  556. if (cam_speed < (-CAM_DECEL) ) cam_speed+=CAM_DECEL;
  557. else
  558.    cam_speed = 0;
  559. // move camera
  560. cam.pos.x += cam_speed*Fast_Sin(cam.dir.y);
  561. cam.pos.z += cam_speed*Fast_Cos(cam.dir.y);
  562. // move point light source in ellipse around game world
  563. lights2[POINT_LIGHT_INDEX].pos.x = 1000*Fast_Cos(plight_ang);
  564. lights2[POINT_LIGHT_INDEX].pos.y = 100;
  565. lights2[POINT_LIGHT_INDEX].pos.z = 1000*Fast_Sin(plight_ang);
  566. if ((plight_ang+=3) > 360)
  567.     plight_ang = 0;
  568. // move spot light source in ellipse around game world
  569. lights2[SPOT_LIGHT2_INDEX].pos.x = 1000*Fast_Cos(slight_ang);
  570. lights2[SPOT_LIGHT2_INDEX].pos.y = 200;
  571. lights2[SPOT_LIGHT2_INDEX].pos.z = 1000*Fast_Sin(slight_ang);
  572. if ((slight_ang-=5) < 0)
  573.     slight_ang = 360;
  574. // generate camera matrix
  575. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  576. ////////////////////////////////////////////////////////
  577. // insert the scenery into universe
  578. for (index = 0; index < NUM_SCENE_OBJECTS; index++)
  579.     {
  580.     // reset the object (this only matters for backface and object removal)
  581.     Reset_OBJECT4DV2(obj_work);
  582.     // set position of tower
  583.     obj_work->world_pos.x = scene_objects[index].x;
  584.     obj_work->world_pos.y = scene_objects[index].y;
  585.     obj_work->world_pos.z = scene_objects[index].z;
  586.     // attempt to cull object   
  587.     if (!Cull_OBJECT4DV2(obj_work, &cam, CULL_OBJECT_XYZ_PLANES))
  588.        {
  589.        MAT_IDENTITY_4X4(&mrot);
  590.  
  591.        // rotate the local coords of the object
  592.        Transform_OBJECT4DV2(obj_work, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  593.        // perform world transform
  594.        Model_To_World_OBJECT4DV2(obj_work, TRANSFORM_TRANS_ONLY);
  595.        // insert the object into render list
  596.        Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list,obj_work,0);
  597.        } // end if
  598.  
  599.     } // end for
  600. // reset number of polys rendered
  601. debug_polys_rendered_per_frame = 0;
  602. debug_polys_lit_per_frame = 0;
  603. // remove backfaces
  604. if (backface_mode==1)
  605.    Remove_Backfaces_RENDERLIST4DV2(&rend_list, &cam);
  606. // apply world to camera transform
  607. World_To_Camera_RENDERLIST4DV2(&rend_list, &cam);
  608. // clip the polygons themselves now
  609. Clip_Polys_RENDERLIST4DV2(&rend_list, &cam, CLIP_POLY_X_PLANE | CLIP_POLY_Y_PLANE | CLIP_POLY_Z_PLANE);
  610. // light scene all at once 
  611. if (lighting_mode==1)
  612.    {
  613.    Transform_LIGHTSV2(lights2, 4, &cam.mcam, TRANSFORM_LOCAL_TO_TRANS);
  614.    Light_RENDERLIST4DV2_World2_16(&rend_list, &cam, lights2, 4);
  615.    } // end if
  616. // sort the polygon list (hurry up!)
  617. if (zsort_mode == 1)
  618.    Sort_RENDERLIST4DV2(&rend_list,  SORT_POLYLIST_AVGZ);
  619. // apply camera to perspective transformation
  620. Camera_To_Perspective_RENDERLIST4DV2(&rend_list, &cam);
  621. // apply screen transform
  622. Perspective_To_Screen_RENDERLIST4DV2(&rend_list, &cam);
  623. // lock the back buffer
  624. DDraw_Lock_Back_Surface();
  625. // reset number of polys rendered
  626. debug_polys_rendered_per_frame = 0;
  627. // render the renderinglist
  628. if (wireframe_mode  == 0)
  629.    Draw_RENDERLIST4DV2_Wire16(&rend_list, back_buffer, back_lpitch);
  630. else
  631. if (wireframe_mode  == 1)
  632.    {
  633.    // initialize zbuffer to 16000 fixed point
  634.    Clear_Zbuffer(&zbuffer, (000 << FIXP16_SHIFT));
  635.    // set up rendering context
  636.    rc.attr         = RENDER_ATTR_INVZBUFFER  
  637.                      | (mip_mode==1 ? RENDER_ATTR_MIPMAP   : 0)
  638.                      | (bilinear_mode==1 ? RENDER_ATTR_BILERP : 0)
  639.                      | RENDER_ATTR_TEXTURE_PERSPECTIVE_AFFINE;
  640.    
  641.    rc.video_buffer   = back_buffer;
  642.    rc.lpitch         = back_lpitch;
  643.    rc.mip_dist       = mipdistance;
  644.    rc.zbuffer        = (UCHAR *)zbuffer.zbuffer;
  645.    rc.zpitch         = WINDOW_WIDTH*4;
  646.    rc.rend_list      = &rend_list;
  647.    rc.texture_dist   = 0;
  648.    rc.alpha_override = -1;
  649.    // render scene
  650.    Draw_RENDERLIST4DV2_RENDERCONTEXTV1_16(&rc);
  651.    
  652.    } // end if
  653. // unlock the back buffer
  654. DDraw_Unlock_Back_Surface();
  655. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, Spot=%d, BckFceRM [%s], Zsort [%s]",
  656.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  657.                                                                                  lights[AMBIENT_LIGHT_INDEX].state,
  658.                                                                                  lights[INFINITE_LIGHT_INDEX].state, 
  659.                                                                                  lights[POINT_LIGHT_INDEX].state,
  660.                                                                                  lights[SPOT_LIGHT2_INDEX].state,
  661.                                                                                  ((backface_mode == 1) ? "ON" : "OFF"),
  662.                                                                                  ((zsort_mode == 1) ? "ON" : "OFF"));                               
  663. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16, RGB(0,255,0), lpddsback);
  664. #if 1
  665. sprintf(work_string,"Bilinear Filtering [%s], Mipmapping [%s], Mip Distance [%5.2f]",((bilinear_mode == 1) ? "ON" : "OFF"),
  666.                                                                                   ((mip_mode == 1) ? "ON" : "OFF"), 
  667.                                                                                   mipdistance);
  668. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34, RGB(0,255,0), lpddsback);
  669. #endif
  670. // draw instructions
  671. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  672. // should we display help
  673. int text_y = 16;
  674. if (help_mode==1)
  675.     {
  676.     // draw help menu
  677.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  678.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  679.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  680.     Draw_Text_GDI("<S>..............Toggle spot light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  681.     Draw_Text_GDI("<N>..............Next object.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  682.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  683.     Draw_Text_GDI("<S>..............Toggle Z sorting.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  684.     Draw_Text_GDI("<B>..............Toggle Bilinear Filtering.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  685.     Draw_Text_GDI("<M>..............Toggle Mip Mapping.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  686.     Draw_Text_GDI("<1>, <2>.........Decrease/Increase Mipdistance.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  687.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  688.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  689.     } // end help
  690. sprintf(work_string,"Polys Rendered: %d, Polys lit: %d", debug_polys_rendered_per_frame, debug_polys_lit_per_frame);
  691. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-32, RGB(0,255,0), lpddsback);
  692. sprintf(work_string,"CAM [%5.2f, %5.2f, %5.2f]",  cam.pos.x, cam.pos.y, cam.pos.z);
  693. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-48, RGB(0,255,0), lpddsback);
  694. // flip the surfaces
  695. DDraw_Flip2();
  696. // sync to 30ish fps
  697. Wait_Clock(30);
  698. // check of user is trying to exit
  699. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  700.     {
  701.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  702.     } // end if
  703. // return success
  704. return(1);
  705.  
  706. } // end Game_Main
  707. //////////////////////////////////////////////////////////