Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wrayal

macrumors newbie
Original poster
Aug 23, 2008
9
0
I'm currently writing a gameboy emulator, and I've just got to the stage where I need to be able to draw some output. So I was wondering what the simplest possible way was to do some black and white pixel-by-pixel output? I've seen a couple of candidate ways (using NSImage or openGLView) but these seem quite overpowered for a totally trivial task - could someone possibly point me in the right direction?

Wrayal
 
I realize this is certainly NOT the way to do this, but I'm a bit curious as to what would happen if you made an NSMatrix containing thousands of pixel-sized NSRect objects each with a BOOL for fill colors of black or white?
 
NSBitmapImageRep is exactly what you want.

Your best bet would probably be to not use Cocoa. Libraries like SDL are much better suited to drawing pixel graphics.
 
I doubt a gameboy emulator is going to be refreshing the screen at high rates with millions of sprites. It's probably fairly tame. I'm guessing you could use CoreGraphics for this. I recently used CG functions to draw a view with lots of pixels, and I optimized my code to use CGContextFillRects() and it was definitely fast, although I had a small amount of colors so I could significantly reduce the number of rects being drawn. 160x144 isn't that many pixels :)

But of course, NSOpenGLView will yield the best results. OpenGL for 2D simple graphics is easy and only takes a day or so to setup/understand if you've never done it before.
 
Using a CGBitmapContext would probably be faster than drawing lots of tiny rectangles, and it avoids the overhead of sending tons of Objective-C messages. A CGBitmapContext basically wraps a block of memory, so setting a pixel is as easy/fast as setting an array element to the color you want.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.