home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / game / IconManager.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  7KB  |  192 lines

  1. //----------------------------------------------------------------
  2. // IconManager.cpp
  3. //
  4. // Copyright 2002-2004 Raven Software
  5. //----------------------------------------------------------------
  6.  
  7. #include "../idlib/precompiled.h"
  8. #pragma hdrstop
  9.  
  10. #include "Game_local.h"
  11. #include "IconManager.h"
  12.  
  13. rvIconManager    iconManagerLocal;
  14. rvIconManager*    iconManager = &iconManagerLocal;
  15.  
  16. /*
  17. ===============================================================================
  18.  
  19.     rvIconManager
  20.  
  21. ===============================================================================
  22. */
  23.  
  24. void rvIconManager::AddIcon( int clientNum, const char* iconName ) {
  25.     assert( gameLocal.GetLocalPlayer() );
  26.  
  27.     idPlayer* player = gameLocal.GetLocalPlayer();
  28.   
  29.       icons[ clientNum ].Append( rvPair<rvIcon*, int>(new rvIcon(), gameLocal.time + ICON_STAY_TIME) );
  30.       icons[ clientNum ][ icons[ clientNum ].Num() - 1 ].First()->CreateIcon( player->spawnArgs.GetString( iconName ), (clientNum == gameLocal.localClientNum ? gameLocal.localClientNum + 1 : 0) );
  31. }
  32.  
  33. void rvIconManager::UpdateIcons( void ) {
  34.     if( gameLocal.GetLocalPlayer() == NULL || !gameLocal.GetLocalPlayer()->GetRenderView() ) {
  35.         return;
  36.     }
  37.  
  38.     // draw team icons
  39.     if( gameLocal.IsTeamGame() ) {
  40.         UpdateTeamIcons();
  41.     }
  42.  
  43.     // draw chat icons
  44.     UpdateChatIcons();
  45.  
  46.     // remove old icons and icons not in our snapshot
  47.     // ** if you want to have permanent icons, you'll have to add support
  48.     // ** for the icons to re-appear when the player comes back in your snapshot (like team icons and chat icons)
  49.     for ( int i = 0; i < MAX_CLIENTS; i++ ) {
  50.         for( int j = 0; j < icons[ i ].Num(); j++ ) {
  51.             if( gameLocal.time > icons[ i ][ j ].Second() || (gameLocal.entities[ i ] && gameLocal.entities[ i ]->fl.networkStale) ) {
  52.                 rvIcon* oldIcon = icons[ i ][ j ].First();
  53.                 oldIcon->FreeIcon();
  54.                 icons[ i ].RemoveIndex( j-- );
  55.                 delete oldIcon;
  56.             }
  57.         }
  58.     }
  59.  
  60.     idPlayer* localPlayer = gameLocal.GetLocalPlayer();
  61.  
  62.     // draw extra icons
  63.     for ( int i = 0; i < MAX_CLIENTS; i++ ) {
  64.         if( gameLocal.localClientNum == i ) {
  65.             continue;
  66.         }
  67.  
  68.         if( gameLocal.entities[ i ] && gameLocal.entities[ i ]->IsType( idPlayer::GetClassType() ) ) {
  69.             idPlayer* player = static_cast<idPlayer*>(gameLocal.entities[ i ]);
  70.  
  71.             if( player->IsHidden() || !icons[ i ].Num() ) {
  72.                 continue;
  73.             }
  74.  
  75.             // distribute the icons appropriately
  76.             int maxHeight = 0;
  77.             int totalWidth = 0;
  78.  
  79.             for( int j = 0; j < icons[ i ].Num(); j++ ) {
  80.                 if( icons[ i ][ j ].First()->GetHeight() > maxHeight ) {
  81.                     maxHeight = icons[ i ][ j ].First()->GetHeight();
  82.                 }
  83.                 totalWidth += icons[ i ][ j ].First()->GetWidth();
  84.             }
  85.             
  86.             idVec3 centerIconPosition = player->spawnArgs.GetVector( (player->team ? "team_icon_height_strogg" : "team_icon_height_marine") );
  87.             
  88.  
  89.             if( teamIcons[ player->entityNumber ].GetHandle() >= 0 ) {
  90.                 centerIconPosition[ 2 ] += teamIcons[ player->entityNumber ].GetHeight();
  91.             }
  92.  
  93.             int incrementalWidth = 0;
  94.             for( int j = 0; j < icons[ i ].Num(); j++ ) {
  95.                 idVec3 iconPosition = centerIconPosition;
  96.                 iconPosition += ( (-totalWidth / 2) + incrementalWidth + (icons[ i ][ j ].First()->GetWidth() / 2) ) * localPlayer->GetRenderView()->viewaxis[ 1 ];
  97.                 incrementalWidth += icons[ i ][ j ].First()->GetWidth();
  98.                 icons[ i ][ j ].First()->UpdateIcon( player->GetPhysics()->GetOrigin() + iconPosition, localPlayer->GetRenderView()->viewaxis );
  99.             }
  100.         }
  101.     }
  102. }
  103.  
  104. void rvIconManager::UpdateTeamIcons( void ) {
  105.     idPlayer* localPlayer = gameLocal.GetLocalPlayer();
  106.     
  107.     for ( int i = 0; i < MAX_CLIENTS; i++ ) {
  108.         if( gameLocal.localClientNum == i ) {
  109.             continue;
  110.         }
  111.  
  112.         //if entity i is a player, manage his icon.
  113.         if( gameLocal.entities[ i ] && gameLocal.entities[ i ]->IsType( idPlayer::GetClassType() ) && !gameLocal.entities[ i ]->fl.networkStale ) {
  114.             idPlayer* player = static_cast<idPlayer*>(gameLocal.entities[ i ]);
  115.             
  116.             //if the player is alive and not hidden, show his icon.
  117.             if( player->team == localPlayer->team && !player->IsHidden() && !player->pfl.dead && gameLocal.mpGame.IsInGame( i ) ) {
  118.                 if( teamIcons[ i ].GetHandle() < 0 ) {
  119.                     teamIcons[ i ].CreateIcon( player->spawnArgs.GetString( player->team ? "mtr_team_icon_strogg" : "mtr_team_icon_marine" ), (player == localPlayer ? localPlayer->entityNumber + 1 : 0) );
  120.                 }
  121.                 teamIcons[ i ].UpdateIcon( player->GetPhysics()->GetOrigin() + player->spawnArgs.GetVector( (player->team ? "team_icon_height_strogg" : "team_icon_height_marine") ), localPlayer->GetRenderView()->viewaxis );
  122.             //else, the player is hidden, dead, or otherwise not needing an icon-- free it.
  123.             } else {
  124.                 if( teamIcons[ i ].GetHandle() >= 0 ) {
  125.                     teamIcons[ i ].FreeIcon();
  126.                 }
  127.             }
  128.         //if entity i is not a player, free icon i from the map.
  129.         } else if( teamIcons[ i ].GetHandle() >= 0 ) {
  130.             teamIcons[ i ].FreeIcon();    
  131.         }
  132.     }
  133. }
  134.  
  135. void rvIconManager::UpdateChatIcons( void ) {
  136.  
  137.     int localInst = gameLocal.GetLocalPlayer()->GetInstance();
  138.     for ( int i = 0; i < MAX_CLIENTS; i++ ) {
  139.         if ( gameLocal.localClientNum == i ) {
  140.             continue;
  141.         }
  142.  
  143.         if ( gameLocal.entities[ i ] && gameLocal.entities[ i ]->IsType( idPlayer::GetClassType() ) && !gameLocal.entities[ i ]->fl.networkStale ) {
  144.             idPlayer *player = static_cast< idPlayer* >( gameLocal.entities[ i ] );
  145.  
  146.             if ( player->isChatting && 
  147.                 !player->IsHidden() && 
  148.                 !( ( idPhysics_Player* )player->GetPhysics() )->IsDead() && 
  149.                 gameLocal.mpGame.IsInGame( i ) 
  150.                 && (localInst == player->GetInstance())) {
  151.                 if ( chatIcons[ i ].GetHandle() < 0 ) {
  152.                     chatIcons[ i ].CreateIcon( player->spawnArgs.GetString( "mtr_icon_chatting" ), ( player == gameLocal.GetLocalPlayer() ? player->entityNumber + 1 : 0) );
  153.                 }
  154.                 int maxHeight = 0;
  155.                 for ( int j = 0; j < icons[ i ].Num(); j++ ) {
  156.                     if ( icons[ i ][ j ].First()->GetHeight() > maxHeight ) {
  157.                         maxHeight = icons[ i ][ j ].First()->GetHeight();
  158.                     }
  159.                 }
  160.                 if ( teamIcons[ i ].GetHandle() >= 0 && teamIcons[ i ].GetHeight() > maxHeight ) {
  161.                     maxHeight = teamIcons[ i ].GetHeight();
  162.                 }
  163.                 idVec3 centerIconPosition = player->spawnArgs.GetVector( ( player->team ? "team_icon_height_strogg" : "team_icon_height_marine") );
  164.                 centerIconPosition[ 2 ] += maxHeight;
  165.                 chatIcons[ i ].UpdateIcon( player->GetPhysics()->GetOrigin() + centerIconPosition, gameLocal.GetLocalPlayer()->GetRenderView()->viewaxis );
  166.             } else if ( chatIcons[ i ].GetHandle() >= 0 ) {
  167.                 chatIcons[ i ].FreeIcon();
  168.             }
  169.         } else if ( chatIcons[ i ].GetHandle() >= 0 ) {
  170.             chatIcons[ i ].FreeIcon();
  171.         }
  172.     }
  173. }
  174.  
  175. /*
  176. ===============
  177. rvIconManager::Shutdown
  178. ===============
  179. */
  180. void rvIconManager::Shutdown( void ) {
  181.     int i, j;
  182.  
  183.     for ( i = 0; i < MAX_CLIENTS; i++ ) {
  184.         for ( j = 0; j < icons[ i ].Num(); j++ ) {
  185.             icons[ i ][ j ].First()->FreeIcon();
  186.         }
  187.         icons[ i ].Clear();
  188.         teamIcons[ i ].FreeIcon();
  189.         chatIcons[ i ].FreeIcon();
  190.     }
  191. }
  192.