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

Author Topic: Click and drag sprites?  (Read 7367 times)

0 Members and 1 Guest are viewing this topic.

pabloist

  • Newbie
  • *
  • Posts: 26
    • View Profile
Click and drag sprites?
« on: February 02, 2011, 07:22:30 pm »
I'm trying to figure out how I can click on a sprite, then drag it and drop it at a new position. I'm guessing either I shouldn't be using Sprite.SetPosition() like this OR I'm going about calculating a mouseover incorrectly.

Code: [Select]

#include <SFML/Graphics.hpp>

bool isMouseOver(const sf::Sprite& sprite, const sf::Input& input)
{
    const int mousex = input.GetMouseX();
    const int mousey = input.GetMouseY();

    const sf::Vector2f spritesize   = sprite.GetSize();
    const sf::Vector2f spritecenter = sprite.GetCenter();

    // If the distance between the mouse and the sprite's center
    // is less than the length/width of the sprite.
    if (mousex - spritecenter.x < spritesize.x &&
        mousey - spritecenter.y < spritesize.y)
        return true;
    return false;
}


int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");


    // Usually I use a resource manager to do this.
    sf::Image Image;
    Image.LoadFromFile("cb.bmp");
    sf::Sprite Sprite;

    Sprite.SetImage(Image);

    while (App.IsOpened())
    {
        const sf::Input& Input = App.GetInput();

        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::KeyPressed)
            {
                if (Event.Key.Code == sf::Key::Escape)
                    App.Close();
            }

            if (Event.Type == sf::Event::Closed)
                App.Close();
            }
        }

        if (Input.IsMouseButtonDown(sf::Mouse::Left))
            if (isMouseOver(Sprite, Input))
                Sprite.SetPosition(Input.GetMouseX(), Input.GetMouseY());

        App.Clear();

        App.Draw(Sprite);

        App.Display();
    }

    return EXIT_SUCCESS;
}


What happens is that the sprite DOES get dragged around.. but only as long as the mouse is on the sprite's original sprite area. So I'm guessing that I should be modifying something else about the sprite or something, or resetting the center. I've tried that, but I'm not sure how to go about doing it correctly.

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Click and drag sprites?
« Reply #1 on: February 05, 2011, 07:54:28 pm »
Code: [Select]
const int mousex = input.GetMouseX();
const int mousey = input.GetMouseY();


This seems like the mouse position would stay the same value in the code. I think this is why it only works if the mouse is in the original position. Take out the const and just make them normal.
-Wander

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Click and drag sprites?
« Reply #2 on: February 05, 2011, 07:59:28 pm »
With const in a function like that they will be recreated every call to the function like a normal variable. The difference is that you are only allowed to initialise them. IF they would have been "static" too then that would have been true.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Click and drag sprites?
« Reply #3 on: February 05, 2011, 08:01:49 pm »
Wait. What I said would've been true or his code would've been true? LOL
-Wander

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Click and drag sprites?
« Reply #4 on: February 05, 2011, 08:12:45 pm »
Quote from: "Wander"
Wait. What I said would've been true or his code would've been true? LOL


sorry, should have quoted you. Wander, your statement would be true if the static keyword also were there :P
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Click and drag sprites?
« Reply #5 on: February 05, 2011, 08:19:05 pm »
Oh okay! Haha! :D
That is the only thing I can identify that would've cause a problem, so I have no idea. Sorry. XD
-Wander

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Click and drag sprites?
« Reply #6 on: February 05, 2011, 08:42:10 pm »
Well it's simple, he never compares against the sprite position...

All you compare against is the origin and the size of the sprite. The origin position is defined relative to the position of the sprite.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Click and drag sprites?
« Reply #7 on: February 05, 2011, 08:43:28 pm »
OH! I see it now! ;P
-Wander

pabloist

  • Newbie
  • *
  • Posts: 26
    • View Profile
Click and drag sprites?
« Reply #8 on: February 05, 2011, 11:56:36 pm »
Ah, thanks. Comparing against the position instead of the center fixed it.

 

anything