Cube  Check-in [3d55e077f7]

Overview
Comment:Clean up file handling
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3d55e077f74fc1c036e856cc4fdf0ae3c308c58f7d37eb9392f1a15f10cc237d
User & Date: js on 2025-03-05 21:29:54
Other Links: manifest | tags
Context
2025-03-05
21:55
Clean up console output functions check-in: 003b06901f user: js tags: trunk
21:29
Clean up file handling check-in: 3d55e077f7 user: js tags: trunk
01:15
Convert texture pixel format if necessary check-in: 12641927d9 user: js tags: trunk
Changes

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

1
2
3
4
5
6
7
8
9
10
11

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











+







// main.cpp: initialisation & main loop

#include "cube.h"

OF_APPLICATION_DELEGATE(Cube)

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

@implementation Cube {
	int _width, _height;
	OFIRI *_userDataIRI;
}

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

112
113
114
115
116
117
118


119
120
121
122
123
124
125
126
127
128


































129
130
131
132
133
134
135
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







+
+

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







	SDL_SetRelativeMouseMode(SDL_TRUE);
	SDL_ShowCursor(0);

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

	log("basetex");
	_gameDataIRI = [OFFileManager.defaultManager currentDirectoryIRI];
	_userDataIRI = [OFFileManager.defaultManager currentDirectoryIRI];
	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) ||
	    !installtex(8, path(newstring("data/martin/ball2.png")), xs, ys) ||
	    !installtex(9, path(newstring("data/martin/ball3.png")), xs, ys) ||
	    !installtex(4, path(newstring("data/explosion.jpg")), xs, ys) ||
	    !installtex(5, path(newstring("data/items.png")), xs, ys) ||
	    !installtex(1, path(newstring("data/crosshair.png")), xs, ys))
	if (!installtex(2,
	        [_userDataIRI IRIByAppendingPathComponent:@"data/newchars.png"],
	        &xs, &ys, false) ||
	    !installtex(3,
	        [_userDataIRI
	            IRIByAppendingPathComponent:@"data/martin/base.png"],
	        &xs, &ys, false) ||
	    !installtex(6,
	        [_userDataIRI
	            IRIByAppendingPathComponent:@"data/martin/ball1.png"],
	        &xs, &ys, false) ||
	    !installtex(7,
	        [_userDataIRI
	            IRIByAppendingPathComponent:@"data/martin/smoke.png"],
	        &xs, &ys, false) ||
	    !installtex(8,
	        [_userDataIRI
	            IRIByAppendingPathComponent:@"data/martin/ball2.png"],
	        &xs, &ys, false) ||
	    !installtex(9,
	        [_userDataIRI
	            IRIByAppendingPathComponent:@"data/martin/ball3.png"],
	        &xs, &ys, false) ||
	    !installtex(4,
	        [_userDataIRI
	            IRIByAppendingPathComponent:@"data/explosion.jpg"],
	        &xs, &ys, false) ||
	    !installtex(5,
	        [_userDataIRI IRIByAppendingPathComponent:@"data/items.png"],
	        &xs, &ys, false) ||
	    !installtex(1,
	        [_userDataIRI
	            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("cfg");
265
266
267
268
269
270
271


272
273
274








275
276
277
278
279
280
281
293
294
295
296
297
298
299
300
301



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316







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







				memcpy(dest,
				    (char *)image->pixels +
				        3 * _width * (_height - 1 - idx),
				    3 * _width);
				endianswap(dest, 3, _width);
			}

			@autoreleasepool {
				OFString *path = [OFString
			sprintf_sd(buf)(
			    "screenshots/screenshot_%d.bmp", lastmillis);
			SDL_SaveBMP(temp, path(buf));
				    stringWithFormat:
				        @"screenshots/screenshot_%d.bmp",
				    lastmillis];
				SDL_SaveBMP(temp,
				    [_userDataIRI
				        IRIByAppendingPathComponent:path]
				        .fileSystemRepresentation.UTF8String);
			}
			SDL_FreeSurface(temp);
		}

		SDL_FreeSurface(image);
	}
}

Modified src/clientextras.mm from [138c74a711] to [8bec7c55fe].

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







-
-
+
+








-
+


-


-
-
+
+

-







