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.

181 lines
5.0 KiB

  1. //========= Copyright 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 "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 = -1;
  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 != -1 )
  45. {
  46. TextureDictionary()->DestroyTexture( m_iTextureID );
  47. m_iTextureID = -1;
  48. }
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose: data accessor
  52. //-----------------------------------------------------------------------------
  53. void MemoryBitmap::GetSize(int &wide, int &tall)
  54. {
  55. wide = 0;
  56. tall = 0;
  57. if (!_valid)
  58. return;
  59. g_MatSystemSurface.DrawGetTextureSize(m_iTextureID, wide, tall);
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose: size of the bitmap
  63. //-----------------------------------------------------------------------------
  64. void MemoryBitmap::GetContentSize(int &wide, int &tall)
  65. {
  66. GetSize(wide, tall);
  67. }
  68. //-----------------------------------------------------------------------------
  69. // Purpose: ignored
  70. //-----------------------------------------------------------------------------
  71. void MemoryBitmap::SetSize(int x, int y)
  72. {
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose: data accessor
  76. //-----------------------------------------------------------------------------
  77. void MemoryBitmap::SetPos(int x, int y)
  78. {
  79. _pos[0] = x;
  80. _pos[1] = y;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: data accessor
  84. //-----------------------------------------------------------------------------
  85. void MemoryBitmap::SetColor(Color col)
  86. {
  87. _color = col;
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose: returns the file name of the bitmap
  91. //-----------------------------------------------------------------------------
  92. const char *MemoryBitmap::GetName()
  93. {
  94. return "MemoryBitmap";
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Purpose: Renders the loaded image, uploading it if necessary
  98. // Assumes a valid image is always returned from uploading
  99. //-----------------------------------------------------------------------------
  100. void MemoryBitmap::Paint()
  101. {
  102. if (!_valid)
  103. return;
  104. // if we have not uploaded yet, lets go ahead and do so
  105. if (!_uploaded)
  106. {
  107. ForceUpload(_texture,_w,_h);
  108. }
  109. //set the texture current, set the color, and draw the biatch
  110. g_MatSystemSurface.DrawSetTexture(m_iTextureID);
  111. g_MatSystemSurface.DrawSetColor(_color[0], _color[1], _color[2], _color[3]);
  112. int wide, tall;
  113. GetSize(wide, tall);
  114. g_MatSystemSurface.DrawTexturedRect( _pos[0], _pos[1], _pos[0] + wide, _pos[1] + tall);
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Purpose: ensures the bitmap has been uploaded
  118. //-----------------------------------------------------------------------------
  119. void MemoryBitmap::ForceUpload(unsigned char *texture,int wide, int tall)
  120. {
  121. _texture=texture;
  122. bool sizechanged = ( _w != wide || _h != tall );
  123. _w = wide;
  124. _h = tall;
  125. if (!_valid)
  126. return;
  127. if(_w==0 || _h==0)
  128. return;
  129. // Not our first time through and the size changed, destroy and recreate texture id...
  130. if ( m_iTextureID != -1 && sizechanged )
  131. {
  132. TextureDictionary()->DestroyTexture( m_iTextureID );
  133. m_iTextureID = -1;
  134. }
  135. if ( m_iTextureID == -1 )
  136. {
  137. m_iTextureID = g_MatSystemSurface.CreateNewTextureID( true );
  138. }
  139. g_MatSystemSurface.DrawSetTextureRGBA( m_iTextureID, texture, wide, tall, true, true );
  140. _uploaded = true;
  141. _valid = g_MatSystemSurface.IsTextureIDValid(m_iTextureID);
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Purpose: data accessor
  145. //-----------------------------------------------------------------------------
  146. HTexture MemoryBitmap::GetID()
  147. {
  148. return m_iTextureID;
  149. }