1
Graphics / Re: Drawing a click and drag rectangle?
« on: August 25, 2014, 04:49:33 pm »
Thanks for the help. sf::Event::MouseMoved is what I needed it seems. My code works perfectly now, and I think I can simplify it later, too.
@BaneTrapper Haha, don't worry mate, I never just copy/paste code anyway. Thanks for the example.
#include <SFML/Graphics.hpp>
int main(int argc , char * argv[] )
{
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int BITS_PER_PIXEL = 32;
sf::VideoMode VMode( SCREEN_WIDTH , SCREEN_HEIGHT , BITS_PER_PIXEL );
sf::RenderWindow window( VMode , "SFML!" , sf::Style::Titlebar | sf::Style::Close );
sf::Vector2i starting_position;
starting_position = sf::Mouse::getPosition( window );
sf::Vector2f current_position;
sf::RectangleShape box;
while( window.isOpen() )
{
sf::Event event;
while( window.pollEvent( event ) )
{
if( event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape ) window.close();
if( event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left )
{
starting_position.x = event.mouseButton.x;
starting_position.y = event.mouseButton.y;
}
if( event.type == sf::Event::MouseMoved && sf::Mouse::isButtonPressed(sf::Mouse::Left) )
{
current_position.x = event.mouseMove.x - starting_position.x;
current_position.y = event.mouseMove.y - starting_position.y;
box.setSize( current_position );
box.setPosition( starting_position.x , starting_position.y );
box.setFillColor( sf::Color::White );
}
}
window.clear();
window.draw( box );
window.display();
}
return 0;
}
int main(int argc , char * argv[] )
{
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int BITS_PER_PIXEL = 32;
sf::VideoMode VMode( SCREEN_WIDTH , SCREEN_HEIGHT , BITS_PER_PIXEL );
sf::RenderWindow window( VMode , "SFML!" , sf::Style::Titlebar | sf::Style::Close );
sf::Vector2i starting_position;
starting_position = sf::Mouse::getPosition( window );
sf::Vector2f current_position;
sf::RectangleShape box;
while( window.isOpen() )
{
sf::Event event;
while( window.pollEvent( event ) )
{
if( event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape ) window.close();
if( event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left )
{
starting_position.x = event.mouseButton.x;
starting_position.y = event.mouseButton.y;
}
if( event.type == sf::Event::MouseMoved && sf::Mouse::isButtonPressed(sf::Mouse::Left) )
{
current_position.x = event.mouseMove.x - starting_position.x;
current_position.y = event.mouseMove.y - starting_position.y;
box.setSize( current_position );
box.setPosition( starting_position.x , starting_position.y );
box.setFillColor( sf::Color::White );
}
}
window.clear();
window.draw( box );
window.display();
}
return 0;
}
@BaneTrapper Haha, don't worry mate, I never just copy/paste code anyway. Thanks for the example.