File: mapview_common.h

package info (click to toggle)
freeciv 2.6.2-1~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 212,500 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (373 lines) | stat: -rw-r--r-- 14,594 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**********************************************************************
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

#ifndef FC__MAPVIEW_COMMON_H
#define FC__MAPVIEW_COMMON_H

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* utility */
#include "support.h"            /* bool type */

/* common */
#include "fc_types.h"
#include "featured_text.h"      /* enum text_link_type type */
#include "map.h"

/* include */
#include "colors_g.h"

#include "tilespec.h"

struct canvas_store;		/* opaque type, real type is gui-dep */

struct view {
  float gui_x0, gui_y0;
  int width, height;		/* Size in pixels. */
  int tile_width, tile_height;	/* Size in tiles. Rounded up. */
  int store_width, store_height;
  bool can_do_cached_drawing; /* TRUE if cached drawing is possible. */
  struct canvas *store, *tmp_store;
};

void mapdeco_init(void);
void mapdeco_free(void);
void mapdeco_set_highlight(const struct tile *ptile, bool highlight);
bool mapdeco_is_highlight_set(const struct tile *ptile);
void mapdeco_clear_highlights(void);
void mapdeco_set_crosshair(const struct tile *ptile, bool crosshair);
bool mapdeco_is_crosshair_set(const struct tile *ptile);
void mapdeco_clear_crosshairs(void);
void mapdeco_set_gotoroute(const struct unit *punit);
void mapdeco_add_gotoline(const struct tile *ptile,
                          enum direction8 dir);
void mapdeco_remove_gotoline(const struct tile *ptile,
                             enum direction8 dir);
bool mapdeco_is_gotoline_set(const struct tile *ptile,
                             enum direction8 dir);
void mapdeco_clear_gotoroutes(void);


extern struct view mapview;

/* HACK: Callers can set this to FALSE to disable sliding.  It should be
 * reenabled afterwards. */
extern bool can_slide;

#define BORDER_WIDTH 2
#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-NULL 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, _zoom)                             \
{									\
  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) * _zoom;             \
    const int _t##_h = tileset_tile_height(tileset) * _zoom;	        \
    /* 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 = NULL;						\
      struct tile_edge *_e = NULL;					\
      struct tile_corner *_c = NULL;					\
									\
      _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(_t##_si / 4 - 1, _t##_di / 4);		\
	  } else {							\
	    /* Corner */						\
	    _c = &_t##_c;						\
	    _c->tile[0] = map_pos_to_tile((_t##_si - 6) / 4,		\
					  (_t##_di - 2) / 4);		\
	    _c->tile[1] = map_pos_to_tile((_t##_si - 2) / 4,		\
					  (_t##_di - 2) / 4);		\
	    _c->tile[2] = map_pos_to_tile((_t##_si - 2) / 4,		\
					  (_t##_di + 2) / 4);		\
	    _c->tile[3] = map_pos_to_tile((_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((_t##_si - 4) / 4,		\
					  (_t##_di - 2) / 4);	/*N*/	\
	    _e->tile[1] = map_pos_to_tile((_t##_si - 4) / 4,		\
					  (_t##_di + 2) / 4);	/*S*/	\
	  } else {							\
	    _e->type = EDGE_WE;						\
	    _e->tile[0] = map_pos_to_tile((_t##_si - 6) / 4,		\
					  _t##_di / 4);		/*W*/	\
	    _e->tile[1] = map_pos_to_tile((_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(_t##_xi / 2 - 1,		\
					  _t##_yi / 2 - 1);	/*NW*/	\
	    _c->tile[1] = map_pos_to_tile(_t##_xi / 2,			\
					  _t##_yi / 2 - 1);	/*NE*/	\
	    _c->tile[2] = map_pos_to_tile(_t##_xi / 2,			\
					  _t##_yi / 2);		/*SE*/	\
	    _c->tile[3] = map_pos_to_tile(_t##_xi / 2 - 1,		\
					  _t##_yi / 2);		/*SW*/	\
	  } else {							\
	    /* Tile. */							\
	    _t = map_pos_to_tile((_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((_t##_xi - 1) / 2,		\
					  _t##_yi / 2 - 1);	/*N*/	\
	    _e->tile[1] = map_pos_to_tile((_t##_xi - 1) / 2,		\
					  _t##_yi / 2);		/*S*/	\
	  } else {							\
	    _e->type = EDGE_WE;						\
	    _e->tile[0] = map_pos_to_tile(_t##_xi / 2 - 1,		\
					  (_t##_yi - 1) / 2);	/*W*/	\
	    _e->tile[1] = map_pos_to_tile(_t##_xi / 2,			\
					  (_t##_yi - 1) / 2);	/*E*/	\
	  }								\
	}								\
      }

#define gui_rect_iterate_end						\
    }									\
  }									\
}

#define gui_rect_iterate_coord(GRI_x0, GRI_y0, GRI_width, GRI_height,	\
			       _t, _e, _c, _x, _y, _zoom)               \
  gui_rect_iterate(GRI_x0, GRI_y0, GRI_width, GRI_height,               \
                   _t, _e, _c, _zoom) {                                 \
    int _x, _y;                                                         \
                                                                        \
    _x = _t##_xi * _t##_w / _t##_r2 - _t##_w / 2;                       \
    _y = _t##_yi * _t##_h / _t##_r2 - _t##_h / 2;

#define gui_rect_iterate_coord_end                                      \
  } gui_rect_iterate_end

void refresh_tile_mapcanvas(struct tile *ptile,
			    bool full_refresh, bool write_to_screen);
void refresh_unit_mapcanvas(struct unit *punit, struct tile *ptile,
			    bool full_refresh, bool write_to_screen);
void refresh_city_mapcanvas(struct city *pcity, struct tile *ptile,
			    bool full_refresh, bool write_to_screen);

void unqueue_mapview_updates(bool write_to_screen);

void map_to_gui_vector(const struct tileset *t, float zoom,
		       float *gui_dx, float *gui_dy, int map_dx, int map_dy);
bool tile_to_canvas_pos(float *canvas_x, float *canvas_y, struct tile *ptile);
struct tile *canvas_pos_to_tile(float canvas_x, float canvas_y);
struct tile *canvas_pos_to_nearest_tile(float canvas_x, float canvas_y);

void get_mapview_scroll_window(float *xmin, float *ymin,
                               float *xmax, float *ymax,
                               int *xsize, int *ysize);
void get_mapview_scroll_step(int *xstep, int *ystep);
void get_mapview_scroll_pos(int *scroll_x, int *scroll_y);
void set_mapview_scroll_pos(int scroll_x, int scroll_y);

void set_mapview_origin(float gui_x0, float gui_y0);
struct tile *get_center_tile_mapcanvas(void);
void center_tile_mapcanvas(struct tile *ptile);

bool tile_visible_mapcanvas(struct tile *ptile);
bool tile_visible_and_not_on_border_mapcanvas(struct tile *ptile);

void put_unit(const struct unit *punit, struct canvas *pcanvas, float zoom,
              int canvas_x, int canvas_y);
void put_unittype(const struct unit_type *putype, struct canvas *pcanvas, float zoom,
                  int canvas_x, int canvas_y);
void put_city(struct city *pcity, struct canvas *pcanvas, float zoom,
              int canvas_x, int canvas_y);
void put_terrain(struct tile *ptile, struct canvas *pcanvas, float zoom,
                 int canvas_x, int canvas_y);

void put_unit_city_overlays(struct unit *punit,
                            struct canvas *pcanvas,
                            int canvas_x, int canvas_y, int *upkeep_cost,
                            int happy_cost);
void toggle_city_color(struct city *pcity);
void toggle_unit_color(struct unit *punit);

void put_nuke_mushroom_pixmaps(struct tile *ptile);

void put_one_element(struct canvas *pcanvas, float zoom,
                     enum mapview_layer layer,
                     const struct tile *ptile,
                     const struct tile_edge *pedge,
                     const struct tile_corner *pcorner,
                     const struct unit *punit, const struct city *pcity,
                     int canvas_x, int canvas_y,
                     const struct city *citymode,
                     const struct unit_type *putype);

void put_drawn_sprites(struct canvas *pcanvas, float zoom,
                       int canvas_x, int canvas_y,
                       int count, struct drawn_sprite *pdrawn,
                       bool fog);

void update_map_canvas(int canvas_x, int canvas_y, int width, int height);
void update_map_canvas_visible(void);
void update_city_description(struct city *pcity);
void update_tile_label(struct tile *ptile);

void show_city_descriptions(int canvas_base_x, int canvas_base_y,
                            int width_base, int height_base);
void show_tile_labels(int canvas_base_x, int canvas_base_y,
                      int width_base, int height_base);
bool show_unit_orders(struct unit *punit);

void draw_segment(struct tile *ptile, enum direction8 dir);
void undraw_segment(struct tile *ptile, enum direction8 dir);

void decrease_unit_hp_smooth(struct unit *punit0, int hp0, 
			     struct unit *punit1, int hp1);
void move_unit_map_canvas(struct unit *punit,
			  struct tile *ptile, int dx, int dy);

struct city *find_city_or_settler_near_tile(const struct tile *ptile,
					    struct unit **punit);
struct city *find_city_near_tile(const struct tile *ptile);

void get_city_mapview_production(struct city *pcity,
                                 char *buf, size_t buf_len);
void get_city_mapview_name_and_growth(struct city *pcity,
				      char *name_buffer,
				      size_t name_buffer_len,
				      char *growth_buffer,
				      size_t growth_buffer_len,
				      enum color_std *growth_color,
                      enum color_std *production_color);
void get_city_mapview_trade_routes(struct city *pcity,
                                   char *trade_routes_buffer,
                                   size_t trade_routes_buffer_len,
                                   enum color_std *trade_routes_color);

bool map_canvas_resized(int width, int height);
void init_mapcanvas_and_overview(void);
void free_mapcanvas_and_overview(void);

void get_spaceship_dimensions(int *width, int *height);
void put_spaceship(struct canvas *pcanvas, int canvas_x, int canvas_y,
		   const struct player *pplayer);

void link_marks_init(void);
void link_marks_free(void);

void link_marks_draw_all(void);
void link_marks_clear_all(void);
void link_marks_decrease_turn_counters(void);

void link_mark_add_new(enum text_link_type type, int id);
void link_mark_restore(enum text_link_type type, int id);

enum topo_comp_lvl { TOPO_COMPATIBLE = 0,
                     TOPO_INCOMP_SOFT = 1,
                     TOPO_INCOMP_HARD = 2};

enum topo_comp_lvl tileset_map_topo_compatible(int topology_id,
                                               struct tileset *tset);

void animations_init(void);
void animations_free(void);
void set_frame_by_frame_animation(void);
void update_animation(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* FC__MAPVIEW_COMMON_H */