Source/GGBUtils.m
author Jens Alfke <jens@mooseyard.com>
Thu May 29 15:04:06 2008 -0700 (2008-05-29)
changeset 9 a59acc683080
parent 8 45c82a071aca
child 10 6c78cc6bd7a6
permissions -rw-r--r--
Finally fixed the slow animation performance of board games; all it took was changing the board's z index from 1 to 0, somehow. Games working well now.
     1 /*  Copyright © 2008 Jens Alfke. All Rights Reserved.
     2 
     3     Redistribution and use in source and binary forms, with or without modification, are permitted
     4     provided that the following conditions are met:
     5 
     6     * Redistributions of source code must retain the above copyright notice, this list of conditions
     7       and the following disclaimer.
     8     * Redistributions in binary form must reproduce the above copyright notice, this list of
     9       conditions and the following disclaimer in the documentation and/or other materials provided
    10       with the distribution.
    11 
    12     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    13     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    14     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    15     BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    16     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    17     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    18     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    19     THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    20 */
    21 #import "GGBUtils.h"
    22 
    23 #if TARGET_OS_IPHONE
    24 #import <AudioToolbox/AudioToolbox.h>
    25 #endif
    26 
    27 
    28 void setObj( id *variable, id newValue )
    29 {
    30     if( *variable != newValue ) {
    31         [*variable release];
    32         *variable = [newValue retain];
    33     }
    34 }
    35 
    36 void setObjCopy( id<NSCopying> *variable, id<NSCopying> newValue )
    37 {
    38     if( *variable != newValue ) {
    39         [*variable release];
    40         *variable = [(id)newValue copy];
    41     }
    42 }
    43 
    44 
    45 #if TARGET_OS_IPHONE
    46 static SystemSoundID GetSound( NSString *name )
    47 {
    48     static NSMutableDictionary *sSoundIDs;
    49     NSNumber *soundIDObj = [sSoundIDs objectForKey: name];
    50     if( ! soundIDObj ) {
    51         NSLog(@"Loading sound '%@'",name);
    52         NSString *type = name.pathExtension;
    53         if( ! type.length )
    54             type = @"aiff";
    55         NSString *path = [[NSBundle mainBundle] pathForResource: name.stringByDeletingPathExtension
    56                                                          ofType: type];
    57         NSURL *url;
    58         if( path )
    59             url = [NSURL fileURLWithPath: path];
    60         else {
    61             NSLog(@"Couldn't find sound %@",name);
    62             return 0;
    63         }
    64         //url = [NSURL fileURLWithPath: [@"/Library/Sounds/" stringByAppendingPathComponent: name]];
    65         SystemSoundID soundID;
    66         if( AudioServicesCreateSystemSoundID((CFURLRef)url,&soundID) != noErr ) {
    67             NSLog(@"Couldn't load sound %@",url);
    68             return 0;
    69         }
    70         
    71         soundIDObj = [NSNumber numberWithUnsignedInt: soundID];
    72         if( ! sSoundIDs )
    73             sSoundIDs = [[NSMutableDictionary alloc] init];
    74         [sSoundIDs setObject: soundIDObj forKey: name];
    75     }
    76     return [soundIDObj unsignedIntValue];
    77 }
    78 #endif
    79 
    80 
    81 void PreloadSound( NSString* name )
    82 {
    83 #if TARGET_OS_IPHONE
    84     GetSound(name);
    85 #else
    86     NSSound *sound = [[NSSound soundNamed: @"Pop"] copy];
    87     sound.volume = 0;
    88     [sound play];
    89     [sound release];
    90 #endif
    91 }    
    92 
    93 
    94 void PlaySound( NSString* name )
    95 {
    96 #if TARGET_OS_IPHONE
    97     AudioServicesPlaySystemSound( GetSound(name) );
    98 #else
    99     [[NSSound soundNamed: name] play];
   100 #endif
   101 }
   102 
   103 void Beep()
   104 {
   105 #if TARGET_OS_IPHONE
   106     AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
   107 #else
   108     NSBeep();
   109 #endif
   110 }