Source/QuartzUtils.m
changeset 9 a59acc683080
parent 8 45c82a071aca
child 10 6c78cc6bd7a6
     1.1 --- a/Source/QuartzUtils.m	Wed May 28 12:47:10 2008 -0700
     1.2 +++ b/Source/QuartzUtils.m	Thu May 29 15:04:06 2008 -0700
     1.3 @@ -22,6 +22,7 @@
     1.4  */
     1.5  #import "QuartzUtils.h"
     1.6  #import <QuartzCore/QuartzCore.h>
     1.7 +#import "Piece.h"
     1.8  
     1.9  
    1.10  CGColorRef kBlackColor, kWhiteColor, 
    1.11 @@ -63,41 +64,6 @@
    1.12  #endif
    1.13  
    1.14  
    1.15 -void ChangeSuperlayer( CALayer *layer, CALayer *newSuperlayer, int index )
    1.16 -{
    1.17 -    // Disable actions, else the layer will move to the wrong place and then back!
    1.18 -    [CATransaction flush];
    1.19 -    [CATransaction begin];
    1.20 -    [CATransaction setValue:(id)kCFBooleanTrue
    1.21 -                     forKey:kCATransactionDisableActions];
    1.22 -
    1.23 -    CGPoint pos = layer.position;
    1.24 -    if( layer.superlayer )
    1.25 -        pos = [newSuperlayer convertPoint: pos fromLayer: layer.superlayer];
    1.26 -    [layer retain];
    1.27 -    [layer removeFromSuperlayer];
    1.28 -    layer.position = pos;
    1.29 -    if( index >= 0 )
    1.30 -        [newSuperlayer insertSublayer: layer atIndex: index];
    1.31 -    else
    1.32 -        [newSuperlayer addSublayer: layer];
    1.33 -    [layer release];
    1.34 -
    1.35 -    [CATransaction commit];
    1.36 -}
    1.37 -
    1.38 -
    1.39 -void RemoveImmediately( CALayer *layer )
    1.40 -{
    1.41 -    [CATransaction flush];
    1.42 -    [CATransaction begin];
    1.43 -    [CATransaction setValue:(id)kCFBooleanTrue
    1.44 -                     forKey:kCATransactionDisableActions];
    1.45 -    [layer removeFromSuperlayer];
    1.46 -    [CATransaction commit];
    1.47 -}    
    1.48 -
    1.49 -
    1.50  CGImageRef CreateCGImageFromFile( NSString *path )
    1.51  {
    1.52  #if TARGET_OS_IPHONE
    1.53 @@ -195,6 +161,47 @@
    1.54  #endif
    1.55  
    1.56  
    1.57 +CGImageRef CreateScaledImage( CGImageRef srcImage, CGFloat scale )
    1.58 +{
    1.59 +    int width = CGImageGetWidth(srcImage), height = CGImageGetHeight(srcImage);
    1.60 +    if( scale > 0 ) {
    1.61 +        if( scale >= 4.0 )
    1.62 +            scale /= MAX(width,height);             // interpret scale as target dimensions
    1.63 +        width = ceil( width * scale);
    1.64 +        height= ceil( height* scale);
    1.65 +    }
    1.66 +
    1.67 +    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    1.68 +    CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, 8, 4*width, space,
    1.69 +                                             kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast);
    1.70 +    CGColorSpaceRelease(space);
    1.71 +    CGContextSetInterpolationQuality(ctx,kCGInterpolationHigh);
    1.72 +    CGContextDrawImage(ctx, CGRectMake(0, 0, width, height), srcImage);
    1.73 +    CGImageRef dstImage = CGBitmapContextCreateImage(ctx);
    1.74 +    CGContextRelease(ctx);
    1.75 +    return dstImage;
    1.76 +}
    1.77 +
    1.78 +
    1.79 +CGImageRef GetScaledImageNamed( NSString *imageName, CGFloat scale )
    1.80 +{
    1.81 +    // For efficiency, loaded images are cached in a dictionary by name.
    1.82 +    static NSMutableDictionary *sMap;
    1.83 +    if( ! sMap )
    1.84 +        sMap = [[NSMutableDictionary alloc] init];
    1.85 +    
    1.86 +    NSArray *key = [NSArray arrayWithObjects: imageName, [NSNumber numberWithFloat: scale], nil];
    1.87 +    CGImageRef image = (CGImageRef) [sMap objectForKey: key];
    1.88 +    if( ! image ) {
    1.89 +        // Hasn't been cached yet, so load it:
    1.90 +        image = CreateScaledImage(GetCGImageNamed(imageName), scale);
    1.91 +        [sMap setObject: (id)image forKey: key];
    1.92 +        CGImageRelease(image);
    1.93 +    }
    1.94 +    return image;
    1.95 +}
    1.96 +
    1.97 +
    1.98  float GetPixelAlpha( CGImageRef image, CGSize imageSize, CGPoint pt )
    1.99  {
   1.100  #if TARGET_OS_IPHONE