class EE::Graphics::TextureRegion

Overview

A TextureRegion is a part of a texture that represent an sprite. More…

#include <textureregion.hpp>

class TextureRegion: public EE::Graphics::DrawableResource {
public:
    // construction

    TextureRegion();
    TextureRegion(Texture* tex, const std::string& name = "");
    TextureRegion(Texture* tex, const Rect& srcRect, const std::string& name = "");
    TextureRegion(Texture* tex, const Rect& srcRect, const Sizef& destSize, const std::string& name = "");
    TextureRegion(Texture* tex, const Rect& srcRect, const Sizef& destSize, const Vector2i& offset, const std::string& name = "");
    virtual ~TextureRegion();

    // methods

    static TextureRegion* New();
    static TextureRegion* New(const Uint32& TexId, const std::string& name = "");
    static TextureRegion* New(const Uint32& TexId, const Rect& srcRect, const std::string& name = "");
    static TextureRegion* New(const Uint32& TexId, const Rect& srcRect, const Sizef& destSize, const std::string& name = "");
    static TextureRegion* New(const Uint32& TexId, const Rect& srcRect, const Sizef& destSize, const Vector2i& offset, const std::string& name = "");
    static TextureRegion* New(Texture* tex, const std::string& name = "");
    static TextureRegion* New(Texture* tex, const Rect& srcRect, const std::string& name = "");
    static TextureRegion* New(Texture* tex, const Rect& srcRect, const Sizef& destSize, const std::string& name = "");
    static TextureRegion* New(Texture* tex, const Rect& srcRect, const Sizef& destSize, const Vector2i& offset, const std::string& name = "");
    void setTextureId(const Uint32& TexId);
    void setTexture(Texture* texture);
    const Rect& getSrcRect() const;
    void setSrcRect(const Rect& rect);
    const Sizef& getDestSize() const;
    void setDestSize(const Sizef& destSize);
    const Vector2i& getOffset() const;
    void setOffset(const Vector2i& offset);

    void draw(
        const Float& X,
        const Float& Y,
        const Color& color = Color::White,
        const Float& Angle = 0.f,
        const Vector2f& Scale = Vector2f::One,
        const BlendMode& Blend = BlendMode::Alpha(),
        const RenderMode& Effect = RENDER_NORMAL,
        OriginPoint Center = OriginPoint(OriginPoint::OriginCenter)
    );

    void draw(
        const Float& X,
        const Float& Y,
        const Float& Angle,
        const Vector2f& Scale,
        const Color& Color0 = Color::White,
        const Color& Color1 = Color::White,
        const Color& Color2 = Color::White,
        const Color& Color3 = Color::White,
        const BlendMode& Blend = BlendMode::Alpha(),
        const RenderMode& Effect = RENDER_NORMAL,
        OriginPoint Center = OriginPoint(OriginPoint::OriginCenter)
    );

    void draw(
        const Quad2f Q,
        const Vector2f& offset = Vector2f(),
        const Float& Angle = 0.f,
        const Vector2f& Scale = Vector2f::One,
        const Color& Color0 = Color::White,
        const Color& Color1 = Color::White,
        const Color& Color2 = Color::White,
        const Color& Color3 = Color::White,
        const BlendMode& Blend = BlendMode::Alpha()
    );

    virtual void draw();
    virtual void draw(const Vector2f& position);
    virtual void draw(const Vector2f& position, const Sizef& size);
    virtual bool isStateful();
    Graphics::Texture* getTexture();
    void replaceColor(Color ColorKey, Color NewColor);
    void createMaskFromColor(Color ColorKey, Uint8 Alpha);
    void createMaskFromColor(RGB ColorKey, Uint8 Alpha);
    void cacheAlphaMask();
    void cacheColors();
    Uint8 getAlphaAt(const Int32& X, const Int32& Y);
    Color getColorAt(const Int32& X, const Int32& Y);
    void setColorAt(const Int32& X, const Int32& Y, const Color& Color);
    void clearCache();
    Uint8* lock();
    bool unlock(const bool& KeepData = false, const bool& Modified = false);
    Sizei getRealSize();
    virtual Sizef getSize();
    const Uint8* getPixelsPtr();
    bool saveToFile(const std::string& filepath, const Image::SaveType& Format, const Image::FormatConfiguration& imageFormatConfiguration = Image::FormatConfiguration());
    void resetDestSize();
    Float getPixelDensity() const;
    void setPixelDensity(const Float& pixelDensity);
    Sizei getDpSize();
    virtual Sizef getPixelsSize();
    Sizef getOriDestSize() const;
    void setOriDestSize(const Sizef& oriDestSize);
};

Inherited Members

