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.

215 lines
5.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Simple texture object used for sprites. Handed to the renderer
  4. // for binding. May become a general purpose texture object.
  5. //
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include "Texture.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include <tier0/memdbgon.h>
  11. //-----------------------------------------------------------------------------
  12. // Purpose: Constuctor. Initializes data members.
  13. //-----------------------------------------------------------------------------
  14. CTexture::CTexture( void )
  15. {
  16. m_nWidth = 0;
  17. m_nHeight = 0;
  18. m_bHasAlpha = false;
  19. m_pImageData = NULL;
  20. m_nTextureID = TEXTURE_ID_NONE;
  21. m_szName[0] = '\0';
  22. }
  23. //-----------------------------------------------------------------------------
  24. // Purpose: Destructor. Frees the texture image data.
  25. //-----------------------------------------------------------------------------
  26. CTexture::~CTexture( void )
  27. {
  28. if ( m_pImageData != NULL )
  29. {
  30. delete [] m_pImageData;
  31. }
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Purpose: Allocates image data for a given texture size and pixel format.
  35. // Input : nWidth - Desired width of image, in texels.
  36. // nHeight - Desired height of image, in texels.
  37. // nFlags - Flags indicating the pixel format of the image data. The default
  38. // is 24 bit RGB, no alpha component, but can be set to:
  39. //
  40. // TEXTURE_HAS_ALPHA: the image has an alpha component. Each texel is 32 bits.
  41. //
  42. // Output : Returns true on success, false on failure.
  43. //-----------------------------------------------------------------------------
  44. bool CTexture::Allocate( int nWidth, int nHeight, int nFlags )
  45. {
  46. if ( m_pImageData != NULL )
  47. {
  48. delete [] m_pImageData;
  49. m_pImageData = NULL;
  50. m_nWidth = 0;
  51. m_nHeight = 0;
  52. }
  53. if (( nWidth == 0 ) || ( nHeight == 0 ))
  54. {
  55. return( false );
  56. }
  57. if ( nFlags & TEXTURE_HAS_ALPHA )
  58. {
  59. m_pImageData = new unsigned char [nWidth * nHeight * 4];
  60. m_bHasAlpha = true;
  61. }
  62. else
  63. {
  64. m_pImageData = new unsigned char [nWidth * nHeight * 3];
  65. m_bHasAlpha = false;
  66. }
  67. if ( m_pImageData != NULL )
  68. {
  69. m_nWidth = nWidth;
  70. m_nHeight = nHeight;
  71. }
  72. return( m_pImageData != NULL );
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose:
  76. // Input : *pDC -
  77. // rect -
  78. // iFontHeight -
  79. // dwFlags -
  80. //-----------------------------------------------------------------------------
  81. void CTexture::Draw(CDC *pDC, RECT &rect, int iFontHeight, int iIconHeight, DrawTexData_t &DrawTexData)
  82. {
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose: Returns the full path of the file from which this texture was loaded.
  86. //-----------------------------------------------------------------------------
  87. const char *CTexture::GetFileName() const
  88. {
  89. static char szEmpty[] = "";
  90. return(szEmpty);
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Purpose: Gets the image data in RGB format, or queries the image data size.
  94. // Input : pData - Buffer to receive image data. If NULL, no image data is
  95. // copied. Otherwise, the buffer must be large enough to hold the
  96. // image data.
  97. // Output : Returns the image data size in bytes.
  98. //-----------------------------------------------------------------------------
  99. int CTexture::GetImageDataRGB( void *pData )
  100. {
  101. int nSize = 0;
  102. if ( m_bHasAlpha )
  103. {
  104. // Conversion from 32 to 24 bits not implemented.
  105. Assert( FALSE );
  106. }
  107. else
  108. {
  109. nSize = m_nWidth * m_nHeight * 3;
  110. if (( pData != NULL ) && ( nSize > 0 ))
  111. {
  112. memcpy( pData, m_pImageData, nSize );
  113. }
  114. }
  115. return( nSize );
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Purpose: Gets the image data in RGBA format, or queries the image data size.
  119. // Input : pData - Buffer to receive image data. If NULL, no image data is
  120. // copied. Otherwise, the buffer must be large enough to hold the
  121. // image data.
  122. // Output : Returns the image data size in bytes.
  123. //-----------------------------------------------------------------------------
  124. int CTexture::GetImageDataRGBA( void *pData )
  125. {
  126. int nSize = 0;
  127. if ( m_bHasAlpha )
  128. {
  129. nSize = m_nWidth * m_nHeight * 4;
  130. if (( pData != NULL ) && ( nSize > 0 ))
  131. {
  132. memcpy( pData, m_pImageData, nSize );
  133. }
  134. }
  135. else
  136. {
  137. // Conversion from 24 to 32 bits not implemented.
  138. Assert( FALSE );
  139. }
  140. return( nSize );
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Purpose: Returns a string of comma delimited keywords associated with this
  144. // material.
  145. // Input : pszKeywords - Buffer to receive keywords, NULL to query string length.
  146. // Output : Returns the number of characters in the keyword string.
  147. //-----------------------------------------------------------------------------
  148. int CTexture::GetKeywords(char *pszKeywords) const
  149. {
  150. if (pszKeywords != NULL)
  151. {
  152. *pszKeywords = '\0';
  153. }
  154. return(0);
  155. }
  156. //-----------------------------------------------------------------------------
  157. // Purpose:
  158. // Input : *pszName -
  159. // Output :
  160. //-----------------------------------------------------------------------------
  161. int CTexture::GetShortName(char *pszName) const
  162. {
  163. if (pszName != NULL)
  164. {
  165. strcpy(pszName, m_szName);
  166. }
  167. return(strlen(m_szName));
  168. }
  169. //-----------------------------------------------------------------------------
  170. // Purpose:
  171. // Output : Returns true on success, false on failure.
  172. //-----------------------------------------------------------------------------
  173. bool CTexture::Load(void)
  174. {
  175. return(true);
  176. }