cocoaTouch/SDLOverrides/SDL_uikitappdelegate.m
author koda
Sun, 21 Mar 2010 01:54:57 +0000
changeset 3027 32890edaa483
parent 3026 1a44c0f2b83b
child 3029 67483e87590c
permissions -rw-r--r--
lotsa optimizations and redraws for the iFrontend
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"
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
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
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
    38
int main (int argc, char *argv[]) {
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    39
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2723
eaa6ac1e95ea LANDSCAPE ON IPHONE
koda
parents: 2720
diff changeset
    40
	int retVal = UIApplicationMain(argc, argv, nil, @"SDLUIKitDelegate");
eaa6ac1e95ea LANDSCAPE ON IPHONE
koda
parents: 2720
diff changeset
    41
	[pool release];
eaa6ac1e95ea LANDSCAPE ON IPHONE
koda
parents: 2720
diff changeset
    42
	return retVal;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    43
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    44
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    45
@implementation SDLUIKitDelegate
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    46
@synthesize uiwindow, window, viewController, overlayController;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    47
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2772
diff changeset
    48
// convenience method
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    49
+(SDLUIKitDelegate *)sharedAppDelegate {
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2772
diff changeset
    50
	// 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
    51
	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    52
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    53
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    54
-(id) init {
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    55
	self = [super init];
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    56
	self.uiwindow = nil;
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    57
	self.window = NULL;
2738
bfccb2ec4334 new graphics from Tiy and frontend is loaded in landscape mode
koda
parents: 2723
diff changeset
    58
	self.viewController = nil;
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    59
    self.overlayController = nil;
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    60
	return self;
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    61
}
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    62
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    63
-(void) dealloc {
2738
bfccb2ec4334 new graphics from Tiy and frontend is loaded in landscape mode
koda
parents: 2723
diff changeset
    64
	[viewController release];
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    65
	[uiwindow release];
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    66
	[super dealloc];
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    67
}
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    68
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    69
#pragma mark -
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    70
#pragma mark Custom stuff
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
    71
-(IBAction) startSDLgame {
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
    72
	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
    73
	[setup startThread:@"engineProtocol"];
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    74
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    75
    [viewController disappear];
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    76
    [viewController.view removeFromSuperview];
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    77
    
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
    78
	const char **gameArgs = [setup getSettings];
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2743
diff changeset
    79
	[setup release];
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    80
    
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    81
    // overlay with controls, become visible after 2 seconds
3026
1a44c0f2b83b move interface files around to use standard names in different versions
koda
parents: 3025
diff changeset
    82
    overlayController = [[overlayViewController alloc] initWithNibName:@"overlayViewController" bundle:nil];
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    83
    [uiwindow addSubview:overlayController.view];
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    84
    [NSTimer scheduledTimerWithTimeInterval:2 target:overlayController selector:@selector(showMenuAfterwards) userInfo:nil repeats:NO];
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    85
    
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    86
	Game(gameArgs); // this is the pascal fuction that starts the game
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    87
    
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    88
    // let's clean memory
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
    89
	free(gameArgs);
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    90
    [overlayController.view removeFromSuperview];
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    91
    [overlayController release];
da6023c2745b restore compilation on simulator and device
koda
parents: 2803
diff changeset
    92
    
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    93
	[uiwindow addSubview:viewController.view];
2740
03df0573a9fd programmatically load main controller
koda
parents: 2738
diff changeset
    94
	[uiwindow makeKeyAndVisible];
03df0573a9fd programmatically load main controller
koda
parents: 2738
diff changeset
    95
	
3027
32890edaa483 lotsa optimizations and redraws for the iFrontend
koda
parents: 3026
diff changeset
    96
    [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
    97
}
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
    98
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
    99
-(NSString *)dataFilePath: (NSString *)fileName {
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   100
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   101
	NSString *documentsDirectory = [paths objectAtIndex:0];
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   102
	return [documentsDirectory stringByAppendingPathComponent:fileName];
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   103
}
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   104
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   105
#pragma mark -
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   106
#pragma mark SDLUIKitDelegate methods
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   107
// 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
   108
-(void) applicationDidFinishLaunching:(UIApplication *)application {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   109
	[application setStatusBarHidden:YES animated:NO];
2738
bfccb2ec4334 new graphics from Tiy and frontend is loaded in landscape mode
koda
parents: 2723
diff changeset
   110
	[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];  
2740
03df0573a9fd programmatically load main controller
koda
parents: 2738
diff changeset
   111
		
03df0573a9fd programmatically load main controller
koda
parents: 2738
diff changeset
   112
	self.uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
03df0573a9fd programmatically load main controller
koda
parents: 2738
diff changeset
   113
	self.uiwindow.backgroundColor = [UIColor blackColor];
2738
bfccb2ec4334 new graphics from Tiy and frontend is loaded in landscape mode
koda
parents: 2723
diff changeset
   114
	
3026
1a44c0f2b83b move interface files around to use standard names in different versions
koda
parents: 3025
diff changeset
   115
	self.viewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController" bundle:nil];
2720
a5111ec4d25f adds a "factory reset" to iphone frontend
koda
parents: 2714
diff changeset
   116
	
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2772
diff changeset
   117
	// Set working directory to resource path
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   118
	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
   119
2738
bfccb2ec4334 new graphics from Tiy and frontend is loaded in landscape mode
koda
parents: 2723
diff changeset
   120
	[uiwindow addSubview:viewController.view];
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   121
	[uiwindow makeKeyAndVisible];
2740
03df0573a9fd programmatically load main controller
koda
parents: 2738
diff changeset
   122
	[uiwindow layoutSubviews];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   123
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   124
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   125
-(void) applicationWillTerminate:(UIApplication *)application {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   126
	SDL_SendQuit();
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2772
diff changeset
   127
	// hack to prevent automatic termination.  See SDL_uikitevents.m for details
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   128
	// have to remove this otherwise game goes on when pushing the home button
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   129
	//longjmp(*(jump_env()), 1);
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
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   132
-(void) applicationWillResignActive:(UIApplication*)application {
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2772
diff changeset
   133
	//NSLog(@"%@", NSStringFromSelector(_cmd));
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   134
	SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   135
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   136
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   137
-(void) applicationDidBecomeActive:(UIApplication*)application {
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2772
diff changeset
   138
	//NSLog(@"%@", NSStringFromSelector(_cmd));
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2702
diff changeset
   139
	SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_RESTORED, 0, 0);
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   140
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   141
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   142
@end