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

游戏

开发平台:

Visual C++

  1. // DEMOII8_8.CPP - .ASC file loading demo with the works!
  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-6.CPP and the headers T3DLIB1-6.H
  6. // be in the working directory of the compiler
  7. // also this program uses a menu, thus you need to include the resource
  8. // template DEMOII8_8.RC to complile
  9. // INCLUDES ///////////////////////////////////////////////
  10. #define DEBUG_ON
  11. #define INITGUID       // make sure al the COM interfaces are available
  12.                        // instead of this you can include the .LIB file
  13.                        // DXGUID.LIB
  14. #define WIN32_LEAN_AND_MEAN  
  15. #include <windows.h>   // include important windows stuff
  16. #include <windowsx.h> 
  17. #include <mmsystem.h>
  18. #include <iostream.h> // include important C/C++ stuff
  19. #include <conio.h>
  20. #include <stdlib.h> 
  21. #include <malloc.h>
  22. #include <memory.h>
  23. #include <string.h>
  24. #include <stdarg.h>
  25. #include <stdio.h> 
  26. #include <math.h>
  27. #include <io.h>
  28. #include <fcntl.h>
  29. #include <ddraw.h>  // directX includes
  30. #include <dsound.h>
  31. #include <dmksctrl.h>
  32. #include <dmusici.h>
  33. #include <dmusicc.h>
  34. #include <dmusicf.h>
  35. #include <dinput.h>
  36. #include "T3DLIB1.h" // game library includes
  37. #include "T3DLIB2.h"
  38. #include "T3DLIB3.h"
  39. #include "T3DLIB4.h"
  40. #include "T3DLIB5.h"
  41. #include "T3DLIB6.h"
  42. #include "demoii8_8res.h" // include the resource header
  43. // DEFINES ////////////////////////////////////////////////
  44. // defines for windows interface
  45. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  46. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  47. #define WINDOW_WIDTH      800 // size of window
  48. #define WINDOW_HEIGHT     600
  49. #define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
  50.                                 // note: if windowed and not
  51.                                 // fullscreen then bitdepth must
  52.                                 // be same as system bitdepth
  53.                                 // also if 8-bit the a pallete
  54.                                 // is created and attached
  55. #define WINDOWED_APP      1     // 0 not windowed, 1 windowed
  56. // defines for the game universe
  57. #define UNIVERSE_RADIUS   4000
  58. #define POINT_SIZE        100
  59. #define NUM_POINTS_X      (2*UNIVERSE_RADIUS/POINT_SIZE)
  60. #define NUM_POINTS_Z      (2*UNIVERSE_RADIUS/POINT_SIZE)
  61. #define NUM_POINTS        (NUM_POINTS_X*NUM_POINTS_Z)
  62. // defines for objects
  63. #define NUM_TOWERS        64 // 96
  64. #define NUM_TANKS         32 // 24
  65. #define TANK_SPEED        15
  66. // create some constants for ease of access
  67. #define AMBIENT_LIGHT_INDEX   0 // ambient light index
  68. #define INFINITE_LIGHT_INDEX  1 // infinite light index
  69. #define POINT_LIGHT_INDEX     2 // point light index
  70. #define SPOT_LIGHT_INDEX      3 // spot light index
  71. // PROTOTYPES /////////////////////////////////////////////
  72. // game console
  73. int Game_Init(void *parms=NULL);
  74. int Game_Shutdown(void *parms=NULL);
  75. int Game_Main(void *parms=NULL);
  76. BOOL CALLBACK DialogProc(HWND hwnddlg,  // handle to dialog box
  77.                          UINT umsg,     // message
  78.                          WPARAM wparam, // first message parameter
  79.                          LPARAM lparam);  // second message parameter
  80. // GLOBALS ////////////////////////////////////////////////
  81. HWND main_window_handle           = NULL; // save the window handle
  82. HINSTANCE main_instance           = NULL; // save the instance
  83. char buffer[2048];                        // used to print text
  84. // initialize camera position and direction
  85. POINT4D  cam_pos    = {0,0,-250,1};
  86. POINT4D  cam_target = {0,0,0,1};
  87. VECTOR4D cam_dir    = {0,0,0,1};
  88. // all your initialization code goes here...
  89. VECTOR4D vscale={1.0,1.0,1.0,1}, 
  90.          vpos = {0,0,0,1}, 
  91.          vrot = {0,0,0,1};
  92. CAM4DV1        cam;       // the single camera
  93. OBJECT4DV1     obj_tower,    // used to hold the master tower
  94.                obj_tank,     // used to hold the master tank
  95.                obj_marker,   // the ground marker
  96.                obj_player;   // the player object             
  97. POINT4D        towers[NUM_TOWERS],
  98.                tanks[NUM_TANKS];
  99.                
  100. RENDERLIST4DV1 rend_list; // the render list
  101. // state variables for different rendering modes and help
  102. int wireframe_mode = -1;
  103. int backface_mode  = 1;
  104. int lighting_mode  = 1;
  105. int help_mode      = 1;
  106. int zsort_mode     = 1;
  107. // controlled during load dialog
  108. int swapyz   = 0,
  109.     iwinding = 0;
  110. char ascfilename[256]; // holds file name when loader loads
  111. // FUNCTIONS //////////////////////////////////////////////
  112. LRESULT CALLBACK WindowProc(HWND hwnd, 
  113.     UINT msg, 
  114.                             WPARAM wparam, 
  115.                             LPARAM lparam)
  116. {
  117. // this is the main message handler of the system
  118. PAINTSTRUCT ps;    // used in WM_PAINT
  119. HDC hdc;    // handle to a device context
  120. // what is the message 
  121. switch(msg)
  122. {
  123. case WM_CREATE: 
  124.         {
  125. // do initialization stuff here
  126. return(0);
  127. } break;
  128.     case WM_PAINT:
  129.          {
  130.          // start painting
  131.          hdc = BeginPaint(hwnd,&ps);
  132.          // end painting
  133.          EndPaint(hwnd,&ps);
  134.          return(0);
  135.         } break;
  136.      case WM_COMMAND:
  137.          {
  138.           switch(LOWORD(wparam))
  139.                 {
  140.                 // handle the FILE menu
  141.                 case ID_FILE_EXIT:
  142.                      {
  143.                      // terminate window
  144.                      PostQuitMessage(0);
  145.                      } break;
  146.                 case ID_FILE_LOADASCFILE:
  147.                      {
  148.                      // call line input dialog
  149.                      if (DialogBox (main_instance, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DialogProc ) == IDOK )
  150.      {
  151.                        // load in default object
  152.                        VECTOR4D_INITXYZ(&vscale,5.00,5.00,5.00);
  153.  
  154.                        Load_OBJECT4DV1_3DSASC(&obj_player,ascfilename,  
  155.                                               &vscale, &vpos, &vrot, 
  156.                                               (swapyz*VERTEX_FLAGS_SWAP_YZ) | 
  157.                                               (iwinding*VERTEX_FLAGS_INVERT_WINDING_ORDER) );
  158.                        } // end if
  159.                      } break;
  160.                  case ID_LIGHTS_TOGGLEAMBIENTLIGHT:
  161.                      {
  162.                      // toggle ambient light
  163.                      if (lights[AMBIENT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  164.                         lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  165.                      else
  166.                         lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  167.                      } break;
  168.                  case ID_LIGHTS_TOGGLEPOINTLIGHT:
  169.                      {
  170.                      // toggle point light
  171.                      if (lights[POINT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  172.                         lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  173.                      else
  174.                         lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  175.                      } break;
  176.                  case ID_LIGHTS_TOGGLESPOTLIGHT:
  177.                      {
  178.                      // toggle spot light
  179.                      if (lights[SPOT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  180.                         lights[SPOT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  181.                      else
  182.                         lights[SPOT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  183.                      } break;
  184.                  case ID_LIGHTS_TOGGLEINFINITELIGHT:
  185.                      {
  186.                      // toggle spot light
  187.                      if (lights[INFINITE_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  188.                         lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  189.                      else
  190.                         lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  191.                      } break;
  192.                  case ID_ZSORTING_ENABLE:
  193.                       {
  194.                       zsort_mode = 1;
  195.                       } break;
  196.                  case ID_ZSORTING_DISABLE:
  197.                       {
  198.                       zsort_mode = -1;
  199.                       } break;
  200.                  case ID_BACKFACEREMOVAL_ENABLE:
  201.                       {
  202.                       backface_mode = 1;
  203.                       } break;
  204.                  case ID_BACKFACEREMOVAL_DISABLE:
  205.                       {
  206.                       backface_mode = -1;
  207.                       } break;
  208.                  case ID_RENDERINGMODE_SOLID:
  209.                       {
  210.                       // toggle wireframe mode
  211.                       wireframe_mode = -1;
  212.                       } break;
  213.                  case ID_RENDERINGMODE_WIREFRAME:
  214.                       {
  215.                       // toggle wireframe mode
  216.                       wireframe_mode = 1;
  217.                       } break;
  218.                 // handle the HELP menu
  219.                 case ID_HELP_ABOUT:                 
  220.                      {
  221.                      sprintf(buffer, "Polys: %d, Vertices: %d", obj_player.num_polys, obj_player.num_vertices);
  222.                      //  pop up a message box
  223.                      MessageBox(hwnd, buffer, 
  224.                                ".ASC File Loader Demo",
  225.                                 MB_OK | MB_ICONEXCLAMATION);
  226.                      } break;
  227.                 default: break;
  228.              } // end switch wparam
  229.           } break; // end WM_COMMAND
  230. case WM_DESTROY: 
  231. {
  232. // kill the application
  233. PostQuitMessage(0);
  234. return(0);
  235. } break;
  236. default:break;
  237.     } // end switch
  238. // process any messages that we didn't take care of 
  239. return (DefWindowProc(hwnd, msg, wparam, lparam));
  240. } // end WinProc
  241. ///////////////////////////////////////////////////////////
  242. BOOL CALLBACK DialogProc(HWND hwnddlg,  // handle to dialog box
  243.                          UINT umsg,     // message
  244.                          WPARAM wparam, // first message parameter
  245.                          LPARAM lparam)  // second message parameter
  246. {
  247. // dialog handler for the line input
  248. // get the handle to the text edit box
  249. HWND htextedit = GetDlgItem (hwnddlg, IDC_EDIT1);
  250. HWND hswapyz   = GetDlgItem(hwnddlg, IDC_SWAPYZ);
  251. HWND hiwinding = GetDlgItem(hwnddlg, IDC_IWINDING);
  252. switch (umsg)
  253.     {
  254. case WM_INITDIALOG:
  255. {
  256. SetFocus ( htextedit );
  257. } break;
  258. case WM_COMMAND:
  259.  {
  260.  switch (LOWORD(wparam))
  261.                 {
  262.                 case IDOK:
  263.     {
  264.  int linelength = SendMessage ( htextedit, EM_LINELENGTH, 0, 0 );
  265.    ascfilename[0] = (unsigned char )255;
  266.  SendMessage (htextedit, EM_GETLINE, 0, (LPARAM) (LPCSTR) ascfilename );
  267.  ascfilename[linelength] = 0;
  268.                      // set swap and winding order globals
  269.                      swapyz   = (int) SendMessage(hswapyz, BM_GETCHECK, 0,0);
  270.                      iwinding = (int) SendMessage(hiwinding, BM_GETCHECK, 0,0);
  271.  EndDialog (hwnddlg, IDOK );
  272.          return TRUE;
  273.  } break;
  274. case IDCANCEL:
  275. {
  276. EndDialog ( hwnddlg, IDCANCEL );
  277. return TRUE;
  278.        } break;
  279.       default: break;
  280. } // end switch 
  281.        
  282.           } break;
  283. } // end switch
  284. // return normal
  285. return 0;
  286. } // end DialogProc
  287. // WINMAIN ////////////////////////////////////////////////
  288. int WINAPI WinMain( HINSTANCE hinstance,
  289. HINSTANCE hprevinstance,
  290. LPSTR lpcmdline,
  291. int ncmdshow)
  292. {
  293. // this is the winmain function
  294. WNDCLASS winclass; // this will hold the class we create
  295. HWND  hwnd; // generic window handle
  296. MSG  msg; // generic message
  297. HDC      hdc;       // generic dc
  298. PAINTSTRUCT ps;     // generic paintstruct
  299. // first fill in the window class stucture
  300. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  301.                           CS_HREDRAW | CS_VREDRAW;
  302. winclass.lpfnWndProc = WindowProc;
  303. winclass.cbClsExtra = 0;
  304. winclass.cbWndExtra = 0;
  305. winclass.hInstance = hinstance;
  306. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  307. winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  308. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  309. winclass.lpszMenuName = MAKEINTRESOURCE(IDR_ASCMENU);
  310. winclass.lpszClassName = WINDOW_CLASS_NAME;
  311. // register the window class
  312. if (!RegisterClass(&winclass))
  313. return(0);
  314. // create the window, note the test to see if WINDOWED_APP is
  315. // true to select the appropriate window flags
  316. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  317.   WINDOW_TITLE,  // title
  318.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  319.     0,0,    // x,y
  320.   WINDOW_WIDTH,  // width
  321.                           WINDOW_HEIGHT, // height
  322.   NULL,    // handle to parent 
  323.   NULL,    // handle to menu
  324.   hinstance,// instance
  325.   NULL))) // creation parms
  326. return(0);
  327. // save the window handle and instance in a global
  328. main_window_handle = hwnd;
  329. main_instance      = hinstance;
  330. // resize the window so that client is really width x height
  331. if (WINDOWED_APP)
  332. {
  333. // now resize the window, so the client area is the actual size requested
  334. // since there may be borders and controls if this is going to be a windowed app
  335. // if the app is not windowed then it won't matter
  336. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  337. // make the call to adjust window_rect
  338. AdjustWindowRectEx(&window_rect,
  339.      GetWindowStyle(main_window_handle),
  340.      GetMenu(main_window_handle) != NULL,  
  341.      GetWindowExStyle(main_window_handle));
  342. // save the global client offsets, they are needed in DDraw_Flip()
  343. window_client_x0 = -window_rect.left;
  344. window_client_y0 = -window_rect.top;
  345. // now resize the window with a call to MoveWindow()
  346. MoveWindow(main_window_handle,
  347.            0, // x position
  348.            0, // y position
  349.            window_rect.right - window_rect.left, // width
  350.            window_rect.bottom - window_rect.top, // height
  351.            FALSE);
  352. // show the window, so there's no garbage on first render
  353. ShowWindow(main_window_handle, SW_SHOW);
  354. } // end if windowed
  355. // perform all game console specific initialization
  356. Game_Init();
  357. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  358. // if it causes your system to crash
  359. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  360. // enter main event loop
  361. while(1)
  362. {
  363. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  364. // test if this is a quit
  365.         if (msg.message == WM_QUIT)
  366.            break;
  367. // translate any accelerator keys
  368. TranslateMessage(&msg);
  369. // send the message to the window proc
  370. DispatchMessage(&msg);
  371. } // end if
  372.     
  373.     // main game processing goes here
  374.     Game_Main();
  375. } // end while
  376. // shutdown game and release all resources
  377. Game_Shutdown();
  378. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  379. // if it causes your system to crash
  380. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  381. // return to Windows like this
  382. return(msg.wParam);
  383. } // end WinMain
  384. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  385. int Game_Init(void *parms)
  386. {
  387. // this function is where you do all the initialization 
  388. // for your game
  389. int index; // looping var
  390. // start up DirectDraw (replace the parms as you desire)
  391. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  392. // initialize directinput
  393. DInput_Init();
  394. // acquire the keyboard 
  395. DInput_Init_Keyboard();
  396. // add calls to acquire other directinput devices here...
  397. // initialize directsound and directmusic
  398. DSound_Init();
  399. DMusic_Init();
  400. // hide the mouse
  401. if (!WINDOWED_APP)
  402.     ShowCursor(FALSE);
  403. // seed random number generator
  404. srand(Start_Clock());
  405. Open_Error_File("ERROR.TXT");
  406. // initialize math engine
  407. Build_Sin_Cos_Tables();
  408. // initialize the camera with 90 FOV, normalized coordinates
  409. Init_CAM4DV1(&cam,      // the camera object
  410.              CAM_MODEL_EULER, // the euler model
  411.              &cam_pos,  // initial camera position
  412.              &cam_dir,  // initial camera angles
  413.              &cam_target,      // no target
  414.              200.0,      // near and far clipping planes
  415.              12000.0,
  416.              120.0,      // field of view in degrees
  417.              WINDOW_WIDTH,   // size of final screen viewport
  418.              WINDOW_HEIGHT);
  419. // load in default object
  420. VECTOR4D_INITXYZ(&vscale,5.00,5.00,5.00);
  421. Load_OBJECT4DV1_3DSASC(&obj_player,"sphere01.asc",  
  422.                        &vscale, &vpos, &vrot, 
  423.                        VERTEX_FLAGS_INVERT_WINDING_ORDER | VERTEX_FLAGS_SWAP_YZ );
  424. // set up lights
  425. Reset_Lights_LIGHTV1();
  426. // create some working colors
  427. RGBAV1 white, gray, black, red, green, blue;
  428. white.rgba = _RGBA32BIT(255,255,255,0);
  429. gray.rgba  = _RGBA32BIT(100,100,100,0);
  430. black.rgba = _RGBA32BIT(0,0,0,0);
  431. red.rgba   = _RGBA32BIT(255,0,0,0);
  432. green.rgba = _RGBA32BIT(0,255,0,0);
  433. blue.rgba  = _RGBA32BIT(0,0,255,0);
  434. // ambient light
  435. Init_Light_LIGHTV1(AMBIENT_LIGHT_INDEX,   
  436.                    LIGHTV1_STATE_ON,      // turn the light on
  437.                    LIGHTV1_ATTR_AMBIENT,  // ambient light type
  438.                    gray, black, black,    // color for ambient term only
  439.                    NULL, NULL,            // no need for pos or dir
  440.                    0,0,0,                 // no need for attenuation
  441.                    0,0,0);                // spotlight info NA
  442. VECTOR4D dlight_dir = {-1,0,-1,0};
  443. // directional light
  444. Init_Light_LIGHTV1(INFINITE_LIGHT_INDEX,  
  445.                    LIGHTV1_STATE_ON,      // turn the light on
  446.                    LIGHTV1_ATTR_INFINITE, // infinite light type
  447.                    black, gray, black,    // color for diffuse term only
  448.                    NULL, &dlight_dir,     // need direction only
  449.                    0,0,0,                 // no need for attenuation
  450.                    0,0,0);                // spotlight info NA
  451. VECTOR4D plight_pos = {0,200,0,0};
  452. // point light
  453. Init_Light_LIGHTV1(POINT_LIGHT_INDEX,
  454.                    LIGHTV1_STATE_ON,      // turn the light on
  455.                    LIGHTV1_ATTR_POINT,    // pointlight type
  456.                    black, green, black,   // color for diffuse term only
  457.                    &plight_pos, NULL,     // need pos only
  458.                    0,.001,0,              // linear attenuation only
  459.                    0,0,1);                // spotlight info NA
  460. VECTOR4D slight_pos = {0,200,0,0};
  461. VECTOR4D slight_dir = {-1,0,-1,0};
  462. // spot light
  463. Init_Light_LIGHTV1(SPOT_LIGHT_INDEX,
  464.                    LIGHTV1_STATE_ON,         // turn the light on
  465.                    LIGHTV1_ATTR_SPOTLIGHT2,  // spot light type 2
  466.                    black, red, black,      // color for diffuse term only
  467.                    &slight_pos, &slight_dir, // need pos only
  468.                    0,.001,0,                 // linear attenuation only
  469.                    0,0,1);    
  470. // create lookup for lighting engine
  471. RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565,  // format we want to build table for
  472.                                   palette,             // source palette
  473.                                   rgblookup);          // lookup table
  474. // return success
  475. return(1);
  476. } // end Game_Init
  477. ///////////////////////////////////////////////////////////
  478. int Game_Shutdown(void *parms)
  479. {
  480. // this function is where you shutdown your game and
  481. // release all resources that you allocated
  482. // shut everything down
  483. // release all your resources created for the game here....
  484. // now directsound
  485. DSound_Stop_All_Sounds();
  486. DSound_Delete_All_Sounds();
  487. DSound_Shutdown();
  488. // directmusic
  489. DMusic_Delete_All_MIDI();
  490. DMusic_Shutdown();
  491. // shut down directinput
  492. DInput_Release_Keyboard();
  493. // shutdown directinput
  494. DInput_Shutdown();
  495. // shutdown directdraw last
  496. DDraw_Shutdown();
  497. Close_Error_File();
  498. // return success
  499. return(1);
  500. } // end Game_Shutdown
  501. //////////////////////////////////////////////////////////
  502. int Game_Main(void *parms)
  503. {
  504. // this is the workhorse of your game it will be called
  505. // continuously in real-time this is like main() in C
  506. // all the calls for you game go here!
  507. static MATRIX4X4 mrot;   // general rotation matrix
  508. // these are used to create a circling camera
  509. static float view_angle = 0; 
  510. static float camera_distance = 6000;
  511. static VECTOR4D pos = {0,0,0,0};
  512. char work_string[256]; // temp string
  513. int index; // looping var
  514. // start the timing clock
  515. Start_Clock();
  516. // clear the drawing surface 
  517. DDraw_Fill_Surface(lpddsback, 0);
  518. // read keyboard and other devices here
  519. DInput_Read_Keyboard();
  520. // game logic here...
  521. // reset the render list
  522. Reset_RENDERLIST4DV1(&rend_list);
  523. // allow user to move camera
  524. // forward/backward
  525. if (keyboard_state[DIK_UP])
  526.    {
  527.    // move forward
  528.    cam.pos.x += 5*Fast_Sin(cam.dir.y);
  529.    cam.pos.z += 5*Fast_Cos(cam.dir.y);
  530.    } // end if
  531. if (keyboard_state[DIK_DOWN])
  532.    {
  533.    // move backward
  534.    cam.pos.x -= 5*Fast_Sin(cam.dir.y);
  535.    cam.pos.z -= 5*Fast_Cos(cam.dir.y);
  536.    } // end if
  537. // rotate object
  538. if (keyboard_state[DIK_RIGHT])
  539.    {
  540.    obj_player.dir.y+=2;
  541.    } // end if
  542. if (keyboard_state[DIK_LEFT])
  543.    {
  544.    obj_player.dir.y-=2;
  545.    } // end if
  546. if (obj_player.dir.y > 360)
  547.     obj_player.dir.y-=360;
  548. if (obj_player.dir.y < 0)
  549.     obj_player.dir.y+=360;
  550. // ambient rotation
  551. obj_player.dir.y++;
  552. // scale object
  553. if (keyboard_state[DIK_PGUP])
  554.    {
  555. VECTOR4D_INITXYZ(&vscale,1.1,1.1,1.1);  
  556.    Scale_OBJECT4DV1(&obj_player, &vscale);
  557.    } // end if
  558. if (keyboard_state[DIK_PGDN])
  559.    {
  560.    VECTOR4D_INITXYZ(&vscale,.9,.9,.9);
  561.    Scale_OBJECT4DV1(&obj_player, &vscale);
  562.    } // end if
  563. // modes and lights
  564. // wireframe mode
  565. if (keyboard_state[DIK_W])
  566.    {
  567.    // toggle wireframe mode
  568.    wireframe_mode = -wireframe_mode;
  569.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  570.    } // end if
  571. // backface removal
  572. if (keyboard_state[DIK_B])
  573.    {
  574.    // toggle backface removal
  575.    backface_mode = -backface_mode;
  576.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  577.    } // end if
  578. // lighting
  579. if (keyboard_state[DIK_L])
  580.    {
  581.    // toggle lighting engine completely
  582.    lighting_mode = -lighting_mode;
  583.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  584.    } // end if
  585. // toggle ambient light
  586. if (keyboard_state[DIK_A])
  587.    {
  588.    // toggle ambient light
  589.    if (lights[AMBIENT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  590.       lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  591.    else
  592.       lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  593.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  594.    } // end if
  595. // toggle infinite light
  596. if (keyboard_state[DIK_I])
  597.    {
  598.    // toggle ambient light
  599.    if (lights[INFINITE_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  600.       lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  601.    else
  602.       lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  603.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  604.    } // end if
  605. // toggle point light
  606. if (keyboard_state[DIK_P])
  607.    {
  608.    // toggle point light
  609.    if (lights[POINT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  610.       lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  611.    else
  612.       lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  613.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  614.    } // end if
  615. // toggle spot light
  616. if (keyboard_state[DIK_S])
  617.    {
  618.    // toggle spot light
  619.    if (lights[SPOT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  620.       lights[SPOT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  621.    else
  622.       lights[SPOT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  623.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  624.    } // end if
  625. // help menu
  626. if (keyboard_state[DIK_H])
  627.    {
  628.    // toggle help menu 
  629.    help_mode = -help_mode;
  630.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  631.    } // end if
  632. // z-sorting
  633. if (keyboard_state[DIK_Z])
  634.    {
  635.    // toggle z sorting
  636.    zsort_mode = -zsort_mode;
  637.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  638.    } // end if
  639. static float plight_ang = 0, slight_ang = 0; // angles for light motion
  640. // move point light source in ellipse around game world
  641. lights[POINT_LIGHT_INDEX].pos.x = 300*Fast_Cos(plight_ang);
  642. lights[POINT_LIGHT_INDEX].pos.y = 200;
  643. lights[POINT_LIGHT_INDEX].pos.z = 300*Fast_Sin(plight_ang);
  644. if ((plight_ang+=3) > 360)
  645.     plight_ang = 0;
  646. // move spot light source in ellipse around game world
  647. lights[SPOT_LIGHT_INDEX].pos.x = 200*Fast_Cos(slight_ang);
  648. lights[SPOT_LIGHT_INDEX].pos.y = 200;
  649. lights[SPOT_LIGHT_INDEX].pos.z = 200*Fast_Sin(slight_ang);
  650. if ((slight_ang-=5) < 0)
  651.     slight_ang = 360;
  652. /////////////////////////////////////////
  653. // generate camera matrix
  654. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  655. // insert the player into the world
  656. // reset the object (this only matters for backface and object removal)
  657. Reset_OBJECT4DV1(&obj_player);
  658. // generate rotation matrix around y axis
  659. Build_XYZ_Rotation_MATRIX4X4(0, obj_player.dir.y, 0, &mrot);
  660. // rotate the local coords of the object
  661. Transform_OBJECT4DV1(&obj_player, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  662. // perform world transform
  663. Model_To_World_OBJECT4DV1(&obj_player, TRANSFORM_TRANS_ONLY);
  664.            // perform lighting
  665.            if (lighting_mode==1)
  666.               Light_OBJECT4DV1_World16(&obj_player, &cam, lights, 4);   
  667.            // insert the object into render list
  668.            Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_player,0,lighting_mode);
  669. // insert the object into render list
  670. //Insert_OBJECT4DV1_RENDERLIST4DV12(&rend_list, &obj_player,0, 0);
  671. //////////////////////////////////////
  672. // remove backfaces
  673. if (backface_mode==1)
  674.    Remove_Backfaces_RENDERLIST4DV1(&rend_list, &cam);
  675. // light scene all at once 
  676. //if (lighting_mode==1)
  677. //   Light_RENDERLIST4DV1_World16(&rend_list, &cam, lights, 4);
  678. // apply world to camera transform
  679. World_To_Camera_RENDERLIST4DV1(&rend_list, &cam);
  680. // sort the polygon list (hurry up!)
  681. if (zsort_mode == 1)
  682.    Sort_RENDERLIST4DV1(&rend_list,  SORT_POLYLIST_AVGZ);
  683. // apply camera to perspective transformation
  684. Camera_To_Perspective_RENDERLIST4DV1(&rend_list, &cam);
  685. // apply screen transform
  686. Perspective_To_Screen_RENDERLIST4DV1(&rend_list, &cam);
  687. sprintf(work_string,"pos:[%f, %f, %f] heading:[%f] elev:[%f], polys[%d]", 
  688.         cam.pos.x, cam.pos.y, cam.pos.z, cam.dir.y, cam.dir.x, debug_polys_rendered_per_frame); 
  689. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-20, RGB(0,255,0), lpddsback);
  690. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, Spot=%d | Zsort [%s], BckFceRM [%s]", 
  691.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  692.                                                                                  lights[AMBIENT_LIGHT_INDEX].state,
  693.                                                                                  lights[INFINITE_LIGHT_INDEX].state, 
  694.                                                                                  lights[POINT_LIGHT_INDEX].state,
  695.                                                                                  lights[SPOT_LIGHT_INDEX].state,
  696.                                                                                  ((zsort_mode == 1) ? "ON" : "OFF"),
  697.                                                                                  ((backface_mode == 1) ? "ON" : "OFF"));
  698. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34, RGB(0,255,0), lpddsback);
  699. // draw instructions
  700. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  701. // should we display help
  702. int text_y = 16;
  703. if (help_mode==1)
  704.     {
  705.     // draw help menu
  706.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  707.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  708.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  709.     Draw_Text_GDI("<S>..............Toggle spot light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  710.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  711.     Draw_Text_GDI("<B>..............Toggle backface removal.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  712.     Draw_Text_GDI("<RIGHT ARROW>....Rotate object right.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  713.     Draw_Text_GDI("<LEFT ARROW>.....Rotate object left.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  714.     Draw_Text_GDI("<UP ARROW>.......Move camera forward.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  715.     Draw_Text_GDI("<DOWN ARROW>.....Move camera backward.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  716.     Draw_Text_GDI("<PG UP>..........Scale object down", 0, text_y+=12, RGB(255,255,255), lpddsback);
  717.     Draw_Text_GDI("<PG DWN>.........Scale object down", 0, text_y+=12, RGB(255,255,255), lpddsback);
  718.     Draw_Text_GDI("<SPACE BAR>......Turbo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  719.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  720.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  721.     } // end help
  722. // lock the back buffer
  723. DDraw_Lock_Back_Surface();
  724. // reset number of polys rendered
  725. debug_polys_rendered_per_frame = 0;
  726. // render the object
  727. if (wireframe_mode  == 1)
  728.    Draw_RENDERLIST4DV1_Wire16(&rend_list, back_buffer, back_lpitch);
  729. else
  730.    Draw_RENDERLIST4DV1_Solid16(&rend_list, back_buffer, back_lpitch);
  731. // unlock the back buffer
  732. DDraw_Unlock_Back_Surface();
  733. // flip the surfaces
  734. DDraw_Flip();
  735. // sync to 30ish fps
  736. Wait_Clock(30);
  737. // check of user is trying to exit
  738. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  739.     {
  740.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  741.     } // end if
  742. // return success
  743. return(1);
  744.  
  745. } // end Game_Main
  746. //////////////////////////////////////////////////////////