cocoaTouch/SDLOverrides/SDL_uikitappdelegate.m
author koda
Sun, 11 Apr 2010 03:43:13 +0000
changeset 3335 2520ee7a5484
parent 3318 591c42242106
child 3361 cfc6cd502f85
permissions -rw-r--r--
new manager for handling (future) rotations added a macro for the settings file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     1
/*
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     2
 SDL - Simple DirectMedia Layer
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     3
 Copyright (C) 1997-2009 Sam Lantinga
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     4
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     5
 This library is free software; you can redistribute it and/or
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     6
 modify it under the terms of the GNU Lesser General Public
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     7
 License as published by the Free Software Foundation; either
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     8
 version 2.1 of the License, or (at your option) any later version.
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     9
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    10
 This library is distributed in the hope that it will be useful,
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    13
 Lesser General Public License for more details.
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    14
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    15
 You should have received a copy of the GNU Lesser General Public
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    16
 License along with this library; if not, write to the Free Software
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    17
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    18
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    19
 Sam Lantinga, mods for Hedgewars by Vittorio Giovara
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    20
 slouken@libsdl.org, vittorio.giovara@gmail.com
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    21
*/
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    22
2691
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    23
#import <pthread.h>
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    24
#import "SDL_uikitappdelegate.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    25
#import "SDL_uikitopenglview.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    26
#import "SDL_events_c.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    27
#import "jumphack.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    28
#import "SDL_video.h"
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents: 2692
diff changeset
    29
#import "GameSetup.h"
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    30
#import "PascalImports.h"
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2743
diff changeset
    31
#import "MainMenuViewController.h"
3116
97dc65a47b15 branch ipad/iphone files to keep compatibility between versions
koda
parents: 3063
diff changeset
    32
#import "OverlayViewController.h"
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    33
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    34
#ifdef main
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    35
#undef main
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    36
#endif
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    37
3316
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    38
#define VALGRIND "/opt/valgrind/bin/valgrind"
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    39
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2701
diff changeset
    40
