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