Counter Strike : Global Offensive Source Code

142 lines
4.2 KiB

  1. //========= Copyright � 1996-2008, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=====================================================================================//
  6. #include "stdafx.h"
  7. #include "scaleformuiimage.h"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. #pragma warning( disable: 4355 ) // disables ' 'this' : used in base member initializer list'
  11. using namespace SF::Render;
  12. //-----------------------------------------------------------------------------
  13. // Purpose: Convert Valve ImageFormat to Scaleform ImageFormat
  14. //-----------------------------------------------------------------------------
  15. SF::Render::ImageFormat ScaleformUIImage::ConvertImageFormat( ::ImageFormat format )
  16. {
  17. SF::Render::ImageFormat dstFormat = Image_R8G8B8A8;
  18. switch ( format )
  19. {
  20. case IMAGE_FORMAT_RGBA8888:
  21. dstFormat = Image_R8G8B8A8;
  22. break;
  23. case IMAGE_FORMAT_BGRA8888:
  24. dstFormat = Image_B8G8R8A8;
  25. break;
  26. case IMAGE_FORMAT_DXT5:
  27. dstFormat = Image_DXT5;
  28. break;
  29. default:
  30. DevMsg("ImageFormat %d not supported by ScaleformUIImage, using RGBA8888!", format);
  31. break;
  32. }
  33. return dstFormat;
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Purpose:
  37. //-----------------------------------------------------------------------------
  38. ScaleformUIImage::ScaleformUIImage( const byte* defaultRgba, int defaultWidth, int defaultHeight, ::ImageFormat defaultFormat, SF::GFx::TextureManager* pTextureManager )
  39. {
  40. m_nWidth = defaultWidth;
  41. m_nHeight = defaultHeight;
  42. m_format = defaultFormat;
  43. m_nRefCount = 0;
  44. m_pImage = NULL;
  45. m_pTextureManager = pTextureManager;
  46. // Set the default bits for the texture so we have something to show while the real icon is loading
  47. if ( defaultRgba )
  48. {
  49. InitFromBuffer( defaultRgba, defaultWidth, defaultHeight, defaultFormat );
  50. }
  51. }
  52. ScaleformUIImage::~ScaleformUIImage( void )
  53. {
  54. if ( m_pImage )
  55. {
  56. m_pImage->pImage->Release();
  57. m_pImage->Release();
  58. }
  59. }
  60. void ScaleformUIImage::OnFinalRelease()
  61. {
  62. delete this;
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose:
  66. //-----------------------------------------------------------------------------
  67. Image* ScaleformUIImage::GetImage( void )
  68. {
  69. m_pImage->AddRef();
  70. return m_pImage;
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose:
  74. //-----------------------------------------------------------------------------
  75. void ScaleformUIImage::InitFromBuffer( const byte *rgba, int width, int height, ::ImageFormat format )
  76. {
  77. // Check image updates properly if Steam persona changes
  78. Assert( rgba && width && height );
  79. // See if we need to create a new texture
  80. if ( m_pImage == NULL || width != m_nWidth || height != m_nHeight || format != m_format )
  81. {
  82. const SF::Render::ImageFormat sfFormat = ConvertImageFormat( format );
  83. const unsigned int mipLevelCount = 1;
  84. if ( m_pImage )
  85. {
  86. m_pImage->pImage->Release();
  87. m_pImage->pImage = RawImage::Create( sfFormat, mipLevelCount, ImageSize( width, height), ImageUse_Update, 0, m_pTextureManager );
  88. // apply scaling matrix
  89. SF::GFx::Size<float> scaleParameters( ( (float) m_nWidth ) / width, ( (float) m_nHeight ) / height );
  90. SF::GFx::Matrix2F textureMatrix = SF::GFx::Matrix2F::Scaling( scaleParameters.Width, scaleParameters.Height );
  91. m_pImage->SetMatrix( textureMatrix );
  92. ScaleformUIImpl::m_Instance.ForceUpdateImages();
  93. }
  94. else
  95. {
  96. m_pImage = new ImageDelegate( RawImage::Create( sfFormat, mipLevelCount, ImageSize( width, height), ImageUse_Update, 0, m_pTextureManager ) );
  97. }
  98. m_nWidth = width;
  99. m_nHeight = height;
  100. m_format = format;
  101. }
  102. if ( m_pImage )
  103. {
  104. // Copy data
  105. ImageData imageData;
  106. ((RawImage*)(m_pImage->GetAsImage()))->GetImageData( &imageData );
  107. ImagePlane &imagePlane = imageData.GetPlaneRef( 0 );
  108. memcpy( imagePlane.pData, rgba, imagePlane.DataSize );
  109. m_pImage->Update();
  110. }
  111. }
  112. //-----------------------------------------------------------------------------
  113. // Purpose:
  114. //-----------------------------------------------------------------------------
  115. void ScaleformUIImage::Update( void )
  116. {
  117. #ifdef _X360
  118. X360_UpdateImageState();
  119. #endif
  120. }