File: IconHandler.h

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; 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 (158 lines) | stat: -rw-r--r-- 3,904 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef ICON_HANDLER_H
#define ICON_HANDLER_H

#include <array>
#include <string>

#include "Icon.h"
#include "System/float3.h"
#include "System/UnorderedMap.hpp"

namespace icon {
	class CIconData {
		public:
			CIconData() = default;
			CIconData(CIconData&& id) { *this = std::move(id); }
			CIconData(
				const std::string& name,
				unsigned int texID,
				float size,
				float distance,
				bool radiusAdjust,
				bool ownTexture,
				unsigned int xsize,
				unsigned int ysize
			);
			~CIconData();

			CIconData& operator = (CIconData&& id) {
				std::swap(name, id.name);

				std::swap(refCount, id.refCount);
				std::swap(texID, id.texID);

				xsize = id.xsize;
				ysize = id.ysize;

				size = id.size;
				distance = id.distance;
				distSqr = id.distSqr;

				std::swap(ownTexture, id.ownTexture);

				radiusAdjust = id.radiusAdjust;
				return *this;
			}

			void Ref() { refCount++; }
			void UnRef() {
				if ((--refCount) > 0)
					return;

				// trigger texture deletion
				*this = {};
			}

			void CopyData(const CIconData* iconData);
			void SwapOwner(CIconData* iconData) {
				ownTexture = true;
				iconData->ownTexture = false;
			}

			void BindTexture() const;

			const std::string& GetName()   const { return name;         }

			unsigned int GetTextureID()    const { return texID;        }
			int          GetSizeX()        const { return xsize;        }
			int          GetSizeY()        const { return ysize;        }

			float        GetSize()         const { return size;         }
			float        GetDistance()     const { return distance;     }
			float        GetDistanceSqr()  const { return distSqr;      }
			float        GetRadiusScale()  const { return 30.0f;        }

			bool         GetRadiusAdjust() const { return radiusAdjust; }

		private:
			std::string name;

			int refCount = 123456;
			unsigned int texID = 0;
			int xsize = 1;
			int ysize = 1;

			float size = 1.0f;
			float distance = 1.0f;
			float distSqr = 1.0f;

			bool ownTexture = false;
			bool radiusAdjust = false;
	};


	class CIconHandler {
		friend class CIcon;

		public:
			CIconHandler() = default;
			CIconHandler(const CIconHandler&) = delete; // no-copy

			void Init() { LoadIcons("gamedata/icontypes.lua"); }
			void Kill();

			bool AddIcon(
				const std::string& iconName,
				const std::string& texName,
				float size,
				float distance,
				bool radiusAdjust
			);

			bool FreeIcon(const std::string& iconName);

			CIcon GetIcon(const std::string& iconName) const;
			CIcon GetSafetyIcon() const { return CIcon(SAFETY_DATA_IDX); }
			CIcon GetDefaultIcon() const { return CIcon(DEFAULT_DATA_IDX); }

			static const CIconData* GetSafetyIconData();
			static const CIconData* GetDefaultIconData();

		private:
			CIconData* GetIconDataMut(unsigned int idx) { return (const_cast<CIconData*>(GetIconData(idx))); }

			const CIconData* GetIconData(unsigned int idx) const {
				switch (idx) {
					case  SAFETY_DATA_IDX: { return (GetSafetyIconData());  } break;
					case DEFAULT_DATA_IDX: { return (GetDefaultIconData()); } break;
					default              : {                                } break;
				}

				return &iconData[idx - ICON_DATA_OFFSET];
			}

			bool LoadIcons(const std::string& filename);
			unsigned int GetDefaultTexture();

		public:
			static constexpr unsigned int  SAFETY_DATA_IDX = 0;
			static constexpr unsigned int DEFAULT_DATA_IDX = 1;
			static constexpr unsigned int ICON_DATA_OFFSET = 2;

			static constexpr unsigned int DEFAULT_TEX_SIZE_X = 128;
			static constexpr unsigned int DEFAULT_TEX_SIZE_Y = 128;

		private:
			unsigned int defTexID = 0;
			unsigned int numIcons = 0;

			spring::unordered_map<std::string, CIcon> iconMap;
			std::array<CIconData, 2048> iconData;
	};

	extern CIconHandler iconHandler;
}

#endif // ICON_HANDLER_H