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

Author Topic: Fill method for Image ?  (Read 10851 times)

0 Members and 2 Guests are viewing this topic.

ShqrdX

  • Newbie
  • *
  • Posts: 12
    • View Profile
Fill method for Image ?
« on: June 27, 2016, 06:36:13 pm »
Maybe it's a good idea to add a method to sf::Image which fills the specified region(or whole) image with specified Color ?

ShqrdX

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Fill method for Image ?
« Reply #1 on: June 27, 2016, 06:38:27 pm »
Ah.. the constructor for this already existed..  :-\ but additional method could exist too.

Quote
sf::Image image;
image.create(100,100,sf::Color::Black);
image.fill(sf::IntRect(20,20,60,60), sf::Color::Red); // create big red dot
« Last Edit: June 27, 2016, 07:05:12 pm by ShqrdX »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Fill method for Image ?
« Reply #2 on: June 27, 2016, 08:19:14 pm »
This might not be the most effecient method (most manual image manipulation is usually slow anyway) but you can do this with a temporary sf::Image (this does what your example code would do):

        sf::Image image;
        image.create(100, 100, sf::Color::Black);
        sf::Image tempRectImage;
        tempRectImage.create(40, 40, sf::Color::Red);
        image.copy(tempRectImage, 20, 20);

If SFML allowed the image to be created in its constructor, it may be a lot nicer:
        sf::Image image;
        image.create(100, 100, sf::Color::Black);
        image.copy(sf::Image(40, 40, sf::Color::Red), 20, 20);
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Fill method for Image ?
« Reply #3 on: June 27, 2016, 08:47:23 pm »
What's wrong with the naive approach?
for (int x = minx; x < maxx; ++x)
    for (int y = miny; y < maxy; ++y)
        image.setPixel(x, y, color);

If you want something efficient don't use sf::Image anyway.

Quote
If SFML allowed the image to be created in its constructor, it may be a lot nicer
Looks like it could be done (the create function has no return value).
Laurent Gomila - SFML developer

ShqrdX

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Fill method for Image ?
« Reply #4 on: June 28, 2016, 06:39:31 am »
What's wrong with the naive approach?

Nothing and its the good way. I just wondering about possibility of simplify this because its "simple"(like S in SFML).

So sorry about my habit to prefer the solutions from box :P
« Last Edit: June 28, 2016, 06:49:53 am by ShqrdX »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Fill method for Image ?
« Reply #5 on: June 28, 2016, 02:46:19 pm »
Quote
If SFML allowed the image to be created in its constructor, it may be a lot nicer
Looks like it could be done (the create function has no return value).
Might be useful for temporary images as I demonstrated above but I'm not sure how common this would be to warrant creating a new constructor just so that is can also call create.

That version does remove some verbosity but I don't think that was the problem. The problem really is that the temporary image isn't very temporary. Obviously, it could be contained in a self-created function to fix this. However, it can, of course, be contained inline to limit its scope very simply (in case your only real problem is that the temporary image outlives its usefulness:
    sf::Image image
    image.create(100, 100, sf::Color::Black);
    {
        sf::Image tempRectImage;
        tempRectImage.create(40, 40, sf::Color::Red);
        image.copy(tempRectImage, 20, 20);
    } // tempRectImage is destroyed here :)

So sorry about my habit to prefer the solutions from box :P
Create a simple function that fills the rectangle for you ;)

Remember that you can do these sorts of things easily with render textures and rectangle shapes:
sf::RenderTexture renderTexture;
renderTexture.create(100, 100);
renderTexture.clear(sf::Color::Black);
{
    sf::RectangleShape rectangle({ 60, 60 });
    rectangle.setFillColor(sf::Color::Red);
    rectangle.setPosition({ 20, 20 });
    renderTexture.draw(rectangle);
} // rectangle is destroyed here
renderTexture.display();
It opens up a lot more options of how to draw to the "image" when it is a render texture (you can use and re-use any SFML drawable, for example).
If you still need to have this as an sf::Image, you can then convert it from a render texture when you're finished:
sf::Image image{ renderTexture.getTexture().copyToImage() };
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

aggsol

  • Newbie
  • *
  • Posts: 24
  • C++
    • View Profile
    • My Github Account
Re: Fill method for Image ?
« Reply #6 on: July 04, 2016, 10:44:23 am »
If you want something efficient don't use sf::Image anyway.

What do you mean by this comment? Efficient in terms of handling or performance? What part of SFML would you recommend then?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Fill method for Image ?
« Reply #7 on: July 04, 2016, 10:55:38 am »
sf::Image is not the most performant when reading or writing pixels. You'll get the best performances with a raw std::vector<sf::Uint8> and direct access to pixel components.
Laurent Gomila - SFML developer

Phanoo

  • Full Member
  • ***
  • Posts: 136
    • View Profile
Re: Fill method for Image ?
« Reply #8 on: August 24, 2016, 02:30:49 pm »
SFML is inefficient because lot of parts of it are bloated with getters/setters and other high-overhead boilerplate...
Directly accessing arrays/members is so much cleaner and faster !

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Fill method for Image ?
« Reply #9 on: August 24, 2016, 08:37:21 pm »
@ratatax, yet you want more overloads for those setters...
http://en.sfml-dev.org/forums/index.php?topic=20052.msg148515#msg148515
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Phanoo

  • Full Member
  • ***
  • Posts: 136
    • View Profile
Re: Fill method for Image ?
« Reply #10 on: August 26, 2016, 10:06:34 am »
It depends.
I prefer direct member access if you let me choose (for actions that are safe to set an arbitrary value). But if i'm forced to go with setters/getters, i want them to be flexible so they don't annoy me with thinks like :

s.SetPosition(10, s.GetPosition().y)

while the same thing could be achieved by :

s.SetX(10)

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Fill method for Image ?
« Reply #11 on: August 26, 2016, 02:56:47 pm »
"s.setPosition(10, s.getPosition().y);" Is that bad?
There can not be such method as "setX", becouse it doesn't tell anything about itself, it would have to be "setPositionX" which is pointless as you saveā€¦ 20 characters? Or something similar.

Phanoo

  • Full Member
  • ***
  • Posts: 136
    • View Profile
Re: Fill method for Image ?
« Reply #12 on: August 26, 2016, 04:55:30 pm »
SFML had those SetX/SetY but they were removed in version 2 or something

SetX is enough self explaining since you use it on a Sprite object...

I just like when things are simple and not overly verbose. That's probably why i prefer C over C++
« Last Edit: August 26, 2016, 04:58:39 pm by ratatax »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Fill method for Image ?
« Reply #13 on: August 26, 2016, 07:53:46 pm »
I wouldn't say clarity is "overly-verbose".

SetX is enough self explaining since you use it on a Sprite object...
I disagree. It's not clear if this is position, origin, scale or even the texture rectangle's position.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Fill method for Image ?
« Reply #14 on: August 30, 2016, 09:45:22 pm »
Quote
SFML is inefficient because lot of parts of it are bloated with getters/setters

I beg to differ. If you take a look at the generated asm of an optimized build of the library + your application I think you'll be surprised how much of this "bloat" the compiler is actually able to strip away. If you can come up with a usecase/benchmark where this matters one iota in any realistic scenario, then I'll buy you a beer.