-
Notifications
You must be signed in to change notification settings - Fork 27
Elastic Sprite
A sprite class that allows you to manipulate the corners of the sprite rectangle individually. This includes offsetting their positions and settings their colours separately.
Elastic Sprite uses an internal shader to interpolate the texture and vertex colours so that the entire quad is taken into account instead of just the two triangles individually. This allows "stretching" of the sprite's texture: offsetting the vertices positions creates a non-affine transformation. The shader can be enabled or disabled; if it's disabled, the sprite is interpolated in the usual way. Interpolation can be one of two types: bi-linear or perspective; perspective interpolation reverts the interpolation of any vertex colours to the 'normal', default method.
-
sw::ElasticSprite sprite;
creates a Elastic Sprite -
sw::ElasticSprite sprite(sf::Texture);
creates a Elastic Sprite and sets the texture to use to be the passed sf::Texture (see setTexture below) -
sw::ElasticSprite sprite(sf::Texture, sf::FloatRect);
creates a Elastic Sprite and sets the texture to use to be the passed sf::Texture (see setTexture below). It also sets the texture rectangle to the passed sf::FloatRect (see setTextureRect blow)
This class inherits from sf::Drawable so it is drawn in the same way as all SFML drawables:
window.draw(sprite);
where window is an sf::RenderWindow.
Note: actually, window could be any sf::RenderTarget.
This class inherits from sf::Transformable so it has all the usual SFML transformations available.
-
setTexture(sf::Texture, bool)
takes an sf::Texture and stores a pointer to it. This means that the texture should exist (at its current memory position) as long as Elastic Sprite requires it. This texture can be changed whenever you like and should be reset if the current texture is moved in memory. You can also use this method to nullify the texture by omitting the parameter. If no, texture is set, the vertices' colours only are used. If the boolean parameter is true (default value is false), the texture rectangle is reset to match the rectangle of the new texture. Note that, unlike SFML's standard sprite, setting a texture when then no texture is currently set does not automatically reset the texture rectangle. This is because Elastic Sprite is designed to be used with or without textures whereas sf::Sprite is only designed to be used with a texture. -
setTextureRect(sf::FloatRect)
changes the texture rectangle to the supplied rectangle (sf::FloatRect). -
setTextureFlipX(bool)
will force the texture to be flipped horizontally when drawn. This works with both interpolation shaders as well as without a shader. Can be reset by setting to false. Can be combined with setTextureFlipY. -
setTextureFlipY(bool)
will force the texture to be flipped vertically when drawn. This works with both interpolation shaders as well as without a shader. Can be reset by setting to false. Can be combined with setTextureFlipX.
-
setUseShader(bool)
sets whether the shader should be used or not. This method returns a bool of the actual state of shader usage (setting to true may fail and return false).
-
activateBilinearInterpolation()
activates bi-linear interpolation that is used when shader is used. Perspective interpolation is disabled. -
activatePerspectiveInterpolation()
activates perspective interpolation that is used when shader is used. Bi-linear interpolation is disabled. Note that with perspective interpolation, the vertex colours reverts to the 'normal', default method of interpolation; that is, it treats the two triangles separately (this is only for the colours; the textures are interpolated using perspective).
Elastic Sprite updates itself when it is drawn, if anything has changed.
-
setColor()
sets the colour of the Elastic Sprite. This colour, as with a standard sf::Sprite, is 'multiplied' with the texture. Note that if no texture exists, this becomes the colour of the resulting rectangle. Note also that this sets the colour of all four vertices. -
setVertexColor(vertexIndex)
sets the colour of the individual vertex. Each vertex (corner) of the sprite can have its own colour. Note that if no texture exists, this becomes the colour of the resulting rectangle. The vertex index must be in the range 0 to 3 (four vertices). -
setVertexOffset(vertexIndex, offset)
sets the position offset of the vertex represented by the supplied vertex index to the passed sf::Vector2f, offset. The vertex index must be in the range 0 to 3 (four vertices). -
resetVertexOffsets()
resets the position offsets of all vertices to (0, 0).
-
getTexture()
returns an const reference to the sf::Texture used by the Elastic Sprite. -
getTextureRect()
returns the texture rectangle (sf::FloatRect). Note that this is a copy of the stored rectangle. -
getTextureFlipX()
returns a boolean representing whether or not the texture is being flipped horizontally. -
getTextureFlipY()
returns a boolean representing whether or not the texture is being flipped vertically.
-
getUseShader()
returns a bool representing whether or not the shader is set to be used.
-
isActiveBilinearInterpolation()
returns a bool representing if bi-linear interpolation is currently active. -
isActivePerspectiveInterpolation()
returns a bool representing if perspective interpolation is currently active.
-
getVertexColor(vertexIndex)
returns an sf::Color representing the colour of the vertex specified by vertexIndex. The vertex index must be in the range 0 to 3 (four vertices). -
getColor()
returns the average colour of all four vertices. If all four vertices' colours are identical, this colour equals them.
-
getLocalBounds()
returns an sf::FloatRect that contains the Elastic Sprite before any SFML transformations -
getBaseLocalBounds()
returns an sf::FloatRect that contains the local bounds of the Elastic Sprite's base rectangle (ignoring vertex offsets) before any SFML transformations -
getGlobalBounds()
returns an sf::FloatRect that contains the Elastic Sprite after any SFML transformations -
getBaseGlobalBounds()
returns an sf::FloatRect that contains the global bounds of the Elastic Sprite's base rectangle (ignoring vertex offsets) after any SFML transformations
-
getVertexLocalPosition(vertexIndex)
returns an sf::Vector2f of the local (before any SFML transformations) position of the vertex, including its offset. -
getVertexBaseLocalPosition(vertexIndex)
returns an sf::Vector2f of the local (before any SFML transformations) position of the vertex, ignoring its offset. -
getVertexGlobalPosition(vertexIndex)
returns an sf::Vector2f of the global (after any SFML transformations) position of the vertex, including its offset. -
getVertexBaseGlobalPosition(vertexIndex)
returns an sf::Vector2f of the global (after any SFML transformations) position of the vertex, ignoring its offset.
#include <SFML/Graphics.hpp>
#include <SelbaWard/ElasticSprite.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(406, 256), "Elastic Sprite simple example - texture");
sf::Texture texture;
if (!texture.loadFromFile("resources/uv map.jpg"))
return EXIT_FAILURE;
sw::ElasticSprite sprite(texture);
sprite.setVertexOffset(2, { 150.f, -30.f });
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(sprite);
window.display();
}
return EXIT_SUCCESS;
}
The code above displays:
#include <SFML/Graphics.hpp>
#include <SelbaWard/ElasticSprite.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(350, 200), "Elastic Sprite simple example - colour");
sw::ElasticSprite sprite;
sprite.setTextureRect({ { 0.f, 0.f }, { 200.f, 200.f } });
sprite.setVertexOffset(2, { 150.f, -30.f });
sprite.setVertexColor(0, sf::Color::Red);
sprite.setVertexColor(1, sf::Color::Yellow);
sprite.setVertexColor(2, sf::Color::Green);
sprite.setVertexColor(3, sf::Color::Blue);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(sprite);
window.display();
}
return EXIT_SUCCESS;
}
The code above displays:
#include <SFML/Graphics.hpp>
#include <SelbaWard/ElasticSprite.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(406, 256), "Elastic Sprite simple example - texture and colour");
sf::Texture texture;
if (!texture.loadFromFile("resources/uv map.jpg"))
return EXIT_FAILURE;
sw::ElasticSprite sprite(texture);
sprite.setTextureRect({ { 0.f, 0.f }, { 200.f, 200.f } });
sprite.setVertexOffset(2, { 150.f, -30.f });
sprite.setVertexColor(0, sf::Color::Red);
sprite.setVertexColor(1, sf::Color::Yellow);
sprite.setVertexColor(2, sf::Color::Green);
sprite.setVertexColor(3, sf::Color::Blue);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(sprite);
window.display();
}
return EXIT_SUCCESS;
}
The code above displays:
#include <SFML/Graphics.hpp>
#include <SelbaWard/ElasticSprite.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(406, 256), "Elastic Sprite simple example - perspective interpolation");
sf::Texture texture;
if (!texture.loadFromFile("resources/uv map.jpg"))
return EXIT_FAILURE;
sw::ElasticSprite sprite(texture);
sprite.setVertexOffset(2, { 150.f, -30.f });
sprite.activatePerspectiveInterpolation();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(sprite);
window.display();
}
return EXIT_SUCCESS;
}
The code above displays:
Note: the texture used in these examples is available, along with more examples, in the examples folder, although you can use your own images.
(ElasticSprite v1.3)