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

Author Topic: Cover an image except for a little part  (Read 2907 times)

0 Members and 1 Guest are viewing this topic.

Xorlium

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Cover an image except for a little part
« on: July 04, 2010, 08:34:14 pm »
I'm developing a game, and I have a question that I can't figure out how to do it.

Say I have a sf::Sprite displaying a sf::Image in the 'background'. I have thousands of little circles floating on top of that sf::Sprite, and I want the image to display ONLY where those circles are.

In other words, I'm thinking of the little circles as "loopholes" where you can peek into the image.

Can anyone think of a quick way to do this?

What I tried was making the little circles all have images in the shape of round white circles, and then changing those images to copy from the background image (using GetPixel and SetColor).

That was terribly slow. I'm talking about a reduction from ~1500 frames per second I was getting to ~17, and it looked terrible, since the whole solid circle (even if pretty small) became one color.

I'm sure there is a way with blend modes or something. Maybe with anti-alpha?

Recruit0

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Cover an image except for a little part
« Reply #1 on: July 05, 2010, 06:24:51 pm »
That's called image masking. Basically:

  • You have the "image mask", which is black and white. Say the background's black and the circles are white.
  • You have an image that you want to apply this to.
  • You put the image mask over the viewing image. Any white parts (circles) will be filled with the viewing image (i.e. the white parts become "holes").
There should be tutorials for this. I'm not sure if SFML has this capability built in.

Your method was (obviously) slow because it's doing pixel-by-pixel operations rather than blitting (i.e. throwing everything on at once). Also I think there is hardware that supports image masking operations, but I'm not that familiar with it.

pdusen

  • Newbie
  • *
  • Posts: 30
    • View Profile
Cover an image except for a little part
« Reply #2 on: July 05, 2010, 06:56:13 pm »
This can be accomplished with sf::Image::CreateMaskFromColor.

Basically, if your image is a white circle with black background, just pass the image the color you want to be transparent with that function.

Xorlium

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Cover an image except for a little part
« Reply #3 on: July 06, 2010, 11:27:57 pm »
Thanks, both.

But I didn't explain well enough what I wanted. If I had only one immobile circle, then yes, using masking would be good. But I have thousands of little circles moving around. I want a pixel of the image to show if and only if there is at least one circle over it.

I could go thorough all the pixels, one by one, checking them against the circles, one by one. Or I could go through the circles, one by one, and setting a bool "show" to all the pixels. But that would be very, very slow.

Anyway, I would still like to know out of curiosity, if there is a way to accomplish this. But I decided to go on a different path for this.