Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Min and Max blend modes #1756

Merged
merged 1 commit into from Mar 23, 2021
Merged

Add Min and Max blend modes #1756

merged 1 commit into from Mar 23, 2021

Conversation

Sakarah
Copy link
Contributor

@Sakarah Sakarah commented Mar 9, 2021

  • Has this change been discussed on the forum or in an issue before?
  • Does the code follow the SFML Code Style Guide?
  • Have you provided some example/test code for your changes?

Description

This PR adds two blend modes that are necessary for some effects, namely BlendMin and BlendMax.
Given the fact that constructing Min and Max from factors and equation might seem weird (because factors are ignored), I chose to provide constants.
It supports both OpenGL and OpenGL ES with the appropriate extension GL_EXT_blend_minmax.
It should also allow implementations with GL_EXT_blend_subtract but without GL_EXT_blend_minmax to use BlendMode::Subtract and BlendMode::ReverseSubtract.

This PR implements the suggestion #1710.

Tasks

  • Tested on Linux
  • Tested on Windows
  • Tested on macOS
  • Tested on iOS
  • Tested on Android

How to test this PR?

Here is a simple example to test the added blending modes. Place two images 1.png and 2.png in the working directory. Then press A to see them max blended, and Z to see them min blended.

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window;
    window.create(sf::VideoMode(128,128), "Min/Max blending test", sf::Style::Default);

    sf::Texture texture1;
    texture1.loadFromFile("1.png");
    texture1.setSmooth(true);
    sf::Sprite sprite1;
    sprite1.setTexture(texture1);

    sf::Texture texture2;
    texture2.loadFromFile("2.png");
    texture2.setSmooth(true);
    sf::Sprite sprite2;
    sprite2.setTexture(texture2);

    sf::Color clearColor = sf::Color::Black;
    sf::RenderStates states;
    states.blendMode = sf::BlendMax;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::Escape))
            {
                window.close();
            }
            else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::A)
            {
                states.blendMode = sf::BlendMax;
                clearColor = sf::Color::Black;
            }
            else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::Z)
            {
                states.blendMode = sf::BlendMin;
                clearColor = sf::Color::White;
            }
        }

        window.clear(clearColor);
        window.draw(sprite1, states);
        window.draw(sprite2, states);
        window.display();
    }

    return 0;
}

@eXpl0it3r eXpl0it3r added this to Discussion in SFML 2.6.0 via automation Mar 13, 2021
@eXpl0it3r eXpl0it3r added this to the 2.6 milestone Mar 13, 2021
@eXpl0it3r eXpl0it3r moved this from Discussion to Ready in SFML 2.6.0 Mar 13, 2021
@eXpl0it3r eXpl0it3r merged commit 05c8361 into SFML:master Mar 23, 2021
SFML 2.6.0 automation moved this from Ready to Done Mar 23, 2021
@eXpl0it3r
Copy link
Member

Tested it locally, it works and looks correct.

Thank you for your contribution! 🙂

@Sakarah Sakarah deleted the blend_minmax branch March 24, 2021 18:29
ChrisThrasher added a commit to SFML/CSFML that referenced this pull request Jul 3, 2023
ChrisThrasher added a commit to SFML/CSFML that referenced this pull request Jul 3, 2023
ChrisThrasher added a commit to SFML/CSFML that referenced this pull request Jul 3, 2023
ChrisThrasher added a commit to SFML/CSFML that referenced this pull request Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
SFML 2.6.0
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants