Simplistic drawing code

Discussion in 'Mac Programming' started by wrayal, Aug 24, 2009.

  1. wrayal macrumors newbie

    Joined:
    Aug 23, 2008
    #1
    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
     
  2. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #2
    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?
     
  3. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #3
    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.
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    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.
     
  5. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #5
    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.
     

Share This Page