FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
fplbase::Texture Class Reference

Abstraction for a texture object loaded on the GPU. More...

#include <texture.h>

Inheritance diagram for fplbase::Texture:
fplbase::AsyncAsset fplbase::Asset

Detailed Description

Abstraction for a texture object loaded on the GPU.

Contains functions for loading, marshalling, and disposal of textures.

Public Member Functions

 Texture (const char *filename=nullptr, TextureFormat format=kFormatAuto, TextureFlags flags=kTextureFlagsUseMipMaps)
 Constructor for a Texture.
 
virtual ~Texture ()
 Destructor for a Texture. More...
 
virtual void Load ()
 Loads and unpacks the Texture from filename_ into data_. It also sets the original size, if it has not yet been set.
 
virtual void LoadFromMemory (const uint8_t *data, const mathfu::vec2i &size, TextureFormat texture_format)
 Create a texture from data in memory. More...
 
virtual bool Finalize ()
 Creates a Texture from data_ and stores the handle in id_.
 
bool IsValid ()
 Whether this object loaded and finalized correctly. Call after Finalize has been called (by AssetManager::TryFinalize).
 
void Set (size_t unit, Renderer *renderer)
 Set the active Texture and binds id_ to GL_TEXTURE_2D. More...
 
void Set (size_t unit)
 
void Set (size_t unit, Renderer *renderer) const
 Set the active Texture and binds id_ to GL_TEXTURE_2D. More...
 
void Set (size_t unit) const
 
void Delete ()
 Delete the Texture stored in id_, and reset id_ to 0.
 
void UpdateTexture (size_t unit, TextureFormat format, int xoffset, int yoffset, int width, int height, const void *data)
 Update (part of) the current texture with new pixel data. For now, must always update at least entire row. More...
 
void SetTextureId (TextureTarget target, TextureHandle id)
 Set texture target and id directly for textures that have been created outside of this class. The creator is responsible for deleting the texture id. More...
 
const TextureHandleid () const
 Get the Texture handle ID. More...
 
const mathfu::vec2i & size () const
 Get the Texture size. More...
 
const mathfu::vec2 & scale () const
 Get the Texture scale. More...
 
void set_scale (const mathfu::vec2 &scale)
 Set the Texture scale. More...
 
TextureFlags flags () const
 returns the texture flags.
 
const mathfu::vec2i & original_size () const
 Get the original size of the Texture. More...
 
void set_original_size (const mathfu::vec2i &size)
 Set the original size of the Texture. More...
 
TextureFormat format () const
 Get the Texture format. More...
 
void SetOriginalSizeIfNotYetSet (const mathfu::vec2i &size)
 If the original size has not yet been set, then set it. More...
 
TextureImpl * impl ()
 
- Public Member Functions inherited from fplbase::AsyncAsset
 AsyncAsset ()
 Default constructor for an empty AsyncAsset.
 
 AsyncAsset (const char *filename)
 Construct an AsyncAsset with a given file name. More...
 
virtual ~AsyncAsset ()
 AsyncAsset destructor.
 
bool LoadNow ()
 Performs a synchronous load by calling Load & Finalize. More...
 
void set_filename (const std::string &filename)
 Sets the filename that should be loaded. More...
 
const std::string & filename () const
 The name of the file associated with the resource. More...
 
void AddFinalizeCallback (AssetFinalizedCallback callback)
 Adds a callback to be called when the asset is finalized. More...
 
- Public Member Functions inherited from fplbase::Asset
void IncreaseRefCount ()
 indicate there is an additional owner of this asset. By default, when you call any of the UnLoad*() functions in the AssetManager, that will directly delete the asset since they all start out with a single reference count. Call this function to indicate multiple owners will call Unload*() independently, and only have the asset deleted by the last one.
 

Static Public Member Functions

