home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP11 / COLLIDE / COLLIDE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-23  |  10.6 KB  |  388 lines

  1. //
  2. // File name: Collide.CPP
  3. //
  4. // Description: The main file for a collision detection program
  5. //
  6. // Author: John De Goes
  7. //
  8. // Project: Cutting Edge 3D Game Programming
  9. //
  10.  
  11. // ------------------------------------------------------------
  12. // | Global include files:                                    |
  13. // ------------------------------------------------------------
  14.  
  15. #include <Dos.H>
  16. #include <Math.H>
  17. #include <Conio.H>
  18. #include <Time.H>
  19. #include <Stdio.H>
  20. #include <Windows.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. #include "TextType.HPP"
  31. #include "3Dclass.HPP"
  32. #include "LineType.HPP"
  33. #include "PalShade.HPP"
  34.  
  35. // ------------------------------------------------------------
  36. // | Global variables/constants:                              |
  37. // ------------------------------------------------------------
  38.  
  39. // Create an image of a cross for the mouse:
  40. unsigned char Cursor [ ] = {
  41.                      0, 0, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
  42.                      0, 0, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
  43.                      0, 0, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
  44.                      0, 0, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
  45.                      0, 0, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
  46.                      10,10,10,10,10,11,11,10,10,10,10,10,
  47.                      10,10,10,10,10,11,11,10,10,10,10,10,
  48.                      0, 0, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
  49.                      0, 0, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
  50.                      0, 0, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
  51.                      0, 0, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
  52.                      0, 0, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
  53.                      };
  54.  
  55. // ------------------------------------------------------------
  56. // | Local structs/classes:                                   |
  57. // ------------------------------------------------------------
  58.  
  59. struct View {
  60. int XRot, YRot, ZRot;
  61. float XPos, YPos, ZPos;
  62. View () { XRot = YRot = ZRot = 0;
  63.           XPos = YPos = ZPos = 0.0F; }
  64. void Clear () { XRot = YRot = ZRot = 0; 
  65.                 XPos = YPos = ZPos = 0.0F; }
  66. };
  67.  
  68. // ------------------------------------------------------------
  69. // | Function section:                                        |
  70. // ------------------------------------------------------------
  71.  
  72. int InitZBuffer ()
  73.   {
  74.   // Allocate memory for Z-buffer:
  75.   if ( ( ZBuffer = new long [ 64000 ] ) == 0 )
  76.      return 0;
  77.   return 1;
  78.   }
  79.  
  80. int DestZBuffer ()
  81.   {
  82.   // Deallocate Z-buffer's memory:
  83.   delete [] ZBuffer;
  84.   if ( ZBuffer )
  85.      return 0;
  86.   return 1;
  87.   }
  88.  
  89. void ClearBuffer ()
  90.    {
  91.    // Clear the Z-buffer:
  92.    long *ZPtr = ZBuffer;
  93.    for ( unsigned int Index = 0; Index < 6400; Index++ )
  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.        *ZPtr++ = 0;
  108.        *ZPtr++ = 0;
  109.        }
  110.    }
  111.  
  112. // Create a virtual track-ball:
  113. void UpdatePos ( PanelObject *World, MousePtr &Mouse, View &V )
  114.    {
  115.    int X, Y;
  116.    static int Px = 0, Py = 0, Pressed = 0, Lx, Ly;
  117.  
  118.    // Get the mouse coordinates and map them to a suitable
  119.    // range:
  120.    X = ( Mouse.GetX () - 50 ) >> 2;
  121.    Y = ( Mouse.GetY () - 50 ) >> 2;
  122.  
  123.    // If the left button is pressed:
  124.    if ( ( Mouse.GetLb () ) && ( !Mouse.GetRb () ) )
  125.       {
  126.       // Do the translations and rotations:
  127.       V.ZPos -= ( float ) Y * 3.0F;
  128.       V.YRot += X;
  129.       Pressed = 0;
  130.       }
  131.  
  132.    // Else if the right button is pressed:   
  133.    else if ( ( Mouse.GetRb () ) && ( ! Mouse.GetLb () ) )
  134.            {
  135.            V.XRot += Y;
  136.            //V.ZRot -= X;
  137.            Pressed = 0;
  138.            }
  139.  
  140.    // Else if both buttons are pressed:
  141.    else if ( Mouse.GetRb () && Mouse.GetLb () )
  142.            {
  143.            if ( Pressed == 0 )
  144.               {
  145.               Pressed = 1;
  146.               // Record the actual screen coordinates of the 
  147.               // mouse pointer:
  148.               Px = Mouse.GetSx ();
  149.               Py = Mouse.GetSy ();
  150.  
  151.               Lx = Mouse.GetSx ();
  152.               Ly = Mouse.GetSy ();
  153.               }
  154.            else {
  155.                 World->MoveText ( Px, Py, 
  156.                                   ( Lx - Mouse.GetSx () ) >> 2, 
  157.                                   ( Ly - Mouse.GetSy () ) >> 2 );
  158.                 Lx = Mouse.GetSx ();
  159.                 Ly = Mouse.GetSy ();
  160.                 }
  161.            }
  162.  
  163.    // Else no buttons are pressed:
  164.    else Pressed = 0;
  165.    }
  166.  
  167. void HandleKey ( PanelObject *World, MousePtr &Mouse, long Key )
  168.    {
  169.    switch ( Key )
  170.           {
  171.           case ('\t'):
  172.                {
  173.                World->SelNext ();
  174.                break;
  175.                }
  176.           case ('t'):
  177.           case ('T'):
  178.                {
  179.                World->NexText ();
  180.                break;
  181.                }
  182.           case ('r'):
  183.           case ('R'):
  184.                {
  185.                World->RotText ();
  186.                break;
  187.                }
  188.           case ('i'):
  189.           case ('I'):
  190.                {
  191.                World->NextInten ( Mouse.GetSx (), 
  192.                                   Mouse.GetSy () );
  193.                break;
  194.                }
  195.           default: break;
  196.           }
  197.    }
  198.  
  199. float RunWorld ( unsigned char *VidMem, unsigned char *VidBuf,
  200.                   PanelObject *World )
  201.    {
  202.    // Enter the main loop, updating view as necessary:
  203.  
  204.    VidMem; VidBuf; World;
  205.    MousePtr Mouse;
  206.    Matrix3D M; View V;
  207.    long QuitFlag = 0, StartTime, EndTime, FrameCount = 0, 
  208.         MaxWait, Input, BackC;
  209.    float FramesPerSecond;
  210.    ZTrans = 0;
  211.  
  212.    // Initialize the mouse driver:
  213.    Mouse.Init ();
  214.  
  215.    // Hide the pointer:   
  216.    Mouse.Hide ();
  217.  
  218.    // Re-map the cursor's coordinates:
  219.    Mouse.MappingRange ( 100, 100 );
  220.  
  221.    // Clip the cursor to a rectangular region:
  222.    Mouse.Clip ( 5, 5, 95, 95 );
  223.  
  224.    // Give the cursor a face-lift:
  225.    Mouse.ChangeCursor ( Cursor, 12, 12 );
  226.  
  227.    // Allocate memory for Z-buffer:
  228.    InitZBuffer ();
  229.  
  230.    // Raise the viewer:
  231.    M.Initialize ();
  232.    M.Translate ( 0, -700, 0 );
  233.    ClearBuffer ();
  234.    World->Display ( M, VidBuf );
  235.    MaxWait = ( long ) pow ( 2, 31 - ZSTEP_PREC  );
  236.  
  237.    // Record the time:
  238.    StartTime = clock ();
  239.  
  240.    // Calculate the background color (a shade of whitish-gray):
  241.    BackC = GetColor ( 150, 150, 150, TextDat.TMap [ 0 ].Palette );
  242.  
  243.    // Loop until ESC pressed:
  244.    while ( !QuitFlag )
  245.          {
  246.          UpdatePos ( World, Mouse, V );
  247.  
  248.          // Simulate gravity:
  249.          V.YPos = -100.0F;
  250.  
  251.          // Perform collision detection:
  252.          World->Collide ( V.XPos, V.YPos, V.ZPos, 100.0F );
  253.  
  254.          M.Translate ( -V.XPos, -V.YPos, -V.ZPos );
  255.          M.Rotate ( -V.XRot, -V.YRot, -V.ZRot );
  256.          V.YPos = 0.0F;
  257.          V.Clear ();
  258.  
  259.          setmem ( VidBuf, 64000, BackC );
  260.  
  261.          // Clear the Z-buffer (if necessary):
  262.          ZTrans += ( 1 << ZSTEP_PREC );
  263.          if ( ( FrameCount % MaxWait ) == 0 )
  264.             {
  265.             ZTrans = 0;
  266.             ClearBuffer ();
  267.             }
  268.  
  269.          World->Display ( M, VidBuf );
  270.          Mouse.Display ( VidBuf );
  271.  
  272.          memmove ( VidMem, VidBuf, 64000 );
  273.          if ( kbhit () )
  274.             {
  275.             Input = getch ();
  276.             if ( Input == 27 )
  277.                QuitFlag = 1;
  278.             else HandleKey ( World, Mouse, Input );
  279.             }
  280.          ++FrameCount;
  281.          }
  282.    EndTime = clock ();
  283.  
  284.    DestZBuffer ();
  285.  
  286.    // Calculate the frames per second:
  287.    FramesPerSecond = ( float ) FrameCount * 
  288.                      ( float )  CLK_TCK /
  289.                      ( float ) ( EndTime - StartTime ); 
  290.    return FramesPerSecond;
  291.    }
  292.  
  293. void SetPalette ( RGBQUAD *Palette )
  294.    {
  295.    short Index, Red, Green, Blue;
  296.    for ( Index = 0; Index < 256; Index++ )
  297.        {
  298.        Red    = ( short ) ( Palette [ Index ].rgbRed   >> 2 );
  299.        Green  = ( short ) ( Palette [ Index ].rgbGreen >> 2 );
  300.        Blue   = ( short ) ( Palette [ Index ].rgbBlue  >> 2 );
  301.        setpalreg ( Index, Green, Blue, Red );
  302.        }
  303.    }
  304.  
  305.  
  306. // ------------------------------------------------------------
  307. // | Program entry:                                           |
  308. // ------------------------------------------------------------
  309.  
  310. void main ( int ArgCount, char *Arg[] )
  311.    {
  312.    // Declare/initialize variables and allocate memory:
  313.    unsigned char *VidMem, *VidBuf;
  314.    VidMem = VideoAddress ();
  315.    float FramesPerSecond;
  316.  
  317.    // Allocate buffer memory - abort if error:
  318.    if ( ( VidBuf = new unsigned char [ 64000 ] ) == NULL )
  319.       return;
  320.  
  321.    // Allocate memory for a panel object:
  322.    PanelObject *World = new PanelObject;
  323.  
  324.    if ( ( ArgCount > 1 ) && 
  325.         ( !strcmp ( strupr ( Arg [ 1 ] ), "WRITEBINARY" ) ) )
  326.       {
  327.       World->LoadDXF ( "Test.DXF" );
  328.       World->WriteBIN ( "Test.BIN" );
  329.       }
  330.  
  331.    else {
  332.         // Read the panel data:
  333.         World->ReadBIN ( "Test.BIN" );
  334.  
  335.         // Read -- if possible -- the texture data:
  336.         if ( World->ReadText ( "Text.BT" ) == 0 )
  337.            {
  338.            if ( TextDat.LoadINI ( "Text.INI" ) == 0 )
  339.               {
  340.               cout << "\nError loading Text.INI\n";
  341.               delete [] VidBuf;
  342.               delete World;
  343.               return;
  344.               }
  345.            // Initialize default texture coords:
  346.            World->InitDefText ();
  347.            }
  348.         // Read -- if possible -- the shade table:
  349.         if ( TextShade.LoadTable ( "Shade.TAB" ) == 0 )
  350.            {
  351.            // Generate a palette that fades to white:
  352.            TextShade.GenTable ( 150.0F, 150.0F, 150.0F, 
  353.                                 TextDat.TMap [ 0 ].Palette );
  354.            TextShade.SaveTable ( "Shade.TAB" );
  355.            }
  356.  
  357.         // Set video mode:
  358.         SetVideo ( 0x13 );
  359.  
  360.         // Set the palette:
  361.         SetPalette ( TextDat.TMap [ 0 ].Palette );
  362.  
  363.         // Enter main loop:
  364.         FramesPerSecond = RunWorld ( VidMem, VidBuf, World );
  365.  
  366.         // Set video mode:
  367.         SetVideo ( 0x03 );
  368.  
  369.         // Display FPS:
  370.         cout << "\nFrames per second: " << FramesPerSecond;
  371.  
  372.         // Save the texture data:
  373.         World->WriteText ( "Text.BT" );
  374.         }
  375.  
  376.    // Deallocate memory for buffer:
  377.    cout << "\nDeallocating memory...";
  378.    delete [] VidBuf;
  379.       
  380.    // Deallocate memory for panel object:
  381.    delete World;
  382.  
  383.    cout << "\nReturning control to OS...";
  384.  
  385.    // Exit program:
  386.    return;
  387.    }
  388.