{
	@autoreleasepool {
		if (mapname.length > 0)
			save_world(mapname);
		changemap(mapname);
		mapname = getclientmap();
		int mapsize;
		uchar *mapdata = readmap(mapname.UTF8String, &mapsize);
		if (!mapdata)
		OFData *mapdata = readmap(mapname);
		if (mapdata == nil)
			return;
		ENetPacket *packet = enet_packet_create(
		    NULL, MAXTRANS + mapsize, ENET_PACKET_FLAG_RELIABLE);
		uchar *start = packet->data;
		uchar *p = start + 2;
		putint(p, SV_SENDMAP);
		sendstring(mapname.UTF8String, p);
		putint(p, mapsize);
		if (65535 - (p - start) < mapsize) {
		if (65535 - (p - start) < mapdata.count) {
			conoutf(
			    @"map %s is too large to send", mapname.UTF8String);
			free(mapdata);
			enet_packet_destroy(packet);
			return;
		};
		memcpy(p, mapdata, mapsize);
		}
		memcpy(p, mapdata.items, mapdata.count);
		p += mapsize;
		free(mapdata);
		*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
		enet_packet_resize(packet, p - start);
		sendpackettoserv(packet);
		conoutf(@"sending map %s to server...", mapname.UTF8String);
		sprintf_sd(msg)(
		    "[map %s uploaded to server, \"getmap\" to receive it]",
		    mapname.UTF8String);

Modified src/command.mm from [926958b6fe] to [9583e64f42].

553
554
555
556
557
558
559

560
561


562
563

564

565
566





567
568
569
570
571
572
573
553
554
555
556
557
558
559
560


561
562


563
564
565


566
567
568
569
570
571
572
573
574
575
576
577







+
-
-
+
+
-
-
+

+
-
-
+
+
+
+
+







		completeidx = 0;
}

bool
execfile(OFString *cfgfile)
{
	@autoreleasepool {
		OFMutableData *data;
		string s;
		strcpy_s(s, cfgfile.UTF8String);
		@try {
			data = [OFMutableData dataWithContentsOfFile:cfgfile];
		char *buf = loadfile(path(s), NULL);
		if (!buf)
		} @catch (id e) {
			return false;
		}
		execute(buf);
		free(buf);

		// Ensure \0 termination.
		[data addItem:""];

		execute((char *)data.mutableItems);
		return true;
	}
}

void
exec(OFString *cfgfile)
{

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

1
2
3
4
5
6
7
8
9
10
11
12
13

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













+







// 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 (readonly, nonatomic) OFIRI *gameDataIRI;
@property (nonatomic) bool repeatsKeys;
@property (nonatomic) int framesInMap;
@end

enum // block types, order matters!
{
	SOLID = 0, // entirely solid cube [only specifies wtex]

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

40
41
42
43
44
45
46
47

48
49
50
51
52

53
54
55
56
57
58
59
40
41
42
43
44
45
46

47

48
49
50

51
52
53
54
55
56
57
58







-
+
-



-
+







extern char *getservername(int n);
extern void writeservercfg();

// rendergl
extern void gl_init(int w, int h);
extern void cleangl();
extern void gl_drawframe(int w, int h, float curfps);
extern bool installtex(
extern bool installtex(int tnum, OFIRI *IRI, int *xs, int *ys, bool clamp);
    int tnum, char *texname, int &xs, int &ys, bool clamp = false);
extern void mipstats(int a, int b, int c);
extern void vertf(float v1, float v2, float v3, sqr *ls, float t1, float t2);
extern void addstrip(int tex, int start, int n);
extern int lookuptexture(int tex, int &xs, int &ys);
extern int lookuptexture(int tex, int *xs, int *ys);

// rendercubes
extern void resetcubes();
extern void render_flat(int tex, int x, int y, int size, int h, sqr *l1,
    sqr *l2, sqr *l3, sqr *l4, bool isceil);
extern void render_flatdelta(int wtex, int x, int y, int size, float h1,
    float h2, float h3, float h4, sqr *l1, sqr *l2, sqr *l3, sqr *l4,
181
182
183
184
185
186
187
188

189
190
191
192
193
194
195
180
181
182
183
184
185
186

187
188
189
190
191
192
193
194







-
+







    int type, int fade, OFVector3D &from, OFVector3D &to);
extern void render_particles(int time);

// worldio
extern void save_world(OFString *fname);
extern void load_world(char *mname);
extern void writemap(char *mname, int msize, uchar *mdata);
extern uchar *readmap(const char *mname, int *msize);
extern OFData *readmap(OFString *mname);
extern void loadgamerest();
extern void incomingdemodata(uchar *buf, int len, bool extras = false);
extern void demoplaybackstep();
extern void stop();
extern void stopifrecording();
extern void demodamage(int damage, OFVector3D &o);
extern void demoblend(int damage);

Modified src/rendercubes.mm from [bb9341fd13] to [3909c7f58d].

99
100
101
102
103
104
105
106

107
108
109
110
111
112
113
99
100
101
102
103
104
105

106
107
108
109
110
111
112
113







-
+







	vertcheck();
	if (showm) {
		l3 = l1 = &sbright;
		l4 = l2 = &sdark;
	};

	int sx, sy;
	int gltex = lookuptexture(wtex, sx, sy);
	int gltex = lookuptexture(wtex, &sx, &sy);
	float xf = TEXTURESCALE / sx;
	float yf = TEXTURESCALE / sy;
	float xs = size * xf;
	float ys = size * yf;
	float xo = xf * x;
	float yo = yf * y;

180
181
182
183
184
185
186
187

188
189
190
191
192
193
194
180
181
182
183
184
185
186

187
188
189
190
191
192
193
194







-
+







	vertcheck();
	if (showm) {
		l3 = l1 = &sbright;
		l4 = l2 = &sdark;
	};

	int sx, sy;
	int gltex = lookuptexture(wtex, sx, sy);
	int gltex = lookuptexture(wtex, &sx, &sy);
	float xf = TEXTURESCALE / sx;
	float yf = TEXTURESCALE / sy;
	float xs = size * xf;
	float ys = size * yf;
	float xo = xf * x;
	float yo = yf * y;

234
235
236
237
238
239
240
241

242
243
244
245
246
247
248
249
250

251
252
253
254
255
256
257
234
235
236
237
238
239
240

241
242
243
244
245
246
247
248
249

250
251
252
253
254
255
256
257







-
+








-
+







render_2tris(sqr *h, sqr *s, int x1, int y1, int x2, int y2, int x3, int y3,
    sqr *l1, sqr *l2, sqr *l3) // floor/ceil tris on a corner cube
{
	stripend();
	vertcheck();

	int sx, sy;
	int gltex = lookuptexture(h->ftex, sx, sy);
	int gltex = lookuptexture(h->ftex, &sx, &sy);
	float xf = TEXTURESCALE / sx;
	float yf = TEXTURESCALE / sy;

	vertf((float)x1, h->floor, (float)y1, l1, xf * x1, yf * y1);
	vertf((float)x2, h->floor, (float)y2, l2, xf * x2, yf * y2);
	vertf((float)x3, h->floor, (float)y3, l3, xf * x3, yf * y3);
	addstrip(gltex, curvert - 3, 3);

	gltex = lookuptexture(h->ctex, sx, sy);
	gltex = lookuptexture(h->ctex, &sx, &sy);
	xf = TEXTURESCALE / sx;
	yf = TEXTURESCALE / sy;

	vertf((float)x3, h->ceil, (float)y3, l3, xf * x3, yf * y3);
	vertf((float)x2, h->ceil, (float)y2, l2, xf * x2, yf * y2);
	vertf((float)x1, h->ceil, (float)y1, l1, xf * x1, yf * y1);
	addstrip(gltex, curvert - 3, 3);
288
289
290
291
292
293
294
295

296
297
298
299
300
301
302
288
289
290
291
292
293
294

295
296
297
298
299
300
301
302







-
+







	vertcheck();
	if (showm) {
		l1 = &sbright;
		l2 = &sdark;
	};

	int sx, sy;
	int gltex = lookuptexture(wtex, sx, sy);
	int gltex = lookuptexture(wtex, &sx, &sy);
	float xf = TEXTURESCALE / sx;
	float yf = TEXTURESCALE / sy;
	float xs = size * xf;
	float xo = xf * (x1 == x2 ? min(y1, y2) : min(x1, x2));

	if (!flip) {
		vertf((float)x2, ceil2, (float)y2, l2, xo + xs, -yf * ceil2);
348
349
350
351
352
353
354
355

356
357
358
359
360
361
362
348
349
350
351
352
353
354

355
356
357
358
359
360
361
362







-
+







	if (wx1 < 0)
		return nquads;

	glDepthMask(GL_FALSE);
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_SRC_COLOR);
	int sx, sy;
	glBindTexture(GL_TEXTURE_2D, lookuptexture(DEFAULT_LIQUID, sx, sy));
	glBindTexture(GL_TEXTURE_2D, lookuptexture(DEFAULT_LIQUID, &sx, &sy));

	wx1 &= ~(watersubdiv - 1);
	wy1 &= ~(watersubdiv - 1);

	float xf = TEXTURESCALE / sx;
	float yf = TEXTURESCALE / sy;
	float xs = watersubdiv * xf;

Modified src/renderextras.mm from [4e49ca5579] to [6ccf3a9b83].

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







+


-
+
+
+



+
+
-
-
+
+

-
+
+
+
+


+







}

void
loadsky(OFString *basename)
{
	@autoreleasepool {
		static OFString *lastsky = @"";

		if ([lastsky isEqual:basename])
			return;
		char *side[] = {"ft", "bk", "lf", "rt", "dn", "up"};

		static const OFString *side[] = {
		    @"ft", @"bk", @"lf", @"rt", @"dn", @"up"};
		int texnum = 14;
		loopi(6)
		{
			OFString *path =
			    [OFString stringWithFormat:@"packages/%@_%@.jpg",
			sprintf_sd(name)(
			    "packages/%s_%s.jpg", basename.UTF8String, side[i]);
			              basename, side[i]];

			int xs, ys;
			if (!installtex(texnum + i, path(name), xs, ys, true))
			if (!installtex(texnum + i,
			        [Cube.sharedInstance.gameDataIRI
			            IRIByAppendingPathComponent:path],
			        &xs, &ys, true))
				conoutf(@"could not load sky textures");
		}

		lastsky = basename;
	}
}
COMMAND(loadsky, ARG_1STR)

float cursordepth = 0.9f;
GLint viewport[4];

Modified src/rendergl.mm from [154658a1d9] to [86ca5a8f24].

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







-
+

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

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

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

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

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

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

-
+

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

-
-
-
+
+
+

-
-
+
+

-
+

-
+
+







cleangl()
{
	if (qsphere)
		gluDeleteQuadric(qsphere);
}

bool
installtex(int tnum, char *texname, int &xs, int &ys, bool clamp)
installtex(int tnum, OFIRI *IRI, int *xs, int *ys, bool clamp)
{
	@autoreleasepool {
	SDL_Surface *s = IMG_Load(texname);
	if (s == NULL) {
		conoutf(@"couldn't load texture %s", texname);
		return false;
	}
		SDL_Surface *s =
		    IMG_Load(IRI.fileSystemRepresentation.UTF8String);
		if (s == NULL) {
			conoutf(
			    @"couldn't load texture %s", IRI.string.UTF8String);
			return false;
		}

	if (s->format->BitsPerPixel != 24) {
		SDL_PixelFormat *format =
		    SDL_AllocFormat(SDL_PIXELFORMAT_RGB24);
		if (format == NULL) {
			conoutf(@"texture cannot be converted to 24bpp: %s",
		if (s->format->BitsPerPixel != 24) {
			SDL_PixelFormat *format =
			    SDL_AllocFormat(SDL_PIXELFORMAT_RGB24);
			if (format == NULL) {
				conoutf(
				    @"texture cannot be converted to 24bpp: %s",
			    texname);
			return false;
		}
				    IRI.string.UTF8String);
				return false;
			}

		@try {
			SDL_Surface *converted =
			    SDL_ConvertSurface(s, format, 0);
			if (converted == NULL) {
				conoutf(
				    @"texture cannot be converted to 24bpp: %s",
			@try {
				SDL_Surface *converted =
				    SDL_ConvertSurface(s, format, 0);
				if (converted == NULL) {
					conoutf(@"texture cannot be converted "
					        @"to 24bpp: %s",
				    texname);
				return false;
			}
					    IRI.string.UTF8String);
					return false;
				}

			SDL_FreeSurface(s);
			s = converted;
		} @finally {
			SDL_FreeFormat(format);
		}
	}
				SDL_FreeSurface(s);
				s = converted;
			} @finally {
				SDL_FreeFormat(format);
			}
		}

	// loopi(s->w*s->h*3) { uchar *p = (uchar *)s->pixels+i; *p = 255-*p; };
	glBindTexture(GL_TEXTURE_2D, tnum);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
	    clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
	    clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
	    GL_LINEAR_MIPMAP_LINEAR); // NEAREST);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		// loopi(s->w*s->h*3) { uchar *p = (uchar *)s->pixels+i; *p =
		// 255-*p; };
		glBindTexture(GL_TEXTURE_2D, tnum);
		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
		    clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
		    clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
		glTexParameteri(
		    GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
		    GL_LINEAR_MIPMAP_LINEAR); // NEAREST);
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	xs = s->w;
	ys = s->h;
	while (xs > glmaxtexsize || ys > glmaxtexsize) {
		xs /= 2;
		ys /= 2;
	}
		*xs = s->w;
		*ys = s->h;
		while (*xs > glmaxtexsize || *ys > glmaxtexsize) {
			*xs /= 2;
			*ys /= 2;
		}

	void *scaledimg = s->pixels;
		void *scaledimg = s->pixels;

	if (xs != s->w) {
		conoutf(@"warning: quality loss: scaling %s",
		    texname); // for voodoo cards under linux
		scaledimg = alloc(xs * ys * 3);
		gluScaleImage(GL_RGB, s->w, s->h, GL_UNSIGNED_BYTE, s->pixels,
		    xs, ys, GL_UNSIGNED_BYTE, scaledimg);
	}
		if (*xs != s->w) {
			conoutf(@"warning: quality loss: scaling %s",
			    IRI.string
			        .UTF8String); // for voodoo cards under linux
			scaledimg = alloc(*xs * *ys * 3);
			gluScaleImage(GL_RGB, s->w, s->h, GL_UNSIGNED_BYTE,
			    s->pixels, *xs, *ys, GL_UNSIGNED_BYTE, scaledimg);
		}

	if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, xs, ys, GL_RGB,
	        GL_UNSIGNED_BYTE, scaledimg))
		fatal(@"could not build mipmaps");
		if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, *xs, *ys, GL_RGB,
		        GL_UNSIGNED_BYTE, scaledimg))
			fatal(@"could not build mipmaps");

	if (xs != s->w)
		free(scaledimg);
		if (*xs != s->w)
			free(scaledimg);

	SDL_FreeSurface(s);
		SDL_FreeSurface(s);

	return true;
		return true;
	}
}

