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 * Portions of this code are from the MPEG software simulation group
10 * idct implementation. This code will be replaced with a new
11 * implementation soon.
13 * This file is part of Jump'n'Bump.
15 * Jump'n'Bump is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * Jump'n'Bump is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 /* SDLMain.m - main entry point for our Cocoa-ized SDL app
32 These files turn an SDL app into a double-clickable Cocoa app on mac os X.
34 Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
35 Non-NIB-Code & other changes: Max Horn <max@quendi.de>
37 Feel free to customize this file to suit your needs
43 #import <Cocoa/Cocoa.h>
45 @interface SDLMain : NSObject
49 #import <sys/param.h> /* for MAXPATHLEN */
55 static BOOL gFinderLaunch;
57 /* A helper category for NSString */
58 @interface NSString (ReplaceSubString)
59 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
62 @interface SDLApplication : NSApplication
65 @implementation SDLApplication
66 /* Invoked from the Quit menu item */
67 - (void)terminate:(id)sender
69 /* Post a SDL_QUIT event */
71 event.type = SDL_QUIT;
72 SDL_PushEvent(&event);
77 /* The main class of the application, the application's delegate */
78 @implementation SDLMain
79 - (void)quit:(id)sender
81 /* Post a SDL_QUIT event */
83 event.type = SDL_QUIT;
84 SDL_PushEvent(&event);
86 // uMenu::quickexit=true;
88 //this changes the current working directory to the resource folder of
89 //the .app bundle in Mac OS X
90 void MacOSX_SetCWD(char **argv) {
93 strcpy(buffer, argv[0]);
95 if ( !strstr( buffer, ".app") ) {
96 //it's not a .app bundle
100 for (i = strlen(buffer); i > 0 && count > 0; i--) {
101 if (buffer[i] == '/')
108 strcat( buffer, "Resources");
114 /* Fix menu to contain the real app name instead of "SDL App" */
115 - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
118 NSEnumerator *enumerator;
119 NSMenuItem *menuItem;
121 aRange = [[aMenu title] rangeOfString:@"SDL App"];
122 if (aRange.length != 0)
123 [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
125 enumerator = [[aMenu itemArray] objectEnumerator];
126 while ((menuItem = [enumerator nextObject]))
128 aRange = [[menuItem title] rangeOfString:@"SDL App"];
129 if (aRange.length != 0)
130 [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
131 if ([menuItem hasSubmenu])
132 [self fixMenu:[menuItem submenu] withAppName:appName];
137 /* Called when the internal event loop has just started running */
138 - (void) applicationDidFinishLaunching: (NSNotification *) note
142 /* Set the main menu to contain the real app name instead of "SDL App" */
143 [self fixMenu:[NSApp mainMenu] withAppName:[[NSProcessInfo processInfo] processName]];
145 /* Hand off to main application code */
146 status = SDL_main (gArgc, gArgv);
148 /* We're done, thank you for playing */
154 @implementation NSString (ReplaceSubString)
156 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
158 unsigned int bufferSize;
159 unsigned int selfLen = [self length];
160 unsigned int aStringLen = [aString length];
165 bufferSize = selfLen + aStringLen - aRange.length;
166 buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar));
168 /* Get first part into buffer */
169 localRange.location = 0;
170 localRange.length = aRange.location;
171 [self getCharacters:buffer range:localRange];
173 /* Get middle part into buffer */
174 localRange.location = 0;
175 localRange.length = aStringLen;
176 [aString getCharacters:(buffer+aRange.location) range:localRange];
178 /* Get last part into buffer */
179 localRange.location = aRange.location + aRange.length;
180 localRange.length = selfLen - localRange.location;
181 [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
183 /* Build output string */
184 result = [NSString stringWithCharacters:buffer length:bufferSize];
186 NSDeallocateMemoryPages(buffer, bufferSize);
200 /* Main entry point to executable - should *not* be SDL_main! */
201 int main (int argc, char **argv)
204 /* Copy the arguments into a global variable */
207 /* This is passed if we are launched by double-clicking */
208 if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
215 gArgv = (char**) malloc (sizeof(*gArgv) * (gArgc+1));
216 assert (gArgv != NULL);
217 for (i = 0; i < gArgc; i++)
221 MacOSX_SetCWD(gArgv);
223 [SDLApplication poseAsClass:[NSApplication class]];
224 NSApplicationMain (argc, argv);