-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
minimap.cpp
341 lines (278 loc) · 7.82 KB
/
minimap.cpp
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
/*
Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program 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; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/widgets/minimap.hpp"
#include "gui/core/log.hpp"
#include "gui/core/widget_definition.hpp"
#include "gui/core/window_builder.hpp"
#include "gui/core/register_widget.hpp"
#include "gui/widgets/settings.hpp"
#include "map/map.hpp"
#include "map/exception.hpp"
#include "sdl/rect.hpp"
#include "terrain/type_data.hpp"
#include "../../minimap.hpp" // We want the file in src/
#include "utils/functional.hpp"
#include <algorithm>
static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM_INDENT(err, log_config)
#define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
#define LOG_HEADER LOG_SCOPE_HEADER + ':'
// Define this to enable debug output for the minimap cache.
//#define DEBUG_MINIMAP_CACHE
namespace gui2
{
// ------------ WIDGET -----------{
REGISTER_WIDGET(minimap)
minimap::minimap(const implementation::builder_minimap& builder)
: styled_widget(builder, get_control_type())
, map_data_()
, terrain_(nullptr)
{
}
void minimap::set_active(const bool /*active*/)
{
/* DO NOTHING */
}
bool minimap::get_active() const
{
return true;
}
unsigned minimap::get_state() const
{
return 0;
}
/** Key type for the cache. */
struct key_type
{
key_type(const int w, const int h, const std::string& map_data)
: w(w), h(h), map_data(map_data)
{
}
/** Width of the image. */
const int w;
/** Height of the image. */
const int h;
/** The data used to generate the image. */
const std::string map_data;
};
static bool operator<(const key_type& lhs, const key_type& rhs)
{
return lhs.w < rhs.w
|| (lhs.w == rhs.w
&& (lhs.h < rhs.h
|| (lhs.h == rhs.h && lhs.map_data < rhs.map_data)));
}
/** Value type for the cache. */
struct value_type
{
value_type(const surface& surf) : surf(surf), age(1)
{
}
/** The cached image. */
const surface surf;
/**
* The age of the image.
*
* Every time an image is used its age is increased by one. Once the cache
* is full 25% of the cache is emptied. This is done by halving the age of
* the items in the cache and then erase the 25% with the lowest age. If
* items have the same age their order is unspecified.
*/
unsigned age;
};
/**
* Maximum number of items in the cache (multiple of 4).
*
* No testing on the optimal number is done, just seems a nice number.
*/
static const size_t cache_max_size = 100;
/**
* The terrain used to create the cache.
*
* If another terrain config is used the cache needs to be cleared, this
* normally doesn't happen a lot so the clearing of the cache is rather
* unusual.
*/
static const ::config* terrain = nullptr;
/** The cache. */
typedef std::map<key_type, value_type> tcache;
static tcache cache;
static bool compare(const std::pair<unsigned, tcache::iterator>& lhs,
const std::pair<unsigned, tcache::iterator>& rhs)
{
return lhs.first < rhs.first;
}
static void shrink_cache()
{
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << "\nShrink cache from " << cache.size();
#else
DBG_GUI_D << "Shrinking the minimap cache.\n";
#endif
std::vector<std::pair<unsigned, tcache::iterator> > items;
for(tcache::iterator itor = cache.begin(); itor != cache.end(); ++itor) {
itor->second.age /= 2;
items.emplace_back(itor->second.age, itor);
}
std::partial_sort(items.begin(),
items.begin() + cache_max_size / 4,
items.end(),
compare);
for(std::vector<std::pair<unsigned, tcache::iterator> >::iterator vitor
= items.begin();
vitor < items.begin() + cache_max_size / 4;
++vitor) {
cache.erase(vitor->second);
}
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << " to " << cache.size() << ".\n";
#endif
}
bool minimap::disable_click_dismiss() const
{
return false;
}
const surface minimap::get_image(const int w, const int h) const
{
if(!terrain_) {
return nullptr;
}
if(terrain_ != terrain) {
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << "\nFlush cache.\n";
#else
DBG_GUI_D << "Flushing the minimap cache.\n";
#endif
terrain = terrain_;
cache.clear();
}
const key_type key(w, h, map_data_);
tcache::iterator itor = cache.find(key);
if(itor != cache.end()) {
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << '+';
#endif
itor->second.age++;
return itor->second.surf;
}
if(cache.size() >= cache_max_size) {
shrink_cache();
}
try
{
const gamemap map(std::make_shared<terrain_type_data>(*terrain_), map_data_);
const surface surf = image::getMinimap(w, h, map, nullptr, nullptr, true);
cache.emplace(key, value_type(surf));
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << '-';
#endif
return surf;
}
catch(incorrect_map_format_error& e)
{
ERR_CF << "Error while loading the map: " << e.message << '\n';
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << 'X';
#endif
}
return nullptr;
}
void minimap::impl_draw_background(surface& frame_buffer,
int x_offset,
int y_offset)
{
if(!terrain_)
return;
assert(terrain_);
DBG_GUI_D << LOG_HEADER << " size "
<< calculate_blitting_rectangle(x_offset, y_offset) << ".\n";
if(map_data_.empty()) {
return;
}
SDL_Rect rect = calculate_blitting_rectangle(x_offset, y_offset);
assert(rect.w > 0 && rect.h > 0);
const ::surface surf = get_image(rect.w, rect.h);
if(surf) {
sdl_blit(surf, nullptr, frame_buffer, &rect);
}
}
// }---------- DEFINITION ---------{
minimap_definition::minimap_definition(const config& cfg)
: styled_widget_definition(cfg)
{
DBG_GUI_P << "Parsing minimap " << id << '\n';
load_resolutions<resolution>(cfg);
}
/*WIKI
* @page = GUIWidgetDefinitionWML
* @order = 1_minimap
*
* == Minimap ==
*
* @macro = minimap_description
*
* The following states exist:
* * state_enabled, the minimap is enabled.
* @begin{parent}{name="gui/"}
* @begin{tag}{name="minimap_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
* @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
* @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
* @end{tag}{name="state_enabled"}
* @end{tag}{name="resolution"}
* @end{tag}{name="minimap_definition"}
* @end{parent}{name="gui/"}
*/
minimap_definition::resolution::resolution(const config& cfg)
: resolution_definition(cfg)
{
// Note the order should be the same as the enum state_t in minimap.hpp.
state.emplace_back(cfg.child("state_enabled"));
}
// }---------- BUILDER -----------{
/*WIKI_MACRO
* @begin{macro}{minimap_description}
*
* A minimap to show the gamemap, this only shows the map and has no
* interaction options. This version is used for map previews, there
* will be a another version which allows interaction.
* @end{macro}
*/
/*WIKI
* @page = GUIWidgetInstanceWML
* @order = 2_minimap
*
* == Minimap ==
*
* @macro = minimap_description
*
* A minimap has no extra fields.
* @begin{parent}{name="gui/window/resolution/grid/row/column/"}
* @begin{tag}{name="minimap"}{min=0}{max=-1}{super="generic/widget_instance"}
* @end{tag}{name="minimap"}
* @end{parent}{name="gui/window/resolution/grid/row/column/"}
*/
namespace implementation
{
builder_minimap::builder_minimap(const config& cfg) : builder_styled_widget(cfg)
{
}
widget* builder_minimap::build() const
{
minimap* widget = new minimap(*this);
DBG_GUI_G << "Window builder: placed minimap '" << id
<< "' with definition '" << definition << "'.\n";
return widget;
}
} // namespace implementation
// }------------ END --------------
} // namespace gui2