File: NamedTextures.cpp

package info (click to toggle)
spring 98.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,928 kB
  • ctags: 60,665
  • sloc: cpp: 356,167; ansic: 39,434; python: 12,228; java: 12,203; awk: 5,856; sh: 1,719; xml: 997; perl: 405; php: 253; objc: 194; makefile: 72; sed: 2
file content (326 lines) | stat: -rw-r--r-- 8,255 bytes parent folder | download
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
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
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
321
322
323
324
325
326
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

// must be included before streflop! else we get streflop/cmath resolve conflicts in its hash implementation files
#include <boost/unordered_map.hpp>
#include <vector>


#include "NamedTextures.h"

#include "Rendering/GL/myGL.h"
#include "Bitmap.h"
#include "Rendering/GlobalRendering.h"
#include "System/bitops.h"
#include "System/TimeProfiler.h"
#include "System/type2.h"
#include "System/Log/ILog.h"

#ifdef _MSC_VER
	#include <map>
	// only way to compile unordered_map with MSVC appears to require inclusion of math.h instead of streflop,
	// and that cannot be done here because it gives rise to other conflicts
	typedef std::map<std::string, CNamedTextures::TexInfo> TEXMAP;
#else
	typedef boost::unordered_map<std::string, CNamedTextures::TexInfo> TEXMAP;
#endif

namespace CNamedTextures {

	static TEXMAP texMap;
	static std::vector<std::string> texWaiting;

	/******************************************************************************/

	void Init()
	{
	}


	void Kill()
	{
		TEXMAP::iterator it;
		for (it = texMap.begin(); it != texMap.end(); ++it) {
			const GLuint texID = it->second.id;
			glDeleteTextures(1, &texID);
		}
		texMap.clear();
	}


	/******************************************************************************/

	static bool Load(const std::string& texName, unsigned int texID)
	{
		//! strip off the qualifiers
		std::string filename = texName;
		bool border  = false;
		bool clamped = false;
		bool nearest = false;
		bool linear  = false;
		bool aniso   = false;
		bool invert  = false;
		bool greyed  = false;
		bool tint    = false;
		float tintColor[3];
		bool resize  = false;
		int2 resizeDimensions;

		if (filename[0] == ':') {
			int p;
			for (p = 1; p < (int)filename.size(); p++) {
				const char ch = filename[p];
				if (ch == ':')      { break; }
				else if (ch == 'n') { nearest = true; }
				else if (ch == 'l') { linear  = true; }
				else if (ch == 'a') { aniso   = true; }
				else if (ch == 'i') { invert  = true; }
				else if (ch == 'g') { greyed  = true; }
				else if (ch == 'c') { clamped = true; }
				else if (ch == 'b') { border  = true; }
				else if (ch == 't') {
					const char* cstr = filename.c_str() + p + 1;
					const char* start = cstr;
					char* endptr;
					tintColor[0] = (float)strtod(start, &endptr);
					if ((start != endptr) && (*endptr == ',')) {
						start = endptr + 1;
						tintColor[1] = (float)strtod(start, &endptr);
						if ((start != endptr) && (*endptr == ',')) {
							start = endptr + 1;
							tintColor[2] = (float)strtod(start, &endptr);
							if (start != endptr) {
								tint = true;
								p += (endptr - cstr);
							}
						}
					}
				}
				else if (ch == 'r') {
					const char* cstr = filename.c_str() + p + 1;
					const char* start = cstr;
					char* endptr;
					resizeDimensions.x = (int)strtoul(start, &endptr, 10);
					if ((start != endptr) && (*endptr == ',')) {
						start = endptr + 1;
						resizeDimensions.y = (int)strtoul(start, &endptr, 10);
						if (start != endptr) {
							resize = true;
							p += (endptr - cstr);
						}
					}
				}
			}
			if (p < (int)filename.size()) {
				filename = filename.substr(p + 1);
			} else {
				filename.clear();
			}
		}

		//! get the image
		CBitmap bitmap;
		TexInfo texInfo;

		if (!bitmap.Load(filename)) {
			LOG_L(L_WARNING, "Couldn't find texture \"%s\"!", filename.c_str());
			texMap[texName] = texInfo;
			glBindTexture(GL_TEXTURE_2D, 0);
			return false;
		}

		if (bitmap.compressed) {
			texID = bitmap.CreateDDSTexture(texID);
		} else {
			if (resize) {
				bitmap = bitmap.CreateRescaled(resizeDimensions.x,resizeDimensions.y);
			}
			if (invert) {
				bitmap.InvertColors();
			}
			if (greyed) {
				bitmap.GrayScale();
			}
			if (tint) {
				bitmap.Tint(tintColor);
			}


			//! make the texture
			glBindTexture(GL_TEXTURE_2D, texID);

			if (clamped) {
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
			}

			if (nearest || linear) {
				if (border) {
					GLfloat white[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
					glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, white);
				}

				if (nearest) {
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
				} else {
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
				}

				//! Note: NPOTs + nearest filtering seems broken on ATIs
				if ( !(count_bits_set(bitmap.xsize)==1 && count_bits_set(bitmap.ysize)==1) &&
					(!GLEW_ARB_texture_non_power_of_two || (globalRendering->atiHacks && nearest)) )
				{
					bitmap = bitmap.CreateRescaled(next_power_of_2(bitmap.xsize),next_power_of_2(bitmap.ysize));
				}

				glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
							bitmap.xsize, bitmap.ysize, border ? 1 : 0,
							GL_RGBA, GL_UNSIGNED_BYTE, bitmap.mem);
			} else {
				//! MIPMAPPING (default)
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

				if ((count_bits_set(bitmap.xsize)==1 && count_bits_set(bitmap.ysize)==1) ||
					GLEW_ARB_texture_non_power_of_two)
				{
					glBuildMipmaps(GL_TEXTURE_2D, GL_RGBA8, bitmap.xsize, bitmap.ysize,
								GL_RGBA, GL_UNSIGNED_BYTE, bitmap.mem);
				} else {
					//! glu auto resizes to next POT
					gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, bitmap.xsize, bitmap.ysize,
									GL_RGBA, GL_UNSIGNED_BYTE, bitmap.mem);
				}
			}

