class EE::Graphics::Texture

Overview

#include <texture.hpp>

class Texture:
    public EE::Graphics::DrawableResource,
    public EE::Graphics::Image,
    private EE::NonCopyable {
public:
    // enums

    enum ClampMode;
    enum CoordinateType;
    enum Filter;

    // construction

    virtual ~Texture();

    // methods

    static Uint32 getMaximumSize();
    static std::pair<std::vector<Texture*>, int> loadGif(IOStream& stream);
    void setHandle(const int& texture);
    int getHandle() const;
    const String::HashType& getHashName() const;
    void setFilepath(const std::string& filepath);
    std::string getFilepath() const;
    unsigned int getImageWidth() const;
    unsigned int getImageHeight() const;
    void setMipmap(const bool& UseMipmap);
    bool getMipmap() const;
    void setClampMode(const Texture::ClampMode& clampmode);
    ClampMode getClampMode() const;
    Uint8* lock(const bool& ForceRGBA = false);
    bool unlock(const bool& KeepData = false, const bool& Modified = false);
    virtual const Uint8* getPixelsPtr();
    void setFilter(const Filter& filter);
    const Filter& getFilter() const;
    virtual bool saveToFile(const std::string& filepath, const Image::SaveType& Format);
    virtual void replaceColor(const Color& ColorKey, const Color& NewColor);
    virtual void createMaskFromColor(const Color& ColorKey, Uint8 Alpha);
    virtual void fillWithColor(const Color& Color);
    virtual void resize(const Uint32& newWidth, const Uint32& newHeight, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4);
    virtual void scale(const Float& scale, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4);
    virtual void copyImage(Image* image, const Uint32& x, const Uint32& y);
    void update(const Uint8* pixels, Uint32 width, Uint32 height, Uint32 x = 0, Uint32 y = 0, PixelFormat pf = PixelFormat::PIXEL_FORMAT_RGBA);
    void update(const Uint8* pixels);
    void update(Image* image, Uint32 x = 0, Uint32 y = 0);
    void replace(Image* image);
    virtual void flip();
    bool hasLocalCopy();
    void deleteTexture();
    void setGrabed(const bool& isGrabed);
    bool isGrabed() const;
    bool isCompressed() const;

    void drawFast(
        const Float& x,
        const Float& y,
        const Float& Angle = 0.0f,
        const Vector2f& scale = Vector2f::One,
        const Color& color = Color::White,
        const BlendMode& Blend = BlendMode::Alpha(),
        const Float& width = 0,
        const Float& height = 0
    );

    void draw(
        const Float& x,
        const Float& y,
        const Float& Angle = 0,
        const Vector2f& scale = Vector2f::One,
        const Color& color = Color::White,
        const BlendMode& Blend = BlendMode::Alpha(),
        const RenderMode& Effect = RENDER_NORMAL,
        OriginPoint Center = OriginPoint(OriginPoint::OriginCenter),
        const Rect& texSector = Rect(0, 0, 0, 0)
    );

    void drawEx(
        Float x,
        Float y,
        Float width = 0.0f,
        Float height = 0.0f,
        const Float& Angle = 0,
        const Vector2f& scale = Vector2f::One,
        const Color& Color0 = Color::White,
        const Color& Color1 = Color(255, 255, 255, 255),
        const Color& Color2 = Color(255, 255, 255, 255),
        const Color& Color3 = Color(255, 255, 255, 255),
        const BlendMode& Blend = BlendMode::Alpha(),
        const RenderMode& Effect = RENDER_NORMAL,
        OriginPoint Center = OriginPoint(OriginPoint::OriginCenter),
        const Rect& texSector = Rect(0, 0, 0, 0)
    );

    void drawQuad(
        const Quad2f& Q,
        const Vector2f& Offset = Vector2f(),
        const Float& Angle = 0.0f,
        const Vector2f& scale = Vector2f::One,
        const Color& color = Color::White,
        const BlendMode& Blend = BlendMode::Alpha(),
        const Rect& texSector = Rect(0, 0, 0, 0)
    );

    void drawQuadEx(
        Quad2f Q,
        const Vector2f& Offset = Vector2f(),
        const Float& Angle = 0.0f,
        const Vector2f& scale = Vector2f::One,
        const Color& Color0 = Color::White,
        const Color& Color1 = Color(255, 255, 255, 255),
        const Color& Color2 = Color(255, 255, 255, 255),
        const Color& Color3 = Color(255, 255, 255, 255),
        const BlendMode& Blend = BlendMode::Alpha(),
        Rect texSector = Rect(0, 0, 0, 0)
    );

    virtual Sizef getSize();
    virtual Sizef getPixelsSize();
    virtual void draw();
    virtual void draw(const Vector2f& position);
    virtual void draw(const Vector2f& position, const Sizef& size);
    virtual bool isStateful();
    void setTextureId(const Uint32& id);
    const Uint32& getTextureId() const;
    void reload();
    virtual void setPixel(const unsigned int& x, const unsigned int& y, const Color& Color);
    void bind(CoordinateType coordinateType, const Uint32& textureUnit = 0);
    void bind(const Uint32& textureUnit = 0);
    const CoordinateType& getCoordinateType() const;
    void setCoordinateType(const CoordinateType& coordinateType);
};

