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)
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 ***********************************************************************/
15 #include <fc_config.h>
19 #include <QImageReader>
24 #include "fc_client.h"
25 #include "qtg_cxxside.h"
28 static const char **gfx_array_extensions
= nullptr;
30 /****************************************************************************
31 Return a NULL-terminated, permanently allocated array of possible
32 graphics types extensions. Extensions listed first will be checked
34 ****************************************************************************/
35 const char **gfx_fileextensions(void)
37 QList
<QByteArray
> gfx_ext
;
41 if (gfx_array_extensions
!= nullptr) {
42 return gfx_array_extensions
;
45 gfx_ext
= QImageReader::supportedImageFormats();
47 gfx_array_extensions
= new const char *[gfx_ext
.count()];
48 while (gfx_ext
.isEmpty() == false) {
50 cp
= gfx_ext
.takeFirst();
51 ext
= static_cast<char *>(fc_malloc(sizeof(cp
)));
52 strncpy(ext
, cp
.data(), sizeof(cp
));
53 gfx_array_extensions
[j
] = ext
;
57 return gfx_array_extensions
;
60 /****************************************************************************
61 Load the given graphics file into a sprite. This function loads an
62 entire image file, which may later be broken up into individual sprites
64 ****************************************************************************/
65 struct sprite
*qtg_load_gfxfile(const char *filename
)
67 sprite
*entire
= new sprite
;
69 entire
->pm
= new QPixmap(filename
);
74 /****************************************************************************
75 Create a new sprite by cropping and taking only the given portion of
78 source gives the sprite that is to be cropped.
80 x,y, width, height gives the rectangle to be cropped. The pixel at
81 position of the source sprite will be at (0,0) in the new sprite, and
82 the new sprite will have dimensions (width, height).
84 mask gives an additional mask to be used for clipping the new
85 sprite. Only the transparency value of the mask is used in
86 crop_sprite. The formula is: dest_trans = src_trans *
87 mask_trans. Note that because the transparency is expressed as an
88 integer it is common to divide it by 256 afterwards.
90 mask_offset_x, mask_offset_y is the offset of the mask relative to the
91 origin of the source image. The pixel at (mask_offset_x,mask_offset_y)
92 in the mask image will be used to clip pixel (0,0) in the source image
93 which is pixel (-x,-y) in the new image.
94 ****************************************************************************/
95 struct sprite
*qtg_crop_sprite(struct sprite
*source
,
96 int x
, int y
, int width
, int height
,
98 int mask_offset_x
, int mask_offset_y
,
99 float scale
, bool smooth
)
109 if (!width
|| !height
) {
113 if (scale
!= 1.0f
&& (tileset_hex_height(tileset
) > 0
114 || tileset_hex_width(tileset
) > 0)) {
117 widthzoom
= ceil(width
* scale
) + hex
;
118 heightzoom
= ceil(height
* scale
) + hex
;
119 cropped
= new sprite
;
120 cropped
->pm
= new QPixmap(widthzoom
, heightzoom
);
121 cropped
->pm
->fill(Qt::transparent
);
122 source_rect
= QRectF(x
, y
, width
, height
);
123 dest_rect
= QRectF(0, 0, widthzoom
, heightzoom
);
125 p
.begin(cropped
->pm
);
127 p
.setRenderHint(QPainter::SmoothPixmapTransform
);
129 p
.setRenderHint(QPainter::Antialiasing
);
130 p
.drawPixmap(dest_rect
, *source
->pm
, source_rect
);
134 int mw
= mask
->pm
->width();
135 int mh
= mask
->pm
->height();
137 source_rect
= QRectF(0, 0, mw
, mh
);
138 dest_rect
= QRectF(mask_offset_x
- x
, mask_offset_y
- y
, mw
, mh
);
139 p
.begin(cropped
->pm
);
140 p
.setCompositionMode(QPainter::CompositionMode_DestinationIn
);
141 p
.setRenderHint(QPainter::Antialiasing
);
142 p
.setRenderHint(QPainter::SmoothPixmapTransform
);
143 p
.drawPixmap(dest_rect
, *mask
->pm
, source_rect
);
150 /****************************************************************************
151 Find the dimensions of the sprite.
152 ****************************************************************************/
153 void qtg_get_sprite_dimensions(struct sprite
*sprite
, int *width
, int *height
)
155 *width
= sprite
->pm
->width();
156 *height
= sprite
->pm
->height();
159 /****************************************************************************
160 Free a sprite and all associated image data.
161 ****************************************************************************/
162 void qtg_free_sprite(struct sprite
*s
)
168 /****************************************************************************
169 Create a new sprite with the given height, width and color.
170 ****************************************************************************/
171 struct sprite
*qtg_create_sprite(int width
, int height
, struct color
*pcolor
)
173 struct sprite
*created
= new sprite
;
175 created
->pm
= new QPixmap(width
, height
);
177 created
->pm
->fill(pcolor
->qcolor
);