Freeciv-3.1
Loading...
Searching...
No Matches
sprite.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2005 The Freeciv Team
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/* SDL2 */
19#ifdef SDL2_PLAIN_INCLUDE
20#include <SDL.h>
21#include <SDL_image.h>
22#else /* SDL2_PLAIN_INCLUDE */
23#include <SDL2/SDL.h>
24#include <SDL2/SDL_image.h>
25#endif /* SDL2_PLAIN_INCLUDE */
26
27/* utility */
28#include "fcintl.h"
29#include "log.h"
30#include "mem.h"
31
32/* client/gui-sdl2 */
33#include "colors.h"
34#include "graphics.h"
35
36#include "sprite.h"
37
38static struct sprite *ctor_sprite(SDL_Surface *surf);
39
40/************************************************************************/
45const char **gfx_fileextensions(void)
46{
47 static const char *ext[] = {
48 "png",
49 "gif",
50 "jpeg",
51 "xpm",
52 "qoi",
53 "tga",
54 "bmp",
55 "xcf",
56 "pcx",
57 "lbm",
58 "ppm",
59 "pgm",
60 "pbm",
61 "svg",
62 NULL
63 };
64
65 return ext;
66}
67
68/************************************************************************/
73struct sprite *load_gfxfile(const char *filename)
74{
75 SDL_Surface *pbuf = NULL;
76
77 if ((pbuf = IMG_Load(filename)) == NULL) {
78 log_error(_("load_gfxfile: Unable to load graphic file %s!"), filename);
79 return NULL; /* Should I use abort() ? */
80 }
81
82 if (pbuf->format->palette != NULL) {
83 SDL_Surface *pnew = convert_surf(pbuf);
84
85 FREESURFACE(pbuf);
86 pbuf = pnew;
87 }
88
89 return ctor_sprite(pbuf);
90}
91
92/************************************************************************/
118 int x, int y, int width, int height,
119 struct sprite *mask,
120 int mask_offset_x, int mask_offset_y,
121 float scale, bool smooth)
122{
123 SDL_Rect src_rect = {(Sint16) x, (Sint16) y, (Uint16) width, (Uint16) height};
124 SDL_Surface *psrc = crop_rect_from_surface(GET_SURF(source), &src_rect);
125 SDL_Surface *pdest = NULL;
126
127 if (mask) {
128 pdest = mask_surface(psrc, mask->psurface, x - mask_offset_x, y - mask_offset_y);
129 FREESURFACE(psrc);
130 return ctor_sprite(pdest);
131 }
132
133 return ctor_sprite(psrc);
134}
135
136/************************************************************************/
139struct sprite *create_sprite(int width, int height, struct color *pcolor)
140{
141 SDL_Surface *mypixbuf = NULL;
142
143 fc_assert_ret_val(width > 0, NULL);
144 fc_assert_ret_val(height > 0, NULL);
145 fc_assert_ret_val(pcolor != NULL, NULL);
146
147 if (is_bigendian()) {
148 mypixbuf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
149 0x0000FF00, 0x00FF0000,
150 0xFF000000, 0x000000FF);
151 } else {
152 mypixbuf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
153 0x00FF0000, 0x0000FF00,
154 0x000000FF, 0xFF000000);
155 }
156
157 SDL_FillRect(mypixbuf, NULL,
158 SDL_MapRGBA(mypixbuf->format,
159 pcolor->color->r,
160 pcolor->color->g,
161 pcolor->color->b,
162 pcolor->color->a));
163
164 return ctor_sprite(mypixbuf);
165}
166
167/************************************************************************/
171{
172 *width = GET_SURF(sprite)->w;
173 *height = GET_SURF(sprite)->h;
174}
175
176/************************************************************************/
179void free_sprite(struct sprite *s)
180{
181 fc_assert_ret(s != NULL);
183 FC_FREE(s);
184}
185
186/************************************************************************/
189static struct sprite *ctor_sprite(SDL_Surface *surf)
190{
191 struct sprite *result = fc_malloc(sizeof(struct sprite));
192
193 result->psurface = surf;
194
195 return result;
196}
197
198/************************************************************************/
201struct sprite *load_gfxnumber(int num)
202{
203 /* Not supported in sdl2-client */
204 return NULL;
205}
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
struct canvas int int struct sprite bool int int fog_y struct canvas struct sprite struct color * pcolor
Definition canvas_g.h:57
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
#define _(String)
Definition fcintl.h:67
static GtkWidget * source
Definition gotodlg.c:58
void get_sprite_dimensions(struct sprite *sprite, int *width, int *height)
Definition sprite.c:107
void free_sprite(struct sprite *s)
Definition sprite.c:278
struct sprite * load_gfxnumber(int num)
Definition sprite.c:526
struct sprite * load_gfxfile(const char *filename)
Definition sprite.c:170
struct sprite * 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.c:52
const char ** gfx_fileextensions(void)
Definition sprite.c:117
SDL_Surface * mask_surface(SDL_Surface *src, SDL_Surface *mask, int mask_offset_x, int mask_offset_y)
Definition graphics.c:262
SDL_Surface * crop_rect_from_surface(SDL_Surface *psource, SDL_Rect *prect)
Definition graphics.c:236
SDL_Surface * convert_surf(SDL_Surface *surf_in)
Definition graphics.c:359
#define FREESURFACE(ptr)
Definition graphics.h:322
static struct sprite * ctor_sprite(SDL_Surface *surf)
Definition sprite.c:189
#define GET_SURF(m_sprite)
Definition sprite.h:29
#define GET_SURF_REAL(m_sprite)
Definition sprite.h:26
#define fc_assert_ret(condition)
Definition log.h:191
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_error(message,...)
Definition log.h:103
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_malloc(sz)
Definition mem.h:34
create_sprite
Definition sprite_g.h:30
Definition colors.h:20
struct SDL_Surface * psurface
Definition sprite.h:21
static bool is_bigendian(void)
Definition support.h:207