OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

strings_func.h
Go to the documentation of this file.
1 /* $Id: strings_func.h 27758 2017-02-26 19:41:30Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #ifndef STRINGS_FUNC_H
13 #define STRINGS_FUNC_H
14
15 #include "strings_type.h"
16 #include "string_type.h"
17 #include "gfx_type.h"
18 #include "core/bitmath_func.hpp"
19
25 static inline StringTab GetStringTab(StringID str)
26 {
27 StringTab result = (StringTab)(str >> TAB_SIZE_BITS);
28 if (result >= TEXT_TAB_NEWGRF_START) return TEXT_TAB_NEWGRF_START;
29 if (result >= TEXT_TAB_GAMESCRIPT_START) return TEXT_TAB_GAMESCRIPT_START;
30 return result;
31 }
32
38 static inline uint GetStringIndex(StringID str)
39 {
40 return str - (GetStringTab(str) << TAB_SIZE_BITS);
41 }
42
49 static inline StringID MakeStringID(StringTab tab, uint index)
50 {
51 if (tab == TEXT_TAB_NEWGRF_START) {
52 assert(index < TAB_SIZE_NEWGRF);
53 } else if (tab == TEXT_TAB_GAMESCRIPT_START) {
54 assert(index < TAB_SIZE_GAMESCRIPT);
55 } else {
56 assert(tab < TEXT_TAB_END);
57 assert(index < TAB_SIZE);
58 }
59 return (tab << TAB_SIZE_BITS) + index;
60 }
61
62 class StringParameters {
63 StringParameters *parent;
64 uint64 *data;
65 WChar *type;
66
67 public:
68 uint offset;
69 uint num_param;
70
72 StringParameters(uint64 *data, uint num_param, WChar *type) :
73 parent(NULL),
74 data(data),
75 type(type),
76 offset(0),
77 num_param(num_param)
78 { }
79
81 template <size_t Tnum_param>
82 StringParameters(int64 (&data)[Tnum_param]) :
83 parent(NULL),
84 data((uint64 *)data),
85 type(NULL),
86 offset(0),
87 num_param(Tnum_param)
88 {
89 assert_compile(sizeof(data[0]) == sizeof(uint64));
90 }
91
96 StringParameters(StringParameters &parent, uint size) :
97 parent(&parent),
98 data(parent.data + parent.offset),
99 offset(0),
100 num_param(size)
101 {
102 assert(size <= parent.GetDataLeft());
103 if (parent.type == NULL) {
104 this->type = NULL;
105 } else {
106 this->type = parent.type + parent.offset;
107 }
108 }
109
110 ~StringParameters()
111 {
112 if (this->parent != NULL) {
113 this->parent->offset += this->num_param;
114 }
115 }
116
117 void ClearTypeInformation();
118
119 int64 GetInt64(WChar type = 0);
120
122 int32 GetInt32(WChar type = 0)
123 {
124 return (int32)this->GetInt64(type);
125 }
126
127 void ShiftParameters(uint amount);
128
130 uint64 *GetDataPointer() const
131 {
132 return &this->data[this->offset];
133 }
134
136 uint GetDataLeft() const
137 {
138 return this->num_param - this->offset;
139 }
140
142 uint64 *GetPointerToOffset(uint offset) const
143 {
144 assert(offset < this->num_param);
145 return &this->data[offset];
146 }
147
149 bool HasTypeInformation() const
150 {
151 return this->type != NULL;
152 }
153
155 WChar GetTypeAtOffset(uint offset) const
156 {
157 assert(offset < this->num_param);
158 assert(this->HasTypeInformation());
159 return this->type[offset];
160 }
161
162 void SetParam(uint n, uint64 v)
163 {
164 assert(n < this->num_param);
165 this->data[n] = v;
166 }
167
168 uint64 GetParam(uint n) const
169 {
170 assert(n < this->num_param);
171 return this->data[n];
172 }
173 };
174 extern StringParameters _global_string_params;
175
176 char *GetString(char *buffr, StringID string, const char *last);
177 char *GetStringWithArgs(char *buffr, StringID string, StringParameters *args, const char *last, uint case_index = 0, bool game_script = false);
178 const char *GetStringPtr(StringID string);
179
180 uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
181 uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
182
183 void InjectDParam(uint amount);
184
191 static inline void SetDParamX(uint64 *s, uint n, uint64 v)
192 {
193 s[n] = v;
194 }
195
201 static inline void SetDParam(uint n, uint64 v)
202 {
203 _global_string_params.SetParam(n, v);
204 }
205
206 void SetDParamMaxValue(uint n, uint64 max_value, uint min_count = 0, FontSize size = FS_NORMAL);
207 void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL);
208
209 void SetDParamStr(uint n, const char *str);
210
211 void CopyInDParam(int offs, const uint64 *src, int num);
212 void CopyOutDParam(uint64 *dst, int offs, int num);
213 void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num);
214
221 static inline uint64 GetDParamX(const uint64 *s, uint n)
222 {
223 return s[n];
224 }
225
231 static inline uint64 GetDParam(uint n)
232 {
233 return _global_string_params.GetParam(n);
234 }
235
236 extern TextDirection _current_text_dir;
237
238 void InitializeLanguagePacks();
239 const char *GetCurrentLanguageIsoCode();
240
241 int CDECL StringIDSorter(const StringID *a, const StringID *b);
242
246 class MissingGlyphSearcher {
247 public:
249 virtual ~MissingGlyphSearcher() {}
250
255 virtual const char *NextString() = 0;
256
261 virtual FontSize DefaultSize() = 0;
262
266 virtual void Reset() = 0;
267
272 virtual bool Monospace() = 0;
273
279 virtual void SetFontNames(struct FreeTypeSettings *settings, const char *font_name) = 0;
280
281 bool FindMissingGlyphs(const char **str);
282 };
283
284 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = NULL);
285
286 #endif /* STRINGS_FUNC_H */