Skip to content

Commit

Permalink
Migrate the LAYER_BACKGROUND code to its class
Browse files Browse the repository at this point in the history
This commit migrates the code to draw the LAYER_BACKGROUND sprites to the
layer_background class. The feature that allowed to hide the terrain was not
migrated; see #449.

Closes #449.
See #430.
  • Loading branch information
lmoureaux committed Sep 12, 2021
1 parent d58ab0d commit af3fd90
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 150 deletions.
18 changes: 0 additions & 18 deletions client/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2365,19 +2365,6 @@ void request_toggle_city_trade_routes()
update_map_canvas_visible();
}

/**
Toggle display of terrain
*/
void request_toggle_terrain()
{
if (!can_client_change_view()) {
return;
}

gui_options.draw_terrain ^= 1;
update_map_canvas_visible();
}

/**
Toggle display of coastline
*/
Expand Down Expand Up @@ -3692,11 +3679,6 @@ void key_city_productions_toggle() { request_toggle_city_productions(); }
*/
void key_city_trade_routes_toggle() { request_toggle_city_trade_routes(); }

/**
Handle user 'toggle terrain display' input
*/
void key_terrain_toggle() { request_toggle_terrain(); }

/**
Handle user 'toggle coastline display' input
*/
Expand Down
2 changes: 0 additions & 2 deletions client/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ void request_toggle_city_growth();
void request_toggle_city_productions();
void request_toggle_city_buycost();
void request_toggle_city_trade_routes();
void request_toggle_terrain();
void request_toggle_coastline();
void request_toggle_roads_rails();
void request_toggle_irrigation();
Expand Down Expand Up @@ -194,7 +193,6 @@ void key_city_growth_toggle();
void key_city_productions_toggle();
void key_city_buycost_toggle();
void key_city_trade_routes_toggle();
void key_terrain_toggle();
void key_coastline_toggle();
void key_roads_rails_toggle();
void key_irrigation_toggle();
Expand Down
17 changes: 17 additions & 0 deletions client/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class QPixmap;

struct city;
struct player;
struct tileset;
struct unit;
struct unit_type;
Expand Down Expand Up @@ -137,6 +138,22 @@ class layer {
const tile_corner *pcorner, const unit *punit,
const city *pcity, const unit_type *putype) const;

/**
* Initializes data specific to one player. This allows to cache tiles
* depending on the actual players in a game.
*
* \see free_player
*/
virtual void initialize_player(const player *player) { Q_UNUSED(player); }

/**
* Frees data initialized by initialize_player. Note that this takes the
* player ID and not a pointer to the player.
*
* \see initialize_player
*/
virtual void free_player(int player_id) { Q_UNUSED(player_id); }

mapview_layer type() const { return m_layer; }

protected:
Expand Down
69 changes: 67 additions & 2 deletions client/layer_background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,81 @@

#include "layer_background.h"

#include "client_main.h"
#include "colors_common.h"
#include "control.h" // unit_is_in_focus
#include "game.h"
#include "sprite_g.h"
#include "tilespec.h"

