Cube  Check-in [12641927d9]

Overview
Comment:Convert texture pixel format if necessary

This makes the game run on modern macOS.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 12641927d964516871cccb92bc0171dab7fd3239212538f480b617951e7f9e6c
User & Date: js on 2025-03-05 01:15:40
Other Links: manifest | tags
Context
2025-03-05
21:29
Clean up file handling check-in: 3d55e077f7 user: js tags: trunk
01:32
Default to desktop resolution check-in: fd3bba5332 user: js tags: trunk
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
Changes

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

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







-
+





-
+


+
-
+

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











+





+
-
+

+






+
-
+



+


+

+

-
+







};

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

bool
installtex(int tnum, char *texname, int &xs, int &ys, bool clamp)
{
	SDL_Surface *s = IMG_Load(texname);
	if (!s) {
	if (s == NULL) {
		conoutf(@"couldn't load texture %s", texname);
		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",
			    texname);
			return false;
		}

		@try {
			SDL_Surface *converted =
			    SDL_ConvertSurface(s, format, 0);
			if (converted == NULL) {
		conoutf(@"texture must be 24bpp: %s", texname);
		return false;
	};
				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; };
	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;
	}
	};

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

	SDL_FreeSurface(s);

	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;
int texx[MAXTEX]; // ( loaded texture ) -> ( name, size )