Source/GGBUtils.m
author Jens Alfke <jens@mooseyard.com>
Wed May 28 12:47:10 2008 -0700 (2008-05-28)
changeset 8 45c82a071aca
parent 7 428a194e3e59
child 9 a59acc683080
permissions -rw-r--r--
* Got it working with latest iPhone SDK.
* Fixed some text alignment issues that showed up on PlayingCards.
* Working on persistence and move-tracking for Game.
     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 void PreloadSound( NSString* name )
    46 {
    47 #if ! TARGET_OS_IPHONE
    48     NSSound *sound = [[NSSound soundNamed: @"Pop"] copy];
    49     sound.volume = 0;
    50     [sound play];
    51     [sound release];
    52 #endif
    53 }    
    54 
    55 
    56 void PlaySound( NSString* name )
    57 {
    58 #if TARGET_OS_IPHONE
    59     NSURL *url = [NSURL fileURLWithPath: [@"/Library/Sounds/" stringByAppendingPathComponent: name]];
    60     SystemSoundID soundID;
    61     if( AudioServicesCreateSystemSoundID((CFURLRef)url,&soundID) != noErr ) {
    62         NSLog(@"Couldn't load sound %@",url);
    63         return;
    64     }
    65     AudioServicesPlaySystemSound(soundID);
    66 #else
    67     [[NSSound soundNamed: name] play];
    68 #endif
    69 }
    70 
    71 void Beep()
    72 {
    73 #if TARGET_OS_IPHONE
    74     AudioServicesPlayAlertSound(0x00001000/*kSystemSoundID_UserPreferredAlert*/);
    75 #else
    76     NSBeep();
    77 #endif
    78 }