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

Author Topic: [Solved] Moving Object with Mouse  (Read 9079 times)

0 Members and 1 Guest are viewing this topic.

coding99

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
[Solved] Moving Object with Mouse
« on: June 10, 2012, 05:42:37 am »
when i click down on object and mouse move slowly object will move follow.
if i move mouse faster mouse will leave or out of object and stop moving.

what wrong with my code and how to fix it.



#include <SFML/Graphics.hpp>

bool isMouseOver(sf::Sprite &sprite, const sf::Input &input)
{
    sf::Vector2f mousePos(input.GetMouseX(), input.GetMouseY());

    if(
        // check mouse position
        (mousePos.x >= sprite.GetPosition().x)
        &&
        (mousePos.x <= sprite.GetPosition().x + sprite.GetSubRect().GetWidth())
        &&
        (mousePos.y >= sprite.GetPosition().y)
        &&
        (mousePos.y <= sprite.GetPosition().y + sprite.GetSubRect().GetHeight())
        &&
        // check pixel on sprite
        (sprite.GetPixel(mousePos.x - sprite.GetPosition().x, mousePos.y - sprite.GetPosition().y).a == 255)
    )
        return true;
    return false;
}

bool isMouseButtonDown(sf::Sprite &sprite, const sf::Input &input)
{
    if (input.IsMouseButtonDown(sf::Mouse::Left))// && isMouseOver(sprite, input))
        return true;
    return false;
}

int main()
{
    // image
    sf::Image image;
    if (!image.LoadFromFile("image.png"))
        return EXIT_FAILURE;

    // window
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML window: Mouse Event");
    window.SetFramerateLimit(60);
    const sf::Input& input = window.GetInput();

    // sprite
    sf::Sprite sprite;
    sprite.SetImage(image);

    float lastMouseX  = 0;
    float lastMouseY  = 0;
    bool  dragStarted = false;

    // game loop
    while (window.IsOpened())
    {
        sf::Event Event;
        // event loop
        while (window.GetEvent(Event))
        {
            // close window
            if (Event.Type == sf::Event::Closed)
            {
                window.Close();
            }

            // if mouse over sprite
            if(isMouseOver(sprite, input))
            {
                //if mouse button down
                if (isMouseButtonDown(sprite, input))
                {
                    if(!dragStarted)
                    {
                        //start drag
                        dragStarted = true;
                    }
                    else
                    {
                        //drag
                        sprite.Move(input.GetMouseX() - lastMouseX, input.GetMouseY() - lastMouseY);
                    }
                    //get current mouse position
                    lastMouseX = input.GetMouseX();
                    lastMouseY = input.GetMouseY();
                }
                // if mouse button up
                else
                {
                    //stop drag
                    dragStarted = false;
                }
            }
            // mouse not over sprite
            else
            {
                //stop drag
                dragStarted = false;
            }
        }

        // clear screen
        window.Clear(sf::Color(255, 255, 255));

        // draw the sprite
        window.Draw(sprite);

        // update the window
        window.Display();
    }

    return EXIT_SUCCESS;
}
« Last Edit: June 10, 2012, 09:24:18 am by coding99 »
I use SFML version 1.6 and sorry for my poor English.
- sfml1.6 can compile cleanly with gcc 4.4(recommended)-4.5 and gcc 4.6-4.7 must define NULL or modify some code
- compiling sfml1.6 with codeblocks is good on codeblocks 8.02(10.05 alway add .dll suffix)

Sathorod

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Moving Object with Mouse
« Reply #1 on: June 10, 2012, 07:02:24 am »
What's going on is you're limiting the frames per second (window.SetFramerateLimit(60); line 40) which means when you move your mouse too fast your program can't render that many frames in the period of time it took you to move your mouse. Remove that line and you should have no problem. You generally don't need to limit frame rate, there are specific instances where it's necessary but most of the time you shouldn't have that problem. What is necessary (and you may have read this and believed that what you should do is limit the frame rate), is setting a speed for your main loop so that your program will run at the same speed regardless of computer power. This is really only required in games though, but I would still recommend doing it, take a look at this link:

https://github.com/SFML/SFML/wiki/TutorialGQE-Engine#wiki-gameloop

For some more info about that.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: Moving Object with Mouse
« Reply #2 on: June 10, 2012, 09:04:05 am »
The problem isn't just the framerate. I think the real problem is that you stop dragging at the moment you leave the sprite.
You should only stop dragging when the mouse is released and move the object even when the mouse is not on the object (thus outside the isMouseOver check).
TGUI: C++ SFML GUI

coding99

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Moving Object with Mouse
« Reply #3 on: June 10, 2012, 09:23:46 am »
You should only stop dragging when the mouse is released and move the object even when the mouse is not on the object

Thanks for reply. This problem solved.  :)
« Last Edit: June 10, 2012, 09:48:47 am by coding99 »
I use SFML version 1.6 and sorry for my poor English.
- sfml1.6 can compile cleanly with gcc 4.4(recommended)-4.5 and gcc 4.6-4.7 must define NULL or modify some code
- compiling sfml1.6 with codeblocks is good on codeblocks 8.02(10.05 alway add .dll suffix)

 

anything