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

Author Topic: Help in CreateMaskFromColor  (Read 1656 times)

0 Members and 1 Guest are viewing this topic.

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
Help in CreateMaskFromColor
« on: December 09, 2011, 12:04:03 pm »
Nothing happens in my image.CreateMaskFromColor..
Heres my code, Im sorry im new in sfml
Im using SFML 2.0

Im trying to make the black portion transparent.

Code: [Select]
#include "SFML/Graphics.hpp"

int main()
{
    sf::RenderWindow Window(sf::VideoMode(640,480,32),"Graphics!");

    sf::Image image;
    image.LoadFromFile("characters.bmp");
    image.CreateMaskFromColor((sf::Color::Black),0);
    if (!image.LoadFromFile("characters.bmp"))
        return 1;
    sf::Texture Texture;
    Texture.LoadFromImage(image);
    if (!Texture.LoadFromImage(image))
        return 1;

    sf::Sprite Sprite;
    Sprite.SetTexture(Texture);

    while (Window.IsOpened())
    {
        sf::Event Event;

        while (Window.PollEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                Window.Close();
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::LAlt))
           {
               if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::F4))
                Window.Close();
           }
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Down))
                Sprite.SetSubRect(sf::IntRect(70,8,65,65));
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Left))
                Sprite.SetSubRect(sf::IntRect(8,8,65,65));
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Right))
                Sprite.SetSubRect(sf::IntRect(135,8,65,65));
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Up))
                Sprite.SetSubRect(sf::IntRect(195,8,65,65));
        }
        Window.Clear(sf::Color(0,0,255));
        Window.Draw(Sprite);
        Window.Display();
    }
    return 0;
}


Here the output:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Help in CreateMaskFromColor
« Reply #1 on: December 09, 2011, 12:06:59 pm »
You reload the image after calling CreateMaskFromColor, so the modifications that you made to the sf::Image object are lost.

You're also loading the texture twice, once is enough ;)
Laurent Gomila - SFML developer

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
Help in CreateMaskFromColor
« Reply #2 on: December 10, 2011, 03:30:13 am »
Thankyou sir laurent, it works!
But my image fails!
Haha..