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

Author Topic: [Solved]How to stop a color from displaying?  (Read 5763 times)

0 Members and 1 Guest are viewing this topic.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
[Solved]How to stop a color from displaying?
« on: August 16, 2013, 05:15:33 pm »
Hello

I have this png file:



Firstly i load it as a texture(using loadfromFile function) and then create a sprite and decide the part of the texture I want to display(using sf::IntRect).

.
.
.

sf::Texture PlayTexture;

if(!PlayTexture.loadfromFile("Play.png"))
  return EXIT_FAILURE;
sf::Sprite PlayNotSelected;
PlayNotSelected.setTexture(PlayTexture);
PlayNotSelected.setTextureRect(sf::IntRect(1, 1, 80, 30));
.
.
.
 

Now when I draw it in my SFML window, which was previously loaded with another sprite having a magenta background, something like this occurs:



My question is how do I remove the white from the png file display it as if it has no background color? And the way I displayed my window background and play.png, is ther any better way to do so?

Like here:


Details:
I browsed the internet to clarify this and came accross terms like color masking or so but didn't understand them quite well. If this preocess has a particular name, please tell me that also :)

Thanks
« Last Edit: August 16, 2013, 06:59:50 pm by The illusionist mirage »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: How to stop a color from displaying?
« Reply #1 on: August 16, 2013, 05:18:52 pm »
At best you change the original texture to use a transparent background (e.g. use GIMP), so it will be transparent at loading. Otherwise you first load it as sf::Image and then call createMaskFromColor. Afterwards you can simply call loadFromImage from your texture. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: How to stop a color from displaying?
« Reply #2 on: August 16, 2013, 05:25:44 pm »
At best you change the original texture to use a transparent background (e.g. use GIMP), so it will be transparent at loading. Otherwise you first load it as sf::Image and then call createMaskFromColor. Afterwards you can simply call loadFromImage from your texture. ;)

So my approach was also not quite bad, huh?(loading a background image as texture, then converting it to a sprite and displaying it and also the same for that play.png)

And setting alpha = 0 will enable me to display whatever color is in the background (i.e., previously loaded in the window before drawing play.png) ?

Thanks !!! :D



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: How to stop a color from displaying?
« Reply #3 on: August 16, 2013, 05:34:46 pm »
loading a background image as texture, then converting it to a sprite and displaying it
You don't convert a texture to a sprite. You apply a texture to a sprite, i.e. the sprite holds a reference to the texture. A sprite is nothing else than a quad, i.e. four points that forum a rectangle and define where those points are located and what part of the texture is being displayed.
And yes the normal way to go is, load image, apply to sprite, draw sprite.

And setting alpha = 0 will enable me to display whatever color is in the background (i.e., previously loaded in the window before drawing play.png) ?
What settings are you talking about?
If you want to change the background color individually, you'll best use an image with the fancy text and then draw a colored rectangle shape and on top of that your image. And if you only want to use text, you could use SFML's built in sf::Text.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: How to stop a color from displaying?
« Reply #4 on: August 16, 2013, 05:37:08 pm »
I assume he's talking about the color argument passed to window.clear(), since that essentially is the "background color" of the window if nothing opaque is ever drawn on top of it before display() gets called.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: How to stop a color from displaying?
« Reply #5 on: August 16, 2013, 05:51:26 pm »
Quote from: eXpl0it3r
What settings are you talking about?If you want to change the background color individually, you'll best use an image with the fancy text and then draw a colored rectangle shape and on top of that your image. And if you only want to use text, you could use SFML's built in sf::Text.

No I wanted to only use the method of displaying the text from an image without its background(if it had one) to the windwo as if normal text was displayed.

Quote from: eXpl0it3r
What settings are you talking about?

Sorry, as I asked the question in a rather foolish manner after going through the SFML doc.:-X.
Apologies.

Quote from: Ixrec
I assume he's talking about the color argument passed to window.clear(), since that essentially is the "background color" of the window if nothing opaque is ever drawn on top of it before display() gets called.

No, I didn't intend that. I very clearly know that window.clear() is used to update(or clear or whatever it's called) to any color that the user wants.

Well Thanks, since Exploiter already cleared my query and now I just succeded in doing what I wanted(please see the pics at the beginning of the post where a sprite is drawn atop another; first without applying transparent colo and second, after applying transparent color).

P.S. : If I am wrong somewhere, please correct me.

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: [Solved]How to stop a color from displaying?
« Reply #6 on: August 17, 2013, 01:19:43 am »
Just don't get through ALL this fuss, just get paint.net, magic wand and CTRL+X the white background. SFML handles pretty well the alpha channel (= transparency).

 

anything