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

Author Topic: draw rectangle---- 2 or more with different position  (Read 3347 times)

0 Members and 1 Guest are viewing this topic.

iTh3End

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
draw rectangle---- 2 or more with different position
« on: May 10, 2014, 04:05:05 pm »
hi I'm beginner in SFML and i want to draw some rectangle with user choose,,,
but i can draw just one rectangle, what should i do for more draw with different position,
thanks

this is my code please help me...
 RectangleShape rectangle;
    rectangle.setPosition(30,0 );
    rectangle.setSize(Vector2f(100, 30));
    rectangle.setFillColor(Color::Yellow);
    rectangle.setOutlineColor(Color::Red);
    rectangle.setOutlineThickness(2);

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: draw rectangle---- 2 or more with different position
« Reply #1 on: May 10, 2014, 04:17:31 pm »
It's not clear what you're asking for, but it sounds like you just need to make and draw a second RectangleShape, and use sf::Mouse::getPosition() to decide where to put them.

iTh3End

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: draw rectangle---- 2 or more with different position
« Reply #2 on: May 10, 2014, 04:53:58 pm »
thanks for your reply,,,
excuse me i have many mistake in my writing because english is not my main language,
i want create a program for rectangle bin packing and i want to use SFML to solve this problem,

in this program i have many rectangles with different size and different position and All these Be set by the user,
i use
RectangleShape rectangle;
in one (for) loop but not worked and don't show the rectangles, now my question is here What should I use for show rectangles?
thanks again

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: draw rectangle---- 2 or more with different position
« Reply #3 on: May 10, 2014, 05:04:08 pm »
Could you show us a complete and minimal example that doesn't work?  For something this simple just copying the tutorials should be all you need.

You are actually drawing the rectangles to an sf::RenderWindow, right?

And what exactly do you mean "set by the user"?  There are lots of ways that might be done.

iTh3End

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: draw rectangle---- 2 or more with different position
« Reply #4 on: May 10, 2014, 06:51:29 pm »
Thanks for answering
user interface of my program is command line,
yes of course ، i use render window، look...
VideoMode videoMode(x_main,y_main);
    RenderWindow window(videoMode,"Rectangle Bin-packing");
x_mian and y_main are Length and width of main rectangle, and set by user in command line...

how can i use this code in a loop, draw 2 or more time rectangles with different position in one compile?
RectangleShape rectangle;
    rectangle.setPosition(30,10);
    rectangle.setSize(Vector2f(100, 30));
    rectangle.setFillColor(Color::Yellow);
    rectangle.setOutlineColor(Color::Red);
    rectangle.setOutlineThickness(2);

excuse me im beginner
« Last Edit: May 10, 2014, 06:58:00 pm by iTh3End »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: draw rectangle---- 2 or more with different position
« Reply #5 on: May 10, 2014, 06:56:10 pm »
Again, simply reading the tutorials (ALL of them) and copying some of their examples should be all you need to do for this simple task.

Was there something about the tutorials (particularly the Window and Graphics ones) that you couldn't understand?

By the way, a "complete and minimal example" does mean a *complete* program that we can actually compile and run on our own.  If you only show us a few bits and pieces then we have no way of knowing what's actually wrong.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: draw rectangle---- 2 or more with different position
« Reply #6 on: May 10, 2014, 07:05:44 pm »
... i use
RectangleShape rectangle;
in one (for) loop ...

So, every time through the loop you create a new rectangleshape on the stack that then gets destroyed again before the next iteration of the loop. If you intended for these to be kept alive then obviously this won't work. But, that may not even be the problem, as long as you draw them before they die.
It would be really helpful if you could just post a complete and minimal example ( https://github.com/SFML/SFML/wiki/FAQ#tr-grl-minimal ) that shows the problem. Then I'm sure that spotting the bug will be easy.

iTh3End

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: draw rectangle---- 2 or more with different position
« Reply #7 on: May 19, 2014, 04:26:27 pm »
hello I'm back again with bin packing problem,,,
this is my sample code i can't draw 2 or more rectangle with one loop in first step  :-\
please help me...

//
//  main.cpp
//  sfml-tutorial
//
//  Created by Mohammad Alikhani on 5/6/14.
//  Copyright (c) 2014 Mohammad Alikhani. All rights reserved.
//

#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;

int main()
{
    int x_main,y_main,rectangle_number;
    int total_big_area;
    cout << "please Enter the size of main rectangle between 0-1000" << endl;
    cin >> x_main >> y_main;
    while (x_main>1000 && y_main>1000) {
        cout << "wronge number your x or y over than 1000 >>> please Enter again: " << endl;
        cin >> x_main >> y_main ;
    }
    total_big_area=x_main*y_main;
   
    VideoMode videoMode(x_main,y_main);
    RenderWindow window(videoMode,"Rectangle Bin-packing");
   
    for (int i=0; i<10; i++) {
        RectangleShape rectangle;
        rectangle.setPosition(30,10);
        rectangle.setSize(Vector2f(100, 30));
        rectangle.setFillColor(Color::Yellow);
        rectangle.setOutlineColor(Color::Red);
        rectangle.setOutlineThickness(2);
    }
   
    while (window.isOpen())
    {
        Event Event;
        while (window.pollEvent(Event)) {
           
            if (Event.type == Event::Closed) {
                window.close();
            }
        }
        window.clear();
        window.draw(rectangle);
        window.display();
       
       
    }
    return EXIT_SUCCESS;
}


select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: draw rectangle---- 2 or more with different position
« Reply #8 on: May 19, 2014, 04:33:08 pm »
May I suggest you read about variable scope?

EDIT: Found a link with a better explanation
« Last Edit: May 19, 2014, 04:38:35 pm by select_this »
Follow me on Twitter, why don'tcha? @select_this

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: draw rectangle---- 2 or more with different position
« Reply #9 on: May 19, 2014, 04:35:54 pm »
Event Event;
...
if (Event.type == Event::Closed)
Bad idea. Namespaces exist for a reason...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything