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
|
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
|
-
+
-
-
+
+
-
+
-
+
-
+
-
-
+
+
|
// main.cpp: initialisation & main loop
#include "cube.h"
OF_APPLICATION_DELEGATE(Cube)
@implementation Cube
- (void)showMessage: (OFString *)msg
- (void)showMessage:(OFString *)msg
{
#ifdef _WIN32
MessageBoxW(NULL, msg.UTF16String, L"cube fatal error",
MB_OK | MB_SYSTEMMODAL);
MessageBoxW(
NULL, msg.UTF16String, L"cube fatal error", MB_OK | MB_SYSTEMMODAL);
#else
[OFStdOut writeString: msg];
[OFStdOut writeString:msg];
#endif
}
- (void)applicationWillTerminate: (OFNotification *)notification
- (void)applicationWillTerminate:(OFNotification *)notification
{
stop();
disconnect(true);
writecfg();
cleangl();
cleansound();
cleanupserver();
SDL_ShowCursor(1);
SDL_Quit();
}
void
quit() // normal exit
{
writeservercfg();
[OFApplication.sharedApplication terminateWithStatus: 0];
[OFApplication.sharedApplication terminateWithStatus:0];
}
void
fatal(char *s, char *o) // failure exit
{
sprintf_sd(msg)("%s%s (%s)\n", s, o, SDL_GetError());
OFApplication *app = OFApplication.sharedApplication;
[(Cube *)app.delegate showMessage: @(msg)];
[app terminateWithStatus: 1];
[(Cube *)app.delegate showMessage:@(msg)];
[app terminateWithStatus:1];
}
void *
alloc(int s) // for some big chunks... most other allocs use the memory pool
{
void *b = calloc(1, s);
if (!b)
|
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
-
-
+
-
-
+
+
-
-
-
+
-
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
+
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
+
|
SDL_EnableKeyRepeat(
on ? SDL_DEFAULT_REPEAT_DELAY : 0, SDL_DEFAULT_REPEAT_INTERVAL);
}
VARF(gamespeed, 10, 100, 1000, if (multiplayer()) gamespeed = 100);
VARP(minmillis, 0, 5, 1000);
int islittleendian = 1;
int framesinmap = 0;
- (void)applicationDidFinishLaunching: (OFNotification *)notification
- (void)applicationDidFinishLaunching:(OFNotification *)notification
{
bool dedicated = false;
int fs = SDL_FULLSCREEN, par = 0, uprate = 0, maxcl = 4;
bool dedicated, windowed;
int par = 0, uprate = 0, maxcl = 4;
char *sdesc = "", *ip = "", *master = NULL, *passwd = "";
islittleendian = *((char *)&islittleendian);
int argc, *argcPtr;
OFString *__autoreleasing sdesc, *__autoreleasing ip;
char **argv, ***argvPtr;
OFString *__autoreleasing master, *__autoreleasing passwd;
[OFApplication.sharedApplication getArgumentCount: &argcPtr
andArgumentValues: &argvPtr];
argc = *argcPtr;
argv = *argvPtr;
processInitQueue();
#define log(s) conoutf(@"init: %s", s)
log("sdl");
for (int i = 1; i < argc; i++) {
char *a = &argv[i][2];
const OFOptionsParserOption options[] = {
{'d', nil, 0, &dedicated, NULL}, {'t', nil, 0, &windowed, NULL},
{'w', nil, 1, NULL, NULL}, {'h', nil, 1, NULL, NULL},
{'u', nil, 1, NULL, NULL}, {'n', nil, 1, NULL, &sdesc},
{'i', nil, 1, NULL, &ip}, {'m', nil, 1, NULL, &master},
{'p', nil, 1, NULL, &passwd}, {'c', nil, 1, NULL, NULL}};
OFOptionsParser *optionsParser =
[OFOptionsParser parserWithOptions:options];
OFUnichar option;
if (argv[i][0] == '-')
switch (argv[i][1]) {
while ((option = [optionsParser nextOption]) != '\0') {
switch (option) {
case 'd':
dedicated = true;
break;
case 't':
fs = 0;
break;
case 'w':
scr_w = atoi(a);
break;
case 'h':
scr_h = atoi(a);
break;
case 'w':
scr_w = optionsParser.argument.longLongValue;
break;
case 'h':
scr_h = optionsParser.argument.longLongValue;
break;
case 'u':
uprate = atoi(a);
break;
case 'n':
sdesc = a;
break;
case 'i':
ip = a;
break;
case 'm':
master = a;
break;
case 'p':
passwd = a;
case 'u':
uprate = optionsParser.argument.longLongValue;
break;
case 'c':
maxcl = optionsParser.argument.longLongValue;
break;
case ':':
case '=':
break;
case 'c':
case '?':
maxcl = atoi(a);
break;
default:
conoutf(@"unknown commandline option");
}
conoutf(@"unknown commandline option");
[OFApplication terminateWithStatus:1];
}
else
conoutf(@"unknown commandline argument");
};
#ifdef _DEBUG
par = SDL_INIT_NOPARACHUTE;
fs = 0;
#endif
}
if (sdesc == nil)
sdesc = @"";
if (ip == nil)
ip = @"";
if (passwd == nil)
passwd = @"";
if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | par) < 0)
fatal("Unable to initialize SDL");
log("net");
if (enet_initialize() < 0)
fatal("Unable to initialise network module");
initclient();
// never returns if dedicated
initserver(dedicated, uprate, sdesc, ip, master, passwd,
maxcl); // never returns if dedicated
initserver(dedicated, uprate, sdesc.UTF8String, ip.UTF8String,
master.UTF8String, passwd, maxcl);
log("world");
empty_world(7, true);
log("video: sdl");
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
fatal("Unable to initialize SDL Video");
log("video: mode");
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if (SDL_SetVideoMode(scr_w, scr_h, 0, SDL_OPENGL | fs) == NULL)
if (SDL_SetVideoMode(scr_w, scr_h, 0,
SDL_OPENGL | (!windowed ? SDL_FULLSCREEN : 0)) == NULL)
fatal("Unable to create OpenGL screen");
log("video: misc");
SDL_WM_SetCaption("cube engine", NULL);
SDL_WM_GrabInput(SDL_GRAB_ON);
keyrepeat(false);
SDL_ShowCursor(0);
|