TextureHandle.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef _TEXTURE_HANDLE_H_
  23. #define _TEXTURE_HANDLE_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _PLATFORMGL_H_
  28. #include "platform/platformAssert.h"
  29. #include "platform/platformGL.h"
  30. #endif
  31. //-----------------------------------------------------------------------------
  32. class GBitmap;
  33. class TextureObject;
  34. //------------------------------------------------------------------------------
  35. /// This is the main texture manager interface. Texturing can be
  36. /// a bit complicated, but if you follow these easy steps, it is
  37. /// really quite simple!
  38. ///
  39. /// In order to use a texture on disk, first you must create a
  40. /// TextureHandle data structure for it.
  41. /// @code
  42. /// TextureHandle handle = TextureHandle("pathToTexture", textureType);
  43. /// @endcode
  44. /// See the documentation on the different enumerated types for more info
  45. /// on texture types.
  46. ///
  47. /// Ok, now you have your texture loaded into video memory or ram,
  48. /// whichever is chooses. In order to tell OpenGL to use your texture,
  49. /// you have to bind it. GL_TEXTURE_2D is the type of texture you're
  50. /// binding - a 2 dimisional texture. Also note that you only have
  51. /// to do this if you are using direct OpenGL commands to draw rather
  52. /// than dgl. Dgl manages the below on it's own so you don't have to worry about it.
  53. /// @code
  54. /// glBindTexture(GL_TEXTURE_2D, handle.getGLName());
  55. /// @endcode
  56. /// Now you can begin to draw you texture. If you havn't already,
  57. /// make sure you make a call to glEnable(GL_TEXTURE_2D); before
  58. /// you start drawing and a call to glDisable(GL_TEXTURE_2D); when
  59. /// you're done. Failure to call glEnable will cause the texture not
  60. /// to draw, and failure to call glDisable will probably case
  61. /// an assert in debug mode and ugly artifacts in release.
  62. ///
  63. /// If you are going through dgl, all you need is the TextureHandle and
  64. /// some points. See the dgl documentation for more info on each
  65. /// individual function in the dgl library. However, most dgl functions
  66. /// will take a TextureObject data type. And, it just so happens that
  67. /// a TextureHandle has a TextureObject! It also has an
  68. /// operator TextureObject*(), which lets you cast a TextureHandle to
  69. /// a TextureObject. That means that all you have to do is ignore
  70. /// the TextureObject parameter and just give it a TextureHandle.
  71. ///
  72. /// Some tips on texture performance:
  73. ///
  74. /// Instead of using hard-coded paths, use a hook to a console variable.
  75. /// You will probably change the directory structure for your game,
  76. /// and that means that you will have to go back to all of the hardcoded
  77. /// paths and change them by hand, then rebuild the engine. It is much
  78. /// better to use script variables since they are all in one place and
  79. /// easier to change.
  80. ///
  81. /// Add the path string for your texture to the StringTable. Doing so
  82. /// helps in string lookups and faster string performance.
  83. ///
  84. /// Don't create the texture every frame if at all possible. Make it
  85. /// a global variable if you have to - just don't load every frame.
  86. /// Loading data off of the disk every frame WILL cause massive
  87. /// performance loss and should be avoided at all costs. This is
  88. /// not to mention the overhead of generating mip map levels
  89. /// and uploading the texture into memory when the texture is created.
  90. ///
  91. /// @note
  92. /// Texture handles can be allocated in 2 ways - by name to be loaded
  93. /// from disk, or by name to a dynamically generated texture
  94. ///
  95. /// If you create a GBitmap and register it, the Texture manager
  96. /// owns the pointer - so if you re-register a texture with the same
  97. /// name, the texture manager will delete the second copy.
  98. ///
  99. /// Also note the operator TextureObject*, as you can actually cast
  100. /// a TextureHandle to a TextureObject* if necessary.
  101. class TextureHandle
  102. {
  103. public:
  104. enum TextureHandleType
  105. {
  106. // Invalid texture.
  107. InvalidTexture = 0,
  108. /// Bitmap that will be unloaded after texture has been uploaded.
  109. /// The bitmap will need reloading if the textures need to be restored.
  110. /// This is the preferred type.
  111. BitmapTexture = 100,
  112. /// Same as BitmapTexture except that the bitmap is kept which occupies main memory however
  113. /// it does not require loading if textures need to be restored.
  114. BitmapKeepTexture = 200,
  115. };
  116. public:
  117. TextureHandle() : object( NULL ) {}
  118. TextureHandle( TextureObject *to )
  119. {
  120. object = to;
  121. lock();
  122. }
  123. TextureHandle( const TextureHandle &th )
  124. {
  125. object = th.object;
  126. lock();
  127. }
  128. TextureHandle(const char* textureKey, TextureHandleType type, bool clampToEdge = false, bool force16Bit = false );
  129. TextureHandle(const char* textureKey, GBitmap* bmp, TextureHandleType type, bool clampToEdge = false);
  130. ~TextureHandle() { unlock(); }
  131. TextureHandle& operator=(const TextureHandle &t)
  132. {
  133. unlock();
  134. object = t.object;
  135. lock();
  136. return *this;
  137. }
  138. bool set(const char* pTextureKey, TextureHandleType type = BitmapTexture, bool clampToEdge = false, bool force16Bit = false );
  139. bool set(const char* pTextureKey, GBitmap *bmp, TextureHandleType type, bool clampToEdge = false);
  140. bool operator==( const TextureHandle& handle ) const { return handle.object == object; }
  141. bool operator!=( const TextureHandle& handle ) const { return handle.object != object; }
  142. void setClamp( const bool clamp );
  143. void setFilter( const GLuint filter );
  144. void clear( void ) { unlock(); }
  145. void refresh( void );
  146. operator TextureObject*() { return object; }
  147. inline bool NotNull( void ) const { return object != NULL; }
  148. inline bool IsNull( void ) const { return object == NULL; }
  149. const char* getTextureKey( void ) const;
  150. U32 getWidth( void ) const;
  151. U32 getHeight( void ) const;
  152. GBitmap* getBitmap( void );
  153. const GBitmap* getBitmap( void ) const;
  154. U32 getGLName( void ) const;
  155. private:
  156. void lock( void );
  157. void unlock( void );
  158. private:
  159. TextureObject *object;
  160. };
  161. //-----------------------------------------------------------------------------
  162. extern TextureHandle BadTextureHandle;
  163. #endif // _TEXTURE_HANDLE_H_