// management of texture slots
// each texture slot can have multople texture frames, of which currently only
// the first is used additional frames can be used for various shaders

const int MAXTEX = 1000;
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
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







-





-
+





-
-
+
+

-
-
-
-
+
+
+
+






-
-
+
+

-
-
+
+







+
+
-
+

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







	@autoreleasepool {
		int num = curtexnum++, frame = (int)aframe.longLongValue;
		if (num < 0 || num >= 256 || frame < 0 || frame >= MAXFRAMES)
			return;
		mapping[num][frame] = 1;
		char *n = mapname[num][frame];
		strcpy_s(n, name.UTF8String);
		path(n);
	}
}
COMMAND(texture, ARG_2STR)

int
lookuptexture(int tex, int &xs, int &ys)
lookuptexture(int tex, int *xs, int *ys)
{
	int frame = 0; // other frames?
	int tid = mapping[tex][frame];

	if (tid >= FIRSTTEX) {
		xs = texx[tid - FIRSTTEX];
		ys = texy[tid - FIRSTTEX];
		*xs = texx[tid - FIRSTTEX];
		*ys = texy[tid - FIRSTTEX];
		return tid;
	};

	xs = ys = 16;
	if (!tid)
	}

	*xs = *ys = 16;
	if (tid == 0)
		return 1; // crosshair :)

	loopi(curtex) // lazily happens once per "texture" command, basically
	{
		if (strcmp(mapname[tex][frame], texname[i]) == 0) {
			mapping[tex][frame] = tid = i + FIRSTTEX;
			xs = texx[i];
			ys = texy[i];
			*xs = texx[i];
			*ys = texy[i];
			return tid;
		};
	};
		}
	}

	if (curtex == MAXTEX)
		fatal(@"loaded too many textures");

	int tnum = curtex + FIRSTTEX;
	strcpy_s(texname[curtex], mapname[tex][frame]);

	@autoreleasepool {
		OFString *path =
	sprintf_sd(name)("packages%c%s", PATHDIV, texname[curtex]);
		    [OFString stringWithFormat:@"packages/%s", texname[curtex]];

	if (installtex(tnum, name, xs, ys)) {
		mapping[tex][frame] = tnum;
		texx[curtex] = xs;
		texy[curtex] = ys;
		curtex++;
		return tnum;
	} else {
		return mapping[tex][frame] = FIRSTTEX; // temp fix
		if (installtex(tnum,
		        [Cube.sharedInstance.gameDataIRI
		            IRIByAppendingPathComponent:path],
		        xs, ys, false)) {
			mapping[tex][frame] = tnum;
			texx[curtex] = *xs;
			texy[curtex] = *ys;
			curtex++;
			return tnum;
		} else {
			return mapping[tex][frame] = FIRSTTEX; // temp fix
	};
};
		}
	}
}

