Skip to content

Commit

Permalink
Remove all instances of gui_rect_iterate
Browse files Browse the repository at this point in the history
Use the new iterator class instead.
  • Loading branch information
lmoureaux authored and psampathkumar committed Aug 10, 2022
1 parent 25bd5a6 commit 29c2967
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 180 deletions.
21 changes: 10 additions & 11 deletions client/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
https://www.gnu.org/licenses/.
**************************************************************************/

#ifdef HAVE_CONFIG_H
#endif

#include <QRect>
#include <QSet>

// utility
Expand All @@ -32,6 +30,7 @@
#include "control.h"
#include "editor.h"
#include "mapctrl_common.h"
#include "mapview_geometry.h"
#include "tilespec.h"

/* client/include */
Expand Down Expand Up @@ -751,21 +750,21 @@ static void editor_end_selection_rectangle(int canvas_x, int canvas_y)
return;
}

gui_rect_iterate(mapview.gui_x0 + editor->selrect_x,
mapview.gui_y0 + editor->selrect_y, editor->selrect_width,
editor->selrect_height, ptile, pedge, pcorner)
{
if (ptile == nullptr) {
const auto rect = QRect(mapview.gui_x0 + editor->selrect_x,
mapview.gui_y0 + editor->selrect_y,
editor->selrect_width, editor->selrect_height);
for (auto it = freeciv::gui_rect_iterator(tileset, rect); it.next();) {
if (it.current_item() != freeciv::gui_rect_iterator::item_type::tile
|| !it.tile()) {
continue;
}
if (editor->selection_mode == SELECTION_MODE_NEW
|| editor->selection_mode == SELECTION_MODE_ADD) {
editor_selection_add(ptile);
editor_selection_add(it.tile());
} else if (editor->selection_mode == SELECTION_MODE_REMOVE) {
editor_selection_remove(ptile);
editor_selection_remove(it.tile());
}
}
gui_rect_iterate_end;

w = tileset_tile_width(tileset);
h = tileset_tile_height(tileset);
Expand Down
21 changes: 11 additions & 10 deletions client/mapview_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1306,23 +1306,24 @@ void update_map_canvas(int canvas_x, int canvas_y, int width, int height)
* Note that a pixel right on the border of a tile may actually contain a
* goto line from an adjacent tile. Thus we draw any extra goto lines
* from adjacent tiles (if they're close enough). */
gui_rect_iterate(gui_x0 - GOTO_WIDTH, gui_y0 - GOTO_WIDTH,
width + 2 * GOTO_WIDTH, height + 2 * GOTO_WIDTH, ptile,
pedge, pcorner)
{
if (!ptile) {
const auto goto_rect =
QRect(gui_x0 - GOTO_WIDTH, gui_y0 - GOTO_WIDTH, width + 2 * GOTO_WIDTH,
height + 2 * GOTO_WIDTH);
for (auto it = freeciv::gui_rect_iterator(tileset, goto_rect);
it.next();) {
if (it.current_item() != freeciv::gui_rect_iterator::item_type::tile
|| !it.tile()) {
continue;
}
adjc_dir_base_iterate(&(wld.map), ptile, dir)
adjc_dir_base_iterate(&(wld.map), it.tile(), dir)
{
bool safe;
if (mapdeco_is_gotoline_set(ptile, dir, &safe)) {
draw_segment(ptile, dir, safe);
if (mapdeco_is_gotoline_set(it.tile(), dir, &safe)) {
draw_segment(it.tile(), dir, safe);
}
}
adjc_dir_base_iterate_end;
}
gui_rect_iterate_end;

if (!full) {
// Swap store and tmp_store back.
Expand Down Expand Up @@ -1540,7 +1541,7 @@ void show_tile_labels(int canvas_base_x, int canvas_base_y, int width_base,
Draw a goto line at the given location and direction. The line goes from
the source tile to the adjacent tile in the given direction.
*/
void draw_segment(struct tile *src_tile, enum direction8 dir, bool safe)
void draw_segment(const tile *src_tile, enum direction8 dir, bool safe)
{
float canvas_x, canvas_y, canvas_dx, canvas_dy;

Expand Down
160 changes: 1 addition & 159 deletions client/mapview_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,164 +49,6 @@ extern bool can_slide;

#define GOTO_WIDTH 2

/*
* Iterate over all map tiles that intersect with the given GUI rectangle.
* The order of iteration is guaranteed to satisfy the painter's algorithm.
* The iteration covers not only tiles but tile edges and corners.
*
* GRI_x0, GRI_y0: gives the GUI origin of the rectangle.
*
* GRI_width, GRI_height: gives the GUI width and height of the rectangle.
* These values may be negative.
*
* _t, _e, _c: the tile, edge, or corner that is being iterated, declared
* inside the macro. Usually, only one of them will be non-nullptr at a
* time. These values may be passed directly to fill_sprite_array().
*
* _x, _y: the canvas position of the current element, declared inside
* the macro. Each element is assumed to be tileset_tile_width(tileset) *
* tileset_tile_height(tileset) in size. If an element is larger, the
* caller needs to use a larger rectangle of iteration.
*
* The grid of iteration is rather complicated. For a picture of it see
* http://article.gmane.org/gmane.games.freeciv.devel/50449
* (formerly newgrid.png in PR#12085).
*/
#define gui_rect_iterate(GRI_x0, GRI_y0, GRI_width, GRI_height, _t, _e, _c) \
{ \
int _x_##_0 = (GRI_x0), _y_##_0 = (GRI_y0); \
int _x_##_w = (GRI_width), _y_##_h = (GRI_height); \
\
if (_x_##_w < 0) { \
_x_##_0 += _x_##_w; \
_x_##_w = -_x_##_w; \
} \
if (_y_##_h < 0) { \
_y_##_0 += _y_##_h; \
_y_##_h = -_y_##_h; \
} \
if (_x_##_w > 0 && _y_##_h > 0) { \
struct tile_edge _t##_e; \
struct tile_corner _t##_c; \
int _t##_xi, _t##_yi, _t##_si, _t##_di; \
const int _t##_r1 = (tileset_is_isometric(tileset) ? 2 : 1); \
const int _t##_r2 = _t##_r1 * 2; /* double the ratio */ \
const int _t##_w = tileset_tile_width(tileset); \
const int _t##_h = tileset_tile_height(tileset); \
/* Don't divide by _r2 yet, to avoid integer rounding errors. */ \
const int _t##_x0 = DIVIDE(_x_##_0 * _t##_r2, _t##_w) - _t##_r1 / 2; \
const int _t##_y0 = DIVIDE(_y_##_0 * _t##_r2, _t##_h) - _t##_r1 / 2; \
const int _t##_x1 = \
DIVIDE((_x_##_0 + _x_##_w) * _t##_r2 + _t##_w - 1, _t##_w) \
+ _t##_r1; \
const int _t##_y1 = \
DIVIDE((_y_##_0 + _y_##_h) * _t##_r2 + _t##_h - 1, _t##_h) \
+ _t##_r1; \
const int _t##_count = (_t##_x1 - _t##_x0) * (_t##_y1 - _t##_y0); \
int _t##_index = 0; \
\
log_debug("Iterating over %d-%d x %d-%d rectangle.", _t##_x1, \
_t##_x0, _t##_y1, _t##_y0); \
for (; _t##_index < _t##_count; _t##_index++) { \
struct tile *_t = nullptr; \
struct tile_edge *_e = nullptr; \
struct tile_corner *_c = nullptr; \
\
_t##_xi = _t##_x0 + (_t##_index % (_t##_x1 - _t##_x0)); \
_t##_yi = _t##_y0 + (_t##_index / (_t##_x1 - _t##_x0)); \
_t##_si = _t##_xi + _t##_yi; \
_t##_di = _t##_yi - _t##_xi; \
if (2 == _t##_r1 /*tileset_is_isometric(tileset)*/) { \
if ((_t##_xi + _t##_yi) % 2 != 0) { \
continue; \
} \
if (_t##_xi % 2 == 0 && _t##_yi % 2 == 0) { \
if ((_t##_xi + _t##_yi) % 4 == 0) { \
/* Tile */ \
_t = map_pos_to_tile(&(wld.map), _t##_si / 4 - 1, \
_t##_di / 4); \
} else { \
/* Corner */ \
_c = &_t##_c; \
_c->tile[0] = map_pos_to_tile(&(wld.map), (_t##_si - 6) / 4, \
(_t##_di - 2) / 4); \
_c->tile[1] = map_pos_to_tile(&(wld.map), (_t##_si - 2) / 4, \
(_t##_di - 2) / 4); \
_c->tile[2] = map_pos_to_tile(&(wld.map), (_t##_si - 2) / 4, \
(_t##_di + 2) / 4); \
_c->tile[3] = map_pos_to_tile(&(wld.map), (_t##_si - 6) / 4, \
(_t##_di + 2) / 4); \
if (tileset_hex_width(tileset) > 0) { \
_e = &_t##_e; \
_e->type = EDGE_UD; \
_e->tile[0] = _c->tile[0]; \
_e->tile[1] = _c->tile[2]; \
} else if (tileset_hex_height(tileset) > 0) { \
_e = &_t##_e; \
_e->type = EDGE_LR; \
_e->tile[0] = _c->tile[1]; \
_e->tile[1] = _c->tile[3]; \
} \
} \
} else { \
/* Edge. */ \
_e = &_t##_e; \
if (_t##_si % 4 == 0) { \
_e->type = EDGE_NS; \
_e->tile[0] = map_pos_to_tile(&(wld.map), (_t##_si - 4) / 4, \
(_t##_di - 2) / 4); /*N*/ \
_e->tile[1] = map_pos_to_tile(&(wld.map), (_t##_si - 4) / 4, \
(_t##_di + 2) / 4); /*S*/ \
} else { \
_e->type = EDGE_WE; \
_e->tile[0] = map_pos_to_tile(&(wld.map), (_t##_si - 6) / 4, \
_t##_di / 4); /*W*/ \
_e->tile[1] = map_pos_to_tile(&(wld.map), (_t##_si - 2) / 4, \
_t##_di / 4); /*E*/ \
} \
} \
} else { \
if (_t##_si % 2 == 0) { \
if (_t##_xi % 2 == 0) { \
/* Corner. */ \
_c = &_t##_c; \
_c->tile[0] = map_pos_to_tile(&(wld.map), _t##_xi / 2 - 1, \
_t##_yi / 2 - 1); /*NW*/ \
_c->tile[1] = map_pos_to_tile(&(wld.map), _t##_xi / 2, \
_t##_yi / 2 - 1); /*NE*/ \
_c->tile[2] = map_pos_to_tile(&(wld.map), _t##_xi / 2, \
_t##_yi / 2); /*SE*/ \
_c->tile[3] = map_pos_to_tile(&(wld.map), _t##_xi / 2 - 1, \
_t##_yi / 2); /*SW*/ \
} else { \
/* Tile. */ \
_t = map_pos_to_tile(&(wld.map), (_t##_xi - 1) / 2, \
(_t##_yi - 1) / 2); \
} \
} else { \
/* Edge. */ \
_e = &_t##_e; \
if (_t##_yi % 2 == 0) { \
_e->type = EDGE_NS; \
_e->tile[0] = map_pos_to_tile(&(wld.map), (_t##_xi - 1) / 2, \
_t##_yi / 2 - 1); /*N*/ \
_e->tile[1] = map_pos_to_tile(&(wld.map), (_t##_xi - 1) / 2, \
_t##_yi / 2); /*S*/ \
} else { \
_e->type = EDGE_WE; \
_e->tile[0] = map_pos_to_tile(&(wld.map), _t##_xi / 2 - 1, \
(_t##_yi - 1) / 2); /*W*/ \
_e->tile[1] = map_pos_to_tile(&(wld.map), _t##_xi / 2, \
(_t##_yi - 1) / 2); /*E*/ \
} \
} \
}

#define gui_rect_iterate_end \
} \
} \
}

void refresh_tile_mapcanvas(const tile *ptile, bool full_refresh);
void refresh_unit_mapcanvas(struct unit *punit, struct tile *ptile,
bool full_refresh);
Expand Down Expand Up @@ -258,7 +100,7 @@ void show_city_descriptions(int canvas_base_x, int canvas_base_y,
void show_tile_labels(int canvas_base_x, int canvas_base_y, int width_base,
int height_base);

void draw_segment(struct tile *ptile, enum direction8 dir, bool safe);
void draw_segment(const tile *ptile, enum direction8 dir, bool safe);

void decrease_unit_hp_smooth(struct unit *punit0, int hp0,
struct unit *punit1, int hp1);
Expand Down

0 comments on commit 29c2967

Please sign in to comment.