Display: unify handling of debug flags

This commit is contained in:
Charles Dang 2022-10-04 12:18:58 -04:00
parent c6932e8133
commit a6bfc1187e
5 changed files with 75 additions and 74 deletions

View File

@ -89,11 +89,6 @@ static lg::log_domain log_display("display");
#define MaxZoom (zoom_levels.back())
namespace {
// if this is enabled with :benchmark, then everything is marked as invalid and redrawn each time
bool benchmark = false;
bool debug_foreground = false;
int prevLabel = 0;
}
@ -229,9 +224,7 @@ display::display(const display_context* dc,
, invalidated_hexes_(0)
, drawn_hexes_(0)
, redraw_observers_()
, draw_coordinates_(false)
, draw_terrain_codes_(false)
, draw_num_of_bitmaps_(false)
, debug_flags_()
, arrows_map_()
, color_adjust_()
{
@ -1330,16 +1323,6 @@ void display::drawing_buffer_commit()
drawing_buffer_.clear();
}
void display::toggle_benchmark()
{
benchmark = !benchmark;
}
void display::toggle_debug_foreground()
{
debug_foreground = !debug_foreground;
}
// frametime is in milliseconds
static unsigned calculate_fps(unsigned frametime)
{
@ -1391,7 +1374,7 @@ void display::update_fps_label()
font::floating_label flabel(stream.str());
flabel.set_font_size(12);
flabel.set_color(benchmark ? font::BAD_COLOR : font::NORMAL_COLOR);
flabel.set_color(debug_flag_set(DEBUG_BENCHMARK) ? font::BAD_COLOR : font::NORMAL_COLOR);
flabel.set_position(10, 100);
flabel.set_alignment(font::LEFT_ALIGN);
@ -2444,7 +2427,7 @@ void display::draw()
drawing_buffer_commit();
}
if(preferences::show_fps() || benchmark) {
if(preferences::show_fps() || debug_flag_set(DEBUG_BENCHMARK)) {
update_fps_label();
update_fps_count();
} else if(fps_handle_ != 0) {
@ -2464,7 +2447,7 @@ void display::update()
builder_->rebuild_cache_all();
}
if(benchmark) {
if(debug_flag_set(DEBUG_BENCHMARK)) {
invalidate_all();
}
}
@ -2855,7 +2838,7 @@ void display::draw_hex(const map_location& loc)
});
}
if(debug_foreground) {
if(debug_flag_set(DEBUG_FOREGROUND)) {
drawing_buffer_add(
LAYER_UNIT_DEFAULT, loc, [tex = image::get_texture("terrain/foreground.png", image::TOD_COLORED)](const rect& dest) {
draw::blit(tex, dest);
@ -2863,16 +2846,25 @@ void display::draw_hex(const map_location& loc)
}
if(on_map) {
// This might be slight overkill. Basically, we want to check that none of the
// first three bits in the debug flag bitset are set so we can avoid creating
// a stringstream, a temp string, and attempting to trim it for every hex even
// when none of these flags are set. This gives us a temp object with all bits
// past the first three zeroed out.
if((std::as_const(debug_flags_) << (__NUM_DEBUG_FLAGS - DEBUG_FOREGROUND)).none()) {
return;
}
std::ostringstream ss;
if(draw_coordinates_) {
if(debug_flag_set(DEBUG_COORDINATES)) {
ss << loc << '\n';
}
if(draw_terrain_codes_ && (game_config::debug || !is_shrouded)) {
if(debug_flag_set(DEBUG_TERRAIN_CODES) && (game_config::debug || !is_shrouded)) {
ss << get_map().get_terrain(loc) << '\n';
}
if(draw_num_of_bitmaps_) {
if(debug_flag_set(DEBUG_NUM_BITMAPS)) {
ss << (num_images_bg + num_images_fg) << '\n';
}

View File

@ -68,6 +68,7 @@ namespace wb {
#include <boost/circular_buffer.hpp>
#include <bitset>
#include <functional>
#include <chrono>
#include <cstdint>
@ -354,21 +355,6 @@ public:
/** Returns true if location (x,y) is covered in fog. */
bool fogged(const map_location& loc) const;
/** Getter for the x,y debug overlay on tiles */
bool get_draw_coordinates() const { return draw_coordinates_; }
/** Setter for the x,y debug overlay on tiles */
void set_draw_coordinates(bool value) { draw_coordinates_ = value; }
/** Getter for the terrain code debug overlay on tiles */
bool get_draw_terrain_codes() const { return draw_terrain_codes_; }
/** Setter for the terrain code debug overlay on tiles */
void set_draw_terrain_codes(bool value) { draw_terrain_codes_ = value; }
/** Getter for the number of bitmaps debug overlay on tiles */
bool get_draw_num_of_bitmaps() const { return draw_num_of_bitmaps_; }
/** Setter for the terrain code debug overlay on tiles */
void set_draw_num_of_bitmaps(bool value) { draw_num_of_bitmaps_ = value; }
/** Capture a (map-)screenshot into a surface. */
surface screenshot(bool map_screenshot = false);
@ -467,16 +453,6 @@ public:
void clear_mouseover_hex_overlay()
{ mouseover_hex_overlay_.reset(); }
/** Toggle to continuously redraw the screen. */
static void toggle_benchmark();
/**
* Toggle to debug foreground terrain.
* Separate background and foreground layer
* to better spot any error there.
*/
static void toggle_debug_foreground();
terrain_builder& get_builder() {return *builder_;}
void update_fps_label();
@ -991,12 +967,45 @@ private:
std::vector<std::function<void(display&)>> redraw_observers_;
/** Debug flag - overlay x,y coords on tiles */
bool draw_coordinates_;
/** Debug flag - overlay terrain codes on tiles */
bool draw_terrain_codes_;
/** Debug flag - overlay number of bitmaps on tiles */
bool draw_num_of_bitmaps_;
public:
enum DEBUG_FLAG {
/** Overlays x,y coords on tiles */
DEBUG_COORDINATES,
/** Overlays terrain codes on tiles */
DEBUG_TERRAIN_CODES,
/** Overlays number of bitmaps on tiles */
DEBUG_NUM_BITMAPS,
/** Separates background and foreground terrain layers. */
DEBUG_FOREGROUND,
/** Toggle to continuously redraw the whole map. */
DEBUG_BENCHMARK,
/** Dummy entry to size the bitmask. Keep this last! */
__NUM_DEBUG_FLAGS
};
bool debug_flag_set(DEBUG_FLAG flag) const
{
return debug_flags_.test(flag);
}
void set_debug_flag(DEBUG_FLAG flag, bool value)
{
debug_flags_.set(flag, value);
}
void toggle_debug_flag(DEBUG_FLAG flag)
{
debug_flags_.flip(flag);
}
private:
/** Currently set debug flags. */
std::bitset<__NUM_DEBUG_FLAGS> debug_flags_;
typedef std::list<arrow*> arrows_list_t;
typedef std::map<map_location, arrows_list_t > arrows_map_t;

View File

@ -99,9 +99,9 @@ void editor_controller::init_gui()
gui_->change_display_context(&get_current_map_context());
gui_->add_redraw_observer(std::bind(&editor_controller::display_redraw_callback, this, std::placeholders::_1));
floating_label_manager_.reset(new font::floating_label_context());
gui().set_draw_coordinates(preferences::editor::draw_hex_coordinates());
gui().set_draw_terrain_codes(preferences::editor::draw_terrain_codes());
gui().set_draw_num_of_bitmaps(preferences::editor::draw_num_of_bitmaps());
gui().set_debug_flag(display::DEBUG_COORDINATES, preferences::editor::draw_hex_coordinates());
gui().set_debug_flag(display::DEBUG_TERRAIN_CODES, preferences::editor::draw_terrain_codes());
gui().set_debug_flag(display::DEBUG_NUM_BITMAPS, preferences::editor::draw_num_of_bitmaps());
// halo_manager_.reset(new halo::manager(*gui_));
// resources::halo = halo_manager_.get();
// ^ These lines no longer necessary, the gui owns its halo manager.
@ -510,11 +510,11 @@ hotkey::ACTION_STATE editor_controller::get_action_state(hotkey::HOTKEY_COMMAND
case HOTKEY_EDITOR_TOOL_ITEM:
return toolkit_->is_mouse_action_set(command) ? ACTION_ON : ACTION_OFF;
case HOTKEY_EDITOR_DRAW_COORDINATES:
return gui_->get_draw_coordinates() ? ACTION_ON : ACTION_OFF;
return gui_->debug_flag_set(display::DEBUG_COORDINATES) ? ACTION_ON : ACTION_OFF;
case HOTKEY_EDITOR_DRAW_TERRAIN_CODES:
return gui_->get_draw_terrain_codes() ? ACTION_ON : ACTION_OFF;
return gui_->debug_flag_set(display::DEBUG_TERRAIN_CODES) ? ACTION_ON : ACTION_OFF;
case HOTKEY_EDITOR_DRAW_NUM_OF_BITMAPS:
return gui_->get_draw_num_of_bitmaps() ? ACTION_ON : ACTION_OFF;
return gui_->debug_flag_set(display::DEBUG_NUM_BITMAPS) ? ACTION_ON : ACTION_OFF;
case HOTKEY_MINIMAP_DRAW_VILLAGES:
return (preferences::minimap_draw_villages()) ? hotkey::ACTION_ON : hotkey::ACTION_OFF;
@ -963,18 +963,18 @@ bool editor_controller::do_execute_command(const hotkey::hotkey_command& cmd, in
return true;
case HOTKEY_EDITOR_DRAW_COORDINATES:
gui().set_draw_coordinates(!gui().get_draw_coordinates());
preferences::editor::set_draw_hex_coordinates(gui().get_draw_coordinates());
gui().toggle_debug_flag(display::DEBUG_COORDINATES);
preferences::editor::set_draw_hex_coordinates(gui().debug_flag_set(display::DEBUG_COORDINATES));
gui().invalidate_all();
return true;
case HOTKEY_EDITOR_DRAW_TERRAIN_CODES:
gui().set_draw_terrain_codes(!gui().get_draw_terrain_codes());
preferences::editor::set_draw_terrain_codes(gui().get_draw_terrain_codes());
gui().toggle_debug_flag(display::DEBUG_TERRAIN_CODES);
preferences::editor::set_draw_terrain_codes(gui().debug_flag_set(display::DEBUG_TERRAIN_CODES));
gui().invalidate_all();
return true;
case HOTKEY_EDITOR_DRAW_NUM_OF_BITMAPS:
gui().set_draw_num_of_bitmaps(!gui().get_draw_num_of_bitmaps());
preferences::editor::set_draw_num_of_bitmaps(gui().get_draw_num_of_bitmaps());
gui().toggle_debug_flag(display::DEBUG_NUM_BITMAPS);
preferences::editor::set_draw_num_of_bitmaps(gui().debug_flag_set(display::DEBUG_NUM_BITMAPS));
gui().invalidate_all();
return true;
case HOTKEY_EDITOR_REMOVE_LOCATION: {

View File

@ -201,7 +201,7 @@ void terrain_palette::setup_item(
}
tooltip_text << map().get_terrain_editor_string(terrain);
if(gui_.get_draw_terrain_codes()) {
if(gui_.debug_flag_set(display::DEBUG_TERRAIN_CODES)) {
tooltip_text << " " + font::unicode_em_dash + " " << terrain;
}
}

View File

@ -1733,7 +1733,7 @@ void console_handler::do_clear()
void console_handler::do_foreground()
{
menu_handler_.gui_->toggle_debug_foreground();
menu_handler_.gui_->toggle_debug_flag(display::DEBUG_FOREGROUND);
}
void console_handler::do_layers()
@ -1764,7 +1764,7 @@ void console_handler::do_fps()
void console_handler::do_benchmark()
{
menu_handler_.gui_->toggle_benchmark();
menu_handler_.gui_->toggle_debug_flag(display::DEBUG_BENCHMARK);
}
void console_handler::do_save()
@ -2076,18 +2076,18 @@ void console_handler::do_event()
void console_handler::do_toggle_draw_coordinates()
{
menu_handler_.gui_->set_draw_coordinates(!menu_handler_.gui_->get_draw_coordinates());
menu_handler_.gui_->toggle_debug_flag(display::DEBUG_COORDINATES);
menu_handler_.gui_->invalidate_all();
}
void console_handler::do_toggle_draw_terrain_codes()
{
menu_handler_.gui_->set_draw_terrain_codes(!menu_handler_.gui_->get_draw_terrain_codes());
menu_handler_.gui_->toggle_debug_flag(display::DEBUG_TERRAIN_CODES);
menu_handler_.gui_->invalidate_all();
}
void console_handler::do_toggle_draw_num_of_bitmaps()
{
menu_handler_.gui_->set_draw_num_of_bitmaps(!menu_handler_.gui_->get_draw_num_of_bitmaps());
menu_handler_.gui_->toggle_debug_flag(display::DEBUG_NUM_BITMAPS);
menu_handler_.gui_->invalidate_all();
}