void
setupworld()
{
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
414
415
416
417
418
419
420
421

422
423
424
425
426
427
428
427
428
429
430
431
432
433

434
435
436
437
438
439
440
441







-
+







	glMatrixMode(GL_MODELVIEW);

	transplayer();

	glEnable(GL_TEXTURE_2D);

	int xs, ys;
	skyoglid = lookuptexture(DEFAULT_SKY, xs, ys);
	skyoglid = lookuptexture(DEFAULT_SKY, &xs, &ys);

	resetcubes();

	curvert = 0;
	strips.setsize(0);

	render_world(player1->o.x, player1->o.y, player1->o.z,

Modified src/rendermd2.mm from [1644c7ed89] to [d83bc98847].

38
39
40
41
42
43
44
45

46
47
48
49
50
51
52
38
39
40
41
42
43
44

45
46
47
48
49
50
51
52







-
+







}

@property (nonatomic) MapModelInfo *mmi;
@property (copy, nonatomic) OFString *loadname;
@property (nonatomic) int mdlnum;
@property (nonatomic) bool loaded;

- (bool)loadWithPath:(char *)filename;
- (bool)loadWithIRI:(OFIRI *)IRI;
- (void)renderWithLight:(OFVector3D &)light
                  frame:(int)frame
                  range:(int)range
                      x:(float)x
                      y:(float)y
                      z:(float)z
                    yaw:(float)yaw
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
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







-
+

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

-
-
+
+
+
+

-
-
+
+

-
-
-
+
+
+

-
-
+
+
+

-
-
+
+
+

-
-
-
+
+
+

-
-
-
-
+
+
+
+

-
-
-
-
-
+
+
+
+
+

-
+

-
-
+
+

-
+
+







{
	if (_glCommands)
		delete[] _glCommands;
	if (_frames)
		delete[] _frames;
}

- (bool)loadWithPath:(char *)filename
- (bool)loadWithIRI:(OFIRI *)IRI
{
	FILE *file;
	md2_header header;

	if ((file = fopen(filename, "rb")) == NULL)
		return false;
	@autoreleasepool {
		OFSeekableStream *stream;
		@try {
			stream = (OFSeekableStream *)[[OFIRIHandler
			    handlerForIRI:IRI] openItemAtIRI:IRI mode:@"r"];
		} @catch (id e) {
			return false;
		}

		if (![stream isKindOfClass:[OFSeekableStream class]])
			return false;

	fread(&header, sizeof(md2_header), 1, file);
	endianswap(&header, sizeof(int), sizeof(md2_header) / sizeof(int));
		md2_header header;
		[stream readIntoBuffer:&header exactLength:sizeof(md2_header)];
		endianswap(
		    &header, sizeof(int), sizeof(md2_header) / sizeof(int));

	if (header.magic != 844121161 || header.version != 8)
		return false;
		if (header.magic != 844121161 || header.version != 8)
			return false;

	_frames = new char[header.frameSize * header.numFrames];
	if (_frames == NULL)
		return false;
		_frames = new char[header.frameSize * header.numFrames];
		if (_frames == NULL)
			return false;

	fseek(file, header.offsetFrames, SEEK_SET);
	fread(_frames, header.frameSize * header.numFrames, 1, file);
		[stream seekToOffset:header.offsetFrames whence:OFSeekSet];
		[stream readIntoBuffer:_frames
		           exactLength:header.frameSize * header.numFrames];

	for (int i = 0; i < header.numFrames; ++i)
		endianswap(_frames + i * header.frameSize, sizeof(float), 6);
		for (int i = 0; i < header.numFrames; ++i)
			endianswap(
			    _frames + i * header.frameSize, sizeof(float), 6);

	_glCommands = new int[header.numGlCommands];
	if (_glCommands == NULL)
		return false;
		_glCommands = new int[header.numGlCommands];
		if (_glCommands == NULL)
			return false;

	fseek(file, header.offsetGlCommands, SEEK_SET);
	fread(_glCommands, header.numGlCommands * sizeof(int), 1, file);

	endianswap(_glCommands, sizeof(int), header.numGlCommands);
		[stream seekToOffset:header.offsetGlCommands whence:OFSeekSet];
		[stream readIntoBuffer:_glCommands
		           exactLength:header.numGlCommands * sizeof(int)];
		endianswap(_glCommands, sizeof(int), header.numGlCommands);

	_numFrames = header.numFrames;
	_numGlCommands = header.numGlCommands;
	_frameSize = header.frameSize;
	_numTriangles = header.numTriangles;
	_numVerts = header.numVertices;
		_numFrames = header.numFrames;
		_numGlCommands = header.numGlCommands;
		_frameSize = header.frameSize;
		_numTriangles = header.numTriangles;
		_numVerts = header.numVertices;

	fclose(file);
		[stream close];

	_mverts = new OFVector3D *[_numFrames];
	loopj(_numFrames) _mverts[j] = NULL;
		_mverts = new OFVector3D *[_numFrames];
		loopj(_numFrames) _mverts[j] = NULL;

	return true;
		return true;
	}
}

float
snap(int sn, float f)
{
	return sn ? (float)(((int)(f + sn * 0.5f)) & (~(sn - 1))) : f;
}
231
232
233
234
235
236
237

238
239
240
241
242









243


244
245

246
247
248
249
250
251
252
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







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

-
+







const int FIRSTMDL = 20;

void
delayedload(MD2 *m)
{
	if (!m.loaded) {
		@autoreleasepool {
			OFString *path = [OFString
			sprintf_sd(name1)("packages/models/%s/tris.md2",
			    m.loadname.UTF8String);
			if (![m loadWithPath:path(name1)])
				fatal(@"loadmodel: ", @(name1));
			sprintf_sd(name2)("packages/models/%s/skin.jpg",
			    stringWithFormat:@"packages/models/%@", m.loadname];
			OFIRI *baseIRI = [Cube.sharedInstance.gameDataIRI
			    IRIByAppendingPathComponent:path];

			OFIRI *IRI1 =
			    [baseIRI IRIByAppendingPathComponent:@"tris.md2"];
			if (![m loadWithIRI:IRI1])
				fatal(@"loadmodel: ", IRI1.string);

			    m.loadname.UTF8String);
			OFIRI *IRI2 =
			    [baseIRI IRIByAppendingPathComponent:@"skin.jpg"];
			int xs, ys;
			installtex(FIRSTMDL + m.mdlnum, path(name2), xs, ys);
			installtex(FIRSTMDL + m.mdlnum, IRI2, &xs, &ys, false);
			m.loaded = true;
		}
	}
}

int modelnum = 0;

310
311
312
313
314
315
316
317

318
319
320
321
322
323
324
327
328
329
330
331
332
333

334
335
336
337
338
339
340
341







-
+







	if (isoccluded(player1->o.x, player1->o.y, x - rad, z - rad, rad * 2))
		return;

	delayedload(m);

	int xs, ys;
	glBindTexture(GL_TEXTURE_2D,
	    tex ? lookuptexture(tex, xs, ys) : FIRSTMDL + m.mdlnum);
	    tex ? lookuptexture(tex, &xs, &ys) : FIRSTMDL + m.mdlnum);

	int ix = (int)x;
	int iy = (int)z;
	OFVector3D light = OFMakeVector3D(1, 1, 1);

	if (!OUTBORD(ix, iy)) {
		sqr *s = S(ix, iy);

Modified src/sound.mm from [ee786c999c] to [b06342c3fd].

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







-
-
-
+
+
+
+
+

-
+
+




-
+
+



+
-
+







music(OFString *name)
{
	if (nosound)
		return;
	stopsound();
	if (soundvol && musicvol) {
		@autoreleasepool {
			string sn;
			strcpy_s(sn, "packages/");
			strcat_s(sn, name.UTF8String);
			OFString *path =
			    [OFString stringWithFormat:@"packages/%@", name];
			OFIRI *IRI = [Cube.sharedInstance.gameDataIRI
			    IRIByAppendingPathComponent:path];

#ifdef USE_MIXER
			if (mod = Mix_LoadMUS(path(sn))) {
			if (mod = Mix_LoadMUS(
			        IRI.fileSystemRepresentation.UTF8String)) {
				Mix_PlayMusic(mod, -1);
				Mix_VolumeMusic((musicvol * MAXVOL) / 255);
			}
#else
			if (mod = FMUSIC_LoadSong(path(sn))) {
			if (mod = FMUSIC_LoadSong(
			        IRI.fileSystemRepresentation.UTF8String)) {
				FMUSIC_PlaySong(mod);
				FMUSIC_SetMasterVolume(mod, musicvol);
			} else if (stream = FSOUND_Stream_Open(
			               IRI.fileSystemRepresentation.UTF8String,
			               path(sn), FSOUND_LOOP_NORMAL, 0, 0)) {
			               FSOUND_LOOP_NORMAL, 0, 0)) {
				int chan =
				    FSOUND_Stream_Play(FSOUND_FREE, stream);
				if (chan >= 0) {
					FSOUND_SetVolume(
					    chan, (musicvol * MAXVOL) / 255);
					FSOUND_SetPaused(chan, false);
				}
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
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







-
-
+
+

+
-
+
+
+


-
+
+

-
-
+
+
+



-
+
+

-
-
+
+














-
+







	lastsoundmillis = lastmillis;
	if (soundsatonce > 5)
		return; // avoid bursts of sounds with heavy packetloss
		        // and in sp
	if (n < 0 || n >= samples.length()) {
		conoutf(@"unregistered sound: %d", n);
		return;
	};

	}

	if (!samples[n]) {
		OFString *path = [OFString
		sprintf_sd(buf)("packages/sounds/%s.wav", snames[n]);
		    stringWithFormat:@"packages/sounds/%s.wav", snames[n]];
		OFIRI *IRI = [Cube.sharedInstance.gameDataIRI
		    IRIByAppendingPathComponent:path];

#ifdef USE_MIXER
		samples[n] = Mix_LoadWAV(path(buf));
		samples[n] =
		    Mix_LoadWAV(IRI.fileSystemRepresentation.UTF8String);
#else
		samples[n] =
		    FSOUND_Sample_Load(n, path(buf), FSOUND_LOOP_OFF, 0, 0);
		samples[n] = FSOUND_Sample_Load(n,
		    IRI.fileSystemRepresentation.UTF8String, FSOUND_LOOP_OFF, 0,
		    0);
#endif

		if (!samples[n]) {
			conoutf(@"failed to load sample: %s", buf);
			conoutf(@"failed to load sample: %s",
			    IRI.string.UTF8String);
			return;
		};
	};
		}
	}

#ifdef USE_MIXER
	int chan = Mix_PlayChannel(-1, samples[n], 0);
#else
	int chan = FSOUND_PlaySoundEx(FSOUND_FREE, samples[n], NULL, true);
#endif
	if (chan < 0)
		return;
	if (loc)
		newsoundloc(chan, loc);
	updatechanvol(chan, loc);
#ifndef USE_MIXER
	FSOUND_SetPaused(chan, false);
#endif
};
}

void
sound(int n)
{
	playsound(n, NULL);
}
COMMAND(sound, ARG_1INT)

Modified src/tools.h from [fecbc01816] to [257c91a25c].

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
118
119
120
121
122
123
124


125
126
127
128
129
130
131







-
-







		formatstring(d, fmt, ap);                                      \
		va_end(ap);                                                    \
	}
