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 <QChar> // Needed before QImageReader (Qt-6.11 & gcc-16)
20#include <QImageReader>
21#include <QPainter>
22
23// gui-qt
24#include "colors.h"
25#include "fc_client.h"
26#include "qtg_cxxside.h"
27#include "sprite.h"
28
29static const char **gfx_array_extensions = nullptr;
30
31/************************************************************************/
36const char **gfx_fileextensions(void)
37{
39 QList<QByteArray>::iterator iter;
40 int j = 0;
41
42 if (gfx_array_extensions != nullptr) {
44 }
45
46 gfx_ext = QImageReader::supportedImageFormats();
47
48 gfx_array_extensions = new const char *[gfx_ext.count() + 1];
49 for (iter = gfx_ext.begin(); iter != gfx_ext.end(); iter++) {
50 char *ext;
51 int extlen = iter->size() + 1;
52
53 ext = static_cast<char *>(fc_malloc(extlen));
54 strncpy(ext, iter->data(), extlen);
56 j++;
57 }
58 gfx_array_extensions[j] = nullptr;
59
61}
62
63/************************************************************************/
68struct sprite *qtg_load_gfxfile(const char *filename, bool svgflag)
69{
70 sprite *entire = new sprite;
71 QPixmap *pm = new QPixmap;
72
73 if (QPixmapCache::find(QString(filename), pm)) {
74 entire->pm = pm;
75 return entire;
76 }
77 pm->load(QString(filename));
78 entire->pm = pm;
79 QPixmapCache::insert(QString(filename), *pm);
80
81 return entire;
82}
83
84/************************************************************************/
106 int x, int y, int width, int height,
107 struct sprite *mask,
109 float scale, bool smooth)
110{
111 QPainter p;
113 QRectF dest_rect;
115 int widthzoom;
116 int heightzoom;
117 int hex = 0;
118
119 fc_assert_ret_val(source, nullptr);
120
121 if (!width || !height) {
122 return nullptr;
123 }
124 if (scale != 1.0f && (tileset_hex_height(tileset) > 0
125 || tileset_hex_width(tileset) > 0)) {
126 hex = 1;
127 }
130 cropped = new sprite;
132 cropped->pm->fill(Qt::transparent);
134 dest_rect = QRectF(0, 0, widthzoom, heightzoom);
135
136 p.begin(cropped->pm);
137 if (smooth) {
138 p.setRenderHint(QPainter::SmoothPixmapTransform);
139 }
140 p.setRenderHint(QPainter::Antialiasing);
141 p.drawPixmap(dest_rect, *source->pm, source_rect);
142 p.end();
143
144 if (mask) {
145 int mw = mask->pm->width();
146 int mh = mask->pm->height();
147
148 source_rect = QRectF(0, 0, mw, mh);
149 dest_rect = QRectF(mask_offset_x - x, mask_offset_y - y, mw, mh);
150 p.begin(cropped->pm);
151 p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
152 p.setRenderHint(QPainter::Antialiasing);
153 p.setRenderHint(QPainter::SmoothPixmapTransform);
154 p.drawPixmap(dest_rect, *mask->pm, source_rect);
155 p.end();
156 }
157
158 return cropped;
159}
160
161/************************************************************************/
165{
166 *width = sprite->pm->width();
167 *height = sprite->pm->height();
168}
169
170/************************************************************************/
173void qtg_free_sprite(struct sprite *s)
174{
175 delete s->pm;
176 delete s;
177}
178
179/************************************************************************/
183{
184 struct sprite *created = new sprite;
185
186 created->pm = new QPixmap(width, height);
187
188 created->pm->fill(pcolor->qcolor);
189
190 return created;
191}
192
193/************************************************************************/
197{
198 struct sprite *spr = new sprite;
199 QString ns;
200 int w, h;
201 QPixmap *pm;
202 // FIXME: This should not depend on city_productions font setting
204
205 if (gui()->map_scale != 1.0f && gui()->map_font_scale) {
206 int ssize = ceil(gui()->map_scale * fc_font::instance()->prod_fontsize);
207
208 if (qf->pointSize() != ssize) {
209 qf->setPointSize(ssize);
210 }
211 }
212
214
215 if (num > 20) {
216 ns = QString::number(num);
217 } else {
218#ifdef __cpp_char8_t
219 const char8_t
220#else
221 const char
222#endif
223 *numsbuf[21] = {
224 u8"\0xF0\0x9F\0x84\0x8C",
225 u8"\u278A",
226 u8"\u278B",
227 u8"\u278C",
228 u8"\u278D",
229 u8"\u278E",
230 u8"\u278F",
231 u8"\u2790",
232 u8"\u2791",
233 u8"\u2792",
234 u8"\u2793",
235 u8"\u24EB",
236 u8"\u24EC",
237 u8"\u24ED",
238 u8"\u24EE",
239 u8"\u24EF",
240 u8"\u24F0",
241 u8"\u24F1",
242 u8"\u24F2",
243 u8"\u24F3",
244 u8"\u24F4"
245 };
246
247#ifdef FC_QT5_MODE
248 ns = QString((const char *)numsbuf[num]);
249 }
250
251 w = fm.horizontalAdvance(ns);
252 h = fm.height();
253 pm = new QPixmap(w, h);
254 pm->fill(Qt::transparent);
255
256 QPainter paint(pm);
257
258 paint.setFont(*qf);
259 paint.setBrush(Qt::transparent);
260 paint.setPen(QColor(Qt::black));
261 paint.drawText(QRect(0, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
262 QString((const char *)u8"\u26AB"));
263
264 if (num > 20) {
265 paint.setPen(QColor(Qt::yellow));
266 paint.drawText(QRect(-2, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
267 QString((const char *)u8"\u2B24"));
268 paint.drawText(QRect(4, -2, w, h), Qt::AlignLeft | Qt::AlignVCenter,
269 QString((const char *)u8"\u2B24"));
270 paint.drawText(QRect(4, 2, w, h), Qt::AlignLeft | Qt::AlignVCenter,
271 QString((const char *)u8"\u2B24"));
272 paint.drawText(QRect(8, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
273 QString((const char *)u8"\u2B24"));
274 }
275#else // FC_QT5_MODE
276 ns = QString(numsbuf[num]);
277 }
278
279 w = fm.horizontalAdvance(ns);
280 h = fm.height();
281 pm = new QPixmap(w, h);
282 pm->fill(Qt::transparent);
283
284 QPainter paint(pm);
285
286 paint.setFont(*qf);
287 paint.setBrush(Qt::transparent);
288 paint.setPen(QColor(Qt::black));
289 paint.drawText(QRect(0, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
290 QString(u8"\u26AB"));
291
292 if (num > 20) {
293 paint.setPen(QColor(Qt::yellow));
294 paint.drawText(QRect(-2, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
295 QString(u8"\u2B24"));
296 paint.drawText(QRect(4, -2, w, h), Qt::AlignLeft | Qt::AlignVCenter,
297 QString(u8"\u2B24"));
298 paint.drawText(QRect(4, 2, w, h), Qt::AlignLeft | Qt::AlignVCenter,
299 QString(u8"\u2B24"));
300 paint.drawText(QRect(8, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter,
301 QString(u8"\u2B24"));
302 }
303#endif // FC_QT5_MODE
304
305 paint.setPen(QColor((num > 20) ? Qt::black : Qt::yellow));
306 paint.drawText(QRect(0, 0, w, h), Qt::AlignLeft | Qt::AlignVCenter, ns);
307 paint.end();
308
309 spr->pm = pm;
310
311 return spr;
312}
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:105
static const char ** gfx_array_extensions
Definition sprite.cpp:29
void qtg_get_sprite_dimensions(struct sprite *sprite, int *width, int *height)
Definition sprite.cpp:164
struct sprite * qtg_load_gfxfile(const char *filename, bool svgflag)
Definition sprite.cpp:68
void qtg_free_sprite(struct sprite *s)
Definition sprite.cpp:173
struct sprite * qtg_load_gfxnumber(int num)
Definition sprite.cpp:196
struct sprite * qtg_create_sprite(int width, int height, struct color *pcolor)
Definition sprite.cpp:182
const char ** gfx_fileextensions(void)
Definition sprite.cpp:36
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