Freeciv-3.3
Loading...
Searching...
No Matches
sprite.cpp
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18// Qt
19#include <QImageReader>
20#include <QPainter>
21
22// gui-qt
23#include "colors.h"
24#include "fc_client.h"
25#include "qtg_cxxside.h"
26#include "sprite.h"
27
28static const char **gfx_array_extensions = nullptr;
29
30/************************************************************************/
35const char **gfx_fileextensions(void)
36{
38 QList<QByteArray>::iterator iter;
39 int j = 0;
40
41 if (gfx_array_extensions != nullptr) {
43 }
44
45 gfx_ext = QImageReader::supportedImageFormats();
46
47 gfx_array_extensions = new const char *[gfx_ext.count() + 1];
48 for (iter = gfx_ext.begin(); iter != gfx_ext.end(); iter++) {
49 char *ext;
50 int extlen = iter->size() + 1;
51
52 ext = static_cast<char *>(fc_malloc(extlen));
53 strncpy(ext, iter->data(), extlen);
55 j++;
56 }
57 gfx_array_extensions[j] = nullptr;
58
60}
61
62/************************************************************************/
67struct sprite *qtg_load_gfxfile(const char *filename, bool svgflag)
68{
69 sprite *entire = new sprite;
70 QPixmap *pm = new QPixmap;
71
72 if (QPixmapCache::find(QString(filename), pm)) {
73 entire->pm = pm;
74 return entire;
75 }
76 pm->load(QString(filename));
77 entire->pm = pm;
78 QPixmapCache::insert(QString(filename), *pm);
79
80 return entire;
81}
82
83/************************************************************************/
105 int x, int y, int width, int height,
106 struct sprite *mask,
108 float scale, bool smooth)
109{
110 QPainter p;
112 QRectF dest_rect;
114 int widthzoom;
115 int heightzoom;
116 int hex = 0;
117
118 fc_assert_ret_val(source, nullptr);
119
120 if (!width || !height) {
121 return nullptr;
122 }
123 if (scale != 1.0f && (tileset_hex_height(tileset) > 0
124 || tileset_hex_width(tileset) > 0)) {
125 hex = 1;
126 }
129 cropped = new sprite;
131 cropped->pm->fill(Qt::transparent);
133 dest_rect = QRectF(0, 0, widthzoom, heightzoom);
134
135 p.begin(cropped->pm);
136 if (smooth) {
137 p.setRenderHint(QPainter::SmoothPixmapTransform);
138 }
139 p.setRenderHint(QPainter::Antialiasing);
140 p.drawPixmap(dest_rect, *source->pm, source_rect);
141 p.end();
142
143 if (mask) {
144 int mw = mask->pm->width();
145 int mh = mask->pm->height();
146
147 source_rect = QRectF(0, 0, mw, mh);
148 dest_rect = QRectF(mask_offset_x - x, mask_offset_y - y, mw, mh);
149 p.begin(cropped->pm);
150 p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
151 p.setRenderHint(QPainter::Antialiasing);
152 p.setRenderHint(QPainter::SmoothPixmapTransform);
153 p.drawPixmap(dest_rect, *mask->pm, source_rect);
154 p.end();
155 }
156
157 return cropped;
158}
159
160/************************************************************************/
164{
165 *width = sprite->pm->width();
166 *height = sprite->pm->height();
167}
168
169/************************************************************************/
172void qtg_free_sprite(struct sprite *s)
173{
174 delete s->pm;
175 delete s;
176}
177
178/************************************************************************/
182{
183 struct sprite *created = new sprite;
184
185 created->pm = new QPixmap(width, height);
186
187 created->pm->fill(pcolor->qcolor);
188
189 return created;
190}
191
192/************************************************************************/
196{
197 struct sprite *spr = new sprite;
198 QString ns;
199 int w, h;
200 QPixmap *pm;
201 // FIXME: This should not depend on city_productions font setting
203
204 if (gui()->map_scale != 1.0f && gui()->map_font_scale) {
205 int ssize = ceil(gui()->map_scale * fc_font::instance()->prod_fontsize);
206
207 if (qf->pointSize() != ssize) {
208 qf->setPointSize(ssize);
209 }
210 }
211
213
214 if (num > 20) {
215 ns = QString::number(num);
216 } else {
217#ifdef __cpp_char8_t
218 const char8_t
219#else
220 const char
221#endif
222 *numsbuf[21] = {
223 u8"\0xF0\0x9F\0x84\0x8C",
224 u8"\u278A",
225 u8"\u278B",
226 u8"\u278C",
227 u8"\u278D",
228 u8"\u278E",
229 u8"\u278F",
230 u8"\u2790",
231 u8"\u2791",
232 u8"\u2792",
233 u8"\u2793",
234 u8"\u24EB",
235 u8"\u24EC",
236 u8"\u24ED",
237 u8"\u24EE",
238 u8"\u24EF",
239 u8"\u24F0",
240 u8"\u24F1",
241 u8"\u24F2",
242 u8"\u24F3",
243 u8"\u24F4"
244 };
245
246#ifdef FC_QT5_MODE
247 ns = QString((const char *)numsbuf[num]);
248 }
249
250 w = fm.horizontalAdvance(ns);
251 h = fm.height();
252 pm = new QPixmap(w, h);
253 pm->fill(Qt::transparent);
254
255 QPainter paint(pm);
256
257 paint.setFont(*qf);
258 paint.setBrush(Qt::transparent);
259 paint.setPen(QColor(Qt::black));
260 paint.drawText(QRect(0, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
261 QString((const char *)u8"\u26AB"));
262
263 if (num > 20) {
264 paint.setPen(QColor(Qt::yellow));
265 paint.drawText(QRect(-2, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
266 QString((const char *)u8"\u2B24"));
267 paint.drawText(QRect(4, -2, w, h), Qt::AlignLeft | Qt::AlignVCenter,
268 QString((const char *)u8"\u2B24"));
269 paint.drawText(QRect(4, 2, w, h), Qt::AlignLeft | Qt::AlignVCenter,
270 QString((const char *)u8"\u2B24"));
271 paint.drawText(QRect(8, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
272 QString((const char *)u8"\u2B24"));
273 }
274#else // FC_QT5_MODE
275 ns = QString(numsbuf[num]);
276 }
277
278 w = fm.horizontalAdvance(ns);
279 h = fm.height();
280 pm = new QPixmap(w, h);
281 pm->fill(Qt::transparent);
282
283 QPainter paint(pm);
284
285 paint.setFont(*qf);
286 paint.setBrush(Qt::transparent);
287 paint.setPen(QColor(Qt::black));
288 paint.drawText(QRect(0, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
289 QString(u8"\u26AB"));
290
291 if (num > 20) {
292 paint.setPen(QColor(Qt::yellow));
293 paint.drawText(QRect(-2, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
294 QString(u8"\u2B24"));
295 paint.drawText(QRect(4, -2, w, h), Qt::AlignLeft | Qt::AlignVCenter,
296 QString(u8"\u2B24"));
297 paint.drawText(QRect(4, 2, w, h), Qt::AlignLeft | Qt::AlignVCenter,
298 QString(u8"\u2B24"));
299 paint.drawText(QRect(8, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
300 QString(u8"\u2B24"));
301 }
302#endif // FC_QT5_MODE
303
304 paint.setPen(QColor((num > 20) ? Qt::black : Qt::yellow));
305 paint.drawText(QRect(0, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter, ns);
306 paint.end();
307
308 spr->pm = pm;
309
310 return spr;
311}
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
struct canvas int int int int struct sprite *sprite struct canvas struct color * pcolor
Definition canvas_g.h:56
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
static fc_font * instance()
Definition fonts.cpp:41
QFont * get_font(QString name)
Definition fonts.cpp:63
char * incite_cost
Definition comments.c:76
static GtkWidget * source
Definition gotodlg.c:58
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define fc_malloc(sz)
Definition mem.h:34
static mpgui * gui
Definition mpgui_qt.cpp:52
const char *const city_productions
Definition fonts.h:33
struct sprite * qtg_crop_sprite(struct sprite *source, int x, int y, int width, int height, struct sprite *mask, int mask_offset_x, int mask_offset_y, float scale, bool smooth)
Definition sprite.cpp:104
static const char ** gfx_array_extensions
Definition sprite.cpp:28
void qtg_get_sprite_dimensions(struct sprite *sprite, int *width, int *height)
Definition sprite.cpp:163
struct sprite * qtg_load_gfxfile(const char *filename, bool svgflag)
Definition sprite.cpp:67
void qtg_free_sprite(struct sprite *s)
Definition sprite.cpp:172
struct sprite * qtg_load_gfxnumber(int num)
Definition sprite.cpp:195
struct sprite * qtg_create_sprite(int width, int height, struct color *pcolor)
Definition sprite.cpp:181
const char ** gfx_fileextensions(void)
Definition sprite.cpp:35
struct sprite int int int int struct sprite * mask
Definition sprite_g.h:32
struct sprite int int y
Definition sprite_g.h:31
struct sprite int int int int struct sprite int int float scale
Definition sprite_g.h:33
struct sprite int int int int struct sprite int mask_offset_x
Definition sprite_g.h:32
struct sprite int x
Definition sprite_g.h:31
struct sprite int int int int struct sprite int int mask_offset_y
Definition sprite_g.h:32
Definition colors.h:21
QPixmap * pm
Definition sprite.h:25
int tileset_hex_width(const struct tileset *t)
Definition tilespec.c:747
int tileset_hex_height(const struct tileset *t)
Definition tilespec.c:756