Hi eveyrone,
Sorry in advance as I'm not sure if it is the right place to ask that kind of question...
Anyway, I'm currently facing a problem I can't explain and it might (unless I'm wrong) actually be a bug within the sfml.
long story short, code + result:
code:
#include <SFML/Graphics.hpp>
void dump_texture(sf::RenderWindow& rwindow, const sf::Vector2f& pos, sf::Texture& tex);
int main()
{
//
// INIT
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
// Load a sprite to display
sf::Texture texture;
if (!texture.loadFromFile("cute_image.jpeg"))
return EXIT_FAILURE;
sf::Sprite sprite(texture);
// scale to match a 200x200 size
sf::Vector2u tsize = texture.getSize();
sprite.scale(200.0f/tsize.x, 200.0f/tsize.y);
// INIT
//
// Start the game loop
while (window.isOpen())
{
//
// EVENTS
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed)
window.close();
}
// EVENTS
//
//
// RENDER
window.clear();
dump_texture(window, sf::Vector2f(200,0), texture);
window.draw(sprite);
dump_texture(window, sf::Vector2f(400,0), texture);
window.display();
// RENDER
//
}
return EXIT_SUCCESS;
}
void dump_texture(sf::RenderWindow& rwindow, const sf::Vector2f& pos, sf::Texture& tex)
{
sf::Texture::bind(&tex);
sf::Vertex vertices[] =
{
sf::Vertex(sf::Vector2f(pos.x+ 0, pos.y+ 0), sf::Color::White, sf::Vector2f(0,0)),
sf::Vertex(sf::Vector2f(pos.x+ 0, pos.y+200), sf::Color::White, sf::Vector2f(0,1)),
sf::Vertex(sf::Vector2f(pos.x+200, pos.y+200), sf::Color::White, sf::Vector2f(1,1)),
sf::Vertex(sf::Vector2f(pos.x+200, pos.y+ 0), sf::Color::White, sf::Vector2f(1,0))
};
rwindow.draw(vertices, 4, sf::Quads);
sf::Texture::bind(NULL);
}
result
https://drive.google.com/open?id=0BxUZ1JUO9QAbakJyWVhZWkZZRlU (https://drive.google.com/open?id=0BxUZ1JUO9QAbakJyWVhZWkZZRlU)
Basically the sprite seems to "disable" the opengl texture2d state... which would make the second call to dump_texture to fail?
Any idea?
It's not a bug; it's a feature ;)
Texture::bind exists for interaction between OpenGL and SFML code. You're not supposed to use it when drawing SFML entities. The graphics module of SFML is self-contained, everything that you draw uses the entity's internal state + the render-states that you pass to the draw function (which is the way you should have done it). Everything set externally can produce wrong results, especially by breaking the internal state cache of SFML.
The correct version of your code is:
void dump_texture(sf::RenderWindow& rwindow, const sf::Vector2f& pos, sf::Texture& tex)
{
sf::Vertex vertices[] =
{
sf::Vertex(sf::Vector2f(pos.x+ 0, pos.y+ 0), sf::Color::White, sf::Vector2f(0,0)),
sf::Vertex(sf::Vector2f(pos.x+ 0, pos.y+200), sf::Color::White, sf::Vector2f(0,1)),
sf::Vertex(sf::Vector2f(pos.x+200, pos.y+200), sf::Color::White, sf::Vector2f(1,1)),
sf::Vertex(sf::Vector2f(pos.x+200, pos.y+ 0), sf::Color::White, sf::Vector2f(1,0))
};
rwindow.draw(vertices, 4, sf::Quads, &tex);
}
Make sure to read the documentation (http://www.sfml-dev.org/tutorials/2.3/graphics-vertex-array.php#texturing) before trying random stuff.