gfxTextureHandle.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifndef _GFXTEXTUREHANDLE_H_
  23. #define _GFXTEXTUREHANDLE_H_
  24. #ifndef _GFXTEXTUREOBJECT_H_
  25. #include "gfx/gfxTextureObject.h"
  26. #endif
  27. #ifndef _MPOINT2_H_
  28. #include "math/mPoint2.h"
  29. #endif
  30. class GFXTextureProfile;
  31. /// A reference counted handle to a texture resource.
  32. class GFXTexHandle : public StrongRefPtr<GFXTextureObject>
  33. {
  34. public:
  35. GFXTexHandle() {}
  36. GFXTexHandle( GFXTextureObject* obj );
  37. GFXTexHandle( const GFXTexHandle &handle, const String &desc );
  38. // load texture
  39. GFXTexHandle( const String &texName, GFXTextureProfile *profile, const String &desc );
  40. bool set( const String &texName, GFXTextureProfile *profile, const String &desc );
  41. // load composite
  42. GFXTexHandle(const String &texNameR, const String &texNameG, const String &texNameB, const String &texNameA, U32 inputKey[4], GFXTextureProfile *profile, const String &desc);
  43. bool set( const String &texNameR, const String &texNameG, const String &texNameB, const String &texNameA, U32 inputKey[4], GFXTextureProfile *profile, const String &desc );
  44. // register texture
  45. GFXTexHandle( GBitmap *bmp, GFXTextureProfile *profile, bool deleteBmp, const String &desc );
  46. bool set( GBitmap *bmp, GFXTextureProfile *profile, bool deleteBmp, const String &desc );
  47. GFXTexHandle( DDSFile *bmp, GFXTextureProfile *profile, bool deleteDDS, const String &desc );
  48. bool set( DDSFile *bmp, GFXTextureProfile *profile, bool deleteDDS, const String &desc );
  49. // Sized bitmap
  50. GFXTexHandle( U32 width, U32 height, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels = 1, S32 antialiasLevel = 0);
  51. bool set( U32 width, U32 height, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels = 1, S32 antialiasLevel = 0);
  52. bool set( U32 width, U32 height, U32 depth, GFXFormat format, GFXTextureProfile* profile, const String& desc, U32 numMipLevels = 1);
  53. /// Returns the width and height as a point.
  54. Point2I getWidthHeight() const { return getPointer() ? Point2I( getPointer()->getWidth(), getPointer()->getHeight() ) : Point2I::Zero; }
  55. U32 getWidth() const { return getPointer() ? getPointer()->getWidth() : 0; }
  56. U32 getHeight() const { return getPointer() ? getPointer()->getHeight() : 0; }
  57. U32 getDepth() const { return getPointer() ? getPointer()->getDepth() : 0; }
  58. GFXFormat getFormat() const { return getPointer() ? getPointer()->getFormat() : GFXFormat_COUNT; }
  59. /// Reloads the texture.
  60. /// @see GFXTextureManager::reloadTexture
  61. void refresh();
  62. /// Releases the texture handle.
  63. void free() { StrongObjectRef::set( NULL ); }
  64. GFXLockedRect *lock( U32 mipLevel = 0, RectI *inRect = NULL )
  65. {
  66. return getPointer()->lock(mipLevel, inRect);
  67. }
  68. void unlock( U32 mipLevel = 0)
  69. {
  70. getPointer()->unlock(mipLevel);
  71. }
  72. // copy to bitmap. see gfxTetureObject.h for description of what types of textures
  73. // can be copied into bitmaps. returns true if successful, false otherwise
  74. bool copyToBmp(GBitmap* bmp) { return getPointer() ? getPointer()->copyToBmp(bmp) : false; }
  75. //---------------------------------------------------------------------------
  76. // Operator overloads
  77. //---------------------------------------------------------------------------
  78. GFXTexHandle& operator=(const GFXTexHandle &t)
  79. {
  80. StrongObjectRef::set(t.getPointer());
  81. return *this;
  82. }
  83. GFXTexHandle& operator=( GFXTextureObject *to)
  84. {
  85. StrongObjectRef::set(to);
  86. return *this;
  87. }
  88. bool operator==(const GFXTexHandle &t) const { return t.getPointer() == getPointer(); }
  89. bool operator!=(const GFXTexHandle &t) const { return t.getPointer() != getPointer(); }
  90. /// Returns the texture object.
  91. operator GFXTextureObject*() const { return getPointer(); }
  92. /// Returns the backing bitmap for this texture.
  93. GBitmap* getBitmap() { return getPointer() ? getPointer()->getBitmap() : NULL; }
  94. const GBitmap* getBitmap() const { return getPointer() ? getPointer()->getBitmap() : NULL; }
  95. /// Helper 2x2 R8G8B8A8 texture filled with 0.
  96. static GFXTexHandle ZERO;
  97. /// Helper 2x2 R8G8B8A8 texture filled with 255.
  98. static GFXTexHandle ONE;
  99. /// Helper 2x2 R8G8B8A8 normal map texture filled
  100. /// with 128, 128, 255.
  101. static GFXTexHandle ZUP;
  102. };
  103. #endif // _GFXTEXTUREHANDLE_H_