Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor unit map layers #1083

Merged
merged 10 commits into from
Jul 9, 2022
1 change: 1 addition & 0 deletions client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ add_library(
layer_darkness.cpp
layer_special.cpp
layer_terrain.cpp
layer_units.cpp
layer.cpp
luaconsole.cpp
luaconsole_common.cpp
Expand Down
10 changes: 3 additions & 7 deletions client/helpdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "helpdlg.h"
#include "qtg_cxxside.h"
#include "sprite.h"
#include "tilespec.h"

#define MAX_HELP_TEXT_SIZE 8192
#define REQ_LABEL_NEVER _("(Never)")
Expand Down Expand Up @@ -850,7 +851,6 @@ void help_widget::set_topic_unit(const help_item *topic, const char *title)
char buffer[MAX_HELP_TEXT_SIZE];
int upkeep, max_upkeep;
struct advance *tech;
QPixmap *canvas;
const struct unit_type *obsolete;
struct unit_type *utype, *max_utype;
QString str;
Expand All @@ -866,12 +866,8 @@ void help_widget::set_topic_unit(const help_item *topic, const char *title)
max_utype = uclass_max_values(utype->uclass);

// Unit icon
canvas = new QPixmap(tileset_full_tile_width(tileset),
tileset_full_tile_height(tileset));
canvas->fill(Qt::transparent);
put_unittype(utype, canvas, 0, 0);
add_info_pixmap(canvas);
delete canvas;
add_info_pixmap(
get_unittype_sprite(tileset, utype, direction8_invalid()));

add_info_progress(_("Attack:"), utype->attack_strength, 0,
max_utype->attack_strength);
Expand Down
30 changes: 14 additions & 16 deletions client/hudwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "nation.h"
#include "research.h"
#include "tile.h"
#include "tilespec.h"
#include "unit.h"
#include "unitlist.h"
// client
Expand Down Expand Up @@ -1697,24 +1698,22 @@ void hud_unit_combat::init_images(bool redraw)
QImage crdimg, acrimg, at, dt;
QRect dr, ar;
QPainter p;
QPixmap *defender_pixmap;
QPixmap *attacker_pixmap;
int w;

focus = false;
w = 3 * hud_scale * tileset_unit_height(tileset) / 2;
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
setFixedSize(2 * w, w);
defender_pixmap =
new QPixmap(tileset_unit_width(tileset), tileset_unit_height(tileset));
defender_pixmap->fill(Qt::transparent);
QPixmap defender_pixmap(tileset_unit_width(tileset),
tileset_unit_height(tileset));
if (defender != nullptr) {
if (!redraw) {
put_unit(defender, defender_pixmap, 0, 0);
put_unit(defender, &defender_pixmap, 0, 0);
} else {
put_unittype(type_defender, defender_pixmap, 0, 0);
defender_pixmap =
*get_unittype_sprite(tileset, type_defender, direction8_invalid());
}
dimg = defender_pixmap->toImage();
dimg = defender_pixmap.toImage();
dr = zealous_crop_rect(dimg);
crdimg = dimg.copy(dr);
dimg = crdimg.scaledToHeight(w, Qt::SmoothTransformation);
Expand All @@ -1728,16 +1727,17 @@ void hud_unit_combat::init_images(bool redraw)
dimg = dt;
}
dimg = dimg.scaled(w, w, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
attacker_pixmap =
new QPixmap(tileset_unit_width(tileset), tileset_unit_height(tileset));
attacker_pixmap->fill(Qt::transparent);
QPixmap attacker_pixmap(tileset_unit_width(tileset),
tileset_unit_height(tileset));
attacker_pixmap.fill(Qt::transparent);
if (attacker != nullptr) {
if (!redraw) {
put_unit(attacker, attacker_pixmap, 0, 0);
put_unit(attacker, &attacker_pixmap, 0, 0);
} else {
put_unittype(type_attacker, attacker_pixmap, 0, 0);
attacker_pixmap =
*get_unittype_sprite(tileset, type_attacker, direction8_invalid());
}
aimg = attacker_pixmap->toImage();
aimg = attacker_pixmap.toImage();
ar = zealous_crop_rect(aimg);
acrimg = aimg.copy(ar);
aimg = acrimg.scaledToHeight(w, Qt::SmoothTransformation);
Expand All @@ -1751,8 +1751,6 @@ void hud_unit_combat::init_images(bool redraw)
aimg = at;
}
aimg = aimg.scaled(w, w, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
delete defender_pixmap;
delete attacker_pixmap;
}

/****************************************************************************
Expand Down
6 changes: 2 additions & 4 deletions client/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ namespace freeciv {

std::vector<drawn_sprite>
layer::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 tile_corner *pcorner, const unit *punit) const
{
return ::fill_sprite_array(m_ts, m_layer, ptile, pedge, pcorner, punit,
pcity, putype);
return ::fill_sprite_array(m_ts, m_layer, ptile, pedge, pcorner, punit);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions client/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ class layer {

virtual 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 tile_corner *pcorner, const unit *punit) const;

/**
* Initializes data specific to one player. This allows to cache tiles
Expand Down
4 changes: 3 additions & 1 deletion client/layer_background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ layer_background::layer_background(struct tileset *ts)

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
const unit *punit) const
{
const auto pcity = tile_city(ptile);

// Set up background color.
player *owner = nullptr;
if (gui_options.solid_color_behind_units) {
Expand Down
5 changes: 2 additions & 3 deletions client/layer_background.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ class layer_background : public layer {

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 override;
const tile_corner *pcorner,
const unit *punit) const override;

void initialize_player(const player *player) override;
void free_player(int player_id) override;
Expand Down
4 changes: 1 addition & 3 deletions client/layer_base_flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ layer_base_flags::layer_base_flags(struct tileset *ts, int offset_x,

std::vector<drawn_sprite> layer_base_flags::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 unit *punit) const
{
Q_UNUSED(pedge);
Q_UNUSED(pcorner);
Q_UNUSED(punit);
Q_UNUSED(pcity);
Q_UNUSED(putype);

if (ptile == nullptr) {
return {};
Expand Down
5 changes: 2 additions & 3 deletions client/layer_base_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ class layer_base_flags : public layer {

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 override;
const tile_corner *pcorner,
const unit *punit) const override;

private:
int m_offset_x, m_offset_y;
Expand Down
10 changes: 5 additions & 5 deletions client/layer_darkness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ layer_darkness::layer_darkness(struct tileset *ts)
{
}

std::vector<drawn_sprite> layer_darkness::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
std::vector<drawn_sprite>
layer_darkness::fill_sprite_array(const tile *ptile, const tile_edge *pedge,
const tile_corner *pcorner,
const unit *punit) const
{
Q_UNUSED(pedge);
Q_UNUSED(pcorner);
Q_UNUSED(putype);

if (!ptile) {
return {};
}

// Don't draw darkness when the solid background is used
if (!solid_background(ptile, punit, pcity)) {
if (!solid_background(ptile, punit, tile_city(ptile))) {
return {};
}

Expand Down
5 changes: 2 additions & 3 deletions client/layer_darkness.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ class layer_darkness : public layer {

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 override;
const tile_corner *pcorner,
const unit *punit) const override;

private:
darkness_style m_style;
Expand Down
9 changes: 4 additions & 5 deletions client/layer_special.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ void layer_special::set_sprite(const extra_type *extra, const QString &tag,
}
}

std::vector<drawn_sprite> layer_special::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
std::vector<drawn_sprite>
layer_special::fill_sprite_array(const tile *ptile, const tile_edge *pedge,
const tile_corner *pcorner,
const unit *punit) const
{
Q_UNUSED(pedge);
Q_UNUSED(pcorner);
Q_UNUSED(punit);
Q_UNUSED(pcity);
Q_UNUSED(putype);

if (ptile == nullptr) {
return {};
Expand Down
5 changes: 2 additions & 3 deletions client/layer_special.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ class layer_special : public layer {

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 override;
const tile_corner *pcorner,
const unit *punit) const override;

void reset_ruleset() override;

Expand Down
9 changes: 5 additions & 4 deletions client/layer_terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,10 @@ void layer_terrain::initialize_blending(const terrain *terrain,
/**
* \implements layer::fill_sprite_array
*/
std::vector<drawn_sprite> layer_terrain::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
std::vector<drawn_sprite>
layer_terrain::fill_sprite_array(const tile *ptile, const tile_edge *pedge,
const tile_corner *pcorner,
const unit *punit) const
{
if (ptile == nullptr) {
return {};
Expand All @@ -703,7 +704,7 @@ std::vector<drawn_sprite> layer_terrain::fill_sprite_array(
}

// Don't draw terrain when the solid background is used
if (solid_background(ptile, punit, pcity)) {
if (solid_background(ptile, punit, tile_city(ptile))) {
return {};
}

Expand Down
5 changes: 2 additions & 3 deletions client/layer_terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ class layer_terrain : public layer {

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 override;
const tile_corner *pcorner,
const unit *punit) const override;

private:
matching_group *group(const QString &name);
Expand Down
51 changes: 51 additions & 0 deletions client/layer_units.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-FileCopyrightText: 2022 Louis Moureaux <[email protected]>
*
* SPDX-License-Identifier: GPLv3-or-later
*/

#include "layer_units.h"

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

/**
* \class freeciv::layer_units
* \brief Draws units on the map.
*/

namespace freeciv {

/**
* Constructor
*/
layer_units::layer_units(struct tileset *ts, mapview_layer layer)
: freeciv::layer(ts, layer)
{
}

std::vector<drawn_sprite>
layer_units::fill_sprite_array(const tile *ptile, const tile_edge *pedge,
const tile_corner *pcorner,
const unit *punit) const
{
Q_UNUSED(pedge);
Q_UNUSED(pcorner);

// Should we draw anything in the first place?
if (!do_draw_unit(ptile, punit)) {
return {};
}

// Only draw the focused unit on LAYER_FOCUS_UNIT
if ((type() == LAYER_FOCUS_UNIT) != unit_is_in_focus(punit)) {
return {};
}

std::vector<drawn_sprite> sprs;
fill_unit_sprite_array(tileset(), sprs, ptile, punit);
return sprs;
}

} // namespace freeciv
24 changes: 24 additions & 0 deletions client/layer_units.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SPDX-FileCopyrightText: 2022 Louis Moureaux <[email protected]>
*
* SPDX-License-Identifier: GPLv3-or-later
*/

#pragma once

#include "layer.h"

namespace freeciv {

class layer_units : public layer {
public:
explicit layer_units(struct tileset *ts, mapview_layer layer);
virtual ~layer_units() = default;

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

} // namespace freeciv
Loading