gfxTextureHandle.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platform.h"
  23. #include "gfx/gfxTextureHandle.h"
  24. #include "gfx/gfxDevice.h"
  25. #include "gfx/gfxTextureManager.h"
  26. GFXTexHandle GFXTexHandle::ZERO;
  27. GFXTexHandle GFXTexHandle::ONE;
  28. GFXTexHandle GFXTexHandle::ZUP;
  29. GFXTexHandle::GFXTexHandle( GFXTextureObject *obj )
  30. {
  31. StrongObjectRef::set( obj );
  32. }
  33. GFXTexHandle::GFXTexHandle( const GFXTexHandle &handle, const String &desc )
  34. {
  35. StrongObjectRef::set( handle.getPointer() );
  36. #ifdef TORQUE_DEBUG
  37. if ( getPointer() )
  38. getPointer()->mDebugDescription = desc;
  39. #endif
  40. }
  41. GFXTexHandle::GFXTexHandle( const String &texName, GFXTextureProfile *profile, const String &desc )
  42. {
  43. set( texName, profile, desc );
  44. }
  45. GFXTexHandle::GFXTexHandle(const String &texNameR, const String &texNameG, const String &texNameB, const String &texNameA, U32 inputKey[4], GFXTextureProfile *profile, const String &desc)
  46. {
  47. set(texNameR, texNameG, texNameB, texNameA, inputKey, profile, desc);
  48. }
  49. bool GFXTexHandle::set( const String &texName, GFXTextureProfile *profile, const String &desc )
  50. {
  51. // Clear the existing texture first, so that
  52. // its memory is free for the new allocation.
  53. free();
  54. // Create and set the new texture.
  55. AssertFatal( texName.isNotEmpty(), "Texture name is empty" );
  56. StrongObjectRef::set( TEXMGR->createTexture( texName, profile ) );
  57. #ifdef TORQUE_DEBUG
  58. if ( getPointer() )
  59. getPointer()->mDebugDescription = desc;
  60. #endif
  61. return isValid();
  62. }
  63. bool GFXTexHandle::set(const String &texNameR, const String &texNameG, const String &texNameB, const String &texNameA, U32 inputKey[4], GFXTextureProfile *profile, const String &desc)
  64. {
  65. // Clear the existing texture first, so that
  66. // its memory is free for the new allocation.
  67. free();
  68. // Create and set the new texture.
  69. AssertFatal( texNameR.isNotEmpty(), "Texture name is empty" );
  70. StrongObjectRef::set( TEXMGR->createCompositeTexture( texNameR, texNameG, texNameB, texNameA, inputKey, profile ) );
  71. #ifdef TORQUE_DEBUG
  72. if ( getPointer() )
  73. getPointer()->mDebugDescription = desc;
  74. #endif
  75. return isValid();
  76. }
  77. GFXTexHandle::GFXTexHandle( GBitmap *bmp, GFXTextureProfile *profile, bool deleteBmp, const String &desc )
  78. {
  79. set( bmp, profile, deleteBmp, desc );
  80. }
  81. bool GFXTexHandle::set( GBitmap *bmp, GFXTextureProfile *profile, bool deleteBmp, const String &desc )
  82. {
  83. // Clear the existing texture first, so that
  84. // its memory is free for the new allocation.
  85. free();
  86. // Create and set the new texture.
  87. AssertFatal( bmp, "Bitmap is NULL" );
  88. StrongObjectRef::set( TEXMGR->createTexture( bmp, String(), profile, deleteBmp ) );
  89. #ifdef TORQUE_DEBUG
  90. if ( getPointer() )
  91. getPointer()->mDebugDescription = desc;
  92. #endif
  93. return isValid();
  94. }
  95. GFXTexHandle::GFXTexHandle( DDSFile *dds, GFXTextureProfile *profile, bool deleteDDS, const String &desc )
  96. {
  97. set( dds, profile, deleteDDS, desc );
  98. }
  99. bool GFXTexHandle::set( DDSFile *dds, GFXTextureProfile *profile, bool deleteDDS, const String &desc )
  100. {
  101. // Clear the existing texture first, so that
  102. // its memory is free for the new allocation.
  103. free();
  104. // Create and set the new texture.
  105. AssertFatal( dds, "Bitmap is NULL" );
  106. StrongObjectRef::set( TEXMGR->createTexture( dds, profile, deleteDDS ) );
  107. #ifdef TORQUE_DEBUG
  108. if ( getPointer() )
  109. getPointer()->mDebugDescription = desc;
  110. #endif
  111. return isValid();
  112. }
  113. GFXTexHandle::GFXTexHandle( U32 width, U32 height, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels, S32 antialiasLevel)
  114. {
  115. set( width, height, format, profile, desc, numMipLevels, antialiasLevel );
  116. }
  117. bool GFXTexHandle::set( U32 width, U32 height, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels, S32 antialiasLevel)
  118. {
  119. // Clear the existing texture first, so that
  120. // its memory is free for the new allocation.
  121. free();
  122. // Create and set the new texture.
  123. StrongObjectRef::set( TEXMGR->createTexture( width, height, format, profile, numMipLevels, antialiasLevel ) );
  124. #ifdef TORQUE_DEBUG
  125. if ( getPointer() )
  126. getPointer()->mDebugDescription = desc;
  127. #endif
  128. return isValid();
  129. }
  130. bool GFXTexHandle::set(U32 width, U32 height, U32 depth, GFXFormat format, GFXTextureProfile* profile, const String& desc, U32 numMipLevels)
  131. {
  132. // Clear the existing texture first, so that
  133. // its memory is free for the new allocation.
  134. free();
  135. // Create and set the new texture.
  136. StrongObjectRef::set(TEXMGR->createTexture(width, height, depth, format, profile, numMipLevels));
  137. #ifdef TORQUE_DEBUG
  138. if ( getPointer() )
  139. getPointer()->mDebugDescription = desc;
  140. #endif
  141. return isValid();
  142. }
  143. void GFXTexHandle::refresh()
  144. {
  145. TEXMGR->reloadTexture( getPointer() );
  146. }