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