TextureHandle.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 BadTextureHandle;
  27. //-----------------------------------------------------------------------------
  28. TextureHandle::TextureHandle( const char* pTextureKey, TextureHandleType type, bool clampToEdge, bool force16Bit )
  29. {
  30. // Sanity!
  31. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  32. object = TextureManager::loadTexture(pTextureKey, type, clampToEdge, false, force16Bit );
  33. lock();
  34. }
  35. //-----------------------------------------------------------------------------
  36. TextureHandle::TextureHandle( const char* pTextureKey, GBitmap* bmp, TextureHandleType type, bool clampToEdge )
  37. {
  38. // Sanity!
  39. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  40. object = TextureManager::registerTexture(pTextureKey, bmp, type, clampToEdge);
  41. lock();
  42. }
  43. //-----------------------------------------------------------------------------
  44. bool TextureHandle::set( const char* pTextureKey, TextureHandleType type, bool clampToEdge, bool force16Bit )
  45. {
  46. // Sanity!
  47. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  48. TextureObject* newObject = TextureManager::loadTexture(pTextureKey, type, clampToEdge, false, force16Bit );
  49. if (newObject != object)
  50. {
  51. unlock();
  52. object = newObject;
  53. lock();
  54. }
  55. return (object != NULL);
  56. }
  57. //-----------------------------------------------------------------------------
  58. bool TextureHandle::set( const char* pTextureKey, GBitmap *bmp, TextureHandleType type, bool clampToEdge )
  59. {
  60. // Sanity!
  61. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  62. TextureObject* newObject = TextureManager::registerTexture(pTextureKey, bmp, type, clampToEdge );
  63. if (newObject != object)
  64. {
  65. unlock();
  66. object = newObject;
  67. lock();
  68. }
  69. return (object != NULL);
  70. }
  71. //-----------------------------------------------------------------------------
  72. void TextureHandle::refresh( void )
  73. {
  74. TextureManager::refresh(object);
  75. }
  76. //-----------------------------------------------------------------------------
  77. const char* TextureHandle::getTextureKey( void ) const
  78. {
  79. return (object ? object->mTextureKey : NULL);
  80. }
  81. //-----------------------------------------------------------------------------
  82. U32 TextureHandle::getWidth( void ) const
  83. {
  84. return (object ? object->mBitmapWidth : 0);
  85. }
  86. //-----------------------------------------------------------------------------
  87. U32 TextureHandle::getHeight( void ) const
  88. {
  89. return (object ? object->mBitmapHeight : 0);
  90. }
  91. //-----------------------------------------------------------------------------
  92. GBitmap* TextureHandle::getBitmap( void )
  93. {
  94. return (object ? object->mpBitmap : NULL);
  95. }
  96. //-----------------------------------------------------------------------------
  97. const GBitmap* TextureHandle::getBitmap( void ) const
  98. {
  99. return (object ? object->mpBitmap: NULL);
  100. }
  101. //-----------------------------------------------------------------------------
  102. U32 TextureHandle::getGLName( void ) const
  103. {
  104. return object == NULL ? 0 : object->mGLTextureName;
  105. }
  106. //-----------------------------------------------------------------------------
  107. void TextureHandle::setFilter( const GLuint filter )
  108. {
  109. // Finish if no object.
  110. if (object == NULL )
  111. return;
  112. // Set filter.
  113. object->mFilter = filter;
  114. // Finish if no GL texture name.
  115. if ( object->mGLTextureName == 0 )
  116. return;
  117. // Set texture state.
  118. glBindTexture( GL_TEXTURE_2D, object->mGLTextureName );
  119. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter );
  120. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter );
  121. }
  122. //-----------------------------------------------------------------------------
  123. void TextureHandle::setClamp( const bool clamp )
  124. {
  125. // Finish if no object.
  126. if (object == NULL )
  127. return;
  128. // Set clamp.
  129. object->mClamp = clamp;
  130. // Finish if no GL texture name.
  131. if ( object->mGLTextureName == 0 )
  132. return;
  133. // Set texture state.
  134. glBindTexture(GL_TEXTURE_2D, object->mGLTextureName);
  135. GLenum glClamp;
  136. if ( clamp )
  137. glClamp = dglDoesSupportEdgeClamp() ? GL_CLAMP_TO_EDGE : GL_CLAMP;
  138. else
  139. glClamp = GL_REPEAT;
  140. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, glClamp );
  141. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, glClamp );
  142. }
  143. //-----------------------------------------------------------------------------
  144. void TextureHandle::lock( void )
  145. {
  146. if ( object == NULL )
  147. return;
  148. object->mRefCount++;
  149. }
  150. //-----------------------------------------------------------------------------
  151. void TextureHandle::unlock( void )
  152. {
  153. // Finish if the texture manager is not active or the handle does not represent an object.
  154. // Do nothing if the manager isn't active or we do not have an object
  155. if ( IsNull() || TextureManager::getManagerState() == TextureManager::NotInitialized )
  156. return;
  157. if( --object->mRefCount == 0 )
  158. {
  159. TextureManager::freeTexture(object);
  160. }
  161. object = NULL;
  162. }