1
General / Re: Can't figure out how to enable click and move
« on: September 18, 2013, 12:34:56 pm »
Something like that?
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480, 32), "TEST");
sf::RectangleShape shape(sf::Vector2f(30.0f, 30.0f));
shape.setFillColor(sf::Color::Red);
shape.setPosition(sf::Vector2f(100.0f, 200.0f));
shape.setOrigin(15, 15);
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Escape)
window.close();
break;
default:
break;
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
sf::Vector2f pos = (sf::Vector2f)sf::Mouse::getPosition(window);
if(shape.getGlobalBounds().contains(pos)) {
shape.setPosition(pos);
}
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480, 32), "TEST");
sf::RectangleShape shape(sf::Vector2f(30.0f, 30.0f));
shape.setFillColor(sf::Color::Red);
shape.setPosition(sf::Vector2f(100.0f, 200.0f));
shape.setOrigin(15, 15);
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Escape)
window.close();
break;
default:
break;
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
sf::Vector2f pos = (sf::Vector2f)sf::Mouse::getPosition(window);
if(shape.getGlobalBounds().contains(pos)) {
shape.setPosition(pos);
}
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}