Hi all!
This is more a C++ question that OF, but i’m trying to implement in OF, so i think it should’n be too off topic.
This is what i’d like to do:
class Shape{
int x,y;
Shape(int _x, int _y){
x = _x;
y = _y;
}
virtual void draw() = 0;
};
class Circle: public Shape{
float r;
void draw(){
drawCircle();
}
Circle(int _x, int _y, float _r): Shape(_x,_y){
r = _r;
}
};
class Rectangle: public Shape{
float w,h;
void draw(){
drawRectangle();
}
Rectangle(int _x, int _y, float _w, float _h): Shape(_x,_y){
w = _w;
h = _h;
}
};
With a predefined number of shapes, i’ve managed to get what i want. Like this:
Rectangle * rect1, rect2;
Circle * circle1;
vector<shared_ptr<Shape>> shapes;
int currShape;
void ofApp::setup(){
rect1 = new Rectangle(x1,y1,w1,h1);
rect2 = new Rectangle(x2,y2,w2,h2);
circle1 = new Circle(x3,y3,r3);
shapes.emplace_back(rect2);
shapes.emplace_back(circle1);
shapes.emplace_back(rect1);
}
void afApp::update(){
currShape = selectShapeToDraw();
}
void ofApp::draw(){
shapes[currShape]->draw();
}
But i’d like to use an arbitrary number/type of shapes, so i was thinking to something like this:
vector<Rectangle> rectangles;
vector<Circles> circles;
vector<shared_ptr<Shape>> shapes;
but cannot get it working… I know that a vector of pointers to another vector is wrong, and i was trying with a fixed vector, because i’m doing this just in app setup (so rectangles.resize(numOfRect), circles.resize(numOfCircle), shapes.resize(numOfRect+numOfCircle)
)
Any hints?