public:
    // typedefs

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

    // enums

    enum Type;
    enum Event;

    // 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);

Detailed Documentation

A TextureRegion is a part of a texture that represent an sprite.

Construction

TextureRegion()

Creates an empty TextureRegion

TextureRegion(Texture* tex, const std::string& name = "")

Creates a TextureRegion from a Texture. It will use the full Texture as a TextureRegion.

Parameters:

tex

The texture

name

The texture name ( if any )

TextureRegion(Texture* tex, const Rect& srcRect, const std::string& name = "")

Creates a TextureRegion of the indicated part of the texture.

Parameters:

tex

The texture

srcRect

The texture part that will be used as the TextureRegion.

name

The texture name ( if any )

TextureRegion(Texture* tex, const Rect& srcRect, const Sizef& destSize, const std::string& name = "")

Creates a TextureRegion of the indicated part of the texture.

Parameters:

tex

The texture

srcRect

The texture part that will be used as the TextureRegion.

destSize

The destination size that the TextureRegion will have when rendered.

name

The texture name ( if any )

TextureRegion(Texture* tex, const Rect& srcRect, const Sizef& destSize, const Vector2i& offset, const std::string& name = "")

Creates a TextureRegion of the indicated part of the texture.

Parameters:

tex

The texture

srcRect

The texture part that will be used as the TextureRegion.

destSize

The destination size that the TextureRegion will have when rendered.

offset

The offset that will be added to the position passed when any Draw call is used.

name

The texture name ( if any )

Methods

void setTextureId(const Uint32& TexId)

Set the Texture Id that holds the TextureRegion.

void setTexture(Texture* texture)

Set the Texture that holds the TextureRegion.

const Rect& getSrcRect() const

Returns:

The Texture sector that represents the TextureRegion

void setSrcRect(const Rect& rect)

Sets the Texture sector that represents the TextureRegion

const Sizef& getDestSize() const

Returns:

The Destination Size of the TextureRegion.

void setDestSize(const Sizef& destSize)

Sets the Destination Size of the TextureRegion. The size can be different from the original size of the TextureRegion. For example if the TextureRegion width is 32 pixels, by default the destination width is 32 pixels, but it can be changed to anything you want.

const Vector2i& getOffset() const

Returns:

The TextureRegion default offset. The offset is added to the position passed when is drawed.

void setOffset(const Vector2i& offset)

Set the TextureRegion offset.

Graphics::Texture* getTexture()

Returns:

The texture instance used by the TextureRegion.

void replaceColor(Color ColorKey, Color NewColor)

Replaces a color in the TextureRegion ( needs Lock() )

void createMaskFromColor(Color ColorKey, Uint8 Alpha)

Creates a mask from a color.

void createMaskFromColor(RGB ColorKey, Uint8 Alpha)

Creates a mask from a color.

void cacheAlphaMask()

Creates a copy of the alpha mask to memory from the texture loaded in VRAM.

void cacheColors()

Creates a copy in memory from the texture loaded in VRAM.

Uint8 getAlphaAt(const Int32& X, const Int32& Y)

Returns:

The alpha value that corresponds to the position indicated in the TextureRegion. If the TextureRegion wasn’t locked before this call, it will be locked automatically.

Color getColorAt(const Int32& X, const Int32& Y)

Returns:

The color that corresponds to the position indicated in the TextureRegion. If the TextureRegion wasn’t locked before this call, it will be locked automatically.

void setColorAt(const Int32& X, const Int32& Y, const Color& Color)

Set a color to the position indicated in the TextureRegion. If the TextureRegion wasn’t locked before this call, it will be locked automatically.

void clearCache()

Deletes the texture buffer from memory ( not from VRAM ) if it was cached before ( using Lock() ).

Uint8* lock()

Locks the texture to be able to perform read/write operations.

See also:

Texture::Lock

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

Unlocks the current texture locked.

See also:

Texture::Unlock

Sizei getRealSize()

Returns:

The TextureRegion size in the texture. This is the source rect size.

virtual Sizef getSize()

Returns:

This is the same as Destination Size but with the values rounded as integers.

const Uint8* getPixelsPtr()

Returns:

A pixel pointer to the texture loaded in memory ( downloaded from VRAM doing Lock()/Unlock() ).

bool saveToFile(const std::string& filepath, const Image::SaveType& Format, const Image::FormatConfiguration& imageFormatConfiguration = Image::FormatConfiguration())

Saves the TextureRegion to a file in the file format specified. This will get the Texture from VRAM ( it will not work with OpenGL ES )

void resetDestSize()

Sets the Destination Size as the Source Rect Size ( the real size of the TextureRegion) multiplied by the pixel density.