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

Author Topic: Resizing a sf::Image  (Read 1416 times)

0 Members and 1 Guest are viewing this topic.

jonnyprogrammer123

  • Guest
Resizing a sf::Image
« on: December 22, 2015, 10:13:42 pm »
Hey everybody,

I'm currently working on a risk clone, and I'm having trouble with a small feature. What I currently have is a handful of sprites that make up the countries on the game map. Since sprites are rectangles and the countries are different shapes, I can't accurately tell if the mouse is hovering over a single sprite, as the transparent portions of adjacent sprites overlap with each other. To solve this, I simply added an sf::Image to my Entity class, and added a method called containsMouse() which first checks to see if the bounding box of the sprite contains the mouse, and then if the pixel at the mouse coordinate is transparent. If the pixel is transparent, the function will return false, and vice versa. My issue is that I have the functionality to run my game at different resolutions, and thus I am required to scale my sprites based on the size of the window. When I scale the sprites, the image that I load when I set the sprite's texture is a different size than the sprite itself, so when I check to see if the mouse is above a transparent pixel, it does not take into account the scale. Would anybody here happen to have a solution to this problem? Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Resizing a sf::Image
« Reply #1 on: December 22, 2015, 10:22:27 pm »
Transform the position of the mouse by the inverse transform of the sprite, and you'll get direct pixel coordinates to check in the texture.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Resizing a sf::Image
« Reply #2 on: December 22, 2015, 10:23:08 pm »
An sf::Image - at its essence - is only a storage of pixels. Resizing in an sf::Image would be calculating all the new pixels and you'd probably be doing this manually (or via a render texture).

However, instead of resizing the image itself, why don't you just "resize" the coordinates that you use?

EDIT: Looks like Laurent got there first  ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

jonnyprogrammer123

  • Guest
Re: Resizing a sf::Image
« Reply #3 on: December 22, 2015, 11:39:55 pm »
Thanks so much guys! Worked like a charm. In case anyone else comes to this thread looking for a direct answer, this is the code that I used:


bool Entity::containsMouse() {

        sf::Vector2f mousePos = Engine::window.mapPixelToCoords(sf::Mouse::getPosition(Engine::window));

                if (getBoundingBox().contains(mousePos)) {

                mousePos = sprite->getInverseTransform().transformPoint(mousePos);

                sf::Color pColor = image.getPixel(mousePos.x, mousePos.y);

                        if (pColor.a != 0)
                        return 1;      

                }

        return 0;

        }