namespace freeciv {

layer_background::layer_background(struct tileset *ts)
: freeciv::layer(ts, LAYER_BACKGROUND)
{
}

std::vector<drawn_sprite> layer_background::fill_sprite_array(
const tile *ptile, const tile_edge *pedge, const tile_corner *pcorner,
const unit *punit, const city *pcity, const unit_type *putype) const
{
return ::fill_sprite_array(tileset(), LAYER_BACKGROUND, ptile, pedge,
pcorner, punit, pcity, putype);
// Set up background color.
player *owner = nullptr;
if (gui_options.solid_color_behind_units) {
/* Unit drawing is disabled when the view options are turned off,
* but only where we're drawing on the mapview. */
bool do_draw_unit =
(punit
&& (gui_options.draw_units || !ptile
|| (gui_options.draw_focus_unit && unit_is_in_focus(punit))));
if (do_draw_unit) {
owner = unit_owner(punit);
} else if (pcity && gui_options.draw_cities) {
owner = city_owner(pcity);
}
}
if (owner) {
return {drawn_sprite(tileset(),
m_player_background[player_index(owner)].get())};
} else {
return {};
}
}

void layer_background::initialize_player(const player *player)
{
// Get the color. In pregame, players have no color so we just use red.
const auto color = player_has_color(tileset(), player)
? *get_player_color(tileset(), player)
: Qt::red;

auto sprite = create_player_sprite(color);

const auto id = player_index(player);
m_player_background.at(id) = std::unique_ptr<QPixmap>(
crop_sprite(sprite.get(), 0, 0, tileset_tile_width(tileset()),
tileset_tile_height(tileset()), get_mask_sprite(tileset()),
0, 0, tileset_scale(tileset()), false));
}

void layer_background::free_player(int player_id)
{
m_player_background.at(player_id) = nullptr;
}

/**
* Create a sprite with the given color.
*/
std::unique_ptr<QPixmap>
layer_background::create_player_sprite(const QColor &pcolor) const
{
if (tileset_scale(tileset()) == 1.0f) {
return std::unique_ptr<QPixmap>(create_sprite(128, 64, &pcolor));
} else {
return std::unique_ptr<QPixmap>(
create_sprite(tileset_full_tile_width(tileset()),
tileset_full_tile_height(tileset()), &pcolor));
}
}

} // namespace freeciv
19 changes: 15 additions & 4 deletions client/layer_background.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,31 @@
\____/ ********************************************************/
#pragma once

#include "fc_types.h"
#include "layer.h"

#include <QPixmap>

namespace freeciv {

class layer_background : public layer {
public:
layer_background(struct tileset *ts) : freeciv::layer(ts, LAYER_BACKGROUND)
{
}
explicit layer_background(struct tileset *ts);

std::vector<drawn_sprite>
fill_sprite_array(const tile *ptile, const tile_edge *pedge,
const tile_corner *pcorner, const unit *punit,
const city *pcity, const unit_type *putype) const;
const city *pcity,
const unit_type *putype) const override;

void initialize_player(const player *player) override;
void free_player(int player_id) override;

private:
std::unique_ptr<QPixmap> create_player_sprite(const QColor &pcolor) const;

std::array<std::unique_ptr<QPixmap>, MAX_NUM_PLAYER_SLOTS>
m_player_background;
};

} // namespace freeciv
5 changes: 0 additions & 5 deletions client/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ struct client_options gui_options = {
true, //.draw_city_productions =
false, //.draw_city_buycost =
false, //.draw_city_trade_routes =
true, //.draw_terrain =
false, //.draw_coastline =
true, //.draw_roads_rails =
true, //.draw_irrigation =
Expand Down Expand Up @@ -1636,10 +1635,6 @@ static struct client_option client_options[] = {
"between cities which have trade routes."),
COC_GRAPHICS, GUI_STUB, false,
view_option_changed_callback),
GEN_BOOL_OPTION(draw_terrain, N_("Draw the terrain"),
N_("Setting this option will draw the terrain."),
COC_GRAPHICS, GUI_STUB, true,
view_option_changed_callback),
GEN_BOOL_OPTION(
draw_coastline, N_("Draw the coast line"),
N_("Setting this option will draw a line to separate the "
Expand Down
1 change: 0 additions & 1 deletion client/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ struct client_options {
bool draw_city_productions;
bool draw_city_buycost;
bool draw_city_trade_routes;
bool draw_terrain;
bool draw_coastline;
bool draw_roads_rails;
bool draw_irrigation;
Expand Down
2 changes: 0 additions & 2 deletions client/packhand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4557,8 +4557,6 @@ void handle_ruleset_game(const struct packet_ruleset_game *packet)
game.plr_bg_color =
rgbcolor_new(packet->background_red, packet->background_green,
packet->background_blue);

tileset_background_init(tileset);
}

/**
Expand Down
Loading

0 comments on commit af3fd90

Please sign in to comment.