Hi,
I want to draw a white square with half-opacity (alpha 128), but the alpha-blending isn't rendered as expected when I draw on a render texture.
Why don't the two following methods render the same result?
Method A:
1. Draw something on a render texture
2. Draw the render texture on render window
3. Draw a white square with half-opacity on render window
Method B:
1. Draw something on a render texture
2. Draw a white square with half opacity on the render texture
3. Draw the render texture on render window
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(320, 240), "sfml", sf::Style::Close);
sf::RectangleShape black({20 ,240});
black.setPosition(100, 0);
black.setFillColor(sf::Color::Black);
sf::RectangleShape white({60, 60});
white.setPosition(80, 50);
white.setFillColor(sf::Color(255, 255, 255, 128));
sf::RenderTexture texture;
texture.create(window.getSize().x, window.getSize().y);
sf::Sprite sprite(texture.getTexture());
// Method A: Draw white square on render texture
texture.clear(sf::Color::Transparent);
texture.draw(black);
texture.draw(white);
texture.display();
white.setPosition(80, 160);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
// Method B: Draw white square on render window
window.clear(sf::Color::Blue);
window.draw(sprite);
window.draw(white);
window.display();
}
return 0;
}
Image result in attachement.
Am I missing something?
- OS: Debian 9
- GPU: Mesa DRI Intel(R) Haswell Mobile (0xa16)
- SFML Version: 2.5.1