Skip to content

Latest commit

 

History

History
175 lines (136 loc) · 4.64 KB

SDL_main.m

File metadata and controls

175 lines (136 loc) · 4.64 KB
 
Sep 23, 2001
Sep 23, 2001
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"
Sep 23, 2001
Sep 23, 2001
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;
Aug 21, 2001
Aug 21, 2001
14
static NSString *gAppName = 0;
Sep 4, 2001
Sep 4, 2001
15
static BOOL gFinderLaunch;
Aug 21, 2001
Aug 21, 2001
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 */
Sep 23, 2001
Sep 23, 2001
23
@implementation SDL_main
24
25
26
27
/* Invoked from the Quit menu item */
- (void) quit:(id)sender
{
Aug 21, 2001
Aug 21, 2001
28
29
30
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
Sep 4, 2001
Sep 4, 2001
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 */
Sep 4, 2001
Sep 4, 2001
40
- (void) setupWorkingDirectory:(BOOL)shouldChdir
41
42
43
44
{
char parentdir[MAXPATHLEN];
char *c;
Aug 21, 2001
Aug 21, 2001
45
strncpy ( parentdir, gArgv[0], sizeof(parentdir) );
46
c = (char*) parentdir;
Sep 4, 2001
Sep 4, 2001
47
48
49
50
51
52
53
while (*c != '\0') /* go to end */
c++;
while (*c != '/') /* back up to parent */
c--;
Aug 21, 2001
Aug 21, 2001
54
*c++ = '\0'; /* cut off last part (binary name) */
Sep 4, 2001
Sep 4, 2001
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 ]; */
Aug 21, 2001
Aug 21, 2001
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
{
Jun 11, 2001
Jun 11, 2001
90
91
int status;
92
/* Set the working directory to the .app's parent directory */
Sep 4, 2001
Sep 4, 2001
93
[ self setupWorkingDirectory: gFinderLaunch ];
Jun 11, 2001
Jun 11, 2001
94
Aug 21, 2001
Aug 21, 2001
95
/* Set the main menu to contain the real app name instead of "SDL App" */
Sep 4, 2001
Sep 4, 2001
96
gAppName = [ [ NSBundle mainBundle ] bundleIdentifier ];
Aug 21, 2001
Aug 21, 2001
97
98
[ self fixMenu: [ NSApp mainMenu ] ];
99
/* Hand off to main application code */
Jun 11, 2001
Jun 11, 2001
100
101
102
103
status = SDL_main (gArgc, gArgv);
/* We're done, thank you for playing */
exit(status);
Aug 21, 2001
Aug 21, 2001
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;
Jun 11, 2001
Jun 11, 2001
158
159
160
/* This is passed if we are launched by double-clicking */
if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
gArgc = 1;
Sep 4, 2001
Sep 4, 2001
161
gFinderLaunch = YES;
Jun 11, 2001
Jun 11, 2001
162
163
} else {
gArgc = argc;
Sep 4, 2001
Sep 4, 2001
164
gFinderLaunch = NO;
Jun 11, 2001
Jun 11, 2001
165
166
}
gArgv = (char**) malloc (sizeof(*gArgv) * (gArgc+1));
167
168
assert (gArgv != NULL);
for (i = 0; i < gArgc; i++) {
Jun 11, 2001
Jun 11, 2001
169
gArgv[i] = argv[i];
Jun 11, 2001
Jun 11, 2001
171
172
gArgv[i] = NULL;
173
174
NSApplicationMain (argc, argv);
return 0;
Jun 11, 2001
Jun 11, 2001
175
}