Freeciv21
Develop your civilization from humble roots to a global empire
layer_background.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 2021-2023 Freeciv21 contributors.
3 \_ \ / __/ This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include "layer_background.h"
15 
16 #include "client_main.h"
17 #include "colors_common.h"
18 #include "control.h" // unit_is_in_focus
19 #include "sprite_g.h"
20 #include "tilespec.h"
21 
22 namespace freeciv {
23 
25  : freeciv::layer(ts, LAYER_BACKGROUND)
26 {
27 }
28 
29 std::vector<drawn_sprite> layer_background::fill_sprite_array(
30  const tile *ptile, const tile_edge *pedge, const tile_corner *pcorner,
31  const unit *punit) const
32 {
33  const auto pcity = tile_city(ptile);
34 
35  // Set up background color.
36  player *owner = nullptr;
38  /* Unit drawing is disabled when the view options are turned off,
39  * but only where we're drawing on the mapview. */
40  bool do_draw_unit =
41  (punit
42  && (gui_options->draw_units || !ptile
44  if (do_draw_unit) {
45  owner = unit_owner(punit);
46  } else if (pcity && gui_options->draw_cities) {
47  owner = city_owner(pcity);
48  }
49  }
50  if (owner) {
51  return {drawn_sprite(tileset(),
52  m_player_background[player_index(owner)].get())};
53  } else {
54  return {};
55  }
56 }
57 
59 {
60  // Get the color. In pregame, players have no color so we just use red.
61  const auto color = player_has_color(tileset(), player)
63  : Qt::red;
64 
65  auto sprite = create_player_sprite(color);
66 
67  const auto id = player_index(player);
68  m_player_background.at(id) = std::unique_ptr<QPixmap>(crop_sprite(
69  sprite.get(), 0, 0, tileset_tile_width(tileset()),
71 }
72 
73 void layer_background::free_player(int player_id)
74 {
75  m_player_background.at(player_id) = nullptr;
76 }
77 
81 std::unique_ptr<QPixmap>
82 layer_background::create_player_sprite(const QColor &pcolor) const
83 {
84  auto ret = std::make_unique<QPixmap>(tileset_full_tile_width(tileset()),
86  ret->fill(pcolor);
87  return ret;
88 }
89 
90 } // namespace freeciv
QPixmap crop_sprite(const QPixmap *sprite)
Helper function to crop a sprite.
Definition: canvas.cpp:115
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
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 override
Returns the list of sprites drawn by this layer somewhere on the map.
void free_player(int player_id) override
Frees data initialized by initialize_player.
std::unique_ptr< QPixmap > create_player_sprite(const QColor &pcolor) const
Create a sprite with the given color.
std::array< std::unique_ptr< QPixmap >, MAX_NUM_PLAYER_SLOTS > m_player_background
void initialize_player(const player *player) override
Initializes data specific to one player.
A layer when drawing the map.
Definition: layer.h:153
bool do_draw_unit(const tile *ptile, const unit *punit) const
Whether a unit should be drawn.
Definition: layer.cpp:27
struct tileset * tileset() const
Definition: layer.h:241
bool player_has_color(const struct tileset *t, const struct player *pplayer)
Return whether the player has a color assigned yet.
QColor get_player_color(const struct tileset *t, const struct player *pplayer)
Return the color of the player.
bool unit_is_in_focus(const struct unit *punit)
Return TRUE iff this unit is in focus.
Definition: control.cpp:364
Definition: path.cpp:10
client_options * gui_options
Definition: options.cpp:74
int player_index(const struct player *pplayer)
Return the player index.
Definition: player.cpp:748
bool draw_units
Definition: options.h:137
bool draw_cities
Definition: options.h:136
bool draw_focus_unit
Definition: options.h:138
bool solid_color_behind_units
Local Options:
Definition: options.h:79
Definition: player.h:231
Definition: tile.h:42
Definition: unit.h:134
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
int tileset_full_tile_height(const struct tileset *t)
Return the full tile height of the current tileset.
Definition: tilespec.cpp:407
const QPixmap * get_mask_sprite(const struct tileset *t)
Return tile mask sprite.
Definition: tilespec.cpp:3486
int tileset_tile_height(const struct tileset *t)
Return the tile height of the current tileset.
Definition: tilespec.cpp:383
int tileset_full_tile_width(const struct tileset *t)
Return the full tile width of the current tileset.
Definition: tilespec.cpp:394
int tileset_tile_width(const struct tileset *t)
Return the tile width of the current tileset.
Definition: tilespec.cpp:371
#define unit_owner(_pu)
Definition: unit.h:370