Adding Box2D to Xcode

Grab the Box2D folder and drag it into your Xcode project.

b2d1

b2d2

Make sure copy items and the correct target are both checked.

b2d3

In the project build settings, filter by “user”.

b2d4

Under ‘User Header Search Paths’ add a . for the current directory and select recursive.

b2d5

Set ‘Always Search User Paths’ to YES.

b2d6

For the given source file, you need to change the extension to *.mm

b2d7

Doing so, changes the default type to Objective-C++.

b2d8

Finally, import Box2D and begin your journey.

b2d9

CGDataProviderCreateDirect

It’s hard to find an example using CGDataProviderCreateDirect, so here’s a quick snippet:

static unsigned int* data; // used for manipulating pixel data directly

const void * getBytePointerCallback(void *info)
{
	return (void*)data;
}

@implementation SomeView

- (void)createImage
{
    CGDataProviderDirectCallbacks callbacks;
    callbacks.version = 0;
    callbacks.getBytePointer = getBytePointerCallback;
    callbacks.releaseBytePointer = NULL;
    callbacks.getBytesAtPosition = NULL;
    callbacks.releaseInfo = NULL;
    
    float w = self.layer.frame.size.width;
    float h = self.layer.frame.size.height;
    
    w *= [UIScreen mainScreen].scale;
    h *= [UIScreen mainScreen].scale;
    
    int numComponents = 4;
    uint dataLength = w * h * numComponents;
    
    data = malloc(dataLength * sizeof(uint)); // TODO: free
    
    uint r = 255;
    uint g = 0;
    uint b = 0;
    for(int i = 0; i < dataLength; ++i)
    {
        data[i] = 255 << 24 | b << 16 | g << 8 | r;
    }
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    CGDataProviderRef bitmapProvider = CGDataProviderCreateDirect(data,dataLength,&callbacks);

    CGImageRef bitmap = CGImageCreate(w,
                                      h,
                                      8,    // bits per component
                                      8*numComponents,  // total bits
                                      w*numComponents,  // bytes per row
                                      colorSpace,
                                      kCGBitmapByteOrderDefault,
                                      bitmapProvider,
                                      NULL,
                                      false,
                                      kCGRenderingIntentDefault);    
    
    self.layer.contents = (__bridge id)(bitmap);
    
    CGImageRelease(bitmap);
    CGDataProviderRelease(bitmapProvider);
    CGColorSpaceRelease(colorSpace);
}