Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: createMaskFromColor not working when updating image to texture  (Read 3857 times)

0 Members and 1 Guest are viewing this topic.

zeno15

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
createMaskFromColor not working when updating image to texture
« on: November 27, 2012, 03:01:49 am »
I have a background texture that i move around using a view, and I want to add a few circles to it but without the background, so I am using image.createMaskFromColor(sf::Color::White), as the background is white, and then calling texture.update(image, ycoord, xcoord), the texture is updated, but the white background has been turned black, I was wondering if anyone knew the cause of this, the solution or maybe and alternative way to accomplish the same thing,
cheers.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: createMaskFromColor not working when updating image to texture
« Reply #1 on: November 27, 2012, 08:17:24 am »
Do you set a specific blend mode when you draw the texture?
Laurent Gomila - SFML developer

zeno15

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: createMaskFromColor not working when updating image to texture
« Reply #2 on: November 27, 2012, 09:03:59 pm »
No all the default values are used, I load the texture from a file (.png) and load it to a sprite, then draw the sprite, then i update the texture with the new images.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: createMaskFromColor not working when updating image to texture
« Reply #3 on: November 27, 2012, 09:57:01 pm »
Could you write a small example that reproduces the problem?
Laurent Gomila - SFML developer

zeno15

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: createMaskFromColor not working when updating image to texture
« Reply #4 on: November 27, 2012, 10:58:27 pm »
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

int main() {

        sf::RenderWindow *window = new sf::RenderWindow(sf::VideoMode(800, 600), "My window");
       
        sf::Texture *backgroundtexture = new sf::Texture();
        backgroundtexture->loadFromFile("background.png");

        sf::Sprite *backgroundsprite = new sf::Sprite();
        backgroundsprite->setTexture(*backgroundtexture);

        sf::Image ball;
        ball.loadFromFile("ball.png");
        ball.createMaskFromColor(sf::Color::White);

        backgroundtexture->update(ball, 100, 100);




        while (window->isOpen()) {
                sf::Event event;
                while (window->pollEvent(event)) {
                        // check the type of the event...
                        switch (event.type) {
                                // window closed
                                case sf::Event::Closed:
                                        window->close();
                                        break;
                               
                                // we don't process other types of events
                                default:
                                        break;
                         }
                }

                window->clear();
                window->draw(*backgroundsprite);
                window->display();

        }

}
 

For me run on windows 7 using visual studio 2010 this creates the green background and the ball is updated onto the texture and the white background is turned black.



[attachment deleted by admin]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: createMaskFromColor not working when updating image to texture
« Reply #5 on: November 27, 2012, 11:09:58 pm »
The black that you see is the background color (window->clear()). The area outside the circle is transparent, and when you update the texture this area remains transparent. So you see what's behind.

If you want to avoid that, you should draw to a sf::RenderTexture so that the alpha channel is interpreted when you copy the circle to the texture.

PS: why do you allocate every single object dynamically (without releasing them at the end)??
Laurent Gomila - SFML developer

zeno15

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: createMaskFromColor not working when updating image to texture
« Reply #6 on: November 27, 2012, 11:12:28 pm »
Ah thank you :)

I'm allocating them dynamically because that's what I'm doing in a larger project and just didn't release them in this smaller example

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: createMaskFromColor not working when updating image to texture
« Reply #7 on: November 27, 2012, 11:21:32 pm »
Quote
I'm allocating them dynamically because that's what I'm doing in a larger project
That's a really bad habit :P
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: createMaskFromColor not working when updating image to texture
« Reply #8 on: November 27, 2012, 11:31:32 pm »
I'm allocating them dynamically because that's what I'm doing in a larger project and just didn't release them in this smaller example
Enter modern C++.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: createMaskFromColor not working when updating image to texture
« Reply #9 on: November 28, 2012, 12:52:05 am »
Quote
I'm allocating them dynamically because that's what I'm doing in a larger project and just didn't release them in this smaller example

Then write a "TextureHolder" class that uses an STL container for the textures you are going to use. That's the best way to handle it if your project is going to go big. Be wary of std::vector though, it can be used for textures alright if you have a set amount of textures that won't change. If that won't happen in your case then use any other container.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!