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

Author Topic: IsMouseButtonDown freezing problem  (Read 2108 times)

0 Members and 1 Guest are viewing this topic.

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
IsMouseButtonDown freezing problem
« on: March 27, 2011, 09:22:55 am »
I am trying to make simple Drag 'n' Drop but it doesn't work. When I tried to move scrap it froze. Please explain me why it doesn't work.
Here is my source but better way is download my project with pictures for better understand the code.

Full project:
http://www.megaupload.com/?d=OP4KRDE1

Source:
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

int main()
{
int scrapx=400, scrapy=300;
sf::Image itrashcan, iscrap;

itrashcan.LoadFromFile("trashcan.png");
iscrap.LoadFromFile("scrap.png");
itrashcan.SetSmooth(false);
iscrap.SetSmooth(false);

sf::Sprite trashcan(itrashcan), scrap(iscrap);

trashcan.SetPosition(100.f,100.f);

    scrap.SetPosition(scrapx, scrapy);

    sf::RenderWindow App(sf::VideoMode(800, 600), "Drag 'n' Drop");

while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Test for scrap moving
if(App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
            if(App.GetInput().GetMouseX() >= scrapx)
                if(App.GetInput().GetMouseX() <= scrapx+50)
                    if(App.GetInput().GetMouseY() >= scrapy)
                        if(App.GetInput().GetMouseY() <= scrapy+50)
                           while(App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
                           {
                                scrapx = App.GetInput().GetMouseX()-25;
                                scrapy = App.GetInput().GetMouseY()-25;
                                scrap.SetPosition(scrapx, scrapy);

                                App.Clear();
                                App.Draw(trashcan);
                                App.Draw(scrap);
                                App.Display();
                            }
        App.Clear();
        App.Draw(trashcan);
        App.Draw(scrap);

        // Test that scrap is in trash can
        if(scrapx >= 100 && scrapx+50 <= 175 && scrapy >= 100 && scrapy+50 <= 200)
        {
            sf::String cisto("Scrap is in Trash can", sf::Font::GetDefaultFont());
            cisto.SetPosition(0.f,0.f);
            App.Draw(cisto);
        }

        App.Display();

Sleep(0.020f);
}

return 0;
}


devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
IsMouseButtonDown freezing problem
« Reply #1 on: March 27, 2011, 10:55:24 am »
You're not pumping events while inside the while-loop.

Edit: to clarify a bit. Set a boolean variable when you get a mousedown event, then unset it when you get the mouseup event - and do the dragging while the variable is set, instead of doing loops like the one you posted.

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
IsMouseButtonDown freezing problem
« Reply #2 on: March 27, 2011, 11:03:43 am »
I did it like you said at least by myself but I wanted know why it didnt work, so thank you :wink: . And it work also when I added Event check in the loop  :D

 

anything