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

Author Topic: Is there sf::isMouseOnSprite() function? [SOLVED]  (Read 3239 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Guest
Is there sf::isMouseOnSprite() function? [SOLVED]
« on: December 10, 2013, 05:20:24 pm »
Hi, I can only make rectangular buttons. I want to make triangle buttons, circle buttons, oval buttons, rectangular buttons with rounded corners and many other fancy buttons, because I like buttons. Is there any function in SFML that could check if mouse is located on a *.PNG sprite? Or do I have to use fancy mouse position maths to do so? Or is there another way to push buttons?
« Last Edit: December 10, 2013, 06:07:21 pm by Mark »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Is there sf::isMouseOnSprite() function?
« Reply #1 on: December 10, 2013, 05:35:26 pm »
I want to make triangle buttons, circle buttons, oval buttons, rectangular buttons with rounded corners and many other fancy buttons, because I like buttons.
;D

Is there any function in SFML that could check if mouse is located on a *.PNG sprite? Or do I have to use fancy mouse position maths to do so? Or is there another way to push buttons?
There is no such function, but you can load an sf::Image and create a 2D bit array of the same size as the image. If a pixel is part of the button, for example by not being transparent in the original image, then set the bit in the 2D array to 1, otherwise leave it as 0. The array itself can be represented by std::vector<bool> (or another container, if you don't like this specialization), and you can map 2D indices to 1D. To check whether the button was clicked, simply check the bit at the corresponding position in your 2D array.

Alternatively, if you want to support arbitrary polygons rather than pre-defined images, you could have a look at Boost.Geometry and its algorithm within() to check if a point is contained by a polygon.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mark

  • Guest
Re: Is there sf::isMouseOnSprite() function?
« Reply #2 on: December 10, 2013, 05:59:46 pm »
Thanks, I got the first method and it looks fine. And I don't think you really need an array:
if (event.mouseMove.x >= WinPosX1 && event.mouseMove.x <= WinPosX2 &&
    event.mouseMove.y >= WinPosY1 && event.mouseMove.y >= WinPosY2 && !hoveredJustBefore)
{
      if (myButtonImage.GetPixel(event.mouseMove.x - WinPosX1, event.mouseMove.y - WinPosY1).a == 255)
      {
            if (hoveredJustBefore) // ... Redraw ALL the screen!
            hoveredJustBefore = false;
      } else
      {
            // ... Redraw ALL the screen!
            hoveredJustBefore = true;
      }
}
 

I'm not sure if it will even compile but I'm quite sure I got it :D Thanks!
« Last Edit: December 10, 2013, 06:36:31 pm by Mark »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Is there sf::isMouseOnSprite() function? [SOLVED]
« Reply #3 on: December 10, 2013, 06:30:33 pm »
sf::myButton.GetPixel
What's that? Why is there a namespace qualifier?

Which SFML version are you using? There is no sf::Sprite::GetPixel() in SFML 2. The solution I proposed will also be more efficient and less memory-wasting than keeping a sf::Image allocated.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mark

  • Guest
Re: Is there sf::isMouseOnSprite() function? [SOLVED]
« Reply #4 on: December 10, 2013, 06:33:16 pm »
Sorry. I've said I'm new to programming and SFML. I normally fix errors like that when compiler throws a bunch of errors at me :D

I would use both! I would load an image, then load a texture from an image, then a sprite for an image and then draw sprites and check for pixels in the image. As I've said I'm very new to programming I so on I manage to accomplish the things I want  ;) And now I know how to make any-shape buttons! Thank you!

sf::Image myButtonImage;
sf::Texture myButtonTexture;
sf::Sprite myButtonSprite;
myButtonImage.loadFromFile("button.png");
myButtonTexture.loadFromImage(myButtonImage);
myButtonSprite.setTexture(myButtonTexture);
 
« Last Edit: December 10, 2013, 06:42:37 pm by Mark »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Is there sf::isMouseOnSprite() function? [SOLVED]
« Reply #5 on: December 10, 2013, 06:42:26 pm »
Sorry. I've said I'm new to programming and SFML.
Don't learn both at the same time. C++ is a very difficult language, you should at least invest some months into it before using a library. That may sound boring, but it's far less frustrating than having to fight the language again and again during game development. And it will also save you a lot of time.

I would use both! I would load an image, then load a texture from an image, then a sprite for an image and then draw sprites and check for pixels in the image.
sf::Image is simpler to use than a 2D bit array, but you shouldn't choose this approach in a bigger project (see my previous posts to know why)...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mark

  • Guest
Re: Is there sf::isMouseOnSprite() function? [SOLVED]
« Reply #6 on: December 10, 2013, 06:56:54 pm »
Don't learn both at the same time. C++ is a very difficult language, you should at least invest some months into it before using a library. That may sound boring, but it's far less frustrating than having to fight the language again and again during game development. And it will also save you a lot of time.
My programming history:
1. A month with Pascal. Then BAAAM. I realised I'm outdated.
2. A lot of language switching. Finally I decided to get something of a very low level. Tried to learn assembly. I haven't learn't any modern assembly by this day but I have designed my own 6-bit CPU + I/O and at least now I know how computers work now. That helped me a lot when I was learning pointers later. Consumed about 3 months of minecrafting and watching youtube stuff.
3. Finally, C++. I've got to the pointers and I am sick of console by this day. I'm glad I haven't taken ASM.
4. Tried SDL. Version 1.2., haha. Then BAAAAAAAM! You're sooo old school.
5. Returned to C++. Now I use multi-dimensional pointers without a struggle, I've did some linked lists, binary trees and other data structures. Things I haven't learnt: STL (I know it's a huge library, but in the book there are only vectors and maps mentioned, I think I could use a vector only with a reference, I'd need some work on maps), classes (I know they're a new data type, with their own private and global stuff, I've got no problem using them but no experience creating them), templates, and inheritance. I don't know, maybe there is some more stuff in C++, but that's all in the book (Jumping into C++, a good book).
6. Back to graphics. Here I am, SFML 2.1, finally stepping up with the normal people.

I'm sick of win32 console, I've bumped too many walls and I'm coming back to learn classes, even though I don't see a big reason why should I do that as I successfully come over obstacles without it only after I complete this game you will see soon.

P.S. Sorry for my horrible English.
« Last Edit: December 10, 2013, 07:12:25 pm by Mark »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Is there sf::isMouseOnSprite() function? [SOLVED]
« Reply #7 on: December 10, 2013, 07:10:21 pm »
Now I use multi-dimensional pointers without a struggle
And again, BAAM, you're outdated :P

More than one pointer indirection is rarely, if ever, necessary in C++. Same for manual memory management, don't use new and delete. Use RAII instead.

Things I haven't learnt: STL
The STL is the most important part of the standard library. It's something you will use every day, and it simplifies programming tremendously. So it's definitely worth learning.

classes
Also an absolute must as soon as you want to develop own projects.

templates, and inheritance.
They're not as crucial as the other points, but also very useful. And their basics are relatively easy to understand once you know classes, but of course they provide endless possibilities.

Apart from the pure language features, there a lot of programming techniques and idioms that are helpful to know. For example RAII, rule of 3/5, exception safety, ownership and move semantics, type erasure, ... Some of them are advanced, but eventually it's good if you also have a look at them. And they are actually the difficult part in C++ -- the pure language features are "easy" to learn, but how to effectively combine them in greater projects is at least as important.

In conclusion: Never underestimate C++; don't think you know the language after a year. It's one of the most complex and difficult programming languages, but if you like to have powerful tools at hand, it will be rewarding. If you want to see quick results, Java or C# will be more efficient.
« Last Edit: December 10, 2013, 07:14:52 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mark

  • Guest
Re: Is there sf::isMouseOnSprite() function? [SOLVED]
« Reply #8 on: December 10, 2013, 07:16:45 pm »
Okay okay, just let me complete that simple Mario-like game. I already designed it. Thanks for the advices :) I'll follow them just after the game.
« Last Edit: December 10, 2013, 07:21:59 pm by Mark »

 

anything