提交 aeea2e4d 编写于 作者: D David Seguin 提交者: Eric Pierce

Use display cache to boost performance

Also modifies the test to refresh the cache.
上级 17e14970
......@@ -35,6 +35,15 @@ static const flag_id json_flag_THERMOMETER( "THERMOMETER" );
static const itype_id fuel_type_muscle( "muscle" );
// Cache for the overmap widget string
static disp_overmap_cache disp_om_cache;
disp_overmap_cache::disp_overmap_cache()
{
_center = overmap::invalid_tripoint;
_mission = overmap::invalid_tripoint;
}
// Get remotely controlled vehicle, or vehicle character is inside of
vehicle *display::vehicle_driven( const Character &u )
{
......@@ -1232,14 +1241,19 @@ std::string display::colorized_overmap_text( const avatar &u, const int width, c
map &here = get_map();
// Map is roughly centered around this point
const point_abs_omt center_xy = u.global_omt_location().xy();
const tripoint_abs_omt &center_xyz = u.global_omt_location();
const tripoint_abs_omt &mission_xyz = u.get_active_mission_target();
// Retrieve cached string instead of constantly rebuilding it
if( disp_om_cache.is_valid_for( center_xyz, mission_xyz ) ) {
return disp_om_cache.get_val();
}
// Scan each row of overmap tiles
for( int row = -( height / 2 ); row <= height - ( height / 2 ) - 1; row++ ) {
// Scan across the width of the row
for( int col = -( width / 2 ); col <= width - ( width / 2 ) - 1; col++ ) {
// Get colorized symbol for this point
const tripoint_abs_omt omt( center_xy + point( col, row ), here.get_abs_sub().z );
const tripoint_abs_omt omt( center_xyz.xy() + point( col, row ), here.get_abs_sub().z );
std::pair<std::string, nc_color> sym_color = display::overmap_tile_symbol_color( u, omt );
// Highlight player character location in the center
......@@ -1250,6 +1264,10 @@ std::string display::colorized_overmap_text( const avatar &u, const int width, c
}
overmap_text += "\n";
}
// Rebuild the cache so we can reuse it if nothing changes
disp_om_cache.rebuild( center_xyz, mission_xyz, overmap_text );
return overmap_text;
}
......
......@@ -9,6 +9,36 @@
class avatar;
class Character;
struct disp_overmap_cache {
private:
tripoint_abs_omt _center;
tripoint_abs_omt _mission;
std::string _om_wgt_str;
public:
disp_overmap_cache();
// Returns true if the stored overmap string can be used with the given
// center (player) position and mission target.
bool is_valid_for( const tripoint_abs_omt &center, const tripoint_abs_omt &mission ) const {
return _center == center && _mission == mission;
}
// Rebuild the cache using the validation parameters "center" and "mission"
// and store the associated widget string.
void rebuild( const tripoint_abs_omt &center, const tripoint_abs_omt &mission,
const std::string &om_wgt_str ) {
_center = center;
_mission = mission;
_om_wgt_str = om_wgt_str;
}
// Retreive the cached widget string
const std::string &get_val() const {
return _om_wgt_str;
}
};
// The display namespace contains UI string output and colorization functions
// Some return plain strings or translations, some return a (string, color) pair,
// and some return a string with colorization tags embedded.
......
......@@ -4,8 +4,10 @@
#include "player_helpers.h"
#include "map.h"
#include "map_helpers.h"
#include "mission.h"
#include "monster.h"
#include "morale.h"
#include "overmap.h"
#include "overmapbuffer.h"
#include "options_helpers.h"
#include "weather.h"
......@@ -952,11 +954,12 @@ TEST_CASE( "widgets showing weather conditions", "[widget][weather]" )
// Fill a 3x3 overmap area around the avatar with a given overmap terrain
static void fill_overmap_area( const avatar &ava, const oter_id &oter )
{
const tripoint_abs_omt ava_pos( ms_to_omt_copy( get_map().getabs( ava.pos() ) ) );
const tripoint_abs_omt &ava_pos = ava.global_omt_location();
for( int x = -1; x <= 1; ++x ) {
for( int y = -1; y <= 1; ++y ) {
const tripoint offset( x, y, 0 );
overmap_buffer.ter_set( ava_pos + offset, oter );
overmap_buffer.set_seen( ava_pos + offset, true );
}
}
}
......@@ -965,8 +968,12 @@ TEST_CASE( "multi-line overmap text widget", "[widget][overmap]" )
{
widget overmap_w = widget_test_overmap_3x3_text.obj();
avatar &ava = get_avatar();
mission msn;
// Use mission target to invalidate the om cache
msn.set_target( ava.global_omt_location() + tripoint( 5, 0, 0 ) );
clear_avatar();
clear_map();
ava.on_mission_assignment( msn );
SECTION( "field" ) {
const std::string brown_dot = "<color_c_brown>.</color>";
......@@ -978,6 +985,7 @@ TEST_CASE( "multi-line overmap text widget", "[widget][overmap]" )
};
fill_overmap_area( ava, oter_id( "field" ) );
msn.set_target( msn.get_target() + tripoint( 1, 0, 0 ) );
CHECK( overmap_w.layout( ava ) == join( field_3x3, "" ) );
}
......@@ -991,6 +999,7 @@ TEST_CASE( "multi-line overmap text widget", "[widget][overmap]" )
};
fill_overmap_area( ava, oter_id( "forest" ) );
msn.set_target( msn.get_target() + tripoint( 2, 0, 0 ) );
CHECK( overmap_w.layout( ava ) == join( forest_3x3, "" ) );
}
......@@ -1004,6 +1013,7 @@ TEST_CASE( "multi-line overmap text widget", "[widget][overmap]" )
};
fill_overmap_area( ava, oter_id( "central_lab" ) );
msn.set_target( msn.get_target() + tripoint( 3, 0, 0 ) );
CHECK( overmap_w.layout( ava ) == join( lab_3x3, "" ) );
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册