Dynamic Textures

Sparrow lets you create custom textures directly at run-time. However, we didn’t reinvent the wheel for that feature — credit goes to the iPhone SDK, which already contains a powerful API called “Core Graphics”.

You access the drawing API with a special init-method of SPTexture, which takes a block-parameter you can fill with your drawing code.

Remember, we've seen blocks before when we looked at Enhanced Events.

Here's a sample of how to create a custom texture with Core Graphics in Sparrow:

SPTexture *customTexture = [[SPTexture alloc] initWithWidth:200 height:100
    draw:^(CGContextRef context)
    {
        // draw a string
        CGContextSetGrayFillColor(context, 1.0f, 1.0f);
        NSString *string = @"Hello Core Graphics";
        [string drawAtPoint:CGPointMake(20.0f, 20.0f)
                 withFont:[UIFont fontWithName:@"Arial" size:25]];
    }];

As you can see, the drawing code is placed right in the block. Whatever you draw to the context will appear on your texture later.

If you haven’t worked with Core Graphics before, there are numerous resources on the web that describe how to use them. Here is a good starting point to find out more about them.


Next section: Texture Formats

  manual/dynamic_textures.txt · Last modified: 2013/05/28 15:54 by daniel
 
Powered by DokuWiki