Skip to content

Instantly share code, notes, and snippets.

@mntmn
Created December 21, 2013 15:49
Show Gist options
  • Save mntmn/8071085 to your computer and use it in GitHub Desktop.
Save mntmn/8071085 to your computer and use it in GitHub Desktop.
probably the quickest way to draw pixels on OSX in C. catch: compiles only with -m32 (32 bit arch). :|
#include <Carbon/Carbon.h>
#include <CoreGraphics/CoreGraphics.h>
uint32_t framebuffer[FB_W*FB_H];
WindowRef osx_window;
CGContextRef osx_window_context;
CGContextRef osx_bitmap_context;
void updateFrameBuffer() {
//printf("updateFrameBuffer\n");
CGImageRef img = CGBitmapContextCreateImage(osx_bitmap_context);
CGContextDrawImage(osx_window_context, CGRectMake(0,0,FB_W,FB_H), img);
CGContextFlush(osx_window_context);
CGImageRelease(img);
}
void destroyFrameBuffer() {
QDEndCGContext(GetWindowPort(osx_window), &osx_window_context);
}
void createFrameBuffer() {
Rect rect = {100,100,100+FB_H,100+FB_W};
OSStatus err = CreateNewWindow(
kDocumentWindowClass,
kWindowStandardDocumentAttributes,
&rect, &osx_window);
osx_bitmap_context = CGBitmapContextCreate(framebuffer,
FB_W,
FB_H,
FB_D,
FB_W*4,
CGColorSpaceCreateDeviceRGB(),
kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedFirst);
for (int i=0; i<FB_W*FB_H; i++) {
framebuffer[i] = 0x000000ff;
}
ShowWindow(osx_window);
SetPortWindowPort(osx_window);
QDBeginCGContext(GetWindowPort(osx_window), &osx_window_context);
updateFrameBuffer();
}
void putPixel(uint32_t x,uint32_t y,uint32_t color) {
framebuffer[x+y*FB_W] = color;
}
@Lewiscowles1986
Copy link

Do you know of a more modern way to use this, with a 64-bit OSX?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment