Moving data in C++ object very quickly to NSView/CGLayer

TLDR: What is the typical fashion to send lots of data between C++ objects and objC Views?

Hi, I'm new to this forum so I apologize if what I asked has already been answered (I didn't see anything when I used the search function).

I just taught myself cocoa last weekend, because I wanted to provide a front-end GUI and visualization to my C/C++ programs. But that means I don't really know the correct or best way to do things.

For this particular question, I wrote a raytracer in C++ that takes ~4 seconds to render a static scene. Unfortunately, it takes a very long time (~a few seconds) to move the data from the C++ pixelColors[][] matrix to the View and for the View to draw it.

I realize that the cocoa drawing stuff isn't really designed for pixel by pixel drawing, but the best I could come up with is storing the data in a CGLayer member in the View, so once the data has been copied over, it is infinitely fast at scaling and redrawing. I'm not sure if there is something better for my needs.

My app's structure is the following:
Controller - nothing interesting for this discussion. tells the Model when to start rendering the scene.

Model - holds a tracerObject, the instantiation of the C++ code. also holds a pointer to the View, so it can tell it [myView needsToUpdateImage:YES].

View - has a pointer to the Model, and a CGLayer member. calls CGColorRef color = [myModel getPixelColorX:i Y:j] to painfully retrieve each pixel's color one by one from the model (who in turns asks one by one the tracerObject->getPixelColor(i,j)).

What is the typical fashion to send lots of data between C++ objects and objC Views? Should the C++ tracerObject write directly to the CGLayer? that seems to break abstraction and muddle my front-end with the underlying code. Should the model/tracerObject hand the view a pointer to the pixelColors[][] matrix, and avoid going up through all the function calls?

Sorry for the long question, I just wanted to provide as much useful detail as possible.

-C.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Take a look at NSBitmapImageRep

Cocoa, or most other drawing APIs are really not designed for pixel by pixel access, rather they are designed for drawing larger things such as images, shapes, etc...

You've probably already figured this out, but it is MUCH more efficient to draw into a bitmap (just an array of pixel information), and then draw it to the screen.

Basically, you want to take a look at NSBitmapImageRep:

http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBitmapImageRep_Class/Reference/Reference.html

It inherits from the NSImageRep class:
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSImageRep_Class/Reference/Reference.html#//apple_ref/occ/cl/NSImageRep

NSBitmapImageRep allows you to create a bitmap, access the pixels data array via bitmapData message, then draw it to the screen via the - (BOOL)drawAtPoint:(NSPoint)aPoint message.

So,

1: create a NSBitmapImageRep corresponding to the size and color depth of what you want to draw. You might need to resize this when you window size changes. Keep it around.

2: in you drawRect method, grap the pixel data, draw your scene into it, then draw you NSBitmapImageRep to the screen.