I am writing a Cocoa application that uses a C++ library that I am also writing. I want the C++ library to be able to call a draw method in the Cocoa application.
Specifics – to put it into context, I am running OpenNi skeletal tracking and recording the skeletal data. At each new frame, I want to tell the Objective-C code that it can/should draw the data to screen.
The OpenNI tracking code is called by (and has a handle to) a control object SkeletalModuleControl.
The only object the Objective-C code interacts with is this control class.
My thoughts are that it would be best to create callDraw and registerDraw methods in the control class.
The Objective-C code would register its draw method (or multiple draw methods?) and the callDraw would call the registered draw methods (if any).
In general, drawing into a Cocoa view should only be done when Cocoa calls the view’s
-drawRect:method. So, the question you really need to consider is how to get Cocoa to invalidate your view(s) when you have updated data you want to display…Writing a plain-old C function that you can call from your C++ code is probably the simplest approach. Having the C++ library let you register a callback function to invalidate the relevant views is straightforward too. Just keep in mind that either way, the function should only be responsible for invalidating views (e.g. calling
-setNeedsDisplay:or-setNeedsDisplayInRect:). The actual drawing needs to happen later, when Cocoa gets around to calling the-drawRect:method(s).If you want to put code in the C++ library to do the drawing, just wrap it as a plain-old C function that can be called from
-drawRect:.