cocoaTouch/SDLOverrides/SDL_uikitappdelegate.m
author koda
Sun, 10 Jan 2010 00:52:20 +0000
changeset 2691 c0da3a98c01c
parent 2689 dfda97c153a4
child 2692 ce9992075118
permissions -rw-r--r--
initial support for engine protocol
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"
2691
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    29
#import "gameSetup.h"
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    30
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    31
#ifdef main
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    32
#undef main
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    33
#endif
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    34
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    35
extern int SDL_main(int argc, char *argv[]);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    36
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    37
int main (int argc, char **argv) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    38
	int i;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    39
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    40
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    41
	/* store arguments */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    42
	forward_argc = argc;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    43
	forward_argv = (char **)malloc(argc * sizeof(char *));
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    44
	for (i = 0; i < argc; i++) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    45
		forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    46
		strcpy(forward_argv[i], argv[i]);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    47
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    48
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    49
	/* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    50
	UIApplicationMain(argc, argv, NULL, @"SDLUIKitDelegate");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    51
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    52
	[pool release];
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
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    56
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    57
@synthesize window, windowID, controller;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    58
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    59
/* convenience method */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    60
+(SDLUIKitDelegate *)sharedAppDelegate {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    61
	/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    62
	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    63
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    64
2691
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    65
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    66
- (void) startSDLgame {
2691
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    67
	pthread_t threadID;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    68
2691
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    69
	pthread_create (&threadID, NULL, (void *) (*engineProtocolThread), NULL);
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    70
	pthread_detach (threadID);
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    71
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    72
	setupArgsForLocalPlay();
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    73
	
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    74
	/* run the user's application, passing argc and argv */
2691
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    75
	NSLog(@"Game is launching...");
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    76
	SDL_main(forward_argc, forward_argv);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    77
	// can't reach here yet
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    78
	NSLog(@"Game exited");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    79
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    80
	//[self performSelector:@selector(makeNewView) withObject:nil afterDelay:0.0];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    81
	/* exit, passing the return status from the user's application */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    82
	//exit(exit_status);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    83
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    84
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    85
// 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
    86
-(void) applicationDidFinishLaunching:(UIApplication *)application {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    87
	[application setStatusBarHidden:YES animated:NO];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    88
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    89
	/* Set working directory to resource path */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    90
	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    91
//#import "SoundEffect.h"	
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    92
//	SoundEffect *erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]];
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    93
//	SoundEffect *selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    94
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    95
	[window addSubview:controller.view];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    96
	[window makeKeyAndVisible];
2691
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    97
	
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    98
	// REMOVE ME when you're done with reverse engineering the protocol
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    99
	[self performSelector:@selector(startSDLgame)];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   100
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   101
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   102
-(void) applicationWillTerminate:(UIApplication *)application {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   103
	/* free the memory we used to hold copies of argc and argv */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   104
	int i;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   105
	for (i=0; i < forward_argc; i++) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   106
		free(forward_argv[i]);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   107
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   108
	free(forward_argv);	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   109
	SDL_SendQuit();
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   110
	 /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   111
	// 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
   112
	//longjmp(*(jump_env()), 1);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   113
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   114
	NSLog(@"Closing App...");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   115
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   116
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   117
-(void) applicationWillResignActive:(UIApplication*)application
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   118
{
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   119
//	NSLog(@"%@", NSStringFromSelector(_cmd));
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   120
	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   121
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   122
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   123
-(void) applicationDidBecomeActive:(UIApplication*)application
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
//	NSLog(@"%@", NSStringFromSelector(_cmd));
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   126
	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   127
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   128
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   129
/*
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   130
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   131
	NSLog(@"Rotating...");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   132
	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   133
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   134
*/
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
-(void) dealloc {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   137
	[controller release];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   138
	[window release];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   139
	[super dealloc];
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