My Project
TextureEntity.h
1 #pragma once
2 #include "AssetEntity.h"
3 
4 namespace ParaEngine
5 {
6  class ImageEntity;
7 
9  {
11  : FocalLength(0.0f)
12  {
13  }
14 
15  // lens focal length, unit is mm
16  double FocalLength;
17  };
18 
19 
20 
29  struct TextureEntity : public AssetEntity
30  {
31  private:
32  friend class CTextureProcessor;
33  public:
35  static const std::string DEFAULT_STATIC_TEXTURE;
36 
42  static int g_nTextureLOD;
43 
47 
48  /* get image format by filename
49  * @return : -1 unknown, 24 dds, 13 png, 2 jpg, 17 tga
50  */
51  static int GetFormatByFileName(const std::string& filename);
52  public:
53  virtual AssetEntity::AssetType GetType(){ return AssetEntity::texture; };
54 
56  {
59  RenderTarget = 0,
80  } SurfaceType;
81 
83  struct TextureInfo
84  {
85  static const TextureInfo Empty;
86  int m_width, m_height;
87 
88  enum TEXTURE_FORMAT{
89  FMT_A8R8G8B8,
90  FMT_ALPHA8,
91  FMT_X8R8G8B8,
92  FMT_UNKNOWN
93  }m_format;
94 
95  enum SUB_TYPE{
96  TYPE_SCREENSIZE,
97  TYPE_UNKNOWN
98  }m_subtype;
99 
100  public:
101  TextureInfo(int width, int height, TEXTURE_FORMAT format, SUB_TYPE subtype)
102  : m_width(width), m_height(height), m_format(format), m_subtype(subtype){};
103  TextureInfo()
104  : m_width(0), m_height(0), m_format(FMT_UNKNOWN), m_subtype(TYPE_UNKNOWN){};
105  TextureInfo(const TextureInfo& t)
106  : m_width(t.m_width), m_height(t.m_height), m_format(t.m_format), m_subtype(t.m_subtype){};
107  virtual ~TextureInfo(){};
108 
109  TEXTURE_FORMAT GetFormat() const { return m_format; }
110  int GetHeight() const { return m_height; }
111  int GetWidth() const { return m_width; }
112  };
113 
117  float m_fFPS;
118  int m_nFrameCount;
119  int m_nCurrentFrameIndex;
122  public:
123  AnimatedTextureInfo() :m_fFPS(15.f), m_nFrameCount(0), m_nCurrentFrameIndex(0), m_bAutoAnimation(true){};
124  virtual ~AnimatedTextureInfo(){};
125  };
126 
128  union
129  {
134  };
135 
137  char* m_pRawData;
140 
141  // this value will be increased by one every time GetTexture() or GetSurface() is called.
142  int32 m_nHitCount;
143  // the color key(default to 0): Color value to replace with transparent black, or 0 to disable the color key. This is always a 32-bit ARGB color,
144  // independent of the source image format. Alpha is significant and should usually be set to FF for opaque color keys.
145  // Thus, for opaque black, the value would be equal to 0xFF000000. COLOR_XRGB(0,0,0)
146  Color m_dwColorKey;
147 
150 
151  public:
152  TextureEntity(const AssetKey& key);
153  TextureEntity();
154  virtual ~TextureEntity();
155 
156  // TODO: remove this function from base class after refactoring.
157  // the caller needs to cast to implementation class to use this function.
159  virtual DeviceTexturePtr_type GetTexture() { return 0; };
160 
161  virtual HRESULT InitDeviceObjects();
162  virtual HRESULT RestoreDeviceObjects();
163  virtual HRESULT InvalidateDeviceObjects();
164  virtual HRESULT DeleteDeviceObjects();
168  virtual HRESULT LoadFromMemory(const char* buffer, DWORD nFileSize, UINT nMipLevels, D3DFORMAT dwTextureFormat = D3DFMT_UNKNOWN, void** ppTexture = NULL) { return E_FAIL; };
169 
170 
175  virtual bool LoadFromImage(ImageEntity * image, D3DFORMAT dwTextureFormat = D3DFMT_UNKNOWN, UINT nMipLevels = 0, void** ppTexture = NULL);
176 
177 
188  virtual HRESULT CreateTextureFromFile_Async(void* pContext, RenderDevicePtr pDev = NULL, const char* sFileName = NULL, void** ppTexture = NULL, D3DFORMAT dwTextureFormat = D3DFMT_UNKNOWN, UINT nMipLevels = D3DX_DEFAULT, Color dwColorKey = 0);
189 
191  bool IsAsyncLoad() const;
192  void SetAsyncLoad(bool val);
193 
194  // make this texture render target.
195  virtual bool SetRenderTarget(int nIndex = 0) { return false; };
196 
198  virtual void SetSamplerStateBlocky(bool bIsBlocky);
199  virtual bool IsSamplerStateBlocky();
200 
201  // the color key(default to 0): Color value to replace with transparent black, or 0 to disable the color key. This is always a 32-bit ARGB color,
202  // independent of the source image format. Alpha is significant and should usually be set to FF for opaque color keys.
203  // Thus, for opaque black, the value would be equal to 0xFF000000. COLOR_XRGB(0,0,0)
204  void SetColorKey(Color colorKey);
205 
206  Color GetColorKey();
207 
212  virtual bool IsLoaded();
213 
215  bool IsPending();
216 
219  void SetRawData(char* pData, int nSize);
221  char* GetRawData();
223  int GetRawDataSize();
225  bool GiveupRawDataOwnership();
226 
232  virtual void SetTextureInfo(const TextureInfo& tInfo);
236  virtual const TextureInfo* GetTextureInfo();
237 
238  virtual int32 GetWidth();
239  virtual int32 GetHeight();
240 
244  virtual bool IsFlipY();
245 
246  /* this value will be increased by one every time GetTexture() or GetSurface() is called.
247  * normally we never needs to care about this. Except in some rare cases, such as the mini scene graph needs to know if its being used by others in the last frame. */
248  int GetHitCount(){ return m_nHitCount; }
249 
250  /* this value will be increased by one every time GetTexture() or GetSurface() is called.
251  * normally we never needs to care about this. Except in some rare cases, such as the mini scene graph needs to know if its being used by others in the last frame. */
252  void SetHitCount(int nHitCount){ m_nHitCount = nHitCount; }
253 
256  void MakeInvalid();
257 
263  void Refresh(const char* sFilename = NULL, bool bLazyLoad = false);
264 
269  AnimatedTextureInfo* GetAnimatedTextureInfo();
270 
275  void SetTextureFPS(float FPS);
280  void EnableTextureAutoAnimation(bool bEnable);
285  void SetCurrentFrameNumber(int nFrame);
290  int GetCurrentFrameNumber();
291 
296  int GetFrameCount();
297 
301  virtual bool SaveToFile(const char* filename, D3DFORMAT dwFormat, int width, int height, UINT MipLevels = 1, DWORD Filter = D3DX_DEFAULT, Color ColorKey = 0);;
302 
311  static void LoadImage(char *sBufMemFile, int sizeBuf, int &width, int &height, byte ** ppBuffer, bool bAlpha);
316  static bool LoadImageOfFormat(const std::string& sTextureFileName, char *sBufMemFile, int sizeBuf, int &width, int &height, byte ** ppBuffer, int* pBytesPerPixel = NULL, int nFormat = -1);
317  static bool LoadImageOfFormatEx(const std::string& sTextureFileName, char *sBufMemFile, int sizeBuf, int &width, int &height, byte ** ppBuffer, int* pBytesPerPixel = NULL, int nFormat = -1, ImageExtendInfo *info = nullptr);
318 
320  static TextureEntity* CreateTexture(const uint8 * pTexels, int width, int height, int rowLength, int bytesPerPixel, uint32 nMipLevels = 0, D3DPOOL dwCreatePool= D3DPOOL_MANAGED, DWORD nFormat = 0);
322  static TextureEntity* CreateTexture(const char* pFileName, uint32 nMipLevels = 0, D3DPOOL dwCreatePool = D3DPOOL_MANAGED);
323  };
324 }
325 
326 // chose an implementation as Texture Manager
327 #ifdef USE_DIRECTX_RENDERER
328 #include "TextureEntityDirectX.h"
329 #elif defined(USE_OPENGL_RENDERER)
330 #include "TextureEntityOpenGL.h"
331 #else
332 namespace ParaEngine{
334 }
335 #endif
Which DXT Compression to Use? Obviously, there are some trade-offs between the different formats whic...
Definition: TextureEntity.h:29
AnimatedTextureInfo * m_pAnimatedTextureInfo
only animated texture use this type.
Definition: TextureEntity.h:133
bool m_bAutoAnimation
if true, the animation texture will be automatically animated.
Definition: TextureEntity.h:121
CTextureProcessor implementation of IDataProcessor.
Definition: ContentLoaderTexture.h:71
detailed terrain texture
Definition: TextureEntity.h:69
HTML renderer textures, <html>name:initial page url.
Definition: TextureEntity.h:75
a group of textures, such as xxx_a001.jpg, ..., xxx_a009.jpg
Definition: TextureEntity.h:63
virtual DeviceTexturePtr_type GetTexture()
Get the texture for rendering.
Definition: TextureEntity.h:159
AssetType
each asset type has a unique asset type number
Definition: AssetEntity.h:82
basic texture information
Definition: TextureEntity.h:83
different physics engine has different winding order.
Definition: EventBinding.h:32
cube texture for environment mapping, etc.
Definition: TextureEntity.h:71
normal managed texture, with all mip-mapping level created
Definition: TextureEntity.h:61
for animated texture
Definition: TextureEntity.h:115
a very thin wrapper to DirectX device with a portable version of openGL implementation.
Definition: RenderDeviceDirectX.h:10
BLP textures.
Definition: TextureEntity.h:67
char * m_pRawData
raw data
Definition: TextureEntity.h:137
AssetManager manages a set of asset entities of a certain type.
Definition: AssetManager.h:13
Definition: TextureEntity.h:8
a pure texture surface
Definition: TextureEntity.h:77
flash textures, such as swf and flv streaming video files, etc.
Definition: TextureEntity.h:73
TextureInfo * m_pTextureInfo
currently all surface types except the animated texture use this struct
Definition: TextureEntity.h:131
virtual HRESULT LoadFromMemory(const char *buffer, DWORD nFileSize, UINT nMipLevels, D3DFORMAT dwTextureFormat=D3DFMT_UNKNOWN, void **ppTexture=NULL)
load from memory buffer.
Definition: TextureEntity.h:168
depth stencil surface
Definition: TextureEntity.h:79
int32 m_nRawDataSize
raw data size
Definition: TextureEntity.h:139
_SurfaceType
Definition: TextureEntity.h:55
static int g_nTextureLOD
Most detailed level-of-detail value to set for the mipmap chain.
Definition: TextureEntity.h:42
static bool g_bEnable32bitsTexture
whether we will load files whose name ends with _32bits as 32 bits textures.
Definition: TextureEntity.h:46
float m_fFPS
default value is 15
Definition: TextureEntity.h:117
static const std::string DEFAULT_STATIC_TEXTURE
default texture to replaced downloading one.
Definition: TextureEntity.h:35
std::string AssetKey
the unique key object for asset entity.
Definition: AssetEntity.h:13
Definition: ParaColor.h:275
texture in memory
Definition: TextureEntity.h:65
Unlike TextureEntity, ImageEntity is a device independent entity in memory.
Definition: ImageEntity.h:9
bool m_bAsyncLoad
whether to async loading the texture.
Definition: TextureEntity.h:149
Base class for managed asset entity in ParaEngine.
Definition: AssetEntity.h:25