Game Development Reference
In-Depth Information
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
UIColor* color1 = nil;
UIColor* color2 = nil;
if (damage >= 30){
color1 = [UIColor colorWithRed:1.0 green:0.8 blue:0.8 alpha:1.0];
color2 = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
} else if (damage >= 20){
color1 = [UIColor colorWithRed:0.8 green:1.0 blue:0.8 alpha:1.0];
color2 = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
} else {
color1 = [UIColor colorWithRed:0.8 green:0.8 blue:1.0 alpha:1.0];
color2 = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
}
CGColorRef clr[] = { [color1 CGColor], [color2 CGColor] };
CFArrayRef colors = CFArrayCreate(NULL, (const void**)clr, sizeof(clr) / sizeof(CGColorRef),
&kCFTypeArrayCallBacks);
CGGradientRef grad = CGGradientCreateWithColors(space, colors, locations);
CGColorSpaceRelease(space);
CGContextDrawLinearGradient(context, grad, rect.origin, CGPointMake(rect.origin.x +
rect.size.width, rect.origin.y + rect.size.height), 0);
CGGradientRelease(grad);
}
In Listing 7-13, we see the code that draws each bullet on the screen. This code draws a circle with
a gradient based on the damage of the Bullet . In my opinion, this is a lot of code to accomplish
something so simple. Let's break it down piece by piece. The first line of the task creates an oval
clipping region for the graphic context based on the provided rectangle. This means that future
drawing operations will be visible only with this oval region.
The next step is to create the gradient used by the drawing operation. A gradient is made of two
main parts. The first part is an array specifying where each color of the gradient should be applied.
In our case, we are only using a two-color gradient, so we create an array called locations, making
the first value 0 and second 1. This means that the colors should blend evenly from the start of the
gradient to the end.
The second part of a gradient is the colors used. In our example, we specify each color based on
value of damage. We simply assign the variables color1 and color2 to the colors we want to use to
draw the Bullet . Once we have our colors, we create a CFArrayRef to store our colors in.
Once we have the locations of the colors and the colors themselves stored in the correct type of
array, we call CGGradientCreateWithColors and pass the arrays in to create a CGGradientRef call
grad . Note we also have passed in a reference to the color space, as defined by CGColorSpaceRef
call ref . This is the default color space for the display and will only have to be changed if your
application is doing sophisticated color presentation.
Search WWH ::




Custom Search