#define sprintf_sdv(d, fmt) sprintf_sdlv(d, fmt, fmt)

#define fast_f2nat(val) ((int)(val))

extern char *path(char *s);
extern char *loadfile(char *fn, int *size);
extern void endianswap(void *, int, int);

// memory pool that uses buckets and linear allocation for small objects
// VERY fast, and reasonably good memory reuse

struct pool {
	enum { POOLSIZE = 4096 }; // can be absolutely anything

Modified src/tools.mm from [bc3e2d9f84] to [06a94d32b1].

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
96
97
98
99
100
101
102
































103
104
105
106
107
108
109







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







{
	static pool *p = NULL;
	return p ? p : (p = new pool());
};

///////////////////////// misc tools ///////////////////////

char *
path(char *s)
{
	for (char *t = s; t = strpbrk(t, "/\\"); *t++ = PATHDIV)
		;
	return s;
};

char *
loadfile(char *fn, int *size)
{
	FILE *f = fopen(fn, "rb");
	if (!f)
		return NULL;
	fseek(f, 0, SEEK_END);
	int len = ftell(f);
	fseek(f, 0, SEEK_SET);
	char *buf = (char *)malloc(len + 1);
	if (!buf)
		return NULL;
	buf[len] = 0;
	size_t rlen = fread(buf, 1, len, f);
	fclose(f);
	if (len != rlen || len <= 0) {
		free(buf);
		return NULL;
	};
	if (size != NULL)
		*size = len;
	return buf;
};

void
endianswap(
    void *memory, int stride, int length) // little indians as storage format
{
	if (*((char *)&stride))
		return;
	loop(w, length) loop(i, stride / 2)

Modified src/weapon.mm from [53bbf8febc] to [32ce1b4cac].

1
2
3
4
5
6
7
8
9
10
11
12
13




14
15

16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9




10
11
12
13
14

15
16
17
18
19
20
21
22









-
-
-
-
+
+
+
+

-
+







// weapon.cpp: all shooting and effects code

#include "cube.h"

struct guninfo {
	short sound, attackdelay, damage, projspeed, part, kickamount;
	OFString *name;
};

const int MONSTERDAMAGEFACTOR = 4;
const int SGRAYS = 20;
const float SGSPREAD = 2;
OFVector3D sg[SGRAYS];
static const int MONSTERDAMAGEFACTOR = 4;
static const int SGRAYS = 20;
static const float SGSPREAD = 2;
static OFVector3D sg[SGRAYS];

guninfo guns[NUMGUNS] = {
static const guninfo guns[NUMGUNS] = {
    {S_PUNCH1, 250, 50, 0, 0, 1, @"fist"},
    {S_SG, 1400, 10, 0, 0, 20, @"shotgun"}, // *SGRAYS
    {S_CG, 100, 30, 0, 0, 7, @"chaingun"},
    {S_RLFIRE, 800, 120, 80, 0, 10, @"rocketlauncher"},
    {S_RIFLE, 1500, 100, 0, 0, 30, @"rifle"},
    {S_FLAUNCH, 200, 20, 50, 4, 1, @"fireball"},
    {S_ICEBALL, 200, 40, 30, 6, 1, @"iceball"},

Modified src/worldio.mm from [04f91a12de] to [96d53bd5cb].

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



31
32
33
34
35
36
37







-
-
-







		strcpy_s(mapname, name);
	}
	sprintf_s(cgzname)("packages/%s/%s.cgz", pakname, mapname);
	sprintf_s(bakname)(
	    "packages/%s/%s_%d.BAK", pakname, mapname, lastmillis);
	sprintf_s(pcfname)("packages/%s/package.cfg", pakname);
	sprintf_s(mcfname)("packages/%s/%s.cfg", pakname, mapname);

	path(cgzname);
	path(bakname);
}

// the optimize routines below are here to reduce the detrimental effects of
// messy mapping by setting certain properties (vdeltas and textures) to
// neighbouring values wherever there is no visible difference. This allows the
// mipmapper to generate more efficient mips. the reason it is done on save is
// to reduce the amount spend in the mipmapper (as that is done in realtime).
133
134
135
136
137
138
139
140
141


142

143

144
145
146
147
148
149

150
151
152
153
154
155
156
130
131
132
133
134
135
136


137
138
139
140

141




142

143
144
145
146
147
148
149
150







-
-
+
+

+
-
+
-
-
-
-

-
+







		return;
	}
	fwrite(mdata, 1, msize, f);
	fclose(f);
	conoutf(@"wrote map %s as file %s", mname, cgzname);
}

