TextureHandle.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "graphics/TextureHandle.h"
  23. #include "graphics/TextureManager.h"
  24. #include "platform/platformAssert.h"
  25. //-----------------------------------------------------------------------------
  26. TextureHandle::TextureHandle( const char* pTextureKey, TextureHandleType type, bool clampToEdge, bool force16Bit )
  27. {
  28. // Sanity!
  29. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  30. object = TextureManager::loadTexture(pTextureKey, type, clampToEdge, false, force16Bit );
  31. lock();
  32. }
  33. //-----------------------------------------------------------------------------
  34. TextureHandle::TextureHandle( const char* pTextureKey, GBitmap* bmp, TextureHandleType type, bool clampToEdge )
  35. {
  36. // Sanity!
  37. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  38. object = TextureManager::registerTexture(pTextureKey, bmp, type, clampToEdge);
  39. lock();
  40. }
  41. //-----------------------------------------------------------------------------
  42. bool TextureHandle::set( const char* pTextureKey, TextureHandleType type, bool clampToEdge, bool force16Bit )
  43. {
  44. // Sanity!
  45. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  46. TextureObject* newObject = TextureManager::loadTexture(pTextureKey, type, clampToEdge, false, force16Bit );
  47. if (newObject != object)
  48. {
  49. unlock();
  50. object = newObject;
  51. lock();
  52. }
  53. return (object != NULL);
  54. }
  55. //-----------------------------------------------------------------------------
  56. bool TextureHandle::set( const char* pTextureKey, GBitmap *bmp, TextureHandleType type, bool clampToEdge )
  57. {
  58. // Sanity!
  59. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  60. TextureObject* newObject = TextureManager::registerTexture(pTextureKey, bmp, type, clampToEdge );
  61. if (newObject != object)
  62. {
  63. unlock();
  64. object = newObject;
  65. lock();
  66. }
  67. return (object != NULL);
  68. }
  69. //-----------------------------------------------------------------------------
  70. void TextureHandle::refresh( void )
  71. {
  72. TextureManager::refresh(object);
  73. }
  74. //-----------------------------------------------------------------------------
  75. const char* TextureHandle::getTextureKey( void ) const
  76. {
  77. return (object ? object->mTextureKey : NULL);
  78. }
  79. //-----------------------------------------------------------------------------
  80. U32 TextureHandle::getWidth( void ) const
  81. {
  82. return (object ? object->mBitmapWidth : 0);
  83. }
  84. //-----------------------------------------------------------------------------
  85. U32 TextureHandle::getHeight( void ) const
  86. {
  87. return (object ? object->mBitmapHeight : 0);
  88. }
  89. //-----------------------------------------------------------------------------
  90. GBitmap* TextureHandle::getBitmap( void )
  91. {
  92. return (object ? object->mpBitmap : NULL);
  93. }
  94. //-----------------------------------------------------------------------------
  95. const GBitmap* TextureHandle::getBitmap( void ) const
  96. {
  97. return (object ? object->mpBitmap: NULL);
  98. }
  99. //-----------------------------------------------------------------------------
  100. U32 TextureHandle::getGLName( void ) const
  101. {
  102. return object == NULL ? 0 : object->mGLTextureName;
  103. }
  104. //-----------------------------------------------------------------------------
  105. void TextureHandle::setFilter( const GLuint filter )
  106. {
  107. // Finish if no object.
  108. if (object == NULL )
  109. return;
  110. // Set filter.
  111. object->mFilter = filter;
  112. // Finish if no GL texture name.
  113. if ( object->mGLTextureName == 0 )
  114. return;
  115. // Set texture state.
  116. glBindTexture( GL_TEXTURE_2D, object->mGLTextureName );
  117. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter );
  118. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter );
  119. }
  120. //-----------------------------------------------------------------------------
  121. void TextureHandle::setClamp( const bool clamp )
  122. {
  123. // Finish if no object.
  124. if (object == NULL )
  125. return;
  126. // Set clamp.
  127. object->mClamp = clamp;
  128. // Finish if no GL texture name.
  129. if ( object->mGLTextureName == 0 )
  130. return;
  131. // Set texture state.
  132. glBindTexture(GL_TEXTURE_2D, object->mGLTextureName);
  133. GLenum glClamp;
  134. if ( clamp )
  135. glClamp = dglDoesSupportEdgeClamp() ? GL_CLAMP_TO_EDGE : GL_CLAMP;
  136. else
  137. glClamp = GL_REPEAT;
  138. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, glClamp );
  139. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, glClamp );
  140. }
  141. //-----------------------------------------------------------------------------
  142. void TextureHandle::lock( void )
  143. {
  144. if ( object == NULL )
  145. return;
  146. object->mRefCount++;
  147. }
  148. //-----------------------------------------------------------------------------
  149. void TextureHandle::unlock( void )
  150. {
  151. // Finish if the texture manager is not active or the handle does not represent an object.
  152. // Do nothing if the manager isn't active or we do not have an object
  153. if ( IsNull() || TextureManager::getManagerState() == TextureManager::NotInitialized )
  154. return;
  155. if( --object->mRefCount == 0 )
  156. {
  157. TextureManager::freeTexture(object);
  158. }
  159. object = NULL;
  160. }