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.

109 lines
3.5 KiB

  1. //========= Copyright � 1996-2013, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=====================================================================================//
  6. #include "stdafx.h"
  7. #include "sfuiinventoryimage.h"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. using namespace SF::Render;
  11. //-----------------------------------------------------------------------------
  12. // Purpose:
  13. //-----------------------------------------------------------------------------
  14. ScaleformUIInventoryImage::ScaleformUIInventoryImage( uint64 itemid, const byte* defaultRgba, int defaultWidth, int defaultHeight, ::ImageFormat defaultFormat, SF::GFx::TextureManager* pTextureManager )
  15. : ScaleformUIImage( defaultRgba, defaultWidth, defaultHeight, defaultFormat, pTextureManager )
  16. {
  17. m_itemid = itemid;
  18. }
  19. //-----------------------------------------------------------------------------
  20. // Purpose:
  21. //-----------------------------------------------------------------------------
  22. bool ScaleformUIInventoryImage::LoadInventoryImage( const CUtlBuffer* rawImageData, int nWidth, int nHeight, ::ImageFormat format )
  23. {
  24. if ( rawImageData && rawImageData->TellPut() >= ( nWidth * nHeight * 4 ) )
  25. {
  26. #ifdef USE_OVERLAY_ON_INVENTORY_ICONS
  27. OverlayFromBuffer( (byte*)(rawImageData->Base()), nWidth, nHeight, format );
  28. #else
  29. InitFromBuffer( (byte*)(rawImageData->Base()), nWidth, nHeight, format );
  30. #endif
  31. }
  32. return true;
  33. }
  34. #ifdef USE_OVERLAY_ON_INVENTORY_ICONS
  35. void ScaleformUIInventoryImage::OverlayFromBuffer( const byte *rgba, int width, int height, ::ImageFormat format )
  36. {
  37. Assert( rgba && width && height );
  38. bool bForceOverwrite = false;
  39. // See if we need to create a new texture
  40. if ( m_pImage == NULL || width != m_nWidth || height != m_nHeight || format != m_format )
  41. {
  42. const SF::Render::ImageFormat sfFormat = ConvertImageFormat( format );
  43. const unsigned int mipLevelCount = 1;
  44. if ( m_pImage )
  45. {
  46. m_pImage->pImage->Release();
  47. m_pImage->pImage = RawImage::Create( sfFormat, mipLevelCount, ImageSize( width, height), ImageUse_Update, 0, m_pTextureManager );
  48. // apply scaling matrix
  49. SF::GFx::Size<float> scaleParameters( ( (float) m_nWidth ) / width, ( (float) m_nHeight ) / height );
  50. SF::GFx::Matrix2F textureMatrix = SF::GFx::Matrix2F::Scaling( scaleParameters.Width, scaleParameters.Height );
  51. m_pImage->SetMatrix( textureMatrix );
  52. ScaleformUIImpl::m_Instance.ForceUpdateImages();
  53. }
  54. else
  55. {
  56. m_pImage = new ImageDelegate( RawImage::Create( sfFormat, mipLevelCount, ImageSize( width, height), ImageUse_Update, 0, m_pTextureManager ) );
  57. }
  58. m_nWidth = width;
  59. m_nHeight = height;
  60. m_format = format;
  61. bForceOverwrite = true;
  62. }
  63. if ( m_pImage )
  64. {
  65. // Copy data
  66. ImageData imageData;
  67. ((RawImage*)(m_pImage->GetAsImage()))->GetImageData( &imageData );
  68. ImagePlane &imagePlane = imageData.GetPlaneRef( 0 );
  69. if ( bForceOverwrite )
  70. {
  71. memcpy( imagePlane.pData, rgba, imagePlane.DataSize );
  72. }
  73. else
  74. {
  75. Assert( ( width * height * 4 ) <= imagePlane.DataSize );
  76. // pixel by pixel copy overlaying the input rgba onto the pre-existing image data wherever the input is not 0
  77. for ( int y = 0; y < height; y++ )
  78. {
  79. for ( int x = 0; x < width; x++ )
  80. {
  81. if ( *( ( uint * )&( rgba[ ( y * ( width * 4 ) ) + ( x * 4 ) ] ) ) != 0 )
  82. {
  83. *( ( uint * )&( imagePlane.pData[ ( y * ( width * 4 ) ) + ( x * 4 ) ] ) ) = *( ( uint * )&( rgba[ ( y * ( width * 4 ) ) + ( x * 4 ) ] ) );
  84. }
  85. }
  86. }
  87. }
  88. m_pImage->Update();
  89. }
  90. }
  91. #endif