/
SDLMain.m
159 lines (129 loc) · 3.7 KB
/
SDLMain.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* SDLMain.m - main entry point for our Cocoa-ized SDL app
Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
Non-NIB-Code & other changes: Max Horn <max@quendi.de>
Refactor & cleanup for Hero of Allacrost and Valyria Tear: Alastair Lynn <arplynn@gmail.com>
Feel free to customize this file to suit your needs
*/
#import <SDL/SDL.h>
#import <unistd.h>
#import <Cocoa/Cocoa.h>
@interface SDLMain : NSObject
{
}
@end
extern int NXArgc;
extern char** NXArgv;
// these definitions throw out the use of -psn arguments from Carbon
static int SDLArgc;
static char** SDLArgv;
static void GenerateSDLArguments()
{
BOOL removePSN = NO;
int i;
if (NXArgc > 1 && (strncmp("-psn", NXArgv[1], 4)) == 0)
{
removePSN = YES;
}
if (removePSN)
{
SDLArgc = NXArgc - 1;
}
else
{
SDLArgc = NXArgc;
}
NSCAssert(SDLArgc > 0, @"SDLArgc was <= 0");
SDLArgv = malloc(sizeof(char*) * SDLArgc);
if (removePSN)
{
SDLArgv[0] = strdup(NXArgv[0]);
for (i = 1; i < SDLArgc; i++)
{
SDLArgv[i] = strdup(NXArgv[i + 1]);
}
}
else
{
for (i = 0; i < SDLArgc; i++)
{
SDLArgv[i] = strdup(NXArgv[i]);
}
}
}
// Create a window menu
static void SetupWindowMenu()
{
NSMenu* windowMenu;
NSMenuItem* windowMenuItem;
NSMenuItem* menuItem;
windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
// "Minimize" item
menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
[windowMenu addItem:menuItem];
[menuItem release];
// Put menu into the menubar
windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
[windowMenuItem setSubmenu:windowMenu];
[[NSApp mainMenu] addItem:windowMenuItem];
// Tell the application object that this is now the window menu
[NSApp setWindowsMenu:windowMenu];
// Finally give up our references to the objects
[windowMenu release];
[windowMenuItem release];
}
// performs many of the same functions as NSApplicationMain
void CustomApplicationMain ()
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
SDLMain* sdlMain;
// Ensure the application object is initialised
[NSApplication sharedApplication];
// Set up the menubar
[NSApp setMainMenu:[[NSMenu alloc] init]];
SetupWindowMenu();
// Create SDLMain and make it the app delegate
sdlMain = [[SDLMain alloc] init];
[NSApp setDelegate:sdlMain];
// Start the main event loop
[NSApp run];
[sdlMain release];
[pool release];
}
// The main class of the application, the application's delegate
@implementation SDLMain
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app
{
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
return NSTerminateCancel;
}
// Set the working directory to the .app's parent directory
- (void)setupWorkingDirectory
{
NSString* applicationPath = [[NSBundle mainBundle] bundlePath];
[[NSFileManager defaultManager] changeCurrentDirectoryPath:[applicationPath stringByDeletingLastPathComponent]];
}
// Called when the internal event loop has just started running
- (void)applicationDidFinishLaunching:(NSNotification*) note
{
int status;
// Set the working directory to the .app's parent directory
[self setupWorkingDirectory];
// Fix the arguments to remove Carbon process serial numbers
GenerateSDLArguments();
// Hand off to main application code
status = SDL_main (SDLArgc, SDLArgv);
// We're done, thank you for playing
exit(status);
}
@end
#ifdef main
# undef main
#endif
// Main entry point to executable - should *not* be SDL_main!
int main (int argc, char **argv)
{
CustomApplicationMain();
return 0;
}