home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP08 / VWALK1.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  7.3 KB  |  281 lines

  1. //
  2. // File name: Vwalk1.CPP
  3. //
  4. // Description: The main file for a virtual walk-through using 
  5. //              solid colored polygons
  6. //
  7. // Author: John De Goes
  8. //
  9. // Project: Cutting Edge 3D Game Programming
  10. //
  11.  
  12. // ------------------------------------------------------------
  13. // | Global include files:                                    |
  14. // ------------------------------------------------------------
  15.  
  16. #include <Dos.H>
  17. #include <Math.H>
  18. #include <Conio.H>
  19. #include <Time.H>
  20. #include <Stdio.H>
  21. #include <Iostream.H>
  22.  
  23. // ------------------------------------------------------------
  24. // | Local include files:                                     |
  25. // ------------------------------------------------------------
  26.  
  27. #include "32Bit.HPP"
  28. #include "MouseLib.HPP"
  29. #include "Palette.HPP"
  30.  
  31. #include "3Dclass.HPP"
  32. #include "LineType.HPP"
  33.  
  34. // ------------------------------------------------------------
  35. // | Global variables/constants:                              |
  36. // ------------------------------------------------------------
  37.  
  38. // Create an image of a cross for the mouse:
  39. unsigned char Cursor [ ] = {
  40.                      0, 0, 0, 0, 0,17,17, 0, 0, 0, 0, 0,
  41.                      0, 0, 0, 0, 0,18,18, 0, 0, 0, 0, 0,
  42.                      0, 0, 0, 0, 0,19,19, 0, 0, 0, 0, 0,
  43.                      0, 0, 0, 0, 0,20,20, 0, 0, 0, 0, 0,
  44.                      0, 0, 0, 0, 0,21,21, 0, 0, 0, 0, 0,
  45.                      17,18,19,20,21,22,22,21,20,19,18,17,
  46.                      17,18,19,20,21,22,22,21,20,19,18,17,
  47.                      0, 0, 0, 0, 0,21,21, 0, 0, 0, 0, 0,
  48.                      0, 0, 0, 0, 0,20,20, 0, 0, 0, 0, 0,
  49.                      0, 0, 0, 0, 0,19,19, 0, 0, 0, 0, 0,
  50.                      0, 0, 0, 0, 0,18,18, 0, 0, 0, 0, 0,
  51.                      0, 0, 0, 0, 0,17,17, 0, 0, 0, 0, 0,
  52.                      };
  53.  
  54. // ------------------------------------------------------------
  55. // | Local structs/classes:                                   |
  56. // ------------------------------------------------------------
  57.  
  58. struct View {
  59. int XRot, YRot, ZRot;
  60. double ZPos;
  61. View () { XRot = YRot = ZRot = 0; ZPos = 0.0F; }
  62. void Clear () { XRot = YRot = ZRot = 0; ZPos = 0.0F; }
  63. };
  64.  
  65. // ------------------------------------------------------------
  66. // | Function section:                                        |
  67. // ------------------------------------------------------------
  68.  
  69. int InitZBuffer ()
  70.   {
  71.   // Allocate memory for Z-buffer:
  72.   if ( ( ZBuffer = new long [ 64000 ] ) == 0 )
  73.      return 0;
  74.   return 1;
  75.   }
  76.  
  77. int DestZBuffer ()
  78.   {
  79.   // Deallocate Z-buffer's memory:
  80.   delete [] ZBuffer;
  81.   if ( ZBuffer )
  82.      return 0;
  83.   return 1;
  84.   }
  85.  
  86. void ClearBuffer ()
  87.    {
  88.    // Clear the Z-buffer:
  89.    long *ZPtr = ZBuffer;
  90.    for ( unsigned int Index = 0; Index < 6400; Index++ )
  91.        {
  92.        *ZPtr++ = 0;
  93.        *ZPtr++ = 0;
  94.  
  95.        *ZPtr++ = 0;
  96.        *ZPtr++ = 0;
  97.  
  98.        *ZPtr++ = 0;
  99.        *ZPtr++ = 0;
  100.  
  101.        *ZPtr++ = 0;
  102.        *ZPtr++ = 0;
  103.  
  104.        *ZPtr++ = 0;
  105.        *ZPtr++ = 0;
  106.        }
  107.    }
  108.  
  109. // Create a virtual track-ball:
  110. void UpdatePos ( MousePtr &Mouse, View &V )
  111.    {
  112.    // Get the mouse coordinates and map them to a suitable
  113.    // range:
  114.    int X = ( Mouse.GetX () - 50 ) >> 2;
  115.    int Y = ( Mouse.GetY () - 50 ) >> 2;
  116.  
  117.    // If the left button is pressed:
  118.    if ( ( Mouse.GetLb () ) && ( !Mouse.GetRb () ) )
  119.       {
  120.       // Do the translations and rotations:
  121.       V.ZPos -= ( double ) Y * 3.0F;
  122.       V.YRot += X;
  123.       }
  124.  
  125.    // Else if the right button is pressed:   
  126.    else if ( ( Mouse.GetRb () ) && ( ! Mouse.GetLb () ) )
  127.            {
  128.            V.XRot += Y;
  129.            V.ZRot -= X;
  130.            }
  131.    }
  132.  
  133. double RunWorld ( unsigned char *VidMem, unsigned char *VidBuf,
  134.                   PanelObject *World )
  135.    {
  136.    // Enter the main loop, updating view as necessary:
  137.  
  138.    VidMem; VidBuf; World;
  139.    MousePtr Mouse;
  140.    Matrix3D M; View V;
  141.    long QuitFlag = 0, StartTime, EndTime, FrameCount = 0, MaxWait;
  142.    double FramesPerSecond;
  143.    ZTrans = 0;
  144.  
  145.    // Initialize the mouse driver:
  146.    Mouse.Init ();
  147.  
  148.    // Hide the pointer:   
  149.    Mouse.Hide ();
  150.  
  151.    // Re-map the cursor's coordinates:
  152.    Mouse.MappingRange ( 100, 100 );
  153.  
  154.    // Clip the cursor to a rectangular region:
  155.    Mouse.Clip ( 5, 5, 95, 95 );
  156.  
  157.    // Give the cursor a face-lift:
  158.    Mouse.ChangeCursor ( Cursor, 12, 12 );
  159.  
  160.    // Allocate memory for Z-buffer:
  161.    InitZBuffer ();
  162.  
  163.    // Raise the viewer:
  164.    M.Initialize ();
  165.    M.Translate ( 0, -600, 0 );
  166.    ClearBuffer ();
  167.    World->Display ( M, VidBuf );
  168.    MaxWait = ( long ) pow ( 2, 31 - ZSTEP_PREC  );
  169.  
  170.    StartTime = clock ();
  171.  
  172.    // Loop until ESC pressed:
  173.    while ( !QuitFlag )
  174.          {
  175.          UpdatePos ( Mouse, V );
  176.  
  177.          M.Translate ( 0, 0, -V.ZPos );
  178.          M.Rotate ( -V.XRot, -V.YRot, -V.ZRot );
  179.          V.Clear ();
  180.  
  181.          setmem ( VidBuf, 64000, 1 );
  182.  
  183.          // Clear the Z-buffer (if necessary):
  184.          ZTrans += ( 1 << ZSTEP_PREC );
  185.          if ( ( FrameCount % MaxWait ) == 0 )
  186.             {
  187.             ZTrans = 0;
  188.             ClearBuffer ();
  189.             }
  190.  
  191.          World->Display ( M, VidBuf );
  192.          Mouse.Display ( VidBuf );
  193.  
  194.          memmove ( VidMem, VidBuf, 64000 );
  195.          if ( kbhit () )
  196.             {
  197.             if ( getch () == 27 )
  198.                QuitFlag = 1;
  199.             }
  200.          ++FrameCount;
  201.          }
  202.    EndTime = clock ();
  203.  
  204.    DestZBuffer ();
  205.  
  206.    // Calculate the frames per second:
  207.    FramesPerSecond = ( double ) FrameCount * 
  208.                      ( double )  CLK_TCK /
  209.                      ( double ) ( EndTime - StartTime ); 
  210.    return FramesPerSecond;
  211.    }
  212.  
  213. void SetPalette ()
  214.    {
  215.    short int Scale;
  216.    for ( short int N = 0; N < 256; N++ )
  217.        {
  218.        Scale = N >> 2;
  219.        SetPalReg ( N, Scale, Scale, Scale );
  220.        }
  221.    }
  222.  
  223. // ------------------------------------------------------------
  224. // | Program entry:                                           |
  225. // ------------------------------------------------------------
  226.  
  227. void main ( int ArgCount, char *Arg[] )
  228.    {
  229.    // Declare/initialize variables and allocate memory:
  230.    unsigned char *VidMem, *VidBuf;
  231.    VidMem = VideoAddress ();
  232.    double FramesPerSecond;
  233.  
  234.    // Allocate buffer memory - abort if error:
  235.    if ( ( VidBuf = new unsigned char [ 64000 ] ) == NULL )
  236.       return;
  237.  
  238.    // Allocate memory for a panel object:
  239.    PanelObject *World = new PanelObject;
  240.  
  241.    if ( ( ArgCount > 1 ) && 
  242.         ( !strcmp ( strupr ( Arg [ 1 ] ), "WRITEBINARY" ) ) )
  243.       {
  244.       World->LoadDXF ( "Test.DXF" );
  245.       World->WriteBIN ( "Test.BIN" );
  246.       }
  247.  
  248.    else {
  249.         World->ReadBIN ( "Test.BIN" );
  250.  
  251.         // Set video mode:
  252.         SetVideo ( 0x13 );
  253.  
  254.         // Set the palette to shades of gray:
  255.         SetPalette ();
  256.  
  257.         //CommitStack ();
  258.  
  259.         // Enter main loop:
  260.         FramesPerSecond = RunWorld ( VidMem, VidBuf, World );
  261.  
  262.         // Set video mode:
  263.         SetVideo ( 0x03 );
  264.  
  265.         // Display FPS:
  266.         cout << "\nFrames per second: " << FramesPerSecond;
  267.         }
  268.  
  269.    // Deallocate memory/prepare to shut down:
  270.    cout << "\nDeallocating memory...";
  271.    delete [] VidBuf;
  272.       
  273.    // Deallocate memory for panel object:
  274.    delete World;
  275.  
  276.    cout << "\nReturning control to OS...";
  277.  
  278.    // Exit program:
  279.    return;
  280.    }
  281.