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