Team Fortress 2 Source Code as on 22/4/2020
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.

92 lines
2.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "TGAImagePanel.h"
  7. #include "bitmap/tgaloader.h"
  8. #include "vgui/ISurface.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. using namespace vgui;
  12. CTGAImagePanel::CTGAImagePanel( vgui::Panel *parent, const char *name ) : BaseClass( parent, name )
  13. {
  14. m_iTextureID = -1;
  15. m_bHasValidTexture = false;
  16. m_bLoadedTexture = false;
  17. m_szTGAName[0] = 0;
  18. SetPaintBackgroundEnabled( false );
  19. }
  20. CTGAImagePanel::~CTGAImagePanel()
  21. {
  22. // release the texture memory
  23. if ( vgui::surface() && m_iTextureID != -1 )
  24. {
  25. vgui::surface()->DestroyTextureID( m_iTextureID );
  26. m_iTextureID = -1;
  27. }
  28. }
  29. void CTGAImagePanel::SetTGA( const char *filename )
  30. {
  31. Q_snprintf( m_szTGAName, sizeof(m_szTGAName), "//MOD/%s", filename );
  32. }
  33. void CTGAImagePanel::SetTGANonMod( const char *filename )
  34. {
  35. V_strcpy_safe( m_szTGAName, filename );
  36. }
  37. void CTGAImagePanel::Paint()
  38. {
  39. if ( !m_bLoadedTexture )
  40. {
  41. m_bLoadedTexture = true;
  42. // get a texture id, if we haven't already
  43. if ( m_iTextureID == -1 )
  44. {
  45. m_iTextureID = vgui::surface()->CreateNewTextureID( true );
  46. SetSize( 180, 100 );
  47. }
  48. // load the file
  49. CUtlMemory<unsigned char> tga;
  50. #ifndef _XBOX
  51. if ( TGALoader::LoadRGBA8888( m_szTGAName, tga, m_iImageWidth, m_iImageHeight ) )
  52. {
  53. // set the textureID
  54. surface()->DrawSetTextureRGBA( m_iTextureID, tga.Base(), m_iImageWidth, m_iImageHeight, false, true );
  55. m_bHasValidTexture = true;
  56. // set our size to be the size of the tga
  57. SetSize( m_iImageWidth, m_iImageHeight );
  58. }
  59. else
  60. #endif
  61. {
  62. m_bHasValidTexture = false;
  63. }
  64. }
  65. // draw the image
  66. int wide, tall;
  67. if ( m_bHasValidTexture )
  68. {
  69. surface()->DrawGetTextureSize( m_iTextureID, wide, tall );
  70. surface()->DrawSetTexture( m_iTextureID );
  71. surface()->DrawSetColor( 255, 255, 255, 255 );
  72. surface()->DrawTexturedRect( 0, 0, wide, tall );
  73. }
  74. else
  75. {
  76. // draw a black fill instead
  77. wide = 180, tall = 100;
  78. surface()->DrawSetColor( 0, 0, 0, 255 );
  79. surface()->DrawFilledRect( 0, 0, wide, tall );
  80. }
  81. }