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.

178 lines
4.9 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <vgui/ISurface.h>
  8. #include "memorybitmap.h"
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "MatSystemSurface.h"
  12. #include "materialsystem/IMaterialVar.h"
  13. #include "materialsystem/ITexture.h"
  14. #include "bitmap/imageformat.h"
  15. #include "vtf/vtf.h"
  16. #include "KeyValues.h"
  17. #include "vgui_surfacelib/TextureDictionary.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. extern CMatSystemSurface g_MatSystemSurface;
  21. using namespace vgui;
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Constructor
  24. // Input : *filename - image file to load
  25. //-----------------------------------------------------------------------------
  26. MemoryBitmap::MemoryBitmap(unsigned char *texture,int wide, int tall)
  27. {
  28. _texture=texture;
  29. _uploaded = false;
  30. _color = Color(255, 255, 255, 255);
  31. _pos[0] = _pos[1] = 0;
  32. _valid = true;
  33. _w = wide;
  34. _h = tall;
  35. m_iTextureID = 0;
  36. ForceUpload(texture,wide,tall);
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Purpose: Destructor
  40. //-----------------------------------------------------------------------------
  41. MemoryBitmap::~MemoryBitmap()
  42. {
  43. // Free the old texture ID.
  44. if ( m_iTextureID != 0 )
  45. TextureDictionary()->DestroyTexture( m_iTextureID );
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose: data accessor
  49. //-----------------------------------------------------------------------------
  50. void MemoryBitmap::GetSize(int &wide, int &tall)
  51. {
  52. wide = 0;
  53. tall = 0;
  54. if (!_valid)
  55. return;
  56. g_MatSystemSurface.DrawGetTextureSize(m_iTextureID, wide, tall);
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Purpose: size of the bitmap
  60. //-----------------------------------------------------------------------------
  61. void MemoryBitmap::GetContentSize(int &wide, int &tall)
  62. {
  63. GetSize(wide, tall);
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Purpose: ignored
  67. //-----------------------------------------------------------------------------
  68. void MemoryBitmap::SetSize(int x, int y)
  69. {
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose: data accessor
  73. //-----------------------------------------------------------------------------
  74. void MemoryBitmap::SetPos(int x, int y)
  75. {
  76. _pos[0] = x;
  77. _pos[1] = y;
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose: data accessor
  81. //-----------------------------------------------------------------------------
  82. void MemoryBitmap::SetColor(Color col)
  83. {
  84. _color = col;
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose: returns the file name of the bitmap
  88. //-----------------------------------------------------------------------------
  89. const char *MemoryBitmap::GetName()
  90. {
  91. return "MemoryBitmap";
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Purpose: Renders the loaded image, uploading it if necessary
  95. // Assumes a valid image is always returned from uploading
  96. //-----------------------------------------------------------------------------
  97. void MemoryBitmap::Paint()
  98. {
  99. if (!_valid)
  100. return;
  101. // if we have not uploaded yet, lets go ahead and do so
  102. if (!_uploaded)
  103. {
  104. ForceUpload(_texture,_w,_h);
  105. }
  106. //set the texture current, set the color, and draw the biatch
  107. g_MatSystemSurface.DrawSetTexture(m_iTextureID);
  108. g_MatSystemSurface.DrawSetColor(_color[0], _color[1], _color[2], _color[3]);
  109. int wide, tall;
  110. GetSize(wide, tall);
  111. g_MatSystemSurface.DrawTexturedRect( _pos[0], _pos[1], _pos[0] + wide, _pos[1] + tall);
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Purpose: ensures the bitmap has been uploaded
  115. //-----------------------------------------------------------------------------
  116. void MemoryBitmap::ForceUpload(unsigned char *texture,int wide, int tall)
  117. {
  118. _texture=texture;
  119. bool sizechanged = ( _w != wide || _h != tall );
  120. _w = wide;
  121. _h = tall;
  122. if (!_valid)
  123. return;
  124. if(_w==0 || _h==0)
  125. return;
  126. // If size changed, or first time through, destroy and recreate texture id...
  127. if ( sizechanged && m_iTextureID )
  128. {
  129. TextureDictionary()->DestroyTexture( m_iTextureID );
  130. m_iTextureID = 0;
  131. }
  132. if ( !m_iTextureID )
  133. {
  134. m_iTextureID = g_MatSystemSurface.CreateNewTextureID( true );
  135. }
  136. g_MatSystemSurface.DrawSetTextureRGBA( m_iTextureID, texture, wide, tall );
  137. _uploaded = true;
  138. _valid = g_MatSystemSurface.IsTextureIDValid(m_iTextureID);
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose: data accessor
  142. //-----------------------------------------------------------------------------
  143. HTexture MemoryBitmap::GetID()
  144. {
  145. return m_iTextureID;
  146. }