			if (aniso && GLEW_EXT_texture_filter_anisotropic) {
				static GLfloat maxAniso = -1.0f;
				if (maxAniso == -1.0f) {
					glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAniso);
				}
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAniso);
			}
		}

		texInfo.id    = texID;
		texInfo.xsize = bitmap.xsize;
		texInfo.ysize = bitmap.ysize;

		texMap[texName] = texInfo;

		return true;
	}


	bool Bind(const std::string& texName)
	{
		if (texName.empty()) {
			return false;
		}

		// cached
		TEXMAP::iterator it = texMap.find(texName);
		if (it != texMap.end()) {
			const GLuint texID = it->second.id;
			glBindTexture(GL_TEXTURE_2D, texID);
			return (texID != 0);
		}

		// load texture
		GLboolean inListCompile;
		glGetBooleanv(GL_LIST_INDEX, &inListCompile);
		if (inListCompile) {
			GLuint texID = 0;
			glGenTextures(1, &texID);

			TexInfo texInfo;
			texInfo.id = texID;
			texMap[texName] = texInfo;

			glBindTexture(GL_TEXTURE_2D, texID);

			texWaiting.push_back(texName);
			return true;
		}

		GLuint texID = 0;
		glGenTextures(1, &texID);
		return Load(texName, texID);
	}


	void Update()
	{
		if (texWaiting.empty()) {
			return;
		}

		glPushAttrib(GL_TEXTURE_BIT);
		for (std::vector<std::string>::iterator it = texWaiting.begin(); it != texWaiting.end(); ++it) {
			TEXMAP::iterator mit = texMap.find(*it);
			if (mit != texMap.end()) {
				Load(*it,mit->second.id);
			}
		}
		glPopAttrib();
		texWaiting.clear();
	}


	bool Free(const std::string& texName)
	{
		if (texName.empty()) {
			return false;
		}

		TEXMAP::iterator it = texMap.find(texName);
		if (it != texMap.end()) {
			const GLuint texID = it->second.id;
			glDeleteTextures(1, &texID);
			texMap.erase(it);
			return true;
		}
		return false;
	}


	const TexInfo* GetInfo(const std::string& texName, const bool forceLoad)
	{
		if (texName.empty()) {
			return NULL;
		}

		TEXMAP::const_iterator it = texMap.find(texName);
		if (it != texMap.end()) {
			return &it->second;
		}

		if (forceLoad) {
			// load texture
			GLboolean inListCompile;
			glGetBooleanv(GL_LIST_INDEX, &inListCompile);
			if (inListCompile) {
				GLuint texID = 0;
				glGenTextures(1, &texID);

				TexInfo texInfo;
				texInfo.id = texID;
				texMap[texName] = texInfo;

				texWaiting.push_back(texName);
			} else {
				GLuint texID = 0;
				glGenTextures(1, &texID);
				Load(texName, texID);
			}
			return &texMap[texName];
		}

		return NULL;
	}


	/******************************************************************************/

} // namespace CNamedTextures