Cube  Check-in [02dbc547c1]

Overview
Comment:Reduce global variables
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 02dbc547c1a4a13918d02cb6f5ba92dd653622303979dd09cc5e3d6d0f617203
User & Date: js on 2025-03-05 00:27:18
Other Links: manifest | tags
Context
2025-03-05
01:15
Convert texture pixel format if necessary check-in: 12641927d9 user: js tags: trunk
00:27
Reduce global variables check-in: 02dbc547c1 user: js tags: trunk
2025-03-04
23:51
Port to SDL2 check-in: 42d4b57828 user: js tags: trunk
Changes

Modified src/Cube.mm from [c32806b2e7] to [a97b4669e3].

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
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






-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-



+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







// main.cpp: initialisation & main loop

#include "cube.h"

OF_APPLICATION_DELEGATE(Cube)

@implementation Cube
- (void)showMessage:(OFString *)msg
{
#ifdef _WIN32
	MessageBoxW(
	    NULL, msg.UTF16String, L"cube fatal error", MB_OK | MB_SYSTEMMODAL);
#else
	[OFStdOut writeString:msg];
#endif
}

- (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];
}

void
fatal(OFString *s, OFString *o) // failure exit
{
	OFString *msg =
	    [OFString stringWithFormat:@"%@%@ (%s)\n", s, o, SDL_GetError()];

	OFApplication *app = OFApplication.sharedApplication;
	[(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)
		fatal(@"out of memory!");
	return b;
}

SDL_Window *window;
int scr_w = 640;
int scr_h = 480;

void
screenshot()
{
	SDL_Surface *image;
	SDL_Surface *temp;
	int idx;
	if (image = SDL_CreateRGBSurface(SDL_SWSURFACE, scr_w, scr_h, 24,
	        0x0000FF, 0x00FF00, 0xFF0000, 0)) {
		if (temp = SDL_CreateRGBSurface(SDL_SWSURFACE, scr_w, scr_h, 24,
		        0x0000FF, 0x00FF00, 0xFF0000, 0)) {
			glReadPixels(0, 0, scr_w, scr_h, GL_RGB,
			    GL_UNSIGNED_BYTE, image->pixels);
			for (idx = 0; idx < scr_h; idx++) {
				char *dest =
				    (char *)temp->pixels + 3 * scr_w * idx;
				memcpy(dest,
				    (char *)image->pixels +
				        3 * scr_w * (scr_h - 1 - idx),
				    3 * scr_w);
				endianswap(dest, 3, scr_w);
			};
			sprintf_sd(buf)(
			    "screenshots/screenshot_%d.bmp", lastmillis);
			SDL_SaveBMP(temp, path(buf));
			SDL_FreeSurface(temp);
		};
		SDL_FreeSurface(image);
	};
}

COMMAND(screenshot, ARG_NONE)
COMMAND(quit, ARG_NONE)

bool keyrepeat = false;

VARF(gamespeed, 10, 100, 1000, if (multiplayer()) gamespeed = 100);
VARP(minmillis, 0, 5, 1000);

@implementation Cube {
	int _width, _height;
}
int framesinmap = 0;

+ (Cube *)sharedInstance
{
	return (Cube *)OFApplication.sharedApplication.delegate;
}

- (instancetype)init
{
	self = [super init];

	_width = 1920;
	_height = 1080;

	return self;
}

- (void)applicationDidFinishLaunching:(OFNotification *)notification
{
	bool dedicated, windowed;
	int par = 0, uprate = 0, maxcl = 4;
	OFString *__autoreleasing sdesc, *__autoreleasing ip;
	OFString *__autoreleasing master, *__autoreleasing passwd;
121
122
123
124
125
126
127
128

129
130
131

132
133
134

135
136
137

138
139
140
141
142
143
144
49
50
51
52
53
54
55

56
57
58

59
60
61

62
63
64

65
66
67
68
69
70
71
72







-
+


-
+


-
+


-
+







	    {'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':
			scr_w = optionsParser.argument.longLongValue;
			_width = (int)optionsParser.argument.longLongValue;
			break;
		case 'h':
			scr_h = optionsParser.argument.longLongValue;
			_height = (int)optionsParser.argument.longLongValue;
			break;
		case 'u':
			uprate = optionsParser.argument.longLongValue;
			uprate = (int)optionsParser.argument.longLongValue;
			break;
		case 'c':
			maxcl = optionsParser.argument.longLongValue;
			maxcl = (int)optionsParser.argument.longLongValue;
			break;
		case ':':
		case '=':
		case '?':
			conoutf(@"unknown commandline option");
			[OFApplication terminateWithStatus:1];
		}
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
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







-
-
+
+


-
+



-
+

-



-
+








	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 ((window = SDL_CreateWindow("cube engine", SDL_WINDOWPOS_UNDEFINED,
	         SDL_WINDOWPOS_UNDEFINED, scr_w, scr_h,
	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)
	    SDL_GL_CreateContext(_window) == NULL)
		fatal(@"Unable to create OpenGL screen");

	log("video: misc");
	SDL_SetWindowGrab(window, SDL_TRUE);
	SDL_SetWindowGrab(_window, SDL_TRUE);
	SDL_SetRelativeMouseMode(SDL_TRUE);
	keyrepeat = false;
	SDL_ShowCursor(0);

	log("gl");
	gl_init(scr_w, scr_h);
	gl_init(_width, _height);

	log("basetex");
	int xs, ys;
	if (!installtex(2, path(newstring("data/newchars.png")), xs, ys) ||
	    !installtex(3, path(newstring("data/martin/base.png")), xs, ys) ||
	    !installtex(6, path(newstring("data/martin/ball1.png")), xs, ys) ||
	    !installtex(7, path(newstring("data/martin/smoke.png")), xs, ys) ||
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
287
288
289
290
291
292













293

















































294
295

































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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320







+


+


+


+

-
-
+
+


-
-
-
+
+
+
-
+

-
+


+
-
+
+





-
+

-


-
+




-







-




-
-
+
+
+








+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
		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);

		if (!demoplayback)
			serverslice((int)time(NULL), 0);

		static float fps = 30.0f;
		fps = (1000.0f / curtime + fps * 50) / 51;

		computeraytable(player1->o.x, player1->o.y);
		readdepth(scr_w, scr_h);
		SDL_GL_SwapWindow(window);
		readdepth(_width, _height);
		SDL_GL_SwapWindow(_window);
		extern void updatevol();
		updatevol();
		if (framesinmap++ <
		    5) // cheap hack to get rid of initial sparklies, even when
		       // triple buffering etc.

		// cheap hack to get rid of initial sparklies, even when triple
		// buffering etc.
		{
		if (_framesInMap++ < 5) {
			player1->yaw += 5;
			gl_drawframe(scr_w, scr_h, fps);
			gl_drawframe(_width, _height, fps);
			player1->yaw -= 5;
		}

		gl_drawframe(scr_w, scr_h, fps);
		gl_drawframe(_width, _height, fps);

		SDL_Event event;
		int lasttype = 0, lastbut = 0;
		while (SDL_PollEvent(&event)) {
			switch (event.type) {
			case SDL_QUIT:
				quit();
				[self quit];
				break;

			case SDL_KEYDOWN:
			case SDL_KEYUP:
				if (keyrepeat || event.key.repeat == 0)
				if (_repeatsKeys || event.key.repeat == 0)
					keypress(event.key.keysym.sym,
					    event.key.state == SDL_PRESSED,
					    event.key.keysym.sym);
				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)
					break; // why?? get event twice without
					       // it
					// why?? get event twice without it
					break;

				keypress(-event.button.button,
				    event.button.state != 0, 0);
				lasttype = event.type;
				lastbut = event.button.button;
				break;
			}
		}
	}

	[self quit];
}

- (void)applicationWillTerminate:(OFNotification *)notification
{
	stop();
	disconnect(true);
	writecfg();
	cleangl();
	cleansound();
	cleanupserver();
	SDL_ShowCursor(1);
	quit();
	SDL_Quit();
}

- (void)showMessage:(OFString *)msg
{
#ifdef _WIN32
	MessageBoxW(
	    NULL, msg.UTF16String, L"cube fatal error", MB_OK | MB_SYSTEMMODAL);
#else
	[OFStdOut writeString:msg];
#endif
}

- (void)screenshot
{
	SDL_Surface *image;
	SDL_Surface *temp;

	if ((image = SDL_CreateRGBSurface(SDL_SWSURFACE, _width, _height, 24,
	         0x0000FF, 0x00FF00, 0xFF0000, 0)) != NULL) {
		if ((temp = SDL_CreateRGBSurface(SDL_SWSURFACE, _width, _height,
		         24, 0x0000FF, 0x00FF00, 0xFF0000, 0)) != NULL) {
			glReadPixels(0, 0, _width, _height, GL_RGB,
			    GL_UNSIGNED_BYTE, image->pixels);

			for (int idx = 0; idx < _height; idx++) {
				char *dest =
				    (char *)temp->pixels + 3 * _width * idx;
				memcpy(dest,
				    (char *)image->pixels +
				        3 * _width * (_height - 1 - idx),
				    3 * _width);
				endianswap(dest, 3, _width);
			}

			sprintf_sd(buf)(
			    "screenshots/screenshot_%d.bmp", lastmillis);
			SDL_SaveBMP(temp, path(buf));
			SDL_FreeSurface(temp);
		}

		SDL_FreeSurface(image);
	}
}

- (void)quit
{
	writeservercfg();
	[OFApplication terminateWithStatus:0];
}
@end

void
fatal(OFString *s, OFString *o) // failure exit
{
	OFString *msg =
	    [OFString stringWithFormat:@"%@%@ (%s)\n", s, o, SDL_GetError()];

	[Cube.sharedInstance showMessage:msg];
	[OFApplication terminateWithStatus:1];
}

void *
alloc(int s) // for some big chunks... most other allocs use the memory pool
{
	void *b = calloc(1, s);
	if (!b)
		fatal(@"out of memory!");
	return b;
}

void
quit() // normal exit
{
	[Cube.sharedInstance quit];
}
COMMAND(quit, ARG_NONE)

void
screenshot()
{
	[Cube.sharedInstance screenshot];
}
COMMAND(screenshot, ARG_NONE)

Modified src/clientgame.mm from [7f736a9f83] to [48319d1ae1].

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
23
24
25
26
27
28
29


30
31
32
33
34
35
36







-
-







VARP(sensitivityscale, 1, 1, 10000);
VARP(invmouse, 0, 0, 1);

int lastmillis = 0;
int curtime = 10;
OFString *clientmap;

extern int framesinmap;

OFString *
getclientmap()
{
	return clientmap;
}

void
515
516
517
518
519
520
521
522

523
524
525
526
513
514
515
516
517
518
519

520
521
522
523
524







-
+




	if (editmode)
		toggleedit();
	setvar(@"gamespeed", 100);
	setvar(@"fog", 180);
	setvar(@"fogcolour", 0x8099B3);
	showscores(false);
	intermission = false;
	framesinmap = 0;
	Cube.sharedInstance.framesInMap = 0;
	conoutf(@"game mode is %s", modestr(gamemode));
}

