3 * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
5 * Copyright (C) 2001 Chuck Mason <cemason@users.sourceforge.net>
7 * Copyright (C) 2002 Florian Schulze <crow@icculus.org>
9 * This file is part of Jump'n'Bump.
11 * Jump'n'Bump is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * Jump'n'Bump is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SDLMain.m - main entry point for our Cocoa-ized SDL app
28 These files turn an SDL app into a double-clickable Cocoa app on mac os X.
30 Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
31 Non-NIB-Code & other changes: Max Horn <max@quendi.de>
33 Feel free to customize this file to suit your needs
39 #import <Cocoa/Cocoa.h>
41 @interface SDLMain : NSObject
45 #import <sys/param.h> /* for MAXPATHLEN */
51 static BOOL gFinderLaunch;
53 /* A helper category for NSString */
54 @interface NSString (ReplaceSubString)
55 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
58 @interface SDLApplication : NSApplication
61 @implementation SDLApplication
62 /* Invoked from the Quit menu item */
63 - (void)terminate:(id)sender
65 /* Post a SDL_QUIT event */
67 event.type = SDL_QUIT;
68 SDL_PushEvent(&event);
73 /* The main class of the application, the application's delegate */
74 @implementation SDLMain
75 - (void)quit:(id)sender
77 /* Post a SDL_QUIT event */
79 event.type = SDL_QUIT;
80 SDL_PushEvent(&event);
82 // uMenu::quickexit=true;
84 //this changes the current working directory to the resource folder of
85 //the .app bundle in Mac OS X
86 void MacOSX_SetCWD(char **argv) {
89 strcpy(buffer, argv[0]);
91 if ( !strstr( buffer, ".app") ) {
92 //it's not a .app bundle
96 for (i = strlen(buffer); i > 0 && count > 0; i--) {
104 strcat( buffer, "Resources");
110 /* Fix menu to contain the real app name instead of "SDL App" */
111 - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
114 NSEnumerator *enumerator;
115 NSMenuItem *menuItem;
117 aRange = [[aMenu title] rangeOfString:@"SDL App"];
118 if (aRange.length != 0)
119 [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
121 enumerator = [[aMenu itemArray] objectEnumerator];
122 while ((menuItem = [enumerator nextObject]))
124 aRange = [[menuItem title] rangeOfString:@"SDL App"];
125 if (aRange.length != 0)
126 [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
127 if ([menuItem hasSubmenu])
128 [self fixMenu:[menuItem submenu] withAppName:appName];
133 /* Called when the internal event loop has just started running */
134 - (void) applicationDidFinishLaunching: (NSNotification *) note
138 /* Set the main menu to contain the real app name instead of "SDL App" */
139 [self fixMenu:[NSApp mainMenu] withAppName:[[NSProcessInfo processInfo] processName]];
141 /* Hand off to main application code */
142 status = SDL_main (gArgc, gArgv);
144 /* We're done, thank you for playing */
150 @implementation NSString (ReplaceSubString)
152 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
154 unsigned int bufferSize;
155 unsigned int selfLen = [self length];
156 unsigned int aStringLen = [aString length];
161 bufferSize = selfLen + aStringLen - aRange.length;
162 buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar));
164 /* Get first part into buffer */
165 localRange.location = 0;
166 localRange.length = aRange.location;
167 [self getCharacters:buffer range:localRange];
169 /* Get middle part into buffer */
170 localRange.location = 0;
171 localRange.length = aStringLen;
172 [aString getCharacters:(buffer+aRange.location) range:localRange];
174 /* Get last part into buffer */
175 localRange.location = aRange.location + aRange.length;
176 localRange.length = selfLen - localRange.location;
177 [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
179 /* Build output string */
180 result = [NSString stringWithCharacters:buffer length:bufferSize];
182 NSDeallocateMemoryPages(buffer, bufferSize);
196 /* Main entry point to executable - should *not* be SDL_main! */
197 int main (int argc, char **argv)
200 /* Copy the arguments into a global variable */
203 /* This is passed if we are launched by double-clicking */
204 if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
211 gArgv = (char**) malloc (sizeof(*gArgv) * (gArgc+1));
212 assert (gArgv != NULL);
213 for (i = 0; i < gArgc; i++)
217 MacOSX_SetCWD(gArgv);
219 [SDLApplication poseAsClass:[NSApplication class]];
220 NSApplicationMain (argc, argv);