File: IconHandler.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (345 lines) | stat: -rw-r--r-- 7,739 bytes parent folder | download | duplicates (3)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "IconHandler.h"

#include <algorithm>
#include <cassert>
#include <locale>
#include <cctype>
#include <cmath>

#include "Rendering/GL/myGL.h"
#include "System/Log/ILog.h"
#include "Lua/LuaParser.h"
#include "Textures/Bitmap.h"
#include "System/Exceptions.h"

namespace icon {

CIconHandler iconHandler;

static CIconData dummyIconData[CIconHandler::ICON_DATA_OFFSET];


/******************************************************************************/
//
//  CIconHandler
//

void CIconHandler::Kill()
{
	glDeleteTextures(1, &defTexID);

	defTexID = 0;
	numIcons = 0;

	dummyIconData[ SAFETY_DATA_IDX] = {};
	dummyIconData[DEFAULT_DATA_IDX] = {};

	iconMap.clear();

	for (CIconData& id: iconData) {
		id = {};
	}
}


bool CIconHandler::LoadIcons(const std::string& filename)
{
	LuaParser luaParser(filename, SPRING_VFS_MOD_BASE, SPRING_VFS_MOD_BASE);

	if (!luaParser.Execute())
		LOG_L(L_WARNING, "%s: %s", filename.c_str(), luaParser.GetErrorLog().c_str());

	const LuaTable iconTypes = luaParser.GetRoot();

	std::vector<std::string> iconNames;
	iconTypes.GetKeys(iconNames);

	dummyIconData[ SAFETY_DATA_IDX] = {};
	dummyIconData[DEFAULT_DATA_IDX] = {"default", GetDefaultTexture(), 1.0f, 1.0f, false, false, DEFAULT_TEX_SIZE_X, DEFAULT_TEX_SIZE_Y};

	for (const std::string& iconName : iconNames) {
		const LuaTable iconTable = iconTypes.SubTable(iconName);

		AddIcon(
			iconName,
			iconTable.GetString("bitmap",       ""),
			iconTable.GetFloat ("size",         1.0f),
			iconTable.GetFloat ("distance",     1.0f),
			iconTable.GetBool  ("radiusAdjust", false)
		);
	}

	const auto it = iconMap.find("default");

	if (it != iconMap.end()) {
		dummyIconData[DEFAULT_DATA_IDX].CopyData(GetIconData(it->second.dataIdx));
		dummyIconData[DEFAULT_DATA_IDX].SwapOwner(GetIconDataMut(it->second.dataIdx));
	} else {
		iconMap["default"] = CIcon(DEFAULT_DATA_IDX);
	}

	return true;
}


bool CIconHandler::AddIcon(
	const std::string& iconName,
	const std::string& texName,
	float size,
	float distance,
	bool radAdj
) {
	if (numIcons == iconData.size()) {
		LOG_L(L_DEBUG, "[IconHandler::%s] too many icons added (maximum=%u)", __func__, numIcons);
		return false;
	}

	unsigned int texID = 0;
	unsigned int xsize = 0;
	unsigned int ysize = 0;

	bool ownTexture = true;

	try {
		CBitmap bitmap;

		if ((ownTexture = !texName.empty() && bitmap.Load(texName))) {
			texID = bitmap.CreateMipMapTexture();
			
			glBindTexture(GL_TEXTURE_2D, texID);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
			xsize = bitmap.xsize;
			ysize = bitmap.ysize;
		} else {
			texID = GetDefaultTexture();
			xsize = DEFAULT_TEX_SIZE_X;
			ysize = DEFAULT_TEX_SIZE_Y;
		}
	} catch (const content_error& ex) {
		// bail on non-existant file
		LOG_L(L_DEBUG, "[IconHandler::%s] exception \"%s\" adding icon \"%s\" with texture \"%s\"", __func__, ex.what(), iconName.c_str(), texName.c_str());
		return false;
	}

	const auto it = iconMap.find(iconName);

	if (it != iconMap.end())
		FreeIcon(iconName);

	// data must be constructed first since CIcon's ctor will Ref() it
	iconData[numIcons] = {iconName, texID,  size, distance, radAdj, ownTexture, xsize, ysize};
	// indices 0 and 1 are reserved
	iconMap[iconName] = CIcon(ICON_DATA_OFFSET + numIcons++);

	if (iconName == "default") {
		dummyIconData[DEFAULT_DATA_IDX].CopyData(&iconData[numIcons - 1]);
		dummyIconData[DEFAULT_DATA_IDX].SwapOwner(&iconData[numIcons - 1]);
	}

	return true;
}


bool CIconHandler::FreeIcon(const std::string& iconName)
{
	const auto it = iconMap.find(iconName);

	if (it == iconMap.end())
		return false;
	if (iconName == "default")
		return false;

	// fill with default data (TODO: reuse freed slots)
	GetIconDataMut(it->second.dataIdx)->CopyData(&dummyIconData[DEFAULT_DATA_IDX]);

	iconMap.erase(iconName);
	return true;
}


CIcon CIconHandler::GetIcon(const std::string& iconName) const
{
	const auto it = iconMap.find(iconName);

	if (it == iconMap.end())
		return GetDefaultIcon();

	return it->second;
}

const CIconData* CIconHandler::GetSafetyIconData() { return &dummyIconData[SAFETY_DATA_IDX]; }
const CIconData* CIconHandler::GetDefaultIconData() { return &dummyIconData[DEFAULT_DATA_IDX]; }


unsigned int CIconHandler::GetDefaultTexture()
{
	// FIXME: just use a PNG ?

	if (defTexID != 0)
		return defTexID;

	unsigned char si[DEFAULT_TEX_SIZE_X * DEFAULT_TEX_SIZE_Y * 4];
	for (int y = 0; y < DEFAULT_TEX_SIZE_Y; ++y) {
		for (int x = 0; x < DEFAULT_TEX_SIZE_X; ++x) {
			const int index = ((y * DEFAULT_TEX_SIZE_X) + x) * 4;
			const int dx = (x - 64);
			const int dy = (y - 64);
			const float r = std::sqrt((dx * dx) + (dy * dy)) / 64.0f;
			if (r > 1.0f) {
				si[index + 0] = 0;
				si[index + 1] = 0;
				si[index + 2] = 0;
				si[index + 3] = 0;
			} else {
				const unsigned char val = (unsigned char)(255 - (r * r * r * 255));
				si[index + 0] = val;
				si[index + 1] = val;
				si[index + 2] = val;
				si[index + 3] = 255;
			}
		}
	}

	CBitmap bitmap(si, DEFAULT_TEX_SIZE_X, DEFAULT_TEX_SIZE_Y);

	glBindTexture(GL_TEXTURE_2D, defTexID = bitmap.CreateTexture());
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	return defTexID;
}



/******************************************************************************/
//
//  CIcon
//

CIcon::CIcon() 
{
	CIconData* data = nullptr;

	// use default data if handler is initialized
	if (iconHandler.defTexID != 0) {
		data = &dummyIconData[dataIdx = CIconHandler::DEFAULT_DATA_IDX];
	} else {
		data = &dummyIconData[dataIdx = CIconHandler::SAFETY_DATA_IDX];
	}

	data->Ref();
}


CIcon::CIcon(unsigned int idx)
{
	iconHandler.GetIconDataMut(dataIdx = idx)->Ref();
}

CIcon::CIcon(const CIcon& icon)
{
	iconHandler.GetIconDataMut(dataIdx = icon.dataIdx)->Ref();
}

CIcon::~CIcon()
{
	// NB: icons can outlive the handler
	UnRefData(&iconHandler);
}


CIcon& CIcon::operator=(const CIcon& icon)
{
	if (dataIdx != icon.dataIdx) {
		CIconData* iconData = iconHandler.GetIconDataMut(dataIdx);

		iconData->UnRef();
		iconData = iconHandler.GetIconDataMut(dataIdx = icon.dataIdx);
		iconData->Ref();
	}

	return *this;
}


void CIcon::UnRefData(CIconHandler* ih) {
	if (ih != nullptr)
		ih->GetIconDataMut(dataIdx)->UnRef();

	dataIdx = CIconHandler::SAFETY_DATA_IDX;
}

const CIconData* CIcon::operator->()  const { return iconHandler.GetIconData(dataIdx); }
const CIconData* CIcon::GetIconData() const { return iconHandler.GetIconData(dataIdx); }






/******************************************************************************/
//
//  CIconData
//

CIconData::CIconData(
	const std::string& _name,
	unsigned int _texID,
	float _size,
	float _distance,
	bool radAdj,
	bool ownTex,
	unsigned int _xsize,
	unsigned int _ysize
)
	: name(_name)
	, refCount(0)

	, texID(_texID)
	, xsize(_xsize)
	, ysize(_ysize)

	, size(_size)
	, distance(_distance)
	, distSqr(distance * distance)

	, ownTexture(ownTex)
	, radiusAdjust(radAdj)
{
}

CIconData::~CIconData()
{
	if (ownTexture) {
		#ifndef HEADLESS
		assert(texID != 0);
		#endif
		glDeleteTextures(1, &texID);
	}

	texID = 0;
}


void CIconData::CopyData(const CIconData* iconData)
{
	name         = iconData->name;
	texID        = iconData->texID;
	size         = iconData->size;
	distance     = iconData->distance;
	distSqr      = iconData->distSqr;
	radiusAdjust = iconData->radiusAdjust;
	xsize        = iconData->xsize;
	ysize        = iconData->ysize;
	ownTexture   = false;
}

void CIconData::BindTexture() const
{
	glBindTexture(GL_TEXTURE_2D, texID);
}

}