Skip to content

Commit

Permalink
GUI2/Minimap: refactored drawing code
Browse files Browse the repository at this point in the history
Instead of using the minimap surface method (which was getting converted from surface to texture every
draw cycle since the caching mechanism the canvas implemented wasn't available and the weird custom
cache the widget implemented itself didn't seem to do actually do anything) we use the new render-to-
canvas-texture method which is a lot cleaner and should be a lot faster. This also means we no longer
override styled_widget::impl_draw_background.

Do note I might use the design of the removed cache here to add some age functionality to the
font::pango_text cache.
  • Loading branch information
Vultraz committed Jul 31, 2017
1 parent 23e4a59 commit b590837
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 193 deletions.
198 changes: 25 additions & 173 deletions src/gui/widgets/minimap.cpp
Expand Up @@ -48,208 +48,60 @@ namespace gui2

REGISTER_WIDGET(minimap)

void minimap::set_active(const bool /*active*/)
minimap::minimap()
: styled_widget()
, map_data_()
, terrain_(nullptr)
, map_(nullptr)
{
/* DO NOTHING */
}

bool minimap::get_active() const
void minimap::init()
{
return true;
get_canvas(0).set_draw_function(std::bind(&minimap::canvas_draw_background, this, _1));
}

unsigned minimap::get_state() const
{
return 0;
}

/** Key type for the cache. */
struct key_type
{
key_type(const int w, const int h, const std::string& map_data)
: w(w), h(h), map_data(map_data)
{
}

/** Width of the image. */
const int w;

/** Height of the image. */
const int h;

/** The data used to generate the image. */
const std::string map_data;
};

static bool operator<(const key_type& lhs, const key_type& rhs)
void minimap::set_active(const bool /*active*/)
{
return lhs.w < rhs.w
|| (lhs.w == rhs.w
&& (lhs.h < rhs.h
|| (lhs.h == rhs.h && lhs.map_data < rhs.map_data)));
/* DO NOTHING */
}

/** Value type for the cache. */
struct value_type
{
value_type(const surface& surf) : surf(surf), age(1)
{
}

/** The cached image. */
const surface surf;

/**
* The age of the image.
*
* Every time an image is used its age is increased by one. Once the cache
* is full 25% of the cache is emptied. This is done by halving the age of
* the items in the cache and then erase the 25% with the lowest age. If
* items have the same age their order is unspecified.
*/
unsigned age;
};

/**
* Maximum number of items in the cache (multiple of 4).
*
* No testing on the optimal number is done, just seems a nice number.
*/
static const size_t cache_max_size = 100;

/**
* The terrain used to create the cache.
*
* If another terrain config is used the cache needs to be cleared, this
* normally doesn't happen a lot so the clearing of the cache is rather
* unusual.
*/
static const ::config* terrain = nullptr;

/** The cache. */
typedef std::map<key_type, value_type> tcache;
static tcache cache;

static bool compare(const std::pair<unsigned, tcache::iterator>& lhs,
const std::pair<unsigned, tcache::iterator>& rhs)
bool minimap::get_active() const
{
return lhs.first < rhs.first;
return true;
}

static void shrink_cache()
unsigned minimap::get_state() const
{
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << "\nShrink cache from " << cache.size();
#else
DBG_GUI_D << "Shrinking the minimap cache.\n";
#endif

std::vector<std::pair<unsigned, tcache::iterator> > items;
for(tcache::iterator itor = cache.begin(); itor != cache.end(); ++itor) {

itor->second.age /= 2;
items.emplace_back(itor->second.age, itor);
}

std::partial_sort(items.begin(),
items.begin() + cache_max_size / 4,
items.end(),
compare);

for(std::vector<std::pair<unsigned, tcache::iterator> >::iterator vitor
= items.begin();
vitor < items.begin() + cache_max_size / 4;
++vitor) {

cache.erase(vitor->second);
}

#ifdef DEBUG_MINIMAP_CACHE
std::cerr << " to " << cache.size() << ".\n";
#endif
return 0;
}

bool minimap::disable_click_dismiss() const
{
return false;
}

const surface minimap::get_image(const int w, const int h) const
void minimap::set_map_data(const std::string& map_data)
{
if(!terrain_) {
return nullptr;
}

if(terrain_ != terrain) {
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << "\nFlush cache.\n";
#else
DBG_GUI_D << "Flushing the minimap cache.\n";
#endif
terrain = terrain_;
cache.clear();
}

const key_type key(w, h, map_data_);
tcache::iterator itor = cache.find(key);

if(itor != cache.end()) {
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << '+';
#endif
itor->second.age++;
return itor->second.surf;
if(map_data == map_data_) {
return;
}

if(cache.size() >= cache_max_size) {
shrink_cache();
}
map_data_ = map_data;

try
{
const gamemap map(std::make_shared<terrain_type_data>(*terrain_), map_data_);
const surface surf = image::getMinimap(w, h, map, nullptr);
cache.emplace(key, value_type(surf));
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << '-';
#endif
return surf;
}
catch(incorrect_map_format_error& e)
{
try {
map_.reset(new gamemap(std::make_shared<terrain_type_data>(*terrain_), map_data_));
} catch(incorrect_map_format_error& e) {
ERR_CF << "Error while loading the map: " << e.message << '\n';
#ifdef DEBUG_MINIMAP_CACHE
std::cerr << 'X';
#endif
}
return nullptr;

// Flag the background canvas as dirty so the minimap is redrawn.
get_canvas(0).set_is_dirty(true);
}

void minimap::impl_draw_background(int x_offset, int y_offset)
void minimap::canvas_draw_background(texture& tex)
{
styled_widget::impl_draw_background(x_offset, y_offset);

if(!terrain_)
return;
assert(terrain_);

DBG_GUI_D << LOG_HEADER << " size "
<< calculate_blitting_rectangle(x_offset, y_offset) << ".\n";

if(map_data_.empty()) {
return;
}

SDL_Rect rect = calculate_blitting_rectangle(x_offset, y_offset);
assert(rect.w > 0 && rect.h > 0);

const ::surface surf = get_image(rect.w, rect.h);
if(surf) {
SDL_Rect dst {0, 0, surf->w, surf->h};
texture txt(surf);

CVideo::get_singleton().render_copy(txt, nullptr, &dst);
}
image::render_minimap(tex, *map_);
}

const std::string& minimap::get_control_type() const
Expand Down
30 changes: 10 additions & 20 deletions src/gui/widgets/minimap.hpp
Expand Up @@ -20,6 +20,7 @@
#include "gui/core/window_builder.hpp"

class config;
class gamemap;

namespace gui2
{
Expand All @@ -35,9 +36,7 @@ namespace gui2
class minimap : public styled_widget
{
public:
minimap() : styled_widget(), map_data_(), terrain_(nullptr)
{
}
minimap();

/***** ***** ***** ***** Inherited ***** ***** ***** *****/

Expand All @@ -55,12 +54,7 @@ class minimap : public styled_widget

/***** ***** ***** setters / getters for members ***** ****** *****/

void set_map_data(const std::string& map_data)
{
if(map_data != map_data_) {
map_data_ = map_data;
}
}
void set_map_data(const std::string& map_data);

std::string get_map_data() const
{
Expand Down Expand Up @@ -88,18 +82,14 @@ class minimap : public styled_widget
*/
const ::config* terrain_;

/**
* Gets the image for the minimap.
*
* @param w The wanted width of the image.
* @param h The wanted height of the image.
*
* @returns The image, nullptr upon error.
*/
const surface get_image(const int w, const int h) const;
/** Game map generated from the provided data. */
std::unique_ptr<gamemap> map_;

/** Inherited from @ref styled_widget. */
virtual void init() override;

/** See @ref widget::impl_draw_background. */
virtual void impl_draw_background(int x_offset, int y_offset) override;
/** Drawing function passed to the background canvas. */
void canvas_draw_background(texture& tex);

/** See @ref styled_widget::get_control_type. */
virtual const std::string& get_control_type() const override;
Expand Down

0 comments on commit b590837

Please sign in to comment.