Inherited Members

public:
    // typedefs

    typedef std::function<void(Uint32, Event, DrawableResource*)> OnResourceChangeCallback;

    // enums

    enum Type;
    enum Event;
    enum Format;
    enum PixelFormat;
    enum ResamplerFilter;
    enum SaveType;

    // classes

    class FormatConfiguration;

    // methods

    virtual Sizef getSize() = 0;
    virtual Sizef getPixelsSize() = 0;
    virtual void draw() = 0;
    virtual void draw(const Vector2f& position) = 0;
    virtual void draw(const Vector2f& position, const Sizef& size) = 0;
    virtual bool isStateful() = 0;
    void setAlpha(Uint8 alpha);
    const Uint8& getAlpha();
    void setColor(const Color& color);
    const Color& getColor() const;
    void setColorFilter(const Color& color);
    RGB getColorFilter();
    void clearColor();
    void clearColorFilter();
    void resetAlpha();
    Type getDrawableType() const;
    const Vector2f& getPosition() const;
    void setPosition(const Vector2f& position);
    virtual bool isDrawableResource() const;
    const String::HashType& getId() const;
    const std::string getName() const;
    void setName(const std::string& name);
    virtual bool isDrawableResource() const;
    Uint32 pushResourceChangeCallback(const OnResourceChangeCallback& cb);
    bool popResourceChangeCallback(const Uint32& callbackId);
    static std::pair<std::vector<Image>, int> loadGif(IOStream& stream);
    static std::string saveTypeToExtension(const Int32& Format);
    static SaveType extensionToSaveType(const std::string& Extension);
    static PixelFormat channelsToPixelFormat(const Uint32& channels);
    static bool getInfo(const std::string& path, int* width, int* height, int* channels, const FormatConfiguration& imageFormatConfiguration = FormatConfiguration());
    static bool getInfoFromMemory(const unsigned char* data, const size_t& dataSize, int* width, int* height, int* channels, const FormatConfiguration& imageFormatConfiguration = FormatConfiguration());
    static bool isImage(const std::string& path);
    static bool isImage(const unsigned char* data, const size_t& dataSize);
    static Image::Format getFormat(const std::string& path);
    static Image::Format getFormat(const unsigned char* data, const size_t& dataSize);
    static bool isImageExtension(const std::string& path);
    static std::vector<std::string> getImageExtensionsSupported();
    static std::string getLastFailureReason();
    static Image* New();
    static Image* New(Graphics::Image* image);
    static Image* New(Uint8* data, const unsigned int& width, const unsigned int& height, const unsigned int& channels);
    static Image* New(const Uint8* data, const unsigned int& width, const unsigned int& height, const unsigned int& channels);
    static Image* New(const Uint32& width, const Uint32& height, const Uint32& channels, const Color& DefaultColor = Color(0, 0, 0, 0), const bool& initWithDefaultColor = true);
    static Image* New(std::string Path, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    static Image* New(const Uint8* imageData, const unsigned int& imageDataSize, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    static Image* New(Pack* Pack, std::string FilePackPath, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    static Image* New(IOStream& stream, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    void create(const Uint32& width, const Uint32& height, const Uint32& channels, const Color& DefaultColor = Color(0, 0, 0, 0), const bool& initWithDefaultColor = true);
    virtual Color getPixel(const unsigned int& x, const unsigned int& y);
    virtual void setPixel(const unsigned int& x, const unsigned int& y, const Color& Color);
    virtual void setPixels(const Uint8* data);
    virtual const Uint8* getPixelsPtr();
    virtual const Uint8* getPixelsPtr() const;
    Uint8* getPixels() const;
    void setWidth(const unsigned int& width);
    unsigned int getWidth() const;
    void setHeight(const unsigned int& height);
    unsigned int getHeight() const;
    unsigned int getChannels() const;
    void setChannels(const unsigned int& setChannels);
    void clearCache();
    unsigned int getMemSize() const;
    Sizei getSize();
    virtual bool saveToFile(const std::string& filepath, const SaveType& Format);
    virtual void createMaskFromColor(const Color& ColorKey, Uint8 Alpha);
    void createMaskFromColor(const RGB& ColorKey, Uint8 Alpha);
    virtual void replaceColor(const Color& ColorKey, const Color& NewColor);
    virtual void fillWithColor(const Color& Color);
    virtual void copyImage(Graphics::Image* image, const Uint32& x = 0, const Uint32& y = 0);
    virtual void scale(const Float& scale, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4);
    virtual void resize(const Uint32& newWidth, const Uint32& newHeight, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4);
    virtual void flip();
    Graphics::Image* thumbnail(const Uint32& maxWidth, const Uint32& maxHeight, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4);
    Graphics::Image* crop(Rect rect);
    void avoidFreeImage(const bool& AvoidFree);
    void blit(Graphics::Image* image, const Uint32& x = 0, const Uint32& y = 0);
    Graphics::Image* copy();
    Graphics::Image& operator=(const Image& right);
    void setImageFormatConfiguration(const FormatConfiguration& imageFormatConfiguration);
    const FormatConfiguration& getImageFormatConfiguration() const;

Detailed Documentation

Methods

void setHandle(const int& texture)

Set the OpenGL Texture Id (texture handle)

int getHandle() const

Returns:

The OpenGL Texture Id (texture handle)

const String::HashType& getHashName() const

Returns:

The hash of the filename

void setFilepath(const std::string& filepath)

Set the Texture File Path

std::string getFilepath() const

Returns:

The Texture File Path

unsigned int getImageWidth() const

Returns:

The Image Width

unsigned int getImageHeight() const

Returns:

The Image Height

void setMipmap(const bool& UseMipmap)

Set if the Texture use Mipmaps

bool getMipmap() const

Returns:

If the texture use Mipmaps

void setClampMode(const Texture::ClampMode& clampmode)

Set the Texture Clamp Mode

ClampMode getClampMode() const

Returns:

The Texture Clamp Mode

Uint8* lock(const bool& ForceRGBA = false)

Lock the Texture for direct access. It is needed to have any read/write access to the texture. This feature is not supported in OpenGL ES.

bool unlock(const bool& KeepData = false, const bool& Modified = false)

Unlock the previously locked Texture. Unlocking the texture will upload the local copy of the texture ( that could have been modified ) to the GPU.

Parameters:

KeepData

If true keeps the local copy of the texture un memory, otherwise it will be released.

Modified

The flag indicates of the texture was modified between the Lock and Unlock calls. This is to force reloading the texture from memory to VRAM. In the case that the texture in ram was modified using the Texture methods to do this, it will already know that this is true, so it will upload the changes to the GPU.

virtual const Uint8* getPixelsPtr()

Returns:

A pointer to the first pixel of the texture ( keeped with a local copy ).

You must have a copy of the texture on local memory. For that you need to Lock the texture first.

void setFilter(const Filter& filter)

Set the Texture Filter Mode

const Filter& getFilter() const

Returns:

The texture filter used by the texture

virtual bool saveToFile(const std::string& filepath, const Image::SaveType& Format)

Save the Texture to a new File

virtual void replaceColor(const Color& ColorKey, const Color& NewColor)

Replace a color on the texture

virtual void createMaskFromColor(const Color& ColorKey, Uint8 Alpha)

Create an Alpha mask from a Color

virtual void fillWithColor(const Color& Color)

Fill a texture with a color

virtual void resize(const Uint32& newWidth, const Uint32& newHeight, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4)

Resize the texture

virtual void scale(const Float& scale, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4)

Scale the texture

virtual void copyImage(Image* image, const Uint32& x, const Uint32& y)

Copy an image inside the texture

void update(const Uint8* pixels, Uint32 width, Uint32 height, Uint32 x = 0, Uint32 y = 0, PixelFormat pf = PixelFormat::PIXEL_FORMAT_RGBA)

Update a part of the texture from an array of pixels The size of the pixel array must match the width and height arguments. No additional check is performed on the size of the pixel array or the bounds of the area to update, passing invalid arguments will lead to an undefined behaviour. This function does nothing if pixels is null or if the texture was not previously created.

Parameters:

pixels

Array of pixels to copy to the texture

width

Width of the pixel region contained in pixels

height

Height of the pixel region contained in pixels

x

X offset in the texture where to copy the source pixels

y

Y offset in the texture where to copy the source pixels

pf

The pixel format of the pixel

void update(const Uint8* pixels)

Update the whole texture from an array of pixels The pixel array is assumed to have the same size as the area rectangle. No additional check is performed on the size of the pixel array, passing invalid arguments will lead to an undefined behaviour. This function does nothing if pixels is null or if the texture was not previously created.

Parameters:

pixels

Array of pixels to copy to the texture

void update(Image* image, Uint32 x = 0, Uint32 y = 0)

Update a part of the texture from an image The pixel format is automatically detected No additional check is performed on the size of the image, passing an invalid combination of image size and offset will lead to an undefined behaviour. This function does nothing if the texture was not previously created.

Parameters:

image

Image to copy to the texture

x

X offset in the texture where to copy the source image

y

Y offset in the texture where to copy the source image

void replace(Image* image)

Replaces the current texture with the image provided, reusing the current texture id.

virtual void flip()

Flip the texture ( rotate the texture 90º ). Warning: This is flipped in memory, a real flipping.

bool hasLocalCopy()

Returns:

If the Texture has a copy on the local memory

void deleteTexture()

Unload the Texture from Memory

void setGrabed(const bool& isGrabed)

Set if the Texture is Grabed

bool isGrabed() const

Returns:

If the texture is Grabed

bool isCompressed() const

Returns:

If the texture was compressed on load (DXT compression)

void drawFast(
    const Float& x,
    const Float& y,
    const Float& Angle = 0.0f,
    const Vector2f& scale = Vector2f::One,
    const Color& color = Color::White,
    const BlendMode& Blend = BlendMode::Alpha(),
    const Float& width = 0,
    const Float& height = 0
)

Render the texture on screen ( with less internal mess, a little bit faster way )

Parameters:

x

The x position on screen

y

The y position on screen

Angle

The Angle of the texture rendered

scale

The Scale factor of the rendered texture

color

The texture color

Blend

Set the Blend Mode ( default BlendMode::Alpha())

width

The width of the texture rendered

height

The height of the texture rendered

void draw(
    const Float& x,
    const Float& y,
    const Float& Angle = 0,
    const Vector2f& scale = Vector2f::One,
    const Color& color = Color::White,
    const BlendMode& Blend = BlendMode::Alpha(),
    const RenderMode& Effect = RENDER_NORMAL,
    OriginPoint Center = OriginPoint(OriginPoint::OriginCenter),
    const Rect& texSector = Rect(0, 0, 0, 0)
)

Render the texture on screen

Parameters:

x

The x position on screen

y

The y position on screen

Angle

The Angle of the texture rendered

scale

The Scale factor of the rendered texture

color

The texture color

Blend

Set the Blend Mode ( default BlendMode::Alpha())

Effect

Set the Render Effect ( default RN_NORMAL, no effect )

Center

The rotation and scaling center. The center point is relative to the top-left corner of the object.

texSector

The texture sector to render. You can render only a part of the texture. ( default render all the texture )

void drawEx(
    Float x,
    Float y,
    Float width = 0.0f,
    Float height = 0.0f,
    const Float& Angle = 0,
    const Vector2f& scale = Vector2f::One,
    const Color& Color0 = Color::White,
    const Color& Color1 = Color(255, 255, 255, 255),
    const Color& Color2 = Color(255, 255, 255, 255),
    const Color& Color3 = Color(255, 255, 255, 255),
    const BlendMode& Blend = BlendMode::Alpha(),
    const RenderMode& Effect = RENDER_NORMAL,
    OriginPoint Center = OriginPoint(OriginPoint::OriginCenter),
    const Rect& texSector = Rect(0, 0, 0, 0)
)

Render the texture on screen. Extended because can set the vertex colors individually

Parameters:

x

The x position on screen

y

The y position on screen

width

The width of the texture rendered ( when Scale = 1, otherwise this width will be scaled like width * Scale )

height

The height of the texture rendered ( when Scale = 1, otherwise this height will be scaled like height * Scale )

Angle

The Angle of the texture rendered

scale

The Scale factor of the rendered texture

Color0

The Left - Top vertex color

Color1

The Left - Bottom vertex color

Color2

The Right - Bottom vertex color

Color3

The Right - Top vertex color

Blend

Set the Blend Mode ( default BlendMode::Alpha())

Effect

Set the Render Effect ( default RN_NORMAL, no effect )

Center

The rotation and scaling center. The center point is relative to the top-left corner of the object.

texSector

The texture sector to render. You can render only a part of the texture. ( default render all the texture )

void drawQuad(
    const Quad2f& Q,
    const Vector2f& Offset = Vector2f(),
    const Float& Angle = 0.0f,
    const Vector2f& scale = Vector2f::One,
    const Color& color = Color::White,
    const BlendMode& Blend = BlendMode::Alpha(),
    const Rect& texSector = Rect(0, 0, 0, 0)
)

Render a quad on Screen

Parameters:

Q

The Quad2f

Offset

The Offset applied to all the coordinates on Quad2f

Angle

The Angle of the Quad2f rendered

scale

The Scale of the Quad2f rendered

color

The Quad2f color

Blend

Set the Blend Mode ( default BlendMode::Alpha())

texSector

The texture sector to render. You can render only a part of the texture. ( default render all the texture )

void drawQuadEx(
    Quad2f Q,
    const Vector2f& Offset = Vector2f(),
    const Float& Angle = 0.0f,
    const Vector2f& scale = Vector2f::One,
    const Color& Color0 = Color::White,
    const Color& Color1 = Color(255, 255, 255, 255),
    const Color& Color2 = Color(255, 255, 255, 255),
    const Color& Color3 = Color(255, 255, 255, 255),
    const BlendMode& Blend = BlendMode::Alpha(),
    Rect texSector = Rect(0, 0, 0, 0)
)

Render a quad on Screen

Parameters:

Q

The Quad2f

Offset

The Offset applied to all the coordinates on Quad2f

Angle

The Angle of the Quad2f rendered

scale

The Scale of the Quad2f rendered

Color0

The Left - Top vertex color

Color1

The Left - Bottom vertex color

Color2

The Right - Bottom vertex color

Color3

The Right - Top vertex color

Blend

Set the Blend Mode ( default BlendMode::Alpha())

texSector

The texture sector to render. You can render only a part of the texture. ( default render all the texture )

void setTextureId(const Uint32& id)

Set the texture factory internal id of the texture

const Uint32& getTextureId() const

Returns:

The texture factory internal id of the texture

void reload()

Reload the texture from the current local copy.

virtual void setPixel(const unsigned int& x, const unsigned int& y, const Color& Color)

Set a pixel to the locked texture.

void bind(CoordinateType coordinateType, const Uint32& textureUnit = 0)

Bind the texture. Activate the texture for rendering.

Parameters:

coordinateType

Type of texture coordinates to use

textureUnit

The Texture unit that want to be used to bind ( usually 0 )

void bind(const Uint32& textureUnit = 0)

Bind the texture. Activate the texture for rendering.

Parameters:

textureUnit

The Texture unit that want to be used to bind ( usually 0 )

const CoordinateType& getCoordinateType() const

return The reference coordinate type.

void setCoordinateType(const CoordinateType& coordinateType)

Sets the default coordinate type. This value is not forced when binded, but used as a reference for binding in the case of textures with a reference coordinate type.