Skip to content

Latest commit

 

History

History
163 lines (124 loc) · 4.34 KB

SDLMain.m

File metadata and controls

163 lines (124 loc) · 4.34 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
/* SDLMain.m - main entry point for our Cocoa-ized SDL app
Darrell Walisser - dwaliss1@purdue.edu
Feel free to customize this file to suit your needs
*/
#import "SDL.h"
#import "SDLMain.h"
#import <sys/param.h> /* for MAXPATHLEN */
#import <unistd.h>
static int gArgc;
static char **gArgv;
Aug 21, 2001
Aug 21, 2001
14
15
16
17
18
19
static NSString *gAppName = 0;
@interface NSString (ReplaceSubString)
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
@end
20
21
22
23
24
25
26
/* The main class of the application, the application's delegate */
@implementation SDLMain
/* Invoked from the Quit menu item */
- (void) quit:(id)sender
{
Aug 21, 2001
Aug 21, 2001
27
28
29
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
30
31
32
33
34
35
36
37
}
/* Set the working directory to the .app's parent directory */
- (void) setupWorkingDirectory
{
char parentdir[MAXPATHLEN];
char *c;
Aug 21, 2001
Aug 21, 2001
38
strncpy ( parentdir, gArgv[0], sizeof(parentdir) );
39
40
41
42
43
44
45
46
c = (char*) parentdir;
while (*c != '\0') /* go to end */
c++;
while (*c != '/') /* back up to parent */
c--;
Aug 21, 2001
Aug 21, 2001
47
*c++ = '\0'; /* cut off last part (binary name) */
48
49
50
assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */
assert ( chdir ("../../../") == 0 ); /* chdir to the .app's parent */
Aug 21, 2001
Aug 21, 2001
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
gAppName = [ NSString stringWithCString: c ];
}
/* 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 ];
76
77
78
79
80
}
/* Called when the internal event loop has just started running */
- (void) applicationDidFinishLaunching: (NSNotification *) note
{
Jun 11, 2001
Jun 11, 2001
81
82
int status;
83
84
/* Set the working directory to the .app's parent directory */
[ self setupWorkingDirectory ];
Jun 11, 2001
Jun 11, 2001
85
Aug 21, 2001
Aug 21, 2001
86
87
88
/* Set the main menu to contain the real app name instead of "SDL App" */
[ self fixMenu: [ NSApp mainMenu ] ];
89
/* Hand off to main application code */
Jun 11, 2001
Jun 11, 2001
90
91
92
93
status = SDL_main (gArgc, gArgv);
/* We're done, thank you for playing */
exit(status);
Aug 21, 2001
Aug 21, 2001
97
98
99
100
101
102
103
104
105
106
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
@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
138
139
140
141
142
143
144
145
146
147
#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;
Jun 11, 2001
Jun 11, 2001
148
149
150
151
152
153
154
/* This is passed if we are launched by double-clicking */
if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
gArgc = 1;
} else {
gArgc = argc;
}
gArgv = (char**) malloc (sizeof(*gArgv) * (gArgc+1));
155
156
assert (gArgv != NULL);
for (i = 0; i < gArgc; i++) {
Jun 11, 2001
Jun 11, 2001
157
gArgv[i] = argv[i];
Jun 11, 2001
Jun 11, 2001
159
160
gArgv[i] = NULL;
161
162
NSApplicationMain (argc, argv);
return 0;
Jun 11, 2001
Jun 11, 2001
163
}