SFML community forums
Help => Graphics => Topic started by: mnajrg 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.
#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:
(http://mediafire.com/imgbnc.php/ff82b5f82102d1efc0937a088bc472296e1319225107df9e30676f83f80446aa4g.jpg) (http://mediafire.com/imageview.php?thumb%3D5%26quickkey%3D16s6uc29lla097k)
-
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 ;)
-
Thankyou sir laurent, it works!
But my image fails!
Haha..