SFML community forums
Help => Window => Topic started by: reDo 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:
#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;
}
(http://i51.tinypic.com/2ufpm2s.jpg)
-
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.
-
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