Skip to content

Commit

Permalink
Add helper functions to the base layer class
Browse files Browse the repository at this point in the history
They correspond to two variables declared at the beginning of
fill_sprite_array(), do_draw_unit and solid_bg, that are used by several layer
types.

See #430.
  • Loading branch information
lmoureaux committed Aug 30, 2021
1 parent eee5d50 commit b0142a0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
55 changes: 55 additions & 0 deletions client/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
\_____/ / If not, see https://www.gnu.org/licenses/.
\____/ ********************************************************/

#include "control.h"
#include "tilespec.h"

#include "layer.h"
Expand All @@ -35,4 +36,58 @@ layer::fill_sprite_array(const tile *ptile, const tile_edge *pedge,
pcity, putype);
}

/**
* @brief Whether a unit should be drawn.
* @param ptile The tile where to draw (can be null)
* @param punit The unit that should be drawn (can be null)
*/
bool layer::do_draw_unit(const tile *ptile, const unit *punit) const
{
if (!punit) {
// Can't draw a non-existing unit.
return false;
}

// There is a unit.

if (!ptile) {
// No tile (so not on the map) => always draw.
return true;
}

// Handle options to turn off drawing units.
if (gui_options.draw_units) {
return true;
} else if (gui_options.draw_focus_unit && unit_is_in_focus(punit)) {
return true;
} else {
return false;
}
}

/**
* @brief Whether a solid background should be drawn on a tile instead of its
* terrain.
*
* Query this function to know whether you should refrain from drawing
* "terrain-like" sprites (terrain, darkness, water follow this setting).
*
* @returns `true` when the solid background is enabled and a unit or city is
* drawn on the tile.
*
* @param ptile The tile where to draw (can be null)
* @param punit The unit that could be drawn (can be null)
* @param pcity The city that could be drawn (can be null)
*/
bool layer::solid_background(const tile *ptile, const unit *punit,
const city *pcity) const
{
if (!gui_options.solid_color_behind_units) {
// Solid background turned off (the default).
return false;
}

return do_draw_unit(ptile, punit) || (gui_options.draw_cities && pcity);
}

} // namespace freeciv
4 changes: 4 additions & 0 deletions client/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ class layer {
protected:
struct tileset *tileset() const { return m_ts; }

bool do_draw_unit(const tile *ptile, const unit *punit) const;
bool solid_background(const tile *ptile, const unit *punit,
const city *pcity) const;

private:
struct tileset *m_ts;
mapview_layer m_layer;
Expand Down
8 changes: 1 addition & 7 deletions client/layer_darkness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ std::vector<drawn_sprite> layer_darkness::fill_sprite_array(
}

// Don't draw darkness when the solid background is used
bool do_draw_unit =
(punit
&& (gui_options.draw_units || !ptile
|| (gui_options.draw_focus_unit && unit_is_in_focus(punit))));

if (!(gui_options.solid_color_behind_units
&& (do_draw_unit || (pcity && gui_options.draw_cities)))) {
if (!solid_background(ptile, punit, pcity)) {
return {};
}

Expand Down

0 comments on commit b0142a0

Please sign in to comment.