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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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Application.sharedApplication.delegate;
}
- (void)applicationDidFinishLaunching:(OFNotification *)notification
{
@autoreleasepool {
bool dedicated, windowed;
int par = 0, uprate = 0, maxcl = 4;
OFString *__autoreleasing sdesc, *__autoreleasing ip;
OFString *__autoreleasing master, *__autoreleasing passwd;
bool dedicated, windowed;
int par = 0, uprate = 0, maxcl = 4;
OFString *__autoreleasing sdesc, *__autoreleasing ip;
OFString *__autoreleasing master, *__autoreleasing passwd;
processInitQueue();
processInitQueue();
#define log(s) conoutf(@"init: %@", s)
log(@"sdl");
log(@"sdl");
const OFOptionsParserOption options[] = { { 'd', @"dedicated", 0,
&dedicated, NULL },
{ 't', @"window", 0, &windowed, NULL },
{ 'w', @"width", 1, NULL, NULL },
{ 'h', @"height", 1, NULL, NULL },
{ 'u', @"upload-rate", 1, NULL, NULL },
{ 'n', @"server-desc", 1, NULL, &sdesc },
{ 'i', @"ip", 1, NULL, &ip },
{ 'm', @"master", 1, NULL, &master },
{ 'p', @"password", 1, NULL, &passwd },
{ 'c', @"max-clients", 1, NULL, NULL },
{ '\0', nil, 0, NULL, NULL } };
OFOptionsParser *optionsParser =
[OFOptionsParser parserWithOptions:options];
OFUnichar option;
while ((option = [optionsParser nextOption]) != '\0') {
switch (option) {
case 'w':
_width = optionsParser.argument.intValue;
break;
case 'h':
_height = optionsParser.argument.intValue;
break;
case 'u':
uprate = optionsParser.argument.intValue;
break;
case 'c':
maxcl = optionsParser.argument.intValue;
break;
case ':':
case '=':
case '?':
conoutf(@"unknown commandline option");
[OFApplication terminateWithStatus:1];
}
}
const OFOptionsParserOption options[] = {
{ 'd', @"dedicated", 0, &dedicated, NULL },
{ 't', @"window", 0, &windowed, NULL },
{ 'w', @"width", 1, NULL, NULL },
{ 'h', @"height", 1, NULL, NULL },
{ 'u', @"upload-rate", 1, NULL, NULL },
{ 'n', @"server-desc", 1, NULL, &sdesc },
{ 'i', @"ip", 1, NULL, &ip },
{ 'm', @"master", 1, NULL, &master },
{ 'p', @"password", 1, NULL, &passwd },
{ 'c', @"max-clients", 1, NULL, NULL },
{ '\0', nil, 0, NULL, NULL }
};
OFOptionsParser *optionsParser =
[OFOptionsParser parserWithOptions:options];
OFUnichar option;
while ((option = [optionsParser nextOption]) != '\0') {
switch (option) {
case 'w':
_width = optionsParser.argument.intValue;
break;
case 'h':
_height = optionsParser.argument.intValue;
break;
case 'u':
uprate = optionsParser.argument.intValue;
break;
case 'c':
maxcl = optionsParser.argument.intValue;
break;
case ':':
case '=':
case '?':
conoutf(@"unknown commandline option");
[OFApplication terminateWithStatus:1];
}
}
if (sdesc == nil)
sdesc = @"";
if (ip == nil)
ip = @"";
if (passwd == nil)
passwd = @"";
if (sdesc == nil)
sdesc = @"";
if (ip == nil)
ip = @"";
if (passwd == nil)
passwd = @"";
_gameDataIRI =
_gameDataIRI = [OFFileManager.defaultManager currentDirectoryIRI];
_userDataIRI = [OFFileManager.defaultManager currentDirectoryIRI];
[OFFileManager.defaultManager currentDirectoryIRI];
_userDataIRI =
[OFFileManager.defaultManager currentDirectoryIRI];
[OFFileManager.defaultManager
createDirectoryAtIRI:[_userDataIRI
IRIByAppendingPathComponent:@"demos"]
createParents:true];
[OFFileManager.defaultManager
createDirectoryAtIRI:[_userDataIRI
IRIByAppendingPathComponent:@"savegames"]
createParents:true];
[OFFileManager.defaultManager
createDirectoryAtIRI:
[_userDataIRI IRIByAppendingPathComponent:@"demos"]
createParents:true];
[OFFileManager.defaultManager
createDirectoryAtIRI:
[_userDataIRI IRIByAppendingPathComponent:@"savegames"]
createParents:true];
if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | par) < 0)
fatal(@"Unable to initialize SDL");
if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | par) < 0)
fatal(@"Unable to initialize SDL");
initPlayers();
initPlayers();
log(@"net");
if (enet_initialize() < 0)
fatal(@"Unable to initialise network module");
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);
initclient();
// never returns if dedicated
initserver(dedicated, uprate, sdesc, ip, master, passwd, maxcl);
log(@"world");
empty_world(7, true);
log(@"world");
empty_world(7, true);
log(@"video: sdl");
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
fatal(@"Unable to initialize SDL Video");
log(@"video: sdl");
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
fatal(@"Unable to initialize SDL Video");
if (_width == 0 || _height == 0) {
SDL_DisplayMode mode;
if (_width == 0 || _height == 0) {
SDL_DisplayMode mode;
if (SDL_GetDesktopDisplayMode(0, &mode) == 0) {
_width = mode.w;
_height = mode.h;
} else {
_width = 1920;
_height = 1080;
}
}
if (SDL_GetDesktopDisplayMode(0, &mode) == 0) {
_width = mode.w;
_height = mode.h;
} else {
_width = 1920;
_height = 1080;
}
}
log(@"video: mode");
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if ((_window = SDL_CreateWindow("cube engine", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, _width, _height,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL |
(!windowed ? SDL_WINDOW_FULLSCREEN : 0))) == NULL ||
SDL_GL_CreateContext(_window) == NULL)
fatal(@"Unable to create OpenGL screen");
log(@"video: mode");
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if ((_window = SDL_CreateWindow("cube engine",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
_width, _height,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL |
(!windowed ? SDL_WINDOW_FULLSCREEN : 0))) ==
NULL ||
SDL_GL_CreateContext(_window) == NULL)
fatal(@"Unable to create OpenGL screen");
log(@"video: misc");
SDL_SetWindowGrab(_window, SDL_TRUE);
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_ShowCursor(0);
log(@"video: misc");
SDL_SetWindowGrab(_window, SDL_TRUE);
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_ShowCursor(0);
log(@"gl");
gl_init(_width, _height);
log(@"gl");
gl_init(_width, _height);
log(@"basetex");
int xs, ys;
if (!installtex(2,
[_gameDataIRI IRIByAppendingPathComponent:@"data/newchars.png"],
&xs, &ys, false) ||
!installtex(3,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/martin/base.png"],
&xs, &ys, false) ||
!installtex(6,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/martin/ball1.png"],
&xs, &ys, false) ||
!installtex(7,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/martin/smoke.png"],
&xs, &ys, false) ||
!installtex(8,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/martin/ball2.png"],
&xs, &ys, false) ||
!installtex(9,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/martin/ball3.png"],
&xs, &ys, false) ||
!installtex(4,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/explosion.jpg"],
&xs, &ys, false) ||
!installtex(5,
[_gameDataIRI IRIByAppendingPathComponent:@"data/items.png"],
&xs, &ys, false) ||
!installtex(1,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/crosshair.png"],
&xs, &ys, false))
fatal(@"could not find core textures (hint: run cube from the "
@"parent of the bin directory)");
log(@"basetex");
int xs, ys;
if (!installtex(2,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/newchars.png"],
&xs, &ys, false) ||
!installtex(3,
[_gameDataIRI IRIByAppendingPathComponent:
@"data/martin/base.png"],
&xs, &ys, false) ||
!installtex(6,
[_gameDataIRI IRIByAppendingPathComponent:
@"data/martin/ball1.png"],
&xs, &ys, false) ||
!installtex(7,
[_gameDataIRI IRIByAppendingPathComponent:
@"data/martin/smoke.png"],
&xs, &ys, false) ||
!installtex(8,
[_gameDataIRI IRIByAppendingPathComponent:
@"data/martin/ball2.png"],
&xs, &ys, false) ||
!installtex(9,
[_gameDataIRI IRIByAppendingPathComponent:
@"data/martin/ball3.png"],
&xs, &ys, false) ||
!installtex(4,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/explosion.jpg"],
&xs, &ys, false) ||
!installtex(5,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/items.png"],
&xs, &ys, false) ||
!installtex(1,
[_gameDataIRI
IRIByAppendingPathComponent:@"data/crosshair.png"],
&xs, &ys, false))
fatal(@"could not find core textures (hint: run cube "
@"from the parent of the bin directory)");
log(@"sound");
initsound();
log(@"sound");
initsound();
log(@"cfg");
newmenu(@"frags\tpj\tping\tteam\tname");
newmenu(@"ping\tplr\tserver");
exec(@"data/keymap.cfg");
exec(@"data/menus.cfg");
exec(@"data/prefabs.cfg");
exec(@"data/sounds.cfg");
exec(@"servers.cfg");
if (!execfile([_userDataIRI IRIByAppendingPathComponent:@"config.cfg"]))
execfile([_gameDataIRI
IRIByAppendingPathComponent:@"data/defaults.cfg"]);
exec(@"autoexec.cfg");
log(@"cfg");
newmenu(@"frags\tpj\tping\tteam\tname");
newmenu(@"ping\tplr\tserver");
exec(@"data/keymap.cfg");
exec(@"data/menus.cfg");
exec(@"data/prefabs.cfg");
exec(@"data/sounds.cfg");
exec(@"servers.cfg");
if (!execfile([_userDataIRI
IRIByAppendingPathComponent:@"config.cfg"]))
execfile([_gameDataIRI
IRIByAppendingPathComponent:@"data/defaults.cfg"]);
exec(@"autoexec.cfg");
log(@"localconnect");
localconnect();
// if this map is changed, also change depthcorrect()
changemap(@"metl3");
log(@"localconnect");
localconnect();
// if this map is changed, also change depthcorrect()
changemap(@"metl3");
log(@"mainloop");
int ignore = 5;
log(@"mainloop");
}
OFDate *past = [OFDate date];
int ignore = 5;
for (;;) {
@autoreleasepool {
[OFRunLoop.mainRunLoop runUntilDate:past];
[OFRunLoop.mainRunLoop runUntilDate:past];
int millis = SDL_GetTicks() * gamespeed / 100;
if (millis - lastmillis > 200)
lastmillis = millis - 200;
else if (millis - lastmillis < 1)
lastmillis = millis - 1;
if (millis - lastmillis < minmillis)
SDL_Delay(minmillis - (millis - lastmillis));
int millis = SDL_GetTicks() * gamespeed / 100;
if (millis - lastmillis > 200)
lastmillis = millis - 200;
else if (millis - lastmillis < 1)
lastmillis = millis - 1;
if (millis - lastmillis < minmillis)
SDL_Delay(minmillis - (millis - lastmillis));
cleardlights();
updateworld(millis);
cleardlights();
updateworld(millis);
if (!demoplayback)
serverslice((int)time(NULL), 0);
if (!demoplayback)
serverslice((int)time(NULL), 0);
static float fps = 30.0f;
fps = (1000.0f / curtime + fps * 50) / 51;
static float fps = 30.0f;
fps = (1000.0f / curtime + fps * 50) / 51;
computeraytable(player1.o.x, player1.o.y);
readdepth(_width, _height);
SDL_GL_SwapWindow(_window);
extern void updatevol();
updatevol();
computeraytable(player1.o.x, player1.o.y);
readdepth(_width, _height);
SDL_GL_SwapWindow(_window);
extern void updatevol();
updatevol();
// cheap hack to get rid of initial sparklies, even when triple
// buffering etc.
if (_framesInMap++ < 5) {
player1.yaw += 5;
gl_drawframe(_width, _height, fps);
player1.yaw -= 5;
}
// cheap hack to get rid of initial sparklies, even when
// triple buffering etc.
if (_framesInMap++ < 5) {
player1.yaw += 5;
gl_drawframe(_width, _height, fps);
player1.yaw -= 5;
}
gl_drawframe(_width, _height, fps);
gl_drawframe(_width, _height, fps);
SDL_Event event;
int lasttype = 0, lastbut = 0;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
[self quit];
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
if (_repeatsKeys || event.key.repeat == 0)
keypress(event.key.keysym.sym,
event.key.state == SDL_PRESSED);
break;
case SDL_TEXTINPUT:
SDL_Event event;
int lasttype = 0, lastbut = 0;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
[self quit];
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
if (_repeatsKeys ||
event.key.repeat == 0)
keypress(event.key.keysym.sym,
event.key.state ==
SDL_PRESSED);
break;
case SDL_TEXTINPUT:
@autoreleasepool {
input(@(event.text.text));
}
break;
case SDL_MOUSEMOTION:
if (ignore) {
ignore--;
break;
}
mousemove(event.motion.xrel, event.motion.yrel);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
if (lasttype == event.type &&
lastbut == event.button.button)
// why?? get event twice without it
break;
break;
case SDL_MOUSEMOTION:
if (ignore) {
ignore--;
break;
}
mousemove(event.motion.xrel,
event.motion.yrel);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
if (lasttype == event.type &&
lastbut == event.button.button)
// why?? get event twice without
// it
break;
keypress(-event.button.button,
event.button.state != 0);
lasttype = event.type;
lastbut = event.button.button;
break;
}
}
}
keypress(-event.button.button,
event.button.state != 0);
lasttype = event.type;
lastbut = event.button.button;
break;
}
}
}
}
[self quit];
}
- (void)applicationWillTerminate:(OFNotification *)notification
{
stop();
disconnect(true);
writecfg();
|