⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtexmanager.h

📁 五行MMORPG引擎系统V1.0
💻 H
📖 第 1 页 / 共 2 页
字号:
/// glBindTexture(GL_TEXTURE_2D, handle.getGLName());
/// @endcode
/// Now you can begin to draw you texture.  If you havn't already,
/// make sure you make a call to glEnable(GL_TEXTURE_2D);  before
/// you start drawing and a call to glDisable(GL_TEXTURE_2D); when
/// you're done.  Failure to call glEnable will cause the texture not
/// to draw, and failure to call glDisable will probably case
/// an assert in debug mode and ugly artifacts in release.
///
/// If you are going through dgl, all you need is the TextureHandle and
/// some points.  See the dgl documentation for more info on each
/// individual function in the dgl library.  However, most dgl functions
/// will take a TextureObject data type.  And, it just so happens that
/// a TextureHandle has a TextureObject!  It also has an
/// operator TextureObject*(), which lets you cast a TextureHandle to
/// a TextureObject.  That means that all you have to do is ignore
/// the TextureObject parameter and just give it a TextureHandle.
///
/// Some tips on texture performance:
///
/// Instead of using hard-coded paths, use a hook to a console variable.
/// You will probably change the directory structure for your game,
/// and that means that you will have to go back to all of the hardcoded
/// paths and change them by hand, then rebuild the engine.  It is much
/// better to use script variables since they are all in one place and
/// easier to change.
///
/// Add the path string for your texture to the StringTable.  Doing so
/// helps in string lookups and faster string performance.
///
/// Don't create the texture every frame if at all possible.  Make it
/// a global variable if you have to - just don't load every frame.
/// Loading data off of the disk every frame WILL cause massive
/// performance loss and should be avoided at all costs.  This is
/// not to mention the overhead of generating mip map levels
/// and uploading the texture into memory when the texture is created.
///
/// @note
/// Texture handles can be allocated in 2 ways - by name to be loaded
/// from disk, or by name to a dynamically generated texture
///
/// If you create a GBitmap and register it, the Texture manager
/// owns the pointer - so if you re-register a texture with the same
/// name, the texture manager will delete the second copy.
///
/// Also note the operator TextureObject*, as you can actually cast
/// a TextureHandle to a TextureObject* if necessary.
class TextureHandle
{
   friend class DynamicTexture;

   TextureObject *object;
   void lock();
   void unlock();

  public:
   TextureHandle() { object = NULL; }

   TextureHandle(TextureObject *to)
   {
      object = to;
      lock();
   }

   TextureHandle(const TextureHandle &th) 
   {
      object = th.object;
      lock();
   }

   TextureHandle(const char*       textureName,
                 TextureHandleType type,                 // was =BitmapTexture - dc removed to eliminate overload confusion.
                 bool              clampToEdge = false)
   {
      object = TextureManager::loadTexture(textureName, type, clampToEdge);
      lock();
   }

   TextureHandle(const char*    textureName,
                 const GBitmap* bmp,
                 bool           clampToEdge = false) 
   {
      object = TextureManager::registerTexture(textureName, bmp, clampToEdge);
      lock();
   }

   TextureHandle(const char*       textureName,
                 GBitmap*          bmp,
                 TextureHandleType type,
                 bool              clampToEdge = false) 
   {
      object = TextureManager::registerTexture(textureName, bmp, type, clampToEdge);
      lock();
   }

   ~TextureHandle() { unlock(); }

   TextureHandle& operator=(const TextureHandle &t) 
   {
      unlock();
      object = t.object;
      lock();
      return *this;
   }

   bool set(const char *textureName,
            TextureHandleType type=BitmapTexture,
            bool clampToEdge = false) 
   {
      TextureObject* newObject = TextureManager::loadTexture(textureName, type, clampToEdge);
      if (newObject != object)
      {
         unlock();
         object = newObject;
         lock();
      }
      return (object != NULL);
   }

   bool set(const char *textureName,
            const GBitmap *data,
            bool clampToEdge = false) 
   {
      TextureObject* newObject = TextureManager::registerTexture(textureName, data, clampToEdge);
      if (newObject != object)
      {
         unlock();
         object = newObject;
         lock();
      }
      return (object != NULL);
   }

   bool set(const char *textureName,
            GBitmap *bmp,
            TextureHandleType type,
            bool clampToEdge = false) 
   {
      TextureObject* newObject = TextureManager::registerTexture(textureName, bmp, type, clampToEdge);
      if (newObject != object)
      {
         unlock();
         object = newObject;
         lock();
      }
      return (object != NULL);
   }

   bool operator==(const TextureHandle &t) const { return t.object == object; }
   bool operator!=(const TextureHandle &t) const { return t.object != object; }

   void setClamp(const bool);
   void setFilterNearest();

   void refresh();
   void refresh(GBitmap*);
   operator TextureObject*()        { return object; }
   /// Returns the texture's filename if it exists
   const char* getName() const      { return (object ? object->texFileName      : NULL); }
   U32 getWidth() const             { return (object ? object->bitmapWidth      : 0UL);    }
   U32 getHeight() const            { return (object ? object->bitmapHeight     : 0UL);    }
   U32 getDownloadedWidth() const   { return (object ? object->downloadedWidth  : 0UL);    }
   U32 getDownloadedHeight() const  { return (object ? object->downloadedHeight : 0UL);    }
   GBitmap* getBitmap()             { return (object ? object->bitmap           : NULL); }
   /// Gets the OpenGL index of the texture for use in glBindTexture().
   U32 getGLName() const;
};

#if defined(TORQUE_GATHER_METRICS) && TORQUE_GATHER_METRICS > 1
#ifndef _PLATFORMGL_H_
#if defined(TORQUE_OS_MAC)
#include "PlatformMacCarb/platformGL.h"
#elif defined(TORQUE_OS_WIN32)
#include "PlatformWin32/platformGL.h"
#endif
#endif

inline U32 TextureHandle::getGLName() const
{
   if (!object)
      return 0;

   U32 useName = object->texGLName;
   if (TextureManager::areSmallTexturesActive() && object->smallTexGLName != 0)
      useName = object->smallTexGLName;

   if (useName != 0) 
   {
      GLboolean res;
      glAreTexturesResident(1, &useName, &res);
      if (res == GL_FALSE)
         TextureManager::smTextureCacheMisses++;
   }

   return useName;
}

#else

inline U32 TextureHandle::getGLName() const
{
   if (!object)
      return 0;

   U32 useName = object->texGLName;
   if (TextureManager::areSmallTexturesActive() && object->smallTexGLName != 0)
      useName = object->smallTexGLName;

   return useName;
}

#endif

#endif // _GTEXMANAGER_H_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -