Cube  Diff

Differences From Artifact [e315f901d3]:

To Artifact [6d1422bea1]:


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







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

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

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

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








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

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

-
+

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

-
-
-
+
+
+

-
-
+
+

-
+

-
+
-







	if (qsphere)
		gluDeleteQuadric(qsphere);
}

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

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

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

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

#if 0
		loopi(s->w * s->h * 3)
		{
			uchar *p = (uchar *)s->pixels + i;
			*p = 255 - *p;
		}
#endif
		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);
	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);
	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 %@",
			    IRI.string); // for voodoo cards under linux
			scaledimg = OFAllocMemory(1, *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 %@",
		    IRI.string); // for voodoo cards under linux
		scaledimg = OFAllocMemory(1, *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

static const int MAXTEX = 1000;
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
188
189
190
191
192
193
194


195
196


197
198
199

200



201
202

203
204
205
206
207
208
209







-
-
+

-
-
+
+

-
+
-
-
-
+
+
-







	curtexnum = 0;
}
COMMAND(texturereset, ARG_NONE)

void
texture(OFString *aframe, OFString *name)
{
	@autoreleasepool {
		int num = curtexnum++, frame = aframe.cube_intValue;
	int num = curtexnum++, frame = aframe.cube_intValue;

		if (num < 0 || num >= 256 || frame < 0 || frame >= MAXFRAMES)
			return;
	if (num < 0 || num >= 256 || frame < 0 || frame >= MAXFRAMES)
		return;

		mapping[num][frame] = 1;
	mapping[num][frame] = 1;
		mapname[num][frame] =
		    [name stringByReplacingOccurrencesOfString:@"\\"
		                                    withString:@"/"];
	mapname[num][frame] = [name stringByReplacingOccurrencesOfString:@"\\"
	                                                      withString:@"/"];
	}
}
COMMAND(texture, ARG_2STR)

int
lookuptexture(int tex, int *xs, int *ys)
{
	int frame = 0; // other frames?
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
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







-
-
-
+
+

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








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

	int tnum = curtex + FIRSTTEX;
	texname[curtex] = mapname[tex][frame];

	@autoreleasepool {
		OFString *path =
		    [OFString stringWithFormat:@"packages/%@", texname[curtex]];
	OFString *path =
	    [OFString stringWithFormat:@"packages/%@", texname[curtex]];

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