Convert texture pixel format if necessary

This makes the game run on modern macOS.

FossilOrigin-Name: 12641927d964516871cccb92bc0171dab7fd3239212538f480b617951e7f9e6c
This commit is contained in:
Jonathan Schleifer 2025-03-05 01:15:40 +00:00
parent 7ba16ed96a
commit c6eebefd77

View file

@ -70,20 +70,43 @@ cleangl()
{ {
if (qsphere) if (qsphere)
gluDeleteQuadric(qsphere); gluDeleteQuadric(qsphere);
}; }
bool bool
installtex(int tnum, char *texname, int &xs, int &ys, bool clamp) installtex(int tnum, char *texname, int &xs, int &ys, bool clamp)
{ {
SDL_Surface *s = IMG_Load(texname); SDL_Surface *s = IMG_Load(texname);
if (!s) { if (s == NULL) {
conoutf(@"couldn't load texture %s", texname); conoutf(@"couldn't load texture %s", texname);
return false; return false;
}; }
if (s->format->BitsPerPixel != 24) { if (s->format->BitsPerPixel != 24) {
conoutf(@"texture must be 24bpp: %s", texname); SDL_PixelFormat *format =
return false; SDL_AllocFormat(SDL_PIXELFORMAT_RGB24);
}; if (format == NULL) {
conoutf(@"texture cannot be converted to 24bpp: %s",
texname);
return false;
}
@try {
SDL_Surface *converted =
SDL_ConvertSurface(s, format, 0);
if (converted == NULL) {
conoutf(
@"texture cannot be converted to 24bpp: %s",
texname);
return false;
}
SDL_FreeSurface(s);
s = converted;
} @finally {
SDL_FreeFormat(format);
}
}
// loopi(s->w*s->h*3) { uchar *p = (uchar *)s->pixels+i; *p = 255-*p; }; // loopi(s->w*s->h*3) { uchar *p = (uchar *)s->pixels+i; *p = 255-*p; };
glBindTexture(GL_TEXTURE_2D, tnum); glBindTexture(GL_TEXTURE_2D, tnum);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@ -95,28 +118,35 @@ installtex(int tnum, char *texname, int &xs, int &ys, bool clamp)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR); // NEAREST); GL_LINEAR_MIPMAP_LINEAR); // NEAREST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
xs = s->w; xs = s->w;
ys = s->h; ys = s->h;
while (xs > glmaxtexsize || ys > glmaxtexsize) { while (xs > glmaxtexsize || ys > glmaxtexsize) {
xs /= 2; xs /= 2;
ys /= 2; ys /= 2;
}; }
void *scaledimg = s->pixels; void *scaledimg = s->pixels;
if (xs != s->w) { if (xs != s->w) {
conoutf(@"warning: quality loss: scaling %s", conoutf(@"warning: quality loss: scaling %s",
texname); // for voodoo cards under linux texname); // for voodoo cards under linux
scaledimg = alloc(xs * ys * 3); scaledimg = alloc(xs * ys * 3);
gluScaleImage(GL_RGB, s->w, s->h, GL_UNSIGNED_BYTE, s->pixels, gluScaleImage(GL_RGB, s->w, s->h, GL_UNSIGNED_BYTE, s->pixels,
xs, ys, GL_UNSIGNED_BYTE, scaledimg); xs, ys, GL_UNSIGNED_BYTE, scaledimg);
}; }
if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, xs, ys, GL_RGB, if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, xs, ys, GL_RGB,
GL_UNSIGNED_BYTE, scaledimg)) GL_UNSIGNED_BYTE, scaledimg))
fatal(@"could not build mipmaps"); fatal(@"could not build mipmaps");
if (xs != s->w) if (xs != s->w)
free(scaledimg); free(scaledimg);
SDL_FreeSurface(s); SDL_FreeSurface(s);
return true; return true;
}; }
// management of texture slots // management of texture slots
// each texture slot can have multople texture frames, of which currently only // each texture slot can have multople texture frames, of which currently only