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

Author Topic: Array of different Shapes  (Read 3406 times)

0 Members and 1 Guest are viewing this topic.

calibru99

  • Newbie
  • *
  • Posts: 2
    • View Profile
Array of different Shapes
« on: January 05, 2021, 12:09:01 am »
Hi guys!
Firstly, I'm going to say that this is my first time with any C++ graphic design/non-algorithmic projects and I must say SFML makes it so enjoyable.
However, I have indeed hit a wall that I hope to break with  you.

Project description:
The project must be something like blockly.com, where users can choose from a set of pieces and be able to build a working algorithm if placed in good order. I have all the algorithmic part of the project somewhat done but I'm lacking a bit with the graphic part, mainly the main "work-area" where you can add/delete/move pieces.

My idea:
Make an array of abstract shapes that can be later transformed in the 3 types I'm using: Circle, Rectangle, Convex;

My problem:
How can I make an entity with no specific name that I can access anytime? Making an array of shapes
std::vector<sf::Shape>shapes(101)
and then going thru it like:
shape[i] = new sf::RectangleShape
won't do the deal.

Final result:
Ideally, my project should have a large array/vector of shapes that are freely movable on the work area. I could remodel all the shapes to use the sf::ConvexShape class but I would miss an important part of learning.

P.S: While I'm here, if you can help me understand how can I check if I clicked on a certain entity and add accordingly ( aka drag&drop / make connections if you press on 2 different pieces ) I would appreciate it enormly!
P.S 2: Should I create a new class for each shape (aka a custom shape) so I can hold multiple information in it? Some pieces should have editable text inside them.

If you need any more info on the subject or if I wasn't clear enough please. If I can use as little OOP as possible that would be amazing too!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Array of different Shapes
« Reply #1 on: January 17, 2021, 09:53:53 pm »
Each derived type is a type of its own so multiple shapes cannot be in the same vector; a vector can only store things of the same type. Storing just the base type doesn't allow the derived type to be stored as a base type "is not" a derived type.

This can be "solved" using pointers. That is, the vector would be a pointer all of the same type and then they would have to "converted" to the correct type later when actually used. Although it's possible, it's very confusing and extremely error prone.

The easiest and safest solution is, as you already suggested, is to create separate vectors of each type of (derived) shape and just them as required (re-create the shape on the other vector when changing shape type, for example).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything