[Freeciv-Dev] Re: (PR#4004) A canvas_iterate macro for mapview
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Jason Short wrote:
> I would like to write a new macro, canvas_iterate, to the mapview code.
> The interface is
>
> canvas_iterate(canvas_x0, canvas_y0, width, height, x_itr, y_itr)
>
> and it iterates over the mapview canvas, *in map coordinates*.
I believe this one works, although it's difficult to see why. Although
it is extremely complicated, it isn't too much worse than the current
show_city_descriptions iteration - and certainly better than it would be
if such an iteratation were used in 10 places throughout the code...
jason
#define canvas_iterate(canvas_x0, canvas_y0, width, height, map_x, map_y) \
{ \
int _gui_x, _gui_y; \
int _gui_width, _gui_height; \
int _my_width = width + NORMAL_TILE_WIDTH - 1; \
int _my_height = height + NORMAL_TILE_HEIGHT - 1; \
int _my_canvas_x0 = canvas_x0, _my_canvas_y0 = canvas_y0; \
if (is_isometric) { \
_my_canvas_x0 -= NORMAL_TILE_WIDTH / 2 - 1; \
_my_canvas_y0 -= NORMAL_TILE_HEIGHT / 2 - 1; \
_my_width += NORMAL_TILE_WIDTH - 2; \
_my_height += NORMAL_TILE_HEIGHT - 2; \
} \
_gui_width = _my_width / NORMAL_TILE_WIDTH; \
if (is_isometric) { \
_gui_height = 2 * _my_height / NORMAL_TILE_HEIGHT; \
} else { \
_gui_height = _my_height / NORMAL_TILE_HEIGHT; \
} \
for (_gui_x = 0; _gui_x < _gui_width; _gui_x++) { /* native x */ \
for (_gui_y = 0; _gui_y < _gui_height; _gui_y++) { /* native y */ \
int _gx = 2 * _gui_x + (is_isometric ? (_gui_y & 1) : 0); /* ntl x */ \
int _gy = is_isometric ? _gui_y : (2 * _gui_y); /* ntl y */ \
int _canvas_x = _my_canvas_x0 + _gx * NORMAL_TILE_WIDTH / 2; \
int _canvas_y = _my_canvas_y0 + _gy * NORMAL_TILE_HEIGHT / 2; \
int map_x, map_y; \
if (_canvas_x > canvas_x0 - NORMAL_TILE_WIDTH \
&& _canvas_y > canvas_y0 - NORMAL_TILE_HEIGHT \
&& _canvas_x <= canvas_x0 + width \
&& _canvas_y <= canvas_y0 + height \
&& canvas_to_map_pos(&map_x, &map_y, _canvas_x, _canvas_y)) {
|
|