/
halo.cpp
440 lines (360 loc) · 12 KB
/
halo.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
Copyright (C) 2003 - 2015 by David White <dave@whitevine.net>
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.
*/
/**
* @file
* Maintain halo-effects for units and items.
* Examples: white mage, lighthouse.
*/
#include "global.hpp"
#include "animated.hpp"
#include "display.hpp"
#include "game_preferences.hpp"
#include "halo.hpp"
#include "serialization/string_utils.hpp"
namespace halo
{
namespace {
display* disp = NULL;
class effect
{
public:
effect(int xpos, int ypos, const animated<image::locator>::anim_description& img,
const map_location& loc, ORIENTATION, bool infinite);
void set_location(int x, int y);
bool render();
void unrender();
bool expired() const { return !images_.cycles() && images_.animation_finished(); }
bool need_update() const { return images_.need_update(); }
bool does_change() const { return !images_.does_not_change(); }
bool on_location(const std::set<map_location>& locations) const;
void add_overlay_location(std::set<map_location>& locations);
private:
const image::locator& current_image() { return images_.get_current_frame(); }
animated<image::locator> images_;
ORIENTATION orientation_;
int x_, y_;
surface surf_, buffer_;
SDL_Rect rect_;
/** The location of the center of the halo. */
map_location loc_;
/** All locations over which the halo lies. */
std::vector<map_location> overlayed_hexes_;
};
std::map<int, effect> haloes;
int halo_id = 1;
/**
* Upon unrendering, an invalidation list is send. All haloes in that area and
* the other invalidated haloes are stored in this set. Then there'll be
* tested which haloes overlap and they're also stored in this set.
*/
std::set<int> invalidated_haloes;
/**
* A newly added halo will be added to this list, these haloes don't need to be
* unrendered but do not to be rendered regardless which tiles are invalidated.
* These haloes will stay in this set until there're really rendered (rendering
* won't happen if for example the halo is offscreen).
*/
std::set<int> new_haloes;
/**
* Upon deleting, a halo isn't deleted but added to this set, upon unrendering
* the image is unrendered and deleted.
*/
std::set<int> deleted_haloes;
/**
* Haloes that have an animation or expiration time need to be checked every
* frame and are stored in this set.
*/
std::set<int> changing_haloes;
effect::effect(int xpos, int ypos, const animated<image::locator>::anim_description& img,
const map_location& loc, ORIENTATION orientation, bool infinite) :
images_(img),
orientation_(orientation),
x_(xpos),
y_(ypos),
surf_(NULL),
buffer_(NULL),
rect_(empty_rect),
loc_(loc),
overlayed_hexes_()
{
assert(disp != NULL);
set_location(xpos,ypos);
images_.start_animation(0,infinite);
}
void effect::set_location(int x, int y)
{
const map_location zero_loc(0,0);
int new_x = x - disp->get_location_x(zero_loc);
int new_y = y - disp->get_location_y(zero_loc);
if (new_x != x_ || new_y != y_) {
x_ = new_x;
y_ = new_y;
buffer_.assign(NULL);
overlayed_hexes_.clear();
}
}
bool effect::render()
{
if(disp == NULL) {
return false;
}
if(loc_.x != -1 && loc_.y != -1) {
if(disp->shrouded(loc_)) {
return false;
} else {
// The location of a halo is an x,y value and not a map location.
// This means when a map is zoomed, the halo's won't move,
// This glitch is most visible on [item] haloes.
// This workaround always recalculates the location of the halo
// (item haloes have a location parameter to hide them under the shroud)
// and reapplies that location.
// It might be optimized by storing and comparing the zoom value.
set_location(
disp->get_location_x(loc_) + disp->hex_size() / 2,
disp->get_location_y(loc_) + disp->hex_size() / 2);
}
}
images_.update_last_draw_time();
surf_.assign(image::get_image(current_image(),image::SCALED_TO_ZOOM));
if(surf_ == NULL) {
return false;
}
if(orientation_ == HREVERSE || orientation_ == HVREVERSE) {
surf_.assign(image::reverse_image(surf_));
}
if(orientation_ == VREVERSE || orientation_ == HVREVERSE) {
surf_.assign(flop_surface(surf_));
}
const map_location zero_loc(0,0);
const int screenx = disp->get_location_x(zero_loc);
const int screeny = disp->get_location_y(zero_loc);
const int xpos = x_ + screenx - surf_->w/2;
const int ypos = y_ + screeny - surf_->h/2;
SDL_Rect rect = create_rect(xpos, ypos, surf_->w, surf_->h);
rect_ = rect;
SDL_Rect clip_rect = disp->map_outside_area();
// If rendered the first time, need to determine the area affected.
// If a halo changes size, it is not updated.
if(overlayed_hexes_.empty()) {
display::rect_of_hexes hexes = disp->hexes_under_rect(rect);
display::rect_of_hexes::iterator i = hexes.begin(), end = hexes.end();
for (;i != end; ++i) {
overlayed_hexes_.push_back(*i);
}
}
if(rects_overlap(rect,clip_rect) == false) {
buffer_.assign(NULL);
return false;
}
surface screen = disp->get_screen_surface();
const clip_rect_setter clip_setter(screen, &clip_rect);
if(buffer_ == NULL || buffer_->w != rect.w || buffer_->h != rect.h) {
SDL_Rect rect = rect_;
buffer_.assign(get_surface_portion(screen,rect));
} else {
SDL_Rect rect = rect_;
sdl_blit(screen,&rect,buffer_,NULL);
}
sdl_blit(surf_,NULL,screen,&rect);
update_rect(rect_);
return true;
}
void effect::unrender()
{
if (!surf_ || !buffer_) {
return;
}
// Shrouded haloes are never rendered unless shroud has been re-placed; in
// that case, unrendering causes the hidden terrain (and previous halo
// frame, when dealing with animated halos) to glitch through shroud. We
// don't need to unrender them because shroud paints over the underlying
// area anyway.
if (loc_.x != -1 && loc_.y != -1 && disp->shrouded(loc_)) {
return;
}
surface screen = disp->get_screen_surface();
SDL_Rect clip_rect = disp->map_outside_area();
const clip_rect_setter clip_setter(screen, &clip_rect);
// Due to scrolling, the location of the rendered halo
// might have changed; recalculate
const map_location zero_loc(0,0);
const int screenx = disp->get_location_x(zero_loc);
const int screeny = disp->get_location_y(zero_loc);
const int xpos = x_ + screenx - surf_->w/2;
const int ypos = y_ + screeny - surf_->h/2;
SDL_Rect rect = create_rect(xpos, ypos, surf_->w, surf_->h);
sdl_blit(buffer_,NULL,screen,&rect);
update_rect(rect);
}
bool effect::on_location(const std::set<map_location>& locations) const
{
for(std::vector<map_location>::const_iterator itor = overlayed_hexes_.begin();
itor != overlayed_hexes_.end(); ++itor) {
if(locations.find(*itor) != locations.end()) {
return true;
}
}
return false;
}
void effect::add_overlay_location(std::set<map_location>& locations)
{
for(std::vector<map_location>::const_iterator itor = overlayed_hexes_.begin();
itor != overlayed_hexes_.end(); ++itor) {
locations.insert(*itor);
}
}
} // end anon namespace
manager::manager(display& screen) : old(disp)
{
disp = &screen;
}
manager::~manager()
{
haloes.clear();
invalidated_haloes.clear();
new_haloes.clear();
deleted_haloes.clear();
changing_haloes.clear();
disp = old;
}
int add(int x, int y, const std::string& image, const map_location& loc,
ORIENTATION orientation, bool infinite)
{
const int id = halo_id++;
animated<image::locator>::anim_description image_vector;
std::vector<std::string> items = utils::square_parenthetical_split(image, ',');
std::vector<std::string>::const_iterator itor = items.begin();
for(; itor != items.end(); ++itor) {
const std::vector<std::string>& items = utils::split(*itor, ':');
std::string str;
int time;
if(items.size() > 1) {
str = items.front();
time = atoi(items.back().c_str());
} else {
str = *itor;
time = 100;
}
image_vector.push_back(animated<image::locator>::frame_description(time,image::locator(str)));
}
haloes.insert(std::pair<int,effect>(id,effect(x,y,image_vector,loc,orientation,infinite)));
new_haloes.insert(id);
if(haloes.find(id)->second.does_change() || !infinite) {
changing_haloes.insert(id);
}
return id;
}
void set_location(int handle, int x, int y)
{
const std::map<int,effect>::iterator itor = haloes.find(handle);
if(itor != haloes.end()) {
itor->second.set_location(x,y);
}
}
void remove(int handle)
{
// Silently ignore invalid haloes.
// This happens when Wesnoth is being terminated as well.
if(handle == NO_HALO || haloes.find(handle) == haloes.end()) {
return;
}
deleted_haloes.insert(handle);
}
void unrender(std::set<map_location> invalidated_locations)
{
if(preferences::show_haloes() == false || haloes.empty()) {
return;
}
//assert(invalidated_haloes.empty());
// Remove expired haloes
std::map<int, effect>::iterator itor = haloes.begin();
for(; itor != haloes.end(); ++itor ) {
if(itor->second.expired()) {
deleted_haloes.insert(itor->first);
}
}
// Add the haloes marked for deletion to the invalidation set
std::set<int>::const_iterator set_itor = deleted_haloes.begin();
for(;set_itor != deleted_haloes.end(); ++set_itor) {
invalidated_haloes.insert(*set_itor);
haloes.find(*set_itor)->second.add_overlay_location(invalidated_locations);
}
// Test the multi-frame haloes whether they need an update
for(set_itor = changing_haloes.begin();
set_itor != changing_haloes.end(); ++set_itor) {
if(haloes.find(*set_itor)->second.need_update()) {
invalidated_haloes.insert(*set_itor);
haloes.find(*set_itor)->second.add_overlay_location(invalidated_locations);
}
}
// Find all halo's in a the invalidated area
size_t halo_count;
// Repeat until set of haloes in the invalidated area didn't change
// (including none found) or all existing haloes are found.
do {
halo_count = invalidated_haloes.size();
for(itor = haloes.begin(); itor != haloes.end(); ++itor) {
// Test all haloes not yet in the set
// which match one of the locations
if(invalidated_haloes.find(itor->first) == invalidated_haloes.end() &&
itor->second.on_location(invalidated_locations)) {
// If found, add all locations which the halo invalidates,
// and add it to the set
itor->second.add_overlay_location(invalidated_locations);
invalidated_haloes.insert(itor->first);
}
}
} while (halo_count != invalidated_haloes.size() && halo_count != haloes.size());
if(halo_count == 0) {
return;
}
// Render the haloes:
// iterate through all the haloes and invalidate if in set
for(std::map<int, effect>::reverse_iterator ritor = haloes.rbegin(); ritor != haloes.rend(); ++ritor) {
if(invalidated_haloes.find(ritor->first) != invalidated_haloes.end()) {
ritor->second.unrender();
}
}
// Really delete the haloes marked for deletion
for(set_itor = deleted_haloes.begin(); set_itor != deleted_haloes.end(); ++set_itor) {
// It can happen a deleted halo hasn't been rendered yet, invalidate them as well
new_haloes.erase(*set_itor);
changing_haloes.erase(*set_itor);
invalidated_haloes.erase(*set_itor);
haloes.erase(*set_itor);
}
deleted_haloes.clear();
}
void render()
{
if(preferences::show_haloes() == false || haloes.empty() ||
(new_haloes.empty() && invalidated_haloes.empty())) {
return;
}
// Keep track of not rendered new images they have to be kept scheduled
// for rendering otherwise the invalidation area is never properly set
std::set<int> unrendered_new_haloes;
// Render the haloes:
// iterate through all the haloes and draw if in either set
for(std::map<int, effect>::iterator itor = haloes.begin();
itor != haloes.end(); ++itor) {
if(new_haloes.find(itor->first) != new_haloes.end() &&
! itor->second.render()) {
unrendered_new_haloes.insert(itor->first);
} else if(invalidated_haloes.find(itor->first) != invalidated_haloes.end()) {
itor->second.render();
}
}
invalidated_haloes.clear();
new_haloes = unrendered_new_haloes;
}
} // end namespace halo