Skip to content

Commit

Permalink
Performance: Faster ascii overmap drawing
Browse files Browse the repository at this point in the history
* Makes overmap drawing faster when using ascii overmap view, by doing
  less work in each step of the x/y loop.
* On my (crappy) machine, each keypress to scroll the view when viewing
  the overmap had a ~100ms latency, making it all feel slow. With this
  commit, there is no noticeable latency for each keypress.

A callgrind trace when scrolling the view showed that the latency was
caused by time spent in `draw_window`. Specifically:
* Reading an option for each cell, which causes hashtable lookups. In
  this commit, the option is instead now only read once before the x/y
  loop.
* Drawing many black rectangles. In this commit, we skip each rect draw
  if the cell background is already black, since the whole drawing area
  should have already been cleared ealier in this call anyway.
  • Loading branch information
inogenous committed Apr 27, 2024
1 parent 150fdfe commit 173c06f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/sdltiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,7 @@ static bool draw_window( Font_Ptr &font, const catacurses::window &w, const poin
// TODO: Get this from UTF system to make sure it is exactly the kind of space we need
static const std::string space_string = " ";

const bool option_use_draw_ascii_lines_routine = get_option<bool>( "USE_DRAW_ASCII_LINES_ROUTINE" );
bool update = false;
for( int j = 0; j < win->height; j++ ) {
if( !win->line[j].touched ) {
Expand Down Expand Up @@ -1200,7 +1201,7 @@ static bool draw_window( Font_Ptr &font, const catacurses::window &w, const poin
// utf8_width() may return a negative width
continue;
}
bool use_draw_ascii_lines_routine = get_option<bool>( "USE_DRAW_ASCII_LINES_ROUTINE" );
bool use_draw_ascii_lines_routine = option_use_draw_ascii_lines_routine;
unsigned char uc = static_cast<unsigned char>( cell.ch[0] );
switch( codepoint ) {
case LINE_XOXO_UNICODE:
Expand Down Expand Up @@ -1243,8 +1244,10 @@ static bool draw_window( Font_Ptr &font, const catacurses::window &w, const poin
use_draw_ascii_lines_routine = false;
break;
}
geometry->rect( renderer, draw, font->width * cw, font->height,
color_as_sdl( BG ) );
if( cell.BG != catacurses::black ) {
geometry->rect( renderer, draw, font->width * cw, font->height,
color_as_sdl( BG ) );
}
if( use_draw_ascii_lines_routine ) {
font->draw_ascii_lines( renderer, geometry, uc, draw, FG );
} else {
Expand Down

0 comments on commit 173c06f

Please sign in to comment.