Hi guys, this my first post to this forum. I am trying to learn sfml right now.I am using codeblock as an IDE for c++ ,in windows 10.
I am trying to use left mouse click to draw a selection rectangle box to select multiple sprites/object that lies inside the rectangle box or in touch with the box...
but got stuck at initial stage of rectangle generation.
I have google it and found some example but those were using old version of sfml and are not useful right now and I tried something but even that didn't work out. So have come to this forum to get some help and know-how.
This is a part of my code....
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
sf::Vector2i CurrentMousePosition;
sf::Vector2i StartMousePosition;
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
// TRYING RESIZING EVENT
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
// window.setView(sf::View(visibleArea));
// update the view to the new size of the window and keep the center
window.setView(sf::View(window.getView().getCenter(), sf::Vector2f((float)event.size.width, (float)event.size.height)));
}
if(event.type==sf::Event::MouseButtonEvent(sf::Mouse::Left))
{
// left click...
StartMousePosition = sf::Mouse::getPosition(window);
cout << "LEFT MOUSE BUTTON IS PRESSED ";
}
if (event.type == sf::Event::MouseMoved)
{
// left click...
CurrentMousePosition = sf::Mouse::getPosition(window);
//cout << "LEFT MOUSE BUTTON IS PRESSED and MOUSE MOVE";
}
}
// Clear screen
window.clear();
// DRAW RECTANGLE WITH MOUSE CLICK
if( sf::Mouse::isButtonPressed( sf::Mouse::Left ) )
{
// sf::Vector2i StartMousePosition;
// StartMousePosition = sf::Mouse::getPosition(window);
int x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0 ;
x1=StartMousePosition.x;
y1=StartMousePosition.y;
x3=CurrentMousePosition.x;
y3=CurrentMousePosition.y;
x2=x3;
y2=y1;
x4=x1;
y4=y3;
// draw Rectangle with 4 corner coordinates
draw_rectangle(window,x1,y1,x2,y2,x3,y3,x4,y4,12,Color1);
}
// Update the window
window.display();
}
return EXIT_SUCCESS;
}
draw_rectangle is a function created by my, based on sfml.in this function i am using 4 lines to draw rectangle instead of built-in sfml rectangle with width-height.