static uint8_t * UnpackTGA (const void *tga_buf, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
 Unpacks a memory buffer containing a TGA format file. More...
 
static uint8_t * UnpackWebP (const void *webp_buf, size_t size, const mathfu::vec2 &scale, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
 Unpacks a memory buffer containing a Webp format file. More...
 
static uint8_t * UnpackASTC (const void *astc_buf, size_t size, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
 Reads a memory buffer containing an ASTC format (.astc) file. More...
 
static uint8_t * UnpackPKM (const void *file_buf, size_t size, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
 Reads a memory buffer containing an ETC2 format (.pkm) file. More...
 
static uint8_t * UnpackKTX (const void *file_buf, size_t size, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
 Reads a memory buffer containing an KTX format (.ktx) file. More...
 
static uint8_t * UnpackPng (const void *png_buf, size_t size, const mathfu::vec2 &scale, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
 Unpacks a memory buffer containing a Png format file. More...
 
static uint8_t * UnpackJpg (const void *jpg_buf, size_t size, const mathfu::vec2 &scale, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
 Unpacks a memory buffer containing a Jpeg format file. More...
 
static uint8_t * LoadAndUnpackTexture (const char *filename, const mathfu::vec2 &scale, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
 Loads the file in filename, and then unpacks the file format (supports TGA, WebP, KTX, PKM, ASTC). More...
 
static uint16_t * Convert8888To5551 (const uint8_t *buffer, const mathfu::vec2i &size)
 Utility function to convert 32bit RGBA (8-bits each) to 16bit RGB in hex 5551 format. More...
 
static uint16_t * Convert888To565 (const uint8_t *buffer, const mathfu::vec2i &size)
 Utility function to convert 24bit RGB (8-bits each) to 16bit RGB in hex 565 format. More...
 

Additional Inherited Members

- Public Types inherited from fplbase::AsyncAsset
typedef std::function< void()> AssetFinalizedCallback
 A function pointer to an asset loaded callback function.
 
- Protected Member Functions inherited from fplbase::AsyncAsset
void CallFinalizeCallback ()
 Calls app callbacks when an asset is ready to be used. More...
 
- Protected Attributes inherited from fplbase::AsyncAsset
std::string filename_
 The resource file name.
 
const uint8_t * data_
 The resource data.
 
std::vector
< AssetFinalizedCallback
finalize_callbacks_
 

Constructor & Destructor Documentation

virtual fplbase::Texture::~Texture ( )
virtual

Destructor for a Texture.

Note
Calls Delete().

Member Function Documentation

static uint16_t* fplbase::Texture::Convert8888To5551 ( const uint8_t *  buffer,
const mathfu::vec2i &  size 
)
static

Utility function to convert 32bit RGBA (8-bits each) to 16bit RGB in hex 5551 format.

Note
You must delete[] the return value afterwards.
static uint16_t* fplbase::Texture::Convert888To565 ( const uint8_t *  buffer,
const mathfu::vec2i &  size 
)
static

Utility function to convert 24bit RGB (8-bits each) to 16bit RGB in hex 565 format.

Note
You must delete[] the return value afterwards.
TextureFormat fplbase::Texture::format ( ) const
inline

Get the Texture format.

Returns
Returns the texture format.
const TextureHandle& fplbase::Texture::id ( ) const
inline

Get the Texture handle ID.

Returns
Returns the TextureHandle ID.
static uint8_t* fplbase::Texture::LoadAndUnpackTexture ( const char *  filename,
const mathfu::vec2 &  scale,
TextureFlags  flags,
mathfu::vec2i *  dimensions,
TextureFormat texture_format 
)
static

Loads the file in filename, and then unpacks the file format (supports TGA, WebP, KTX, PKM, ASTC).

Note
KTX/PKM/ASTC will automatically fall-back on WebP if the file is not present or not supported by the GPU.
last_error() contains more information if nullptr is returned. You must free() the returned pointer when done.
Parameters
[in]filenameA C-string corresponding to the name of the file containing the Texture.
[in]scaleA scale value must be a power of two to have correct Texture sizes.
[in]flagsTexture flag, allowing premultiply (only webp now)
[out]dimensionsA mathfu::vec2i pointer the captures the Texture width and height.
[out]texture_formatThe format of the returned buffer, always either 888 or 8888.
Returns
Returns a RGBA array of the returned dimensions or nullptr, if the format is not understood.
Note
You must free() on the returned pointer when done.
virtual void fplbase::Texture::LoadFromMemory ( const uint8_t *  data,
const mathfu::vec2i &  size,
TextureFormat  texture_format 
)
virtual

Create a texture from data in memory.

Parameters
[in]dataThe Texture data in memory to load from.
[in]sizeA const mathfu::vec2i reference to the original Texture size x and y components.
[in]texture_formatThe format of data.
const mathfu::vec2i& fplbase::Texture::original_size ( ) const
inline

Get the original size of the Texture.

Returns
Returns a const mathfu::vec2i reference to the original size of the Texture.
const mathfu::vec2& fplbase::Texture::scale ( ) const
inline

Get the Texture scale.

Returns
Returns a const mathfu::vec2 reference to the scale of the Texture.
void fplbase::Texture::Set ( size_t  unit,
Renderer renderer 
)

Set the active Texture and binds id_ to GL_TEXTURE_2D.

Parameters
[in]unitSpecifies which texture unit to make active.
[in]rendererPointer to the Renderer
Note
Modifies global OpenGL state.
void fplbase::Texture::Set ( size_t  unit)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

void fplbase::Texture::Set ( size_t  unit,
Renderer renderer 
) const

Set the active Texture and binds id_ to GL_TEXTURE_2D.

Parameters
[in]unitSpecifies which texture unit to make active.
[in]rendererPointer to the Renderer
Note
Modifies global OpenGL state.
void fplbase::Texture::Set ( size_t  unit) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

void fplbase::Texture::set_original_size ( const mathfu::vec2i &  size)
inline

Set the original size of the Texture.

Parameters
[in]sizeA mathfu::vec2i containing the original Texture x and y sizes.
void fplbase::Texture::set_scale ( const mathfu::vec2 &  scale)
inline

Set the Texture scale.

Parameters
[in]scaleA const mathfu::vec2 reference containing the x and y scale components to set for the Texture.
void fplbase::Texture::SetOriginalSizeIfNotYetSet ( const mathfu::vec2i &  size)
inline

If the original size has not yet been set, then set it.

Parameters
[in]sizeA mathfu::vec2i containing the original Texture x and y sizes.
void fplbase::Texture::SetTextureId ( TextureTarget  target,
TextureHandle  id 
)

Set texture target and id directly for textures that have been created outside of this class. The creator is responsible for deleting the texture id.

Parameters
[in]targetTexture target to use when binding texture to context.
[in]idTexture handle ID.
const mathfu::vec2i& fplbase::Texture::size ( ) const
inline

Get the Texture size.

Returns
Returns a const mathfu::vec2i reference to the size of the Texture.
static uint8_t* fplbase::Texture::UnpackASTC ( const void *  astc_buf,
size_t  size,
TextureFlags  flags,
mathfu::vec2i *  dimensions,
TextureFormat texture_format 
)
static

Reads a memory buffer containing an ASTC format (.astc) file.

Parameters
[in]astc_bufThe ASTC image data.
[in]sizeThe size of the memory block pointed to by astc_buf.
[in]flagsTexture flag, allowing premultiply (only webp now)
[out]dimensionsA mathfu::vec2i pointer the captures the image width and height.
[out]texture_formatThe format of the returned buffer, always kFormatASTC.
Returns
Returns a buffer ready to be uploaded to GPU memory or nullptr, if the format is not understood.
Note
You must free() on the returned pointer when done.
static uint8_t* fplbase::Texture::UnpackJpg ( const void *  jpg_buf,
size_t  size,
const mathfu::vec2 &  scale,
TextureFlags  flags,
mathfu::vec2i *  dimensions,
TextureFormat texture_format 
)
inlinestatic

Unpacks a memory buffer containing a Jpeg format file.

Parameters
[in]jpg_bufThe Jpeg image data.
[in]sizeThe size of the memory block pointed to by data.
[in]scaleA scale value must be a power of two to have correct Texture sizes.
[in]flagsTexture flag, allowing premultiply (only webp now)
[out]dimensionsA mathfu::vec2i pointer the captures the image width and height.
[out]texture_formatPixel format of unpacked image.
Returns
Returns a RGBA array of the returned dimensions or nullptr, if the format is not understood.
Note
You must free() on the returned pointer when done.
static uint8_t* fplbase::Texture::UnpackKTX ( const void *  file_buf,
size_t  size,
TextureFlags  flags,
mathfu::vec2i *  dimensions,
TextureFormat texture_format 
)
static

Reads a memory buffer containing an KTX format (.ktx) file.

Parameters
[in]file_bufthe loaded file.
[in]sizeThe size of the memory block pointed to by file_buf.
[in]flagsTexture flag, allowing premultiply (only webp now)
[out]dimensionsA mathfu::vec2i pointer the captures the image width and height.
[out]texture_formatThe format of the returned buffer, always kFormatETC2.
Returns
Returns a buffer ready to be uploaded to GPU memory or nullptr, if the format is not understood.
Note
You must free() on the returned pointer when done.
static uint8_t* fplbase::Texture::UnpackPKM ( const void *  file_buf,
size_t  size,
TextureFlags  flags,
mathfu::vec2i *  dimensions,
TextureFormat texture_format 
)
static

Reads a memory buffer containing an ETC2 format (.pkm) file.

Parameters
[in]file_bufthe loaded file.
[in]sizeThe size of the memory block pointed to by file_buf.
[in]flagsTexture flag, allowing premultiply (only webp now)
[out]dimensionsA mathfu::vec2i pointer the captures the image width and height.
[out]texture_formatThe format of the returned buffer, always kFormatETC2.
Returns
Returns a buffer ready to be uploaded to GPU memory or nullptr, if the format is not understood.
Note
You must free() on the returned pointer when done.
static uint8_t* fplbase::Texture::UnpackPng ( const void *  png_buf,
size_t  size,
const mathfu::vec2 &  scale,
TextureFlags  flags,
mathfu::vec2i *  dimensions,
TextureFormat texture_format 
)
inlinestatic

Unpacks a memory buffer containing a Png format file.

Parameters
[in]png_bufThe Png image data.
[in]sizeThe size of the memory block pointed to by data.
[in]scaleA scale value must be a power of two to have correct Texture sizes.
[in]flagsTexture flag, allowing premultiply (only webp now)
[out]dimensionsA mathfu::vec2i pointer the captures the image width and height.
[out]texture_formatPixel format of unpacked image.
Returns
Returns a RGBA array of the returned dimensions or nullptr, if the format is not understood.
Note
You must free() on the returned pointer when done.
static uint8_t* fplbase::Texture::UnpackTGA ( const void *  tga_buf,
TextureFlags  flags,
mathfu::vec2i *  dimensions,
TextureFormat texture_format 
)
static

Unpacks a memory buffer containing a TGA format file.

Note
May only be uncompressed RGB or RGBA data, Y-flipped or not.
Parameters
[in]tga_bufThe TGA image data.
[in]flagsTexture flag, allowing premultiply (only webp now)
[out]dimensionsA mathfu::vec2i pointer the captures the TGA width and height.
[out]texture_formatThe format of the returned buffer, always either 888 or 8888.
Returns
Returns RGBA array of returned dimensions or nullptr if the format is not understood.
Note
You must free() the returned pointer when done.
static uint8_t* fplbase::Texture::UnpackWebP ( const void *  webp_buf,
size_t  size,
const mathfu::vec2 &  scale,
TextureFlags  flags,
mathfu::vec2i *  dimensions,
TextureFormat texture_format 
)
static

Unpacks a memory buffer containing a Webp format file.

Parameters
[in]webp_bufThe WebP image data.
[in]sizeThe size of the memory block pointed to by webp_buf.
[in]scaleA scale value must be a power of two to have correct Texture sizes.
[in]flagsTexture flag, allowing premultiply
[out]dimensionsA mathfu::vec2i pointer the captures the image width and height.
[out]texture_formatThe format of the returned buffer, always either 888 or 8888.
Returns
Returns a RGBA array of the returned dimensions or nullptr, if the format is not understood.
Note
You must free() on the returned pointer when done.
void fplbase::Texture::UpdateTexture ( size_t  unit,
TextureFormat  format,
int  xoffset,
int  yoffset,
int  width,
int  height,
const void *  data 
)

Update (part of) the current texture with new pixel data. For now, must always update at least entire row.

Parameters
[in]unitSpecifies which texture unit to do the update with.
[in]texture_formatThe format of data.
[in]xoffsetLowest x-pixel coordinate to update.
[in]yoffsetLowest y-pixel coordinate to update.
[in]widthNumber of pixels along x-axis to update.
[in]hegihtNumber of pixels along y-axis to update.
[in]dataThe Texture data in memory to load from.

The documentation for this class was generated from the following file: