SFML community forums

Help => Graphics => Topic started by: Evlampiy on August 08, 2021, 01:20:31 pm

Title: Unique names for rectangles in class
Post by: Evlampiy on August 08, 2021, 01:20:31 pm
Hello! I'm new to SFML and I try to create a game 2048. So, I create a class, that have a function to create and draw rectangle with:
sf::RectangleShape rect(sf::Vector2f(130.f, 130.f));
                rect.move(20.f, 20.f);
                rect.setFillColor(sf::Color(255, 160, 122));
                window.draw(rect);
But if I need to create two or more objects of this class, it will not work correctly because it create and draw one rectangle with one name. So, can I create a class, that will give an unique name for all rectangles and how?
And sorry for my english.
Title: Re: Unique names for rectangles in class
Post by: G. on August 08, 2021, 07:10:18 pm
If you want an undefined number of rectangle, you'll need a container (and learn how to use it), like an std::vector (https://www.cplusplus.com/reference/vector/vector/) from the C++ standard library
Title: Re: Unique names for rectangles in class
Post by: Evlampiy on August 08, 2021, 07:28:43 pm
Thanks you! But if I need only 16, can I use another method?
Title: Re: Unique names for rectangles in class
Post by: G. on August 08, 2021, 07:58:11 pm
If you need a fixed number of rectangles you can use arrays, old school arrays or std::array from the standard library.
Title: Re: Unique names for rectangles in class
Post by: Evlampiy on August 08, 2021, 08:03:03 pm
But what I will set like a type of array?
Title: Re: Unique names for rectangles in class
Post by: Stauricus on August 09, 2021, 07:42:17 am
sf::RectangleShape rect[16];
Title: Re: Unique names for rectangles in class
Post by: Evlampiy on August 13, 2021, 12:33:06 am
Thanks you very much and sory for my stupidity.  ;D