forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixel_minimap.cpp
593 lines (476 loc) · 18.7 KB
/
pixel_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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#if defined(TILES)
#include "pixel_minimap.h"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iterator>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "avatar.h"
#include "cata_utility.h"
#include "character.h"
#include "color.h"
#include "coordinate_conversions.h"
#include "creature.h"
#include "debug.h"
#include "game.h"
#include "game_constants.h"
#include "int_id.h"
#include "lightmap.h"
#include "map.h"
#include "mapdata.h"
#include "math_defines.h"
#include "monster.h"
#include "pixel_minimap_projectors.h"
#include "sdl_utils.h"
#include "vehicle.h"
#include "vpart_position.h"
extern void set_displaybuffer_rendertarget();
namespace
{
const point total_tiles_count = { ( MAPSIZE - 2 ) *SEEX, ( MAPSIZE - 2 ) *SEEY };
point get_pixel_size( point tile_size, pixel_minimap_mode mode )
{
switch( mode ) {
case pixel_minimap_mode::solid:
return tile_size;
case pixel_minimap_mode::squares:
return { std::max( tile_size.x - 1, 1 ), std::max( tile_size.y - 1, 1 ) };
case pixel_minimap_mode::dots:
return { point_south_east };
}
return {};
}
/// Returns a number in range [0..1]. The range lasts for @param phase_length_ms (milliseconds).
float get_animation_phase( int phase_length_ms )
{
if( phase_length_ms == 0 ) {
return 0.0f;
}
return std::fmod<float>( SDL_GetTicks(), phase_length_ms ) / phase_length_ms;
}
//creates the texture that individual minimap updates are drawn to
//later, the main texture is drawn to the display buffer
//the surface is needed to determine the color format needed by the texture
SDL_Texture_Ptr create_cache_texture( const SDL_Renderer_Ptr &renderer, int tile_width,
int tile_height )
{
return CreateTexture( renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_TARGET,
tile_width,
tile_height );
}
SDL_Color get_map_color_at( const tripoint &p )
{
const map &here = get_map();
if( const auto vp = here.veh_at( p ) ) {
return curses_color_to_SDL( vp->vehicle().part_color( vp->part_index() ) );
}
if( const auto furn_id = here.furn( p ) ) {
return curses_color_to_SDL( furn_id->color() );
}
return curses_color_to_SDL( here.ter( p )->color() );
}
bool is_critter_animated( Creature *critter )
{
if( const monster *m = dynamic_cast<monster *>( critter ) ) {
//faction status (attacking or tracking) determines if red highlights get applied to creature
const monster_attitude matt = m->attitude( &get_avatar() );
if( MATT_ATTACK == matt || MATT_FOLLOW == matt ) {
return true;
}
}
return false;
}
SDL_Color get_critter_color( Creature *critter, int flicker, int mixture )
{
SDL_Color result = curses_color_to_SDL( critter->symbol_color() );
if( is_critter_animated( critter ) ) {
const SDL_Color red_pixel = SDL_Color{ 0xFF, 0x0, 0x0, 0xFF };
result = adjust_color_brightness( mix_colors( result, red_pixel, mixture ), flicker );
}
return result;
}
} // namespace
// a texture pool to avoid recreating textures every time player changes their view
// at most 142 out of 144 textures can be in use due to regular player movement
// (moving from submap corner to new corner) with MAPSIZE = 11
// textures are dumped when the player moves more than one submap in one update
// (teleporting, z-level change) to prevent running out of the remaining pool
class pixel_minimap::shared_texture_pool
{
public:
shared_texture_pool( const std::function<SDL_Texture_Ptr()> &generator ) {
const size_t pool_size = ( MAPSIZE + 1 ) * ( MAPSIZE + 1 );
texture_pool.reserve( pool_size );
inactive_index.reserve( pool_size );
for( size_t i = 0; i < pool_size; ++i ) {
texture_pool.emplace_back( generator() );
inactive_index.push_back( i );
}
}
//reserves a texture from the inactive group and returns tracking info
SDL_Texture_Ptr request_tex( size_t &index ) {
if( inactive_index.empty() ) {
debugmsg( "Ran out of available textures in the pool." );
//shouldn't be happening, but minimap will just be default color instead of crashing
return nullptr;
}
index = inactive_index.back();
inactive_index.pop_back();
return std::move( texture_pool[index] );
}
//releases the provided texture back into the inactive pool to be used again
//called automatically in the submap cache destructor
void release_tex( size_t index, SDL_Texture_Ptr &&ptr ) {
inactive_index.push_back( index );
texture_pool[index] = std::move( ptr );
}
private:
std::vector<SDL_Texture_Ptr> texture_pool;
std::vector<size_t> inactive_index;
};
struct pixel_minimap::submap_cache {
//the color stored for each submap tile
std::array<SDL_Color, SEEX *SEEY> minimap_colors = {};
//checks if the submap has been looked at by the minimap routine
bool touched;
//the texture updates are drawn to
SDL_Texture_Ptr chunk_tex;
//the submap being handled
size_t texture_index;
//the list of updates to apply to the texture
//reduces render target switching to once per submap
std::vector<point> update_list;
//flag used to indicate that the texture needs to be cleared before first use
bool ready;
shared_texture_pool &pool;
//reserve the SEEX * SEEY submap tiles
submap_cache( shared_texture_pool &pool ) :
touched( false ),
ready( false ),
pool( pool ) {
chunk_tex = pool.request_tex( texture_index );
}
//handle the release of the borrowed texture
~submap_cache() {
pool.release_tex( texture_index, std::move( chunk_tex ) );
}
submap_cache( submap_cache && ) = default;
SDL_Color &color_at( point p ) {
assert( p.x < SEEX );
assert( p.y < SEEY );
return minimap_colors[p.y * SEEX + p.x];
}
};
pixel_minimap::pixel_minimap( const SDL_Renderer_Ptr &renderer,
const GeometryRenderer_Ptr &geometry ) :
renderer( renderer ),
geometry( geometry ),
type( pixel_minimap_type::ortho ),
screen_rect{ 0, 0, 0, 0 }
{
}
pixel_minimap::~pixel_minimap() = default;
void pixel_minimap::set_type( pixel_minimap_type type )
{
this->type = type;
reset();
}
void pixel_minimap::set_settings( const pixel_minimap_settings &settings )
{
this->settings = settings;
reset();
}
void pixel_minimap::prepare_cache_for_updates( const tripoint ¢er )
{
const tripoint new_center_sm = get_map().get_abs_sub() + ms_to_sm_copy( center );
const tripoint center_sm_diff = cached_center_sm - new_center_sm;
//invalidate the cache if the game shifted more than one submap in the last update, or if z-level changed.
if( std::abs( center_sm_diff.x ) > 1 ||
std::abs( center_sm_diff.y ) > 1 ||
std::abs( center_sm_diff.z ) > 0 ) {
cache.clear();
} else {
for( auto &mcp : cache ) {
mcp.second.touched = false;
}
}
cached_center_sm = new_center_sm;
}
//deletes the mapping of unused submap caches from the main map
//the touched flag prevents deletion
void pixel_minimap::clear_unused_cache()
{
for( auto it = cache.begin(); it != cache.end(); ) {
it = it->second.touched ? std::next( it ) : cache.erase( it );
}
}
//draws individual updates to the submap cache texture
//the render target will be set back to display_buffer after all submaps are updated
void pixel_minimap::flush_cache_updates()
{
for( auto &mcp : cache ) {
if( mcp.second.update_list.empty() ) {
continue;
}
SetRenderTarget( renderer, mcp.second.chunk_tex );
if( !mcp.second.ready ) {
mcp.second.ready = true;
SetRenderDrawColor( renderer, 0x00, 0x00, 0x00, 0x00 );
RenderClear( renderer );
for( int y = 0; y < SEEY; ++y ) {
for( int x = 0; x < SEEX; ++x ) {
const point tile_pos = projector->get_tile_pos( { x, y }, { SEEX, SEEY } );
const point tile_size = projector->get_tile_size();
const SDL_Rect rect = SDL_Rect{ tile_pos.x, tile_pos.y, tile_size.x, tile_size.y };
geometry->rect( renderer, rect, SDL_Color() );
}
}
}
for( point p : mcp.second.update_list ) {
const point tile_pos = projector->get_tile_pos( p, { SEEX, SEEY } );
const SDL_Color tile_color = mcp.second.color_at( p );
if( pixel_size.x == 1 && pixel_size.y == 1 ) {
SetRenderDrawColor( renderer, tile_color.r, tile_color.g, tile_color.b, tile_color.a );
RenderDrawPoint( renderer, tile_pos );
} else {
geometry->rect( renderer, tile_pos, pixel_size.x, pixel_size.y, tile_color );
}
}
mcp.second.update_list.clear();
}
}
void pixel_minimap::update_cache_at( const tripoint &sm_pos )
{
const map &here = get_map();
const level_cache &access_cache = here.access_cache( sm_pos.z );
const bool nv_goggle = get_avatar().get_vision_modes()[NV_GOGGLES];
submap_cache &cache_item = get_cache_at( here.get_abs_sub() + sm_pos );
const tripoint ms_pos = sm_to_ms_copy( sm_pos );
cache_item.touched = true;
for( int y = 0; y < SEEY; ++y ) {
for( int x = 0; x < SEEX; ++x ) {
const tripoint p = ms_pos + tripoint{ x, y, 0 };
const lit_level lighting = access_cache.visibility_cache[p.x][p.y];
SDL_Color color;
if( lighting == lit_level::BLANK || lighting == lit_level::DARK ) {
// TODO: Map memory?
color = { 0x00, 0x00, 0x00, 0xFF };
} else {
color = get_map_color_at( p );
//color terrain according to lighting conditions
if( nv_goggle ) {
if( lighting == lit_level::LOW ) {
color = color_pixel_nightvision( color );
} else if( lighting != lit_level::DARK && lighting != lit_level::BLANK ) {
color = color_pixel_overexposed( color );
}
} else if( lighting == lit_level::LOW ) {
color = color_pixel_grayscale( color );
}
color = adjust_color_brightness( color, settings.brightness );
}
SDL_Color ¤t_color = cache_item.color_at( { x, y } );
if( current_color != color ) {
current_color = color;
cache_item.update_list.push_back( { x, y } );
}
}
}
}
pixel_minimap::submap_cache &pixel_minimap::get_cache_at( const tripoint &abs_sm_pos )
{
auto it = cache.find( abs_sm_pos );
if( it == cache.end() ) {
it = cache.emplace( abs_sm_pos, *tex_pool ).first;
}
return it->second;
}
void pixel_minimap::process_cache( const tripoint ¢er )
{
prepare_cache_for_updates( center );
for( int y = 0; y < MAPSIZE; ++y ) {
for( int x = 0; x < MAPSIZE; ++x ) {
update_cache_at( { x, y, center.z } );
}
}
flush_cache_updates();
clear_unused_cache();
}
void pixel_minimap::set_screen_rect( const SDL_Rect &screen_rect )
{
if( this->screen_rect == screen_rect && main_tex && tex_pool && projector ) {
return;
}
this->screen_rect = screen_rect;
projector = create_projector( screen_rect );
pixel_size = get_pixel_size( projector->get_tile_size(), settings.mode );
const point size_on_screen = projector->get_tiles_size( total_tiles_count );
if( settings.scale_to_fit ) {
main_tex_clip_rect = SDL_Rect{ 0, 0, size_on_screen.x, size_on_screen.y };
screen_clip_rect = fit_rect_inside( main_tex_clip_rect, screen_rect );
SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );
main_tex = create_cache_texture( renderer, size_on_screen.x, size_on_screen.y );
SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "0" );
} else {
const point d( ( size_on_screen.x - screen_rect.w ) / 2, ( size_on_screen.y - screen_rect.h ) / 2 );
main_tex_clip_rect = SDL_Rect{
std::max( d.x, 0 ),
std::max( d.y, 0 ),
size_on_screen.x - 2 * std::max( d.x, 0 ),
size_on_screen.y - 2 * std::max( d.y, 0 )
};
screen_clip_rect = SDL_Rect{
screen_rect.x - std::min( d.x, 0 ),
screen_rect.y - std::min( d.y, 0 ),
main_tex_clip_rect.w,
main_tex_clip_rect.h
};
main_tex = create_cache_texture( renderer, size_on_screen.x, size_on_screen.y );
}
cache.clear();
const point chunk_size = projector->get_tiles_size( { SEEX, SEEY } );
const auto chunk_texture_generator = [&chunk_size, this]() {
SDL_Texture_Ptr result = create_cache_texture( renderer, chunk_size.x, chunk_size.y );
SetTextureBlendMode( result, SDL_BLENDMODE_BLEND );
return result;
};
tex_pool = std::make_unique<shared_texture_pool>( chunk_texture_generator );
}
void pixel_minimap::reset()
{
projector.reset();
cache.clear();
main_tex.reset();
tex_pool.reset();
}
void pixel_minimap::render( const tripoint ¢er )
{
SetRenderTarget( renderer, main_tex );
SetRenderDrawColor( renderer, 0x00, 0x00, 0x00, 0x00 );
RenderClear( renderer );
render_cache( center );
render_critters( center );
//set display buffer to main screen
set_displaybuffer_rendertarget();
//paint intermediate texture to screen
RenderCopy( renderer, main_tex, &main_tex_clip_rect, &screen_clip_rect );
}
void pixel_minimap::render_cache( const tripoint ¢er )
{
const tripoint sm_center = get_map().get_abs_sub() + ms_to_sm_copy( center );
const tripoint sm_offset = tripoint{
total_tiles_count.x / SEEX / 2,
total_tiles_count.y / SEEY / 2, 0
};
point ms_offset = center.xy();
ms_to_sm_remain( ms_offset );
ms_offset = point{ SEEX / 2, SEEY / 2 } - ms_offset;
for( const auto &elem : cache ) {
if( !elem.second.touched ) {
continue; // What you gonna do with all that junk?
}
const tripoint rel_pos = elem.first - sm_center;
if( std::abs( rel_pos.x ) > sm_offset.x + 1 ||
std::abs( rel_pos.y ) > sm_offset.y + 1 ||
rel_pos.z != 0 ) {
continue;
}
const tripoint sm_pos = rel_pos + sm_offset;
const tripoint ms_pos = sm_to_ms_copy( sm_pos ) + ms_offset;
const SDL_Rect chunk_rect = projector->get_chunk_rect( ms_pos.xy(), { SEEX, SEEY } );
RenderCopy( renderer, elem.second.chunk_tex, nullptr, &chunk_rect );
}
}
void pixel_minimap::render_critters( const tripoint ¢er )
{
//handles the enemy faction red highlights
//this value should be divisible by 200
const int indicator_length = settings.beacon_blink_interval * 200; //default is 2000 ms, 2 seconds
int flicker = 100;
int mixture = 0;
if( indicator_length > 0 ) {
const float t = get_animation_phase( 2 * indicator_length );
const float s = std::sin( 2 * M_PI * t );
flicker = lerp_clamped( 25, 100, std::abs( s ) );
mixture = lerp_clamped( 0, 100, std::max( s, 0.0f ) );
}
const level_cache &access_cache = get_map().access_cache( center.z );
const int start_x = center.x - total_tiles_count.x / 2;
const int start_y = center.y - total_tiles_count.y / 2;
const point beacon_size = {
std::max<int>( projector->get_tile_size().x *settings.beacon_size / 2, 2 ),
std::max<int>( projector->get_tile_size().y *settings.beacon_size / 2, 2 )
};
cached_has_animated_beacons = false;
for( int y = 0; y < total_tiles_count.y; y++ ) {
for( int x = 0; x < total_tiles_count.x; x++ ) {
const tripoint p = tripoint{ start_x + x, start_y + y, center.z };
const lit_level lighting = access_cache.visibility_cache[p.x][p.y];
if( lighting == lit_level::DARK || lighting == lit_level::BLANK ) {
continue;
}
const auto critter = g->critter_at( p, true );
if( critter == nullptr || !get_avatar().sees( *critter ) ) {
continue;
}
const point critter_pos = projector->get_tile_pos( { x, y }, total_tiles_count );
const SDL_Rect critter_rect = SDL_Rect{ critter_pos.x, critter_pos.y, beacon_size.x, beacon_size.y };
const SDL_Color critter_color = get_critter_color( critter, flicker, mixture );
cached_has_animated_beacons = cached_has_animated_beacons || is_critter_animated( critter );
draw_beacon( critter_rect, critter_color );
}
}
}
//the main call for drawing the pixel minimap to the screen
void pixel_minimap::draw( const SDL_Rect &screen_rect, const tripoint ¢er )
{
if( !g ) {
return;
}
if( screen_rect.w <= 0 || screen_rect.h <= 0 ) {
return;
}
set_screen_rect( screen_rect );
process_cache( center );
render( center );
}
bool pixel_minimap::has_animated_elements() const
{
return settings.beacon_blink_interval && cached_has_animated_beacons;
}
void pixel_minimap::draw_beacon( const SDL_Rect &rect, const SDL_Color &color )
{
for( int x = -rect.w, x_max = rect.w; x <= x_max; ++x ) {
for( int y = -rect.h + std::abs( x ), y_max = rect.h - std::abs( x ); y <= y_max; ++y ) {
const int divisor = 2 * ( std::abs( y ) == rect.h - std::abs( x ) ? 1 : 0 ) + 1;
SetRenderDrawColor( renderer, color.r / divisor, color.g / divisor, color.b / divisor, 0xFF );
RenderDrawPoint( renderer, point( rect.x + x, rect.y + y ) );
}
}
}
std::unique_ptr<pixel_minimap_projector> pixel_minimap::create_projector(
const SDL_Rect &max_screen_rect )
const
{
switch( type ) {
case pixel_minimap_type::ortho:
return std::unique_ptr<pixel_minimap_projector> {
new pixel_minimap_ortho_projector( total_tiles_count, max_screen_rect, settings.square_pixels )
};
case pixel_minimap_type::iso:
return std::unique_ptr<pixel_minimap_projector> {
new pixel_minimap_iso_projector( total_tiles_count, max_screen_rect, settings.square_pixels )
};
}
return nullptr;
}
#endif // SDL_TILES