Mercurial > sdl-ios-xcode
comparison src/main/macosx/SDLMain.m @ 194:ba9e0fcc2ae2
Oops, back out that SDL_main -> SDLMain conversion
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 23 Sep 2001 21:09:08 +0000 |
parents | |
children | 50620ec9c86a |
comparison
equal
deleted
inserted
replaced
193:ea31cf2d61af | 194:ba9e0fcc2ae2 |
---|---|
1 /* SDLMain.m - main entry point for our Cocoa-ized SDL app | |
2 Darrell Walisser - dwaliss1@purdue.edu | |
3 | |
4 Feel free to customize this file to suit your needs | |
5 */ | |
6 | |
7 #import "SDL.h" | |
8 #import "SDLMain.h" | |
9 #import <sys/param.h> /* for MAXPATHLEN */ | |
10 #import <unistd.h> | |
11 | |
12 static int gArgc; | |
13 static char **gArgv; | |
14 static NSString *gAppName = 0; | |
15 static BOOL gFinderLaunch; | |
16 | |
17 @interface NSString (ReplaceSubString) | |
18 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString; | |
19 @end | |
20 | |
21 | |
22 /* The main class of the application, the application's delegate */ | |
23 @implementation SDLMain | |
24 | |
25 /* Invoked from the Quit menu item */ | |
26 - (void) quit:(id)sender | |
27 { | |
28 SDL_Event event; | |
29 event.type = SDL_QUIT; | |
30 SDL_PushEvent(&event); | |
31 } | |
32 | |
33 /* Invoked from the Make Full-Screen menu item */ | |
34 - (void) makeFullscreen:(id)sender | |
35 { | |
36 /* TODO */ | |
37 } | |
38 | |
39 /* Set the working directory to the .app's parent directory */ | |
40 - (void) setupWorkingDirectory:(BOOL)shouldChdir | |
41 { | |
42 char parentdir[MAXPATHLEN]; | |
43 char *c; | |
44 | |
45 strncpy ( parentdir, gArgv[0], sizeof(parentdir) ); | |
46 c = (char*) parentdir; | |
47 | |
48 while (*c != '\0') /* go to end */ | |
49 c++; | |
50 | |
51 while (*c != '/') /* back up to parent */ | |
52 c--; | |
53 | |
54 *c++ = '\0'; /* cut off last part (binary name) */ | |
55 | |
56 if (shouldChdir) | |
57 { | |
58 assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */ | |
59 assert ( chdir ("../../../") == 0 ); /* chdir to the .app's parent */ | |
60 } | |
61 /* gAppName = [ NSString stringWithCString: c ]; */ | |
62 } | |
63 | |
64 /* Fix menu to contain the real app name instead of "SDL App" */ | |
65 - (void) fixMenu:(NSMenu *)aMenu | |
66 { | |
67 NSRange aRange; | |
68 NSEnumerator *enumerator; | |
69 NSMenuItem *menuItem; | |
70 | |
71 aRange = [[aMenu title] rangeOfString:@"SDL App"]; | |
72 if (aRange.length != 0) | |
73 [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:gAppName]]; | |
74 | |
75 enumerator = [[aMenu itemArray] objectEnumerator]; | |
76 while ((menuItem = [enumerator nextObject])) | |
77 { | |
78 aRange = [[menuItem title] rangeOfString:@"SDL App"]; | |
79 if (aRange.length != 0) | |
80 [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:gAppName]]; | |
81 if ([menuItem hasSubmenu]) | |
82 [self fixMenu: [menuItem submenu]]; | |
83 } | |
84 [ aMenu sizeToFit ]; | |
85 } | |
86 | |
87 /* Called when the internal event loop has just started running */ | |
88 - (void) applicationDidFinishLaunching: (NSNotification *) note | |
89 { | |
90 int status; | |
91 | |
92 /* Set the working directory to the .app's parent directory */ | |
93 [ self setupWorkingDirectory: gFinderLaunch ]; | |
94 | |
95 /* Set the main menu to contain the real app name instead of "SDL App" */ | |
96 gAppName = [ [ NSBundle mainBundle ] bundleIdentifier ]; | |
97 [ self fixMenu: [ NSApp mainMenu ] ]; | |
98 | |
99 /* Hand off to main application code */ | |
100 status = SDL_main (gArgc, gArgv); | |
101 | |
102 /* We're done, thank you for playing */ | |
103 exit(status); | |
104 } | |
105 @end | |
106 | |
107 | |
108 @implementation NSString (ReplaceSubString) | |
109 | |
110 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString | |
111 { | |
112 unsigned int bufferSize; | |
113 unsigned int selfLen = [self length]; | |
114 unsigned int aStringLen = [aString length]; | |
115 unichar *buffer; | |
116 NSRange localRange; | |
117 NSString *result; | |
118 | |
119 bufferSize = selfLen + aStringLen - aRange.length; | |
120 buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar)); | |
121 | |
122 // Get first part into buffer | |
123 localRange.location = 0; | |
124 localRange.length = aRange.location; | |
125 [self getCharacters:buffer range:localRange]; | |
126 | |
127 // Get middle part into buffer | |
128 localRange.location = 0; | |
129 localRange.length = aStringLen; | |
130 [aString getCharacters:(buffer+aRange.location) range:localRange]; | |
131 | |
132 // Get last part into buffer | |
133 localRange.location = aRange.location + aRange.length; | |
134 localRange.length = selfLen - localRange.location; | |
135 [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange]; | |
136 | |
137 // Build output string | |
138 result = [NSString stringWithCharacters:buffer length:bufferSize]; | |
139 | |
140 NSDeallocateMemoryPages(buffer, bufferSize); | |
141 | |
142 return result; | |
143 } | |
144 | |
145 @end | |
146 | |
147 | |
148 #ifdef main | |
149 # undef main | |
150 #endif | |
151 | |
152 /* Main entry point to executible - should *not* be SDL_main! */ | |
153 int main (int argc, char **argv) { | |
154 | |
155 /* Copy the arguments into a global variable */ | |
156 int i; | |
157 | |
158 /* This is passed if we are launched by double-clicking */ | |
159 if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) { | |
160 gArgc = 1; | |
161 gFinderLaunch = YES; | |
162 } else { | |
163 gArgc = argc; | |
164 gFinderLaunch = NO; | |
165 } | |
166 gArgv = (char**) malloc (sizeof(*gArgv) * (gArgc+1)); | |
167 assert (gArgv != NULL); | |
168 for (i = 0; i < gArgc; i++) { | |
169 gArgv[i] = argv[i]; | |
170 } | |
171 gArgv[i] = NULL; | |
172 | |
173 NSApplicationMain (argc, argv); | |
174 return 0; | |
175 } |