Skip to content

Instantly share code, notes, and snippets.

@tomgalvin594
Last active May 27, 2022 13:33
Show Gist options
  • Save tomgalvin594/8533203 to your computer and use it in GitHub Desktop.
Save tomgalvin594/8533203 to your computer and use it in GitHub Desktop.
A small test program that demonstrates the new BlendMode usage.
#include <SFML/Graphics.hpp>
int main(int argc, const char* argv[]) {
// Create a window for the game
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Application", sf::Style::Titlebar | sf::Style::Close);
// Load the center sprite
sf::Texture texture;
texture.loadFromFile("background.png");
sf::Sprite center_sprite(texture);
center_sprite.setOrigin(256, 256);
center_sprite.setPosition(400, 300);
// Overlaping circle for mode demonstration
sf::CircleShape circle(150);
circle.setFillColor(sf::Color(120,120,200,128));
circle.setOutlineThickness(40);
circle.setOutlineColor(sf::Color(200,200,20,200));
circle.setOrigin(150, 150);
bool running = true;
while(running)
{
// ---------- Handle Events -----------
sf::Event event;
while(window.pollEvent(event))
{
// Handle events
if(event.type == sf::Event::Closed) {
running = false;
}
if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
running = false;
}
}
// ---------- Draw the scene ----------
window.clear();
// Draw the center textured sprite
window.draw(center_sprite);
// Draw the four pre-defined blending modes
circle.setPosition(144, 44);
window.draw(circle, sf::BlendAlpha);
circle.setPosition(656, 44);
window.draw(circle, sf::BlendAdd);
circle.setPosition(656, 556);
window.draw(circle, sf::BlendMultiply);
circle.setPosition(144, 556);
window.draw(circle, sf::BlendNone);
// Create and draw a modulation blending mode
sf::BlendMode blendmode = sf::BlendMultiply;
blendmode.colorDstFactor = sf::BlendFactor::SrcColor;
circle.setScale(.75f, .75f);
circle.setPosition(400, 300);
window.draw(circle, blendmode);
circle.setScale(1.f, 1.f);
// Display the frame
window.display();
}
// Application has finished
window.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment