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