gfxTextureHandle.h
Engine/source/gfx/gfxTextureHandle.h
Classes:
class
A reference counted handle to a texture resource.
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _GFXTEXTUREHANDLE_H_ 25#define _GFXTEXTUREHANDLE_H_ 26 27#ifndef _GFXTEXTUREOBJECT_H_ 28#include "gfx/gfxTextureObject.h" 29#endif 30#ifndef _MPOINT2_H_ 31#include "math/mPoint2.h" 32#endif 33 34 35class GFXTextureProfile; 36 37 38/// A reference counted handle to a texture resource. 39class GFXTexHandle : public StrongRefPtr<GFXTextureObject> 40{ 41public: 42 GFXTexHandle() {} 43 GFXTexHandle( GFXTextureObject* obj ); 44 GFXTexHandle( const GFXTexHandle &handle, const String &desc ); 45 46 // load texture 47 GFXTexHandle( const String &texName, GFXTextureProfile *profile, const String &desc ); 48 bool set( const String &texName, GFXTextureProfile *profile, const String &desc ); 49 50 // register texture 51 GFXTexHandle( GBitmap *bmp, GFXTextureProfile *profile, bool deleteBmp, const String &desc ); 52 bool set( GBitmap *bmp, GFXTextureProfile *profile, bool deleteBmp, const String &desc ); 53 54 GFXTexHandle( DDSFile *bmp, GFXTextureProfile *profile, bool deleteDDS, const String &desc ); 55 bool set( DDSFile *bmp, GFXTextureProfile *profile, bool deleteDDS, const String &desc ); 56 57 // Sized bitmap 58 GFXTexHandle( U32 width, U32 height, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels = 1, S32 antialiasLevel = 0); 59 bool set( U32 width, U32 height, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels = 1, S32 antialiasLevel = 0); 60 bool set( U32 width, U32 height, U32 depth, void *pixels, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels = 1 ); 61 62 /// Returns the width and height as a point. 63 Point2I getWidthHeight() const { return getPointer() ? Point2I( getPointer()->getWidth(), getPointer()->getHeight() ) : Point2I::Zero; } 64 65 U32 getWidth() const { return getPointer() ? getPointer()->getWidth() : 0; } 66 U32 getHeight() const { return getPointer() ? getPointer()->getHeight() : 0; } 67 U32 getDepth() const { return getPointer() ? getPointer()->getDepth() : 0; } 68 GFXFormat getFormat() const { return getPointer() ? getPointer()->getFormat() : GFXFormat_COUNT; } 69 70 /// Reloads the texture. 71 /// @see GFXTextureManager::reloadTexture 72 void refresh(); 73 74 /// Releases the texture handle. 75 void free() { StrongObjectRef::set( NULL ); } 76 77 GFXLockedRect *lock( U32 mipLevel = 0, RectI *inRect = NULL ) 78 { 79 return getPointer()->lock(mipLevel, inRect); 80 } 81 82 void unlock( U32 mipLevel = 0) 83 { 84 getPointer()->unlock(mipLevel); 85 } 86 87 // copy to bitmap. see gfxTetureObject.h for description of what types of textures 88 // can be copied into bitmaps. returns true if successful, false otherwise 89 bool copyToBmp(GBitmap* bmp) { return getPointer() ? getPointer()->copyToBmp(bmp) : false; } 90 91 //--------------------------------------------------------------------------- 92 // Operator overloads 93 //--------------------------------------------------------------------------- 94 GFXTexHandle& operator=(const GFXTexHandle &t) 95 { 96 StrongObjectRef::set(t.getPointer()); 97 return *this; 98 } 99 100 GFXTexHandle& operator=( GFXTextureObject *to) 101 { 102 StrongObjectRef::set(to); 103 return *this; 104 } 105 106 bool operator==(const GFXTexHandle &t) const { return t.getPointer() == getPointer(); } 107 bool operator!=(const GFXTexHandle &t) const { return t.getPointer() != getPointer(); } 108 109 /// Returns the texture object. 110 operator GFXTextureObject*() const { return getPointer(); } 111 112 /// Returns the backing bitmap for this texture. 113 GBitmap* getBitmap() { return getPointer() ? getPointer()->getBitmap() : NULL; } 114 const GBitmap* getBitmap() const { return getPointer() ? getPointer()->getBitmap() : NULL; } 115 116 117 /// Helper 2x2 R8G8B8A8 texture filled with 0. 118 static GFXTexHandle ZERO; 119 120 /// Helper 2x2 R8G8B8A8 texture filled with 255. 121 static GFXTexHandle ONE; 122 123 /// Helper 2x2 R8G8B8A8 normal map texture filled 124 /// with 128, 128, 255. 125 static GFXTexHandle ZUP; 126 127}; 128 129 130#endif // _GFXTEXTUREHANDLE_H_ 131
