diff -r 45c82a071aca -r a59acc683080 Source/QuartzUtils.m --- a/Source/QuartzUtils.m Wed May 28 12:47:10 2008 -0700 +++ b/Source/QuartzUtils.m Thu May 29 15:04:06 2008 -0700 @@ -22,6 +22,7 @@ */ #import "QuartzUtils.h" #import +#import "Piece.h" CGColorRef kBlackColor, kWhiteColor, @@ -63,41 +64,6 @@ #endif -void ChangeSuperlayer( CALayer *layer, CALayer *newSuperlayer, int index ) -{ - // Disable actions, else the layer will move to the wrong place and then back! - [CATransaction flush]; - [CATransaction begin]; - [CATransaction setValue:(id)kCFBooleanTrue - forKey:kCATransactionDisableActions]; - - CGPoint pos = layer.position; - if( layer.superlayer ) - pos = [newSuperlayer convertPoint: pos fromLayer: layer.superlayer]; - [layer retain]; - [layer removeFromSuperlayer]; - layer.position = pos; - if( index >= 0 ) - [newSuperlayer insertSublayer: layer atIndex: index]; - else - [newSuperlayer addSublayer: layer]; - [layer release]; - - [CATransaction commit]; -} - - -void RemoveImmediately( CALayer *layer ) -{ - [CATransaction flush]; - [CATransaction begin]; - [CATransaction setValue:(id)kCFBooleanTrue - forKey:kCATransactionDisableActions]; - [layer removeFromSuperlayer]; - [CATransaction commit]; -} - - CGImageRef CreateCGImageFromFile( NSString *path ) { #if TARGET_OS_IPHONE @@ -195,6 +161,47 @@ #endif +CGImageRef CreateScaledImage( CGImageRef srcImage, CGFloat scale ) +{ + int width = CGImageGetWidth(srcImage), height = CGImageGetHeight(srcImage); + if( scale > 0 ) { + if( scale >= 4.0 ) + scale /= MAX(width,height); // interpret scale as target dimensions + width = ceil( width * scale); + height= ceil( height* scale); + } + + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, 8, 4*width, space, + kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast); + CGColorSpaceRelease(space); + CGContextSetInterpolationQuality(ctx,kCGInterpolationHigh); + CGContextDrawImage(ctx, CGRectMake(0, 0, width, height), srcImage); + CGImageRef dstImage = CGBitmapContextCreateImage(ctx); + CGContextRelease(ctx); + return dstImage; +} + + +CGImageRef GetScaledImageNamed( NSString *imageName, CGFloat scale ) +{ + // For efficiency, loaded images are cached in a dictionary by name. + static NSMutableDictionary *sMap; + if( ! sMap ) + sMap = [[NSMutableDictionary alloc] init]; + + NSArray *key = [NSArray arrayWithObjects: imageName, [NSNumber numberWithFloat: scale], nil]; + CGImageRef image = (CGImageRef) [sMap objectForKey: key]; + if( ! image ) { + // Hasn't been cached yet, so load it: + image = CreateScaledImage(GetCGImageNamed(imageName), scale); + [sMap setObject: (id)image forKey: key]; + CGImageRelease(image); + } + return image; +} + + float GetPixelAlpha( CGImageRef image, CGSize imageSize, CGPoint pt ) { #if TARGET_OS_IPHONE