Source/GGBUtils.m
author Jens Alfke <jens@mooseyard.com>
Mon Jul 14 21:46:09 2008 -0700 (2008-07-14)
changeset 17 ccc5ed68222d
parent 12 4e567e11f45f
child 21 2eb229411d73
permissions -rw-r--r--
Updated DemoBoardView and the xcode project to work with the latest sources.
     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 #import <AudioToolbox/AudioToolbox.h>
    23 
    24 
    25 #ifndef _MYUTILITIES_COLLECTIONUTILS_
    26 void setObj( id *variable, id newValue )
    27 {
    28     if( *variable != newValue ) {
    29         [*variable release];
    30         *variable = [newValue retain];
    31     }
    32 }
    33 
    34 void setObjCopy( id<NSCopying> *variable, id<NSCopying> newValue )
    35 {
    36     if( *variable != newValue ) {
    37         [*variable release];
    38         *variable = [(id)newValue copy];
    39     }
    40 }
    41 #endif
    42 
    43 
    44 void DelayFor( NSTimeInterval interval )
    45 {
    46     NSDate *end = [NSDate dateWithTimeIntervalSinceNow: interval];
    47     while( [end timeIntervalSinceNow] > 0 ) {
    48         if( ! [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: end] )
    49             break;
    50     }
    51 }    
    52 
    53 
    54 static SystemSoundID GetSound( NSString *name )
    55 {
    56     static NSMutableDictionary *sSoundIDs;
    57     NSNumber *soundIDObj = [sSoundIDs objectForKey: name];
    58     if( ! soundIDObj ) {
    59         NSLog(@"Loading sound '%@'",name);
    60         NSString *type = name.pathExtension;
    61         if( ! type.length )
    62             type = @"aiff";
    63         name = name.stringByDeletingPathExtension;
    64         
    65         NSString *path = [[NSBundle mainBundle] pathForResource: name
    66                                                          ofType: type];
    67 #if ! TARGET_OS_IPHONE
    68         if( ! path )
    69             path = [@"/System/Library/Sounds" stringByAppendingPathComponent: [name stringByAppendingPathExtension: type]];
    70 #endif
    71         NSURL *url;
    72         if( path )
    73             url = [NSURL fileURLWithPath: path];
    74         else {
    75             NSLog(@"WARNING: Couldn't find sound %@",name);
    76             return 0;
    77         }
    78         SystemSoundID soundID;
    79         if( AudioServicesCreateSystemSoundID((CFURLRef)url,&soundID) != noErr ) {
    80             NSLog(@"WARNING: Couldn't load sound %@",url);
    81             return 0;
    82         }
    83         
    84         soundIDObj = [NSNumber numberWithUnsignedInt: soundID];
    85         if( ! sSoundIDs )
    86             sSoundIDs = [[NSMutableDictionary alloc] init];
    87         [sSoundIDs setObject: soundIDObj forKey: name];
    88     }
    89     return [soundIDObj unsignedIntValue];
    90 }
    91 
    92 
    93 void PreloadSound( NSString* name )
    94 {
    95     (void) GetSound(name);
    96 }    
    97 
    98 
    99 void PlaySound( NSString* name )
   100 {
   101     AudioServicesPlaySystemSound( GetSound(name) );
   102 }
   103 
   104 void Beep()
   105 {
   106 #if TARGET_OS_IPHONE
   107     AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
   108 #else
   109     NSBeep();
   110 #endif
   111 }