cocoaTouch/SDL_uikitappdelegate.m
author koda
Thu, 07 Jan 2010 05:23:23 +0000
changeset 2678 334016e8d895
child 2685 0ba746be5d59
permissions -rw-r--r--
injection of custom code in SDL for iPhone in order to implement our frontend initial support for (real) multitouch events
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2678
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
     1
/*
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
     2
 SDL - Simple DirectMedia Layer
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
     3
 Copyright (C) 1997-2009 Sam Lantinga
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
     4
 
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
     5
 This library is free software; you can redistribute it and/or
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
     6
 modify it under the terms of the GNU Lesser General Public
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
     7
 License as published by the Free Software Foundation; either
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
     8
 version 2.1 of the License, or (at your option) any later version.
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
     9
 
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    10
 This library is distributed in the hope that it will be useful,
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    13
 Lesser General Public License for more details.
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    14
 
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    15
 You should have received a copy of the GNU Lesser General Public
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    16
 License along with this library; if not, write to the Free Software
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    17
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    18
 
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    19
 Sam Lantinga, mods for Hedgewars by Vittorio Giovara
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    20
 slouken@libsdl.org, vittorio.giovara@gmail.com
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    21
*/
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    22
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    23
#import "SDL_uikitappdelegate.h"
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    24
#import "SDL_uikitopenglview.h"
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    25
#import "SDL_events_c.h"
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    26
#import "jumphack.h"
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    27
#import "SDL_video.h"
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    28
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    29
#ifdef main
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    30
#undef main
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    31
#endif
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    32
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    33
extern int SDL_main(int argc, char *argv[]);
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    34
static int forward_argc;
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    35
static char **forward_argv;
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    36
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    37
int main(int argc, char **argv) {
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    38
	int i;
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    39
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    40
	
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    41
	/* store arguments */
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    42
	forward_argc = argc;
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    43
	forward_argv = (char **)malloc(argc * sizeof(char *));
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    44
	for (i=0; i<argc; i++) {
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    45
		forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    46
		strcpy(forward_argv[i], argv[i]);
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    47
	}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    48
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    49
	/* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    50
	UIApplicationMain(argc, argv, NULL, @"SDLUIKitDelegate");
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    51
	
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    52
	[pool release];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    53
}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    54
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    55
@implementation SDLUIKitDelegate
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    56
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    57
@synthesize window, windowID, controller;
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    58
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    59
/* convenience method */
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    60
+(SDLUIKitDelegate *)sharedAppDelegate {
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    61
	/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    62
	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    63
}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    64
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    65
/*- (id)init {
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    66
	self = [super init];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    67
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    68
	windowID = 0;
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    69
	return self;
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    70
}*/
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    71
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    72
- (void) startSDLgame {
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    73
	// HACK: remove the current window and let SDL create a new one
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    74
	[self.window release];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    75
	if (nil != self.window)
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    76
		self.window = nil;
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    77
	
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    78
	/* run the user's application, passing argc and argv */
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    79
	NSLog(@"Game is launching");
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    80
	SDL_main(forward_argc, forward_argv);
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    81
	NSLog(@"Game exited");
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    82
	
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    83
	//[self performSelector:@selector(makeNewView) withObject:nil afterDelay:0.0];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    84
	/* exit, passing the return status from the user's application */
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    85
	//exit(exit_status);
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    86
}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    87
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    88
// override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    89
-(void) applicationDidFinishLaunching:(UIApplication *)application {
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    90
	[application setStatusBarHidden:YES animated:NO];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    91
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    92
	/* Set working directory to resource path */
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    93
	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    94
	
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    95
	[window addSubview:controller.view];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    96
	[window makeKeyAndVisible];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    97
}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    98
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
    99
-(void) applicationWillTerminate:(UIApplication *)application {
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   100
	/* free the memory we used to hold copies of argc and argv */
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   101
	int i;
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   102
	for (i=0; i<forward_argc; i++) {
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   103
		free(forward_argv[i]);
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   104
	}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   105
	free(forward_argv);	
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   106
	SDL_SendQuit();
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   107
	 /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   108
	longjmp(*(jump_env()), 1);
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   109
}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   110
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   111
-(void) applicationWillResignActive:(UIApplication*)application
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   112
{
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   113
//	NSLog(@"%@", NSStringFromSelector(_cmd));
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   114
	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   115
}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   116
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   117
-(void) applicationDidBecomeActive:(UIApplication*)application
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   118
{
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   119
//	NSLog(@"%@", NSStringFromSelector(_cmd));
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   120
	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   121
}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   122
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   123
/*
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   124
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   125
	NSLog(@"Rotating...");
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   126
	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   127
}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   128
*/
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   129
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   130
-(void) dealloc {
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   131
	[controller release];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   132
	[window release];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   133
	[super dealloc];
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   134
}
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   135
334016e8d895 injection of custom code in SDL for iPhone in order to implement our frontend
koda
parents:
diff changeset
   136
@end