COMMANDN(map, changemap, ARG_1STR)

Modified src/console.mm from [45e0d06f5e] to [d534e6476d].

128
129
130
131
132
133
134
135

136
137
138
139
140
141
142
128
129
130
131
132
133
134

135
136
137
138
139
140
141
142







-
+







{
	saycommandon = (init != NULL);
	if (saycommandon)
		SDL_StartTextInput();
	else
		SDL_StopTextInput();
	if (!editmode)
		keyrepeat = saycommandon;
		Cube.sharedInstance.repeatsKeys = saycommandon;
	if (!init)
		init = "";
	strcpy_s(commandbuf, init);
}
COMMAND(saycommand, ARG_VARI)

void

Modified src/cube.h from [ff82afe0dc] to [25a73b021e].

1
2
3




4
5
6
7




8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22



+
+
+
+




+
+
+
+







// one big bad include file for the whole engine... nasty!

#import <ObjFW/ObjFW.h>

#define gamma gamma__
#include <SDL2/SDL.h>
#undef gamma

#include "tools.h"

@interface Cube : OFObject <OFApplicationDelegate>
@property (class, readonly, nonatomic) Cube *sharedInstance;
@property (readonly, nonatomic) SDL_Window *window;
@property (nonatomic) bool repeatsKeys;
@property (nonatomic) int framesInMap;
@end

