Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.1 KiB

  1. //========= Copyright � 1996-2008, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=====================================================================================//
  6. #include "cbase.h"
  7. #include "uigamedata.h"
  8. #include "uiavatarimage.h"
  9. #include "engineinterface.h"
  10. #include "vgui/ISurface.h"
  11. #ifndef _GAMECONSOLE
  12. #include "steam/steam_api.h"
  13. #endif
  14. #ifndef _GAMECONSOLE
  15. //-----------------------------------------------------------------------------
  16. // Purpose:
  17. //-----------------------------------------------------------------------------
  18. CGameUiAvatarImage::CGameUiAvatarImage( void )
  19. {
  20. m_bValid = false;
  21. m_flFetchedTime = 0.0f;
  22. m_iTextureID = ( -1 );
  23. }
  24. void CGameUiAvatarImage::ClearAvatarSteamID( void )
  25. {
  26. m_bValid = false;
  27. m_flFetchedTime = 0.0f;
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Purpose:
  31. //-----------------------------------------------------------------------------
  32. bool CGameUiAvatarImage::SetAvatarSteamID( CSteamID steamIDUser )
  33. {
  34. ClearAvatarSteamID();
  35. if ( steamapicontext->SteamFriends() && steamapicontext->SteamUtils() )
  36. {
  37. int iAvatar = steamapicontext->SteamFriends()->GetMediumFriendAvatar( steamIDUser );
  38. /*
  39. // See if it's in our list already
  40. */
  41. uint32 wide, tall;
  42. if ( steamapicontext->SteamUtils()->GetImageSize( iAvatar, &wide, &tall ) )
  43. {
  44. bool bUseSteamImage = true;
  45. if ( wide == 0 || tall == 0 )
  46. {
  47. // attempt to handle rare data integrity issue, avatar got lost
  48. bUseSteamImage = false;
  49. // mock up solid white as 64x64
  50. wide = tall = 64;
  51. }
  52. int cubImage = wide * tall * 4;
  53. byte *rgubDest = (byte*)_alloca( cubImage );
  54. if ( bUseSteamImage )
  55. {
  56. steamapicontext->SteamUtils()->GetImageRGBA( iAvatar, rgubDest, cubImage );
  57. }
  58. else
  59. {
  60. // solid white, avoids any issue with where the alpha channel is
  61. memset( rgubDest, 0xFF, cubImage );
  62. }
  63. InitFromRGBA( rgubDest, wide, tall );
  64. m_flFetchedTime = Plat_FloatTime();
  65. }
  66. }
  67. return m_bValid;
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose:
  71. //-----------------------------------------------------------------------------
  72. void CGameUiAvatarImage::InitFromRGBA( const byte *rgba, int width, int height )
  73. {
  74. if ( m_iTextureID == -1 )
  75. {
  76. m_iTextureID = vgui::surface()->CreateNewTextureID( true );
  77. }
  78. vgui::surface()->DrawSetTextureRGBA( m_iTextureID, rgba, width, height );
  79. int screenWide, screenTall;
  80. vgui::surface()->GetScreenSize( screenWide, screenTall );
  81. m_nWide = width * ( ( (float) screenWide ) / 640.0f );
  82. m_nTall = height * ( ( (float) screenTall ) / 480.0f );
  83. m_Color = Color( 255, 255, 255, 255 );
  84. m_bValid = true;
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose:
  88. //-----------------------------------------------------------------------------
  89. void CGameUiAvatarImage::Paint( void )
  90. {
  91. if ( m_bValid )
  92. {
  93. vgui::surface()->DrawSetColor( m_Color );
  94. vgui::surface()->DrawSetTexture( m_iTextureID );
  95. vgui::surface()->DrawTexturedRect( m_nX, m_nY, m_nX + m_nWide, m_nY + m_nTall );
  96. }
  97. }
  98. #endif // !_GAMECONSOLE