1

Is there a way I can draw using libcinder without having to put all my code on the draw() method of the main class. I'm working on a big app and it's not convenient in any way to have everything stuffed in one file.

This is an example of what the idea would be:

class MyApp : public AppBasic {
  public:
    void setup ();
    void update ();
    void draw ();
  private:
    vector<MyObject> myObjects;
};

MyApp::draw () {
  for (int i = 0; i < myObjects.size(); ++i) {
    myObjects[i].render ();
  }
}

CINDER_APP_BASIC (MyApp, RendererGL)

/* ------------------ */

class MyObject {

  public:
    void render ();

};

void MyObject::render () {
  Rectf rect (0, 0, 20, 20);
  gl::drawSolidRoundedRect(rect, 15.0);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
StJimmy
  • 504
  • 3
  • 9
  • 18

1 Answers1

2

Yes, there is a way. Several ways, actually.

  1. Create a class with at least the following methods: void setup(), void update() and void draw(). In the main application you can then create instances of this class, store them in a member variable or std::vector. Then simply call the methods from the main application's setup, update and draw methods respectively.
  2. Use Cinder's event system to connect to its update and draw events. See the ListenerBasic sample: https://github.com/cinder/Cinder/tree/master/samples/ListenerBasic . For a list of all available events, see: https://github.com/cinder/Cinder/blob/master/include/cinder/app/Window.h .
  3. Write or use a scene graph system which handles drawing all your objects. It will call each of your object's draw() method in the correct order, can put objects on top of other objects, can detect which object is under the cursor, etc. A well-known scene graph is http://www.openscenegraph.org/ , but it is not compatible with Cinder. A very basic scene graph can be found in one of my samples: https://github.com/paulhoux/Cinder-Samples/tree/master/SimpleSceneGraph

-Paul

Paul Houx
  • 1,984
  • 1
  • 14
  • 16