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.

102 lines
2.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Various material proxies for TF
  4. //
  5. //=====================================================================================//
  6. #include "cbase.h"
  7. #include "filesystem.h"
  8. #include "materialsystem/imaterialproxy.h"
  9. #include "materialsystem/imaterialvar.h"
  10. #include "materialsystem/itexture.h"
  11. #include "pixelwriter.h"
  12. #include "toolframework_client.h"
  13. #include "econ_gcmessages.h"
  14. #include "tf_item_inventory.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. //-----------------------------------------------------------------------------
  18. // @note Currently not used since we cache the image on disk as a VTF
  19. class CRGBAImageTextureRegenerator : public ITextureRegenerator
  20. {
  21. public:
  22. CRGBAImageTextureRegenerator() {}
  23. virtual ~CRGBAImageTextureRegenerator() {}
  24. // @return true if the image retrieval was successful, false otherwise
  25. virtual bool GetRGBAImage( uint8 *pubDest, int nDestBufferSize ) = 0;
  26. virtual void RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect )
  27. {
  28. const int width = 64;
  29. const int height = 64;
  30. // if the width or height mismatches, we abort
  31. int nWidth = pVTFTexture->Width();
  32. int nHeight = pVTFTexture->Height();
  33. if ( nWidth != width || nHeight != height )
  34. {
  35. return;
  36. }
  37. // retrieve the image
  38. int destBufferSize = width * height * 4;
  39. byte *rgbaAvatarImage = (byte*)malloc( destBufferSize );
  40. if ( GetRGBAImage( rgbaAvatarImage, destBufferSize ) )
  41. {
  42. // Set up the pixel writer to write into the VTF texture
  43. CPixelWriter pixelWriter;
  44. pixelWriter.SetPixelMemory( pVTFTexture->Format(), pVTFTexture->ImageData(), pVTFTexture->RowSizeInBytes( 0 ) );
  45. // only write the pixels requested
  46. for (int y = pRect->y; y < pRect->height; ++y)
  47. {
  48. pixelWriter.Seek( pRect->x, y );
  49. for (int x = pRect->x; x < pRect->width; ++x)
  50. {
  51. int idx = ( y * width + x ) * 4;
  52. byte *rgba = &rgbaAvatarImage[idx];
  53. byte r = rgba[0];
  54. byte g = rgba[1];
  55. byte b = rgba[2];
  56. byte a = rgba[3];
  57. pixelWriter.WritePixel( r, g, b, a );
  58. }
  59. }
  60. }
  61. // cleanup after ourselves
  62. free( rgbaAvatarImage );
  63. }
  64. virtual void Release()
  65. {
  66. }
  67. };
  68. //-----------------------------------------------------------------------------
  69. /* @note Tom Bui: Here for reference
  70. class CPlayerSteamAvatarTextureRegenerator : public CRGBAImageTextureRegenerator
  71. {
  72. public:
  73. CPlayerSteamAvatarTextureRegenerator( const CSteamID &steamID )
  74. : CRGBAImageTextureRegenerator()
  75. , m_steamID(steamID)
  76. {}
  77. virtual ~CPlayerSteamAvatarTextureRegenerator() {}
  78. virtual bool GetRGBAImage( uint8 *pubDest, int nDestBufferSize )
  79. {
  80. int iAvatar = steamapicontext->SteamFriends()->GetFriendAvatar( m_steamID, k_EAvatarSize64x64 );
  81. return steamapicontext->SteamUtils()->GetImageRGBA( iAvatar, pubDest, nDestBufferSize );
  82. }
  83. private:
  84. CSteamID m_steamID;
  85. };
  86. */