enum // block types, order matters!
{
	SOLID = 0, // entirely solid cube [only specifies wtex]
	CORNER,    // half full corner of a wall
	FHF,       // floor heightfield using neighbour vdelta values

Modified src/editing.mm from [693ad7f294] to [78d08b56c5].

65
66
67
68
69
70
71
72

73
74
75
76
77
78
79
65
66
67
68
69
70
71

72
73
74
75
76
77
78
79







-
+







		                 // edited
		player1->health = 100;
		if (m_classicsp)
			monsterclear(); // all monsters back at their spawns for
			                // editing
		projreset();
	}
	keyrepeat = editmode;
	Cube.sharedInstance.repeatsKeys = editmode;
	selset = false;
	editing = editmode;
}
COMMANDN(edittoggle, toggleedit, ARG_NONE)

void
correctsel() // ensures above invariant

Modified src/protos.h from [ec095dab28] to [17a165017a].

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
138
139
140
141
142
143
144


145
146
147
148
149
150
151







-
-







// worldocull
extern void computeraytable(float vx, float vy);
extern int isoccluded(float vx, float vy, float cx, float cy, float csize);

// main
extern void fatal(OFString *s, OFString *o = @"");
extern void *alloc(int s);
extern SDL_Window *window;
extern bool keyrepeat;

// rendertext
extern void draw_text(char *str, int left, int top, int gl_num);
extern void draw_textf(char *fstr, int left, int top, int gl_num, ...);
extern int text_width(char *str);
extern void draw_envbox(int t, int fogdist);

Modified src/rendergl.mm from [deb96ccb7b] to [cefc423876].

273
274
275
276
277
278
279
280


281
282
283
284
285
286
287
273
274
275
276
277
278
279

280
281
282
283
284
285
286
287
288







-
+
+








VARFP(gamma, 30, 100, 300, {
	float f = gamma / 100.0f;
	Uint16 ramp[256];

	SDL_CalculateGammaRamp(f, ramp);

	if (SDL_SetWindowGammaRamp(window, ramp, ramp, ramp) == -1) {
	if (SDL_SetWindowGammaRamp(
	        Cube.sharedInstance.window, ramp, ramp, ramp) == -1) {
		conoutf(
		    @"Could not set gamma (card/driver doesn't support it?)");
		conoutf(@"sdl: %s", SDL_GetError());
	}
})

void