Pushok

Some shit

Apr 12th, 2012
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.42 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "cscreen.h"
  4.  
  5. #define SET_OWNER_COLOR glColor3d( 0, 255, 0 )
  6. #define SET_OPPO_COLOR glColor3d( 255, 0, 0 )
  7.  
  8. namespace Game
  9. {
  10.     float CScreen::fHypo;
  11.  
  12.     void CScreen::Set2DMode( )
  13.     {
  14.         glDisable( GL_DEPTH_TEST );
  15.         glMatrixMode( GL_PROJECTION );
  16.         glLoadIdentity( );
  17.         gluOrtho2D( 0, ScreenWidth, ScreenHeigth, 0 );
  18.         glMatrixMode( GL_MODELVIEW );
  19.         glLoadIdentity( );
  20.     }
  21.  
  22.     void CScreen::DrawPoint( int x, int y )
  23.     {
  24.         glVertex2d( x, y );
  25.     }
  26.  
  27.     void CScreen::DrawLine( int x, int y, int x1, int x2 )
  28.     {
  29.         glPointSize( 1.0 );
  30.  
  31.         glVertex2d( x, y );
  32.  
  33.         glVertex2d( x1, x2 );
  34.     }
  35.  
  36.     void CScreen::DrawMesh( )
  37.     {
  38.         Set2DMode( );
  39.         glPushMatrix( );
  40.         glColor3d( 0, 0, 0 );
  41.         glBegin( GL_LINES );
  42.  
  43.         for( int i = CellSize; i < ScreenWidth; i += CellSize )
  44.         {
  45.             DrawLine( i, ScreenHeigth, i, -ScreenWidth );
  46.         }
  47.  
  48.         for( int i = CellSize; i < ScreenHeigth; i += CellSize )
  49.         {
  50.             DrawLine( -ScreenHeigth, i, ScreenWidth, i );
  51.         }
  52.  
  53.         glEnd( );
  54.         glPopMatrix( );
  55.     }
  56.    
  57.     void CScreen::DrawPlayersPoints( )
  58.     {
  59.         Set2DMode( );
  60.         glPushMatrix( );
  61.         glPointSize( ( float )CellSize * 0.4 );
  62.         for( auto it = CGlobals::m_vPointInfo.begin( ); it < CGlobals::m_vPointInfo.end( ); it++ )
  63.         {
  64.             if( it->IsOppo( &CGlobals::m_LocalPlayer ) )
  65.             {
  66.                 glColor3d( 255, 0, 0 );
  67.             }
  68.             else
  69.             {
  70.                 glColor3d( 0, 255, 0 );
  71.             }
  72.  
  73.             glBegin( GL_POINTS );
  74.  
  75.             cml::vector2i nearest = CScreen::GetNearestPoint( it->GetPoint( )->To2DVector( ) );
  76.  
  77.             CScreen::DrawPoint( nearest[ 0 ], nearest[ 1 ] );
  78.  
  79.             glEnd( );
  80.         }
  81.  
  82.         glPopMatrix(  );
  83.     }
  84.  
  85.     void CScreen::DrawAreas( )
  86.     {
  87.         Set2DMode( );
  88.         glPushMatrix( );
  89.         glPointSize( 1.0 );
  90.  
  91.         PointInfoList vecPoints;
  92.  
  93.         for( auto it = CGlobals::m_vPointInfo.begin( ); it < CGlobals::m_vPointInfo.end( ); it++ )
  94.         {
  95.             vecPoints.push_back( *it );
  96.  
  97.             CScreen::JoinNearestPoints( *it, vecPoints );
  98.         }
  99.  
  100.         glPopMatrix( );
  101.     }
  102.  
  103.     void CScreen::JoinNearestPoints( CPointInfo pointInfo, PointInfoList vecRealPoints )
  104.     {
  105.         cml::vector2i currentPoint = pointInfo.GetPoint()->To2DVector();
  106.  
  107.         for( auto it = vecRealPoints.begin( ); it < vecRealPoints.end( ); it++ )
  108.         {
  109.             Vector2DI nextPoint = it->GetPoint()->To2DVector( );
  110.  
  111.             if( currentPoint == nextPoint )
  112.             {
  113.                 continue;
  114.             }
  115.  
  116.             if( pointInfo.IsOppo( it->GetOwner( ) ) )
  117.             {
  118.                 continue;
  119.             }
  120.  
  121.             if( pointInfo.GetOwner( )->GetPlayerName( ) == CGlobals::m_LocalPlayer.GetPlayerName( ) )
  122.             {
  123.                 SET_OWNER_COLOR;
  124.             }
  125.             else
  126.             {
  127.                 SET_OPPO_COLOR;
  128.             }
  129.  
  130.             glBegin( GL_LINES );
  131.  
  132.             float fDistance = Vector2DI( currentPoint - nextPoint ).length( );
  133.  
  134.             if( fDistance <= fHypo ) // PYTHAGORAAAAAAAAAAAAARTA
  135.             {
  136.                 if( fDistance == fHypo )
  137.                 {
  138.                     Vector2dList vecPoints = CScreen::GetNearestOppoCoords( pointInfo, vecRealPoints );
  139.  
  140.                     if( CScreen::GetNearestPointsCount( nextPoint, vecPoints ) == 2 )
  141.                     {
  142.                         continue;
  143.                     }
  144.                 }
  145.  
  146.                 DrawLine( currentPoint[0], currentPoint[1], nextPoint[0], nextPoint[1] );
  147.             }
  148.  
  149.             glEnd( );
  150.         }
  151.     }
  152.  
  153.     int CScreen::GetNearestPointsCount( Vector2DI point, Vector2dList vecNearestPoints )
  154.     {
  155.         int count;
  156.  
  157.         for( auto it = vecNearestPoints.begin(); it < vecNearestPoints.end(); it++ )
  158.         {
  159.             if( Vector2F( point - *it ).length( ) < fHypo )
  160.             {
  161.                 count++;
  162.             }
  163.         }
  164.  
  165.         return count;
  166.     }
  167.  
  168.     Vector2dList CScreen::GetNearestOppoCoords( CPointInfo pointInfo, PointInfoList vecPointInfo )
  169.     {
  170.         Vector2dList vecPoints;
  171.  
  172.         cml::vector2i currentPoint = pointInfo.GetPoint()->To2DVector();
  173.  
  174.         for( auto it = vecPointInfo.begin( ); it < vecPointInfo.end( ); it++ )
  175.         {
  176.             if( pointInfo.IsOppo( it->GetOwner( ) ) )
  177.             {
  178.                 cml::vector2i nextPoint = it->GetPoint( )->To2DVector( );
  179.  
  180.                 if( cml::vector2d( currentPoint - nextPoint ).length( ) <= fHypo )
  181.                 {
  182.                     vecPoints.push_back( nextPoint );
  183.                 }
  184.  
  185.                 continue;
  186.             }
  187.         }
  188.  
  189.         return vecPoints;
  190.     }
  191.  
  192.     void CScreen::RenderScene( )
  193.     {
  194.         glClear( GL_COLOR_BUFFER_BIT );
  195.  
  196.         DrawMesh( );
  197.         DrawPlayersPoints( );
  198.         DrawAreas( );
  199.  
  200.         glutSwapBuffers( );
  201.     }
  202.  
  203.     void CScreen::UpdateScreen( int width, int heigth )
  204.     {
  205.         float fRatio;
  206.  
  207.         if( !heigth )
  208.         {
  209.             heigth = 1;
  210.         }
  211.  
  212.         glViewport( 0, 0, width, heigth );
  213.         glMatrixMode( GL_PROJECTION );
  214.  
  215.         glLoadIdentity( );
  216.  
  217.         fRatio = (float)width / (float)heigth;
  218.  
  219.         if( width <= heigth )
  220.         {
  221.             glOrtho( -100, 100, -100.0 / fRatio, 100.0 / fRatio, 1, -1 );
  222.         }
  223.         else
  224.         {
  225.             glOrtho( -100 * fRatio, 100 * fRatio, -100, 100, 1, -1 );
  226.         }
  227.  
  228.         glMatrixMode( GL_MODELVIEW );
  229.         glLoadIdentity( );
  230.     }
  231.  
  232.     Vector2DI CScreen::GetNearestPoint( CPoint point )
  233.     {
  234.         return CScreen::GetNearestPoint( Vector2DI( point.GetX(), point.GetY() ) );
  235.     }
  236.  
  237.     Vector2DI CScreen::GetNearestPoint( Vector2DI point )
  238.     {
  239.         Vector2DI nearest( 0, 0 );
  240.  
  241.         for( int i = CellSize; i < ScreenWidth; i += CellSize )
  242.         {
  243.             for( int k = CellSize; k < ScreenHeigth; k += CellSize )
  244.             {
  245.                 Vector2DI newPoint( i, k );
  246.  
  247.                 if( (newPoint - point).length( ) < ( nearest - point ).length( ) )
  248.                 {
  249.                     nearest = newPoint;
  250.                 }
  251.             }
  252.         }
  253.  
  254.         return nearest;
  255.     }
  256.  
  257.     bool CScreen::HaveSamePoint( Vector2DI newPoint )
  258.     {
  259.         if( !CGlobals::m_vPointInfo.empty( ) )
  260.         {
  261.             for( auto it = CGlobals::m_vPointInfo.begin( ); it < CGlobals::m_vPointInfo.end( ); it++ )
  262.             {
  263.                 Vector2DI oldPoint = it->GetPoint( )->To2DVector( );
  264.  
  265.                 if( oldPoint == newPoint )
  266.                 {
  267.                     return true;
  268.                 }
  269.             }
  270.         }
  271.  
  272.         return false;
  273.     }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment