Source/iPhoneAppDelegate.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 16 28392c9a969f
permissions -rwxr-xr-x
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 //
     2 //  iPhoneAppDelegate.m
     3 //  GGB-iPhone
     4 //
     5 //  Created by Jens Alfke on 3/7/08.
     6 //  Copyright __MyCompanyName__ 2008. All rights reserved.
     7 //
     8 
     9 #import "iPhoneAppDelegate.h"
    10 #import "BoardUIView.h"
    11 #import "Game.h"
    12 #import "QuartzUtils.h"
    13 #import "GGBUtils.h"
    14 
    15 
    16 // Temporary HACK to fix logging problem in beta 6 iPhone OS
    17 extern void _NSSetLogCStringFunction(void (*)(const char *string, unsigned length, BOOL withSyslogBanner));
    18 static void PrintNSLogMessage(const char *string, unsigned length, BOOL withSyslogBanner)
    19 {
    20 	puts(string);
    21 }
    22 static void HackNSLog(void) __attribute__((constructor));
    23 static void HackNSLog(void)
    24 {
    25 	_NSSetLogCStringFunction(PrintNSLogMessage);
    26 }
    27 
    28 
    29 @implementation GGB_iPhoneAppDelegate
    30 
    31 
    32 @synthesize window=_window;
    33 @synthesize contentView=_contentView;
    34 @synthesize headline=_headline;
    35 
    36 
    37 - (void)applicationDidFinishLaunching:(UIApplication *)application 
    38 {	
    39     // Create window
    40     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    41     _window.layer.backgroundColor = GetCGPatternNamed(@"Background.png");
    42     
    43     // Set up content view
    44     CGRect rHeadline,rContent;
    45     CGRectDivide([[UIScreen mainScreen] applicationFrame],
    46                  &rHeadline, &rContent, 35, CGRectMinYEdge);
    47     
    48     self.contentView = [[[BoardUIView alloc] initWithFrame: rContent] autorelease];
    49     [_window addSubview: _contentView];
    50     
    51     self.headline = [[[UILabel alloc] initWithFrame: rHeadline] autorelease];
    52     _headline.backgroundColor = nil;
    53     _headline.opaque = NO;
    54     _headline.textAlignment = UITextAlignmentCenter;
    55     _headline.font = [UIFont boldSystemFontOfSize: 20];
    56     _headline.minimumFontSize = 14;
    57     _headline.adjustsFontSizeToFitWidth = YES;
    58     [_window addSubview: _headline];
    59     
    60     // Start game:
    61     [self startGameNamed: @"CheckersGame"];
    62     
    63     // Show window
    64     [_window makeKeyAndVisible];
    65 }
    66 
    67 
    68 - (void)dealloc 
    69 {
    70     [_contentView release];
    71     [_headline release];
    72     [_window release];
    73     [super dealloc];
    74 }
    75 
    76 
    77 - (void) startGameNamed: (NSString*)gameClassName
    78 {
    79     Game *game = _contentView.game;
    80     [game removeObserver: self  forKeyPath: @"currentPlayer"];
    81     [game removeObserver: self forKeyPath: @"winner"];
    82     
    83     if( gameClassName == nil )
    84         gameClassName = [[game class] description];
    85     
    86     [_contentView startGameNamed: gameClassName];
    87     
    88     game = _contentView.game;
    89     [game addObserver: self 
    90            forKeyPath: @"currentPlayer"
    91               options: NSKeyValueObservingOptionInitial
    92               context: NULL];
    93     [game addObserver: self
    94            forKeyPath: @"winner"
    95               options: 0 
    96               context: NULL];
    97 }
    98 
    99 
   100 - (void)observeValueForKeyPath:(NSString *)keyPath 
   101                       ofObject:(id)object 
   102                         change:(NSDictionary *)change
   103                        context:(void *)context
   104 {
   105     Game *game = _contentView.game;
   106     if( object == game ) {
   107         Player *p = game.winner;
   108         NSString *msg;
   109         if( p ) {
   110             PlaySound(@"Sosumi");
   111             msg = @"%@ wins!";
   112         } else {
   113             p = game.currentPlayer;
   114             msg = @"Your turn, %@";
   115         }
   116         _headline.text = [NSString stringWithFormat: msg, p.name];
   117         
   118         if( game.winner ) {
   119             UIAlertView *alert;
   120             alert = [[UIAlertView alloc] initWithTitle: msg
   121                                                message: @"Congratulations!"
   122                                               delegate:self
   123                                      cancelButtonTitle:nil 
   124                                      otherButtonTitles:nil];
   125             [alert show];
   126             [alert release];
   127         }            
   128     }
   129 }
   130 
   131 
   132 - (void)alertView:(UIAlertView *)modalView didDismissWithButtonIndex:(NSInteger)buttonIndex;
   133 {
   134     // Start new game:
   135     [self startGameNamed: nil];
   136 }
   137 
   138 
   139 @end