]> icculus.org git repositories - crow/jumpnbump.git/blob - PB/SDLMain.m
no message
[crow/jumpnbump.git] / PB / SDLMain.m
1 /*
2  * SDLMain.m
3  * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
4  * 
5  * Copyright (C) 2001 Chuck Mason <cemason@users.sourceforge.net>
6  *
7  * Copyright (C) 2002 Florian Schulze <crow@icculus.org>
8  *
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.
12  *
13  * This file is part of Jump'n'Bump.
14  *
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.
19  *
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.
24  *
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
28  */
29
30 /*   SDLMain.m - main entry point for our Cocoa-ized SDL app
31                 
32         These files turn an SDL app into a double-clickable Cocoa app on mac os X.
33         
34     Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
35     Non-NIB-Code & other changes: Max Horn <max@quendi.de>
36
37     Feel free to customize this file to suit your needs
38 */
39
40 #import "SDL.h"
41
42 /* SDLMain.h */
43 #import <Cocoa/Cocoa.h>
44
45 @interface SDLMain : NSObject
46 @end
47 /* SDLMain.h */
48
49 #import <sys/param.h> /* for MAXPATHLEN */
50 #import <unistd.h>
51
52
53 static int    gArgc;
54 static char  **gArgv;
55 static BOOL   gFinderLaunch;
56
57 /* A helper category for NSString */
58 @interface NSString (ReplaceSubString)
59 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
60 @end
61
62 @interface SDLApplication : NSApplication
63 @end
64
65 @implementation SDLApplication
66 /* Invoked from the Quit menu item */
67 - (void)terminate:(id)sender
68 {
69     /* Post a SDL_QUIT event */
70     SDL_Event event;
71     event.type = SDL_QUIT;
72     SDL_PushEvent(&event);
73 }
74 @end
75
76
77 /* The main class of the application, the application's delegate */
78 @implementation SDLMain
79 - (void)quit:(id)sender
80 {
81         /* Post a SDL_QUIT event */
82     SDL_Event event;
83     event.type = SDL_QUIT;
84     SDL_PushEvent(&event);
85
86         //              uMenu::quickexit=true;
87 }
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) {
91     char buffer[300];
92     int count = 2, i;
93     strcpy(buffer, argv[0]);
94
95     if ( !strstr( buffer, ".app") ) {
96         //it's not a .app bundle
97         return;
98     }
99
100     for (i = strlen(buffer); i > 0 && count > 0; i--) {
101         if (buffer[i] == '/')
102             count--;
103     }
104     if (i == 0)
105         return;
106     i+=2;
107     buffer[i] = 0;
108     strcat( buffer, "Resources");
109     printf(buffer);
110     chdir( buffer );
111 }
112
113
114 /* Fix menu to contain the real app name instead of "SDL App" */
115 - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
116 {
117     NSRange aRange;
118     NSEnumerator *enumerator;
119     NSMenuItem *menuItem;
120
121     aRange = [[aMenu title] rangeOfString:@"SDL App"];
122     if (aRange.length != 0)
123         [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
124
125     enumerator = [[aMenu itemArray] objectEnumerator];
126     while ((menuItem = [enumerator nextObject]))
127     {
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];
133     }
134     [ aMenu sizeToFit ];
135 }
136
137 /* Called when the internal event loop has just started running */
138 - (void) applicationDidFinishLaunching: (NSNotification *) note
139 {
140     int status;
141
142     /* Set the main menu to contain the real app name instead of "SDL App" */
143     [self fixMenu:[NSApp mainMenu] withAppName:[[NSProcessInfo processInfo] processName]];
144
145     /* Hand off to main application code */
146     status = SDL_main (gArgc, gArgv);
147
148     /* We're done, thank you for playing */
149     exit(status);
150 }
151 @end
152
153
154 @implementation NSString (ReplaceSubString)
155
156 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
157 {
158     unsigned int bufferSize;
159     unsigned int selfLen = [self length];
160     unsigned int aStringLen = [aString length];
161     unichar *buffer;
162     NSRange localRange;
163     NSString *result;
164
165     bufferSize = selfLen + aStringLen - aRange.length;
166     buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar));
167     
168     /* Get first part into buffer */
169     localRange.location = 0;
170     localRange.length = aRange.location;
171     [self getCharacters:buffer range:localRange];
172     
173     /* Get middle part into buffer */
174     localRange.location = 0;
175     localRange.length = aStringLen;
176     [aString getCharacters:(buffer+aRange.location) range:localRange];
177      
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];
182     
183     /* Build output string */
184     result = [NSString stringWithCharacters:buffer length:bufferSize];
185     
186     NSDeallocateMemoryPages(buffer, bufferSize);
187     
188     return result;
189 }
190
191 @end
192
193
194
195 #ifdef main
196 #  undef main
197 #endif
198
199
200 /* Main entry point to executable - should *not* be SDL_main! */
201 int main (int argc, char **argv)
202 {
203
204     /* Copy the arguments into a global variable */
205     int i;
206     
207     /* This is passed if we are launched by double-clicking */
208     if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
209         gArgc = 1;
210         gFinderLaunch = YES;
211     } else {
212         gArgc = argc;
213         gFinderLaunch = NO;
214     }
215     gArgv = (char**) malloc (sizeof(*gArgv) * (gArgc+1));
216     assert (gArgv != NULL);
217     for (i = 0; i < gArgc; i++)
218         gArgv[i] = argv[i];
219     gArgv[i] = NULL;
220
221         MacOSX_SetCWD(gArgv);
222
223     [SDLApplication poseAsClass:[NSApplication class]];
224     NSApplicationMain (argc, argv);
225
226     return 0;
227 }