uchar *
readmap(const char *mname, int *msize)
OFData *
readmap(OFString *mname)
{
	@autoreleasepool {
	setnames(mname);
		setnames(mname.UTF8String);
	uchar *mdata = (uchar *)loadfile(cgzname, msize);
	if (!mdata) {
		conoutf(@"could not read map %s", cgzname);
		return NULL;
	}
	return mdata;
	return [OFData dataWithContentsOfFile:mname];
}

// save map as .cgz file. uses 2 layers of compression: first does simple
// run-length encoding and leaves out data for certain kinds of cubes, then zlib
// removes the last bits of redundancy. Both passes contribute greatly to the
// miniscule map sizes.

358
359
360
361
362
363
364
365

366
367
368
369
370
371
372
352
353
354
355
356
357
358

359
360
361
362
363
364
365
366







-
+







		if (!SOLID(s))
			texuse[s->utex] = texuse[s->ftex] = texuse[s->ctex] = 1;
	}
	gzclose(f);
	calclight();
	settagareas();
	int xs, ys;
	loopi(256) if (texuse) lookuptexture(i, xs, ys);
	loopi(256) if (texuse) lookuptexture(i, &xs, &ys);
	conoutf(@"read map %s (%d milliseconds)", cgzname,
	    SDL_GetTicks() - lastmillis);
	conoutf(@"%s", hdr.maptitle);
	startmap(mname);
	loopl(256)
	{
		@autoreleasepool {