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

游戏

开发平台:

Visual C++

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