int main (int argc, char *argv[]) {
3316
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    41
#ifdef VALGRIND_REXEC
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    42
    // Using the valgrind build config, rexec ourself in valgrind
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    43
    // from http://landonf.bikemonkey.org/code/iphone/iPhone_Simulator_Valgrind.20081224.html
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    44
    if (argc < 2 || (argc >= 2 && strcmp(argv[1], "-valgrind") != 0)) {
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    45
        execl(VALGRIND, VALGRIND, "--leak-check=full", "--show-reachable=yes", argv[0], "-valgrind", NULL);
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    46
    }
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    47
#endif
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    48
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    49
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2723
eaa6ac1e95ea LANDSCAPE ON IPHONE
koda
parents: 2720
diff changeset
    50
	int retVal = UIApplicationMain(argc, argv, nil, @"SDLUIKitDelegate");
eaa6ac1e95ea LANDSCAPE ON IPHONE
koda
parents: 2720
diff changeset
    51
	[pool release];
eaa6ac1e95ea LANDSCAPE ON IPHONE
koda
parents: 2720
diff changeset
    52
	return retVal;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    53
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    54
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    55
@implementation SDLUIKitDelegate
3316
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    56
@synthesize uiwindow, window, viewController;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    57
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2772
diff changeset
    58
// convenience method
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    59
+(SDLUIKitDelegate *)sharedAppDelegate {
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2772
diff changeset
    60
	// the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    61
	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    62
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    63
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    64
-(id) init {
3122
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3116
diff changeset
    65
	if (self = [super init]){
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3116
diff changeset
    66
        self.uiwindow = nil;
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3116
diff changeset
    67
        self.window = NULL;
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3116
diff changeset
    68
        self.viewController = nil;
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3116
diff changeset
    69
        isInGame = NO;
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3116
diff changeset
    70
        return self;
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3116
diff changeset
    71
    } else 
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3116
diff changeset
    72
        return nil;
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    73
}
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    74
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    75
-(void) dealloc {
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3318
diff changeset
    76
    [viewController release];
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    77
	[uiwindow release];
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    78
	[super dealloc];
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    79
}
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    80
3029
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3027
diff changeset
    81
// main routine for calling the actual game engine
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents: 2692
diff changeset
    82
-(IBAction) startSDLgame {
3029
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3027
diff changeset
    83
    [viewController disappear];
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3027
diff changeset
    84
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3027
diff changeset
    85
    // pull out useful configuration info from various files
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2701
diff changeset
    86
	GameSetup *setup = [[GameSetup alloc] init];
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents: 2692
diff changeset
    87
	[setup startThread:@"engineProtocol"];
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2701
diff changeset
    88
	const char **gameArgs = [setup getSettings];
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2743
diff changeset
    89
	[setup release];
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    90
    
3316
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3305
diff changeset
    91
    OverlayViewController *overlayController;
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    92
    // overlay with controls, become visible after 2 seconds
3318
591c42242106 much better
koda
parents: 3317
diff changeset
    93
    overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
3317
198ec44b6d92 minor tweaks, icon for ipad, merged overlayviewcontroller, pop viewcontroller when selected a hat
koda
parents: 3316
diff changeset
    94
    
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    95
    [uiwindow addSubview:overlayController.view];
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
    96
    [overlayController release];
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
    97
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
    98
    isInGame = YES;
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    99
	Game(gameArgs); // this is the pascal fuction that starts the game
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   100
    isInGame = NO;
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
   101
    
3029
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3027
diff changeset
   102
    free(gameArgs);
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
   103
    [overlayController.view removeFromSuperview];
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
   104
    
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
   105
    [viewController appear];
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2701
diff changeset
   106
}
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2701
diff changeset
   107
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   108
// override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   109
-(void) applicationDidFinishLaunching:(UIApplication *)application {
3116
97dc65a47b15 branch ipad/iphone files to keep compatibility between versions
koda
parents: 3063
diff changeset
   110
	//[application setStatusBarHidden:YES animated:NO];
3199
1a9938a27677 fixes to the mac build system
koda
parents: 3122
diff changeset
   111
    //[application setStatusBarHidden:YES withAnimation:NO];
3270
9bd8fb1707b9 initial support for hat selection on the ifrontend
koda
parents: 3250
diff changeset
   112
    [application setStatusBarHidden:YES];
3116
97dc65a47b15 branch ipad/iphone files to keep compatibility between versions
koda
parents: 3063
diff changeset
   113
    [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];  
2740
03df0573a9fd programmatically load main controller
koda
parents: 2738
diff changeset
   114
		
03df0573a9fd programmatically load main controller
koda
parents: 2738
diff changeset
   115
	self.uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
03df0573a9fd programmatically load main controller
koda
parents: 2738
diff changeset
   116
	self.uiwindow.backgroundColor = [UIColor blackColor];
2738
bfccb2ec4334 new graphics from Tiy and frontend is loaded in landscape mode
koda
parents: 2723
diff changeset
   117
	
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents: 3270
diff changeset
   118
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
91074496d5c9 merged code from ipad and iphone frontends
koda
parents: 3270
diff changeset
   119
        self.viewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents: 3270
diff changeset
   120
    else
91074496d5c9 merged code from ipad and iphone frontends
koda
parents: 3270
diff changeset
   121
        self.viewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil];
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   122
	[uiwindow addSubview:viewController.view];
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   123
    [viewController release];
2720
a5111ec4d25f adds a "factory reset" to iphone frontend
koda
parents: 2714
diff changeset
   124
	
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2772
diff changeset
   125
	// Set working directory to resource path
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   126
	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
   127
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   128
	[uiwindow makeKeyAndVisible];
2740
03df0573a9fd programmatically load main controller
koda
parents: 2738
diff changeset
   129
	[uiwindow layoutSubviews];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   130
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   131
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   132
-(void) applicationWillTerminate:(UIApplication *)application {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   133
	SDL_SendQuit();
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   134
    if (isInGame) {
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   135
        HW_terminate(YES);
3250
d5cd1a617123 intial support for team configuration on the ifrontend
koda
parents: 3199
diff changeset
   136
        // hack to prevent automatic termination. See SDL_uikitevents.m for details
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   137
        longjmp(*(jump_env()), 1);
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   138
    }
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   139
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   140
3029
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3027
diff changeset
   141
-(void) applicationWillResignActive:(UIApplication *)application {
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3318
diff changeset
   142
	//NSLog(@"%@", NSStringFromSelector(_cmd));
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   143
    if (isInGame) HW_pause();
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   144
	//SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   145
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   146
3029
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3027
diff changeset
   147
-(void) applicationDidBecomeActive:(UIApplication *)application {
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3318
diff changeset
   148
	//NSLog(@"%@", NSStringFromSelector(_cmd));
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   149
    if (isInGame) HW_pause();
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3029
diff changeset
   150
	//SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_RESTORED, 0, 0);
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   151
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   152
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   153
@end