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

游戏

开发平台:

Visual C++

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