Freeciv-3.2: /fast/freeciv/nightly/S3_2/client/gui-sdl2/sprite.c Source File

Freeciv-3.2

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/************************************************************************/
45 const char **gfx_fileextensions(void)
46{
47 static const char *ext[] = {
48 "png",
49 "xpm",
50 NULL
51 };
52
53 return ext;
54}
55
56/************************************************************************/
61 struct sprite *load_gfxfile(const char *filename, bool svgflag)
62{
63 SDL_Surface *pbuf = NULL;
64
65 if ((pbuf = IMG_Load(filename)) == NULL) {
66 log_error(_("load_gfxfile: Unable to load graphic file %s!"), filename);
67 return NULL; /* Should I use abort() ? */
68 }
69
70 if (pbuf->format->palette != NULL) {
71 SDL_Surface *pnew = convert_surf(pbuf);
72
73 FREESURFACE(pbuf);
74 pbuf = pnew;
75 }
76
77 return ctor_sprite(pbuf);
78}
79
80/************************************************************************/
105 struct sprite *crop_sprite(struct sprite *source,
106 int x, int y, int width, int height,
107 struct sprite *mask,
108 int mask_offset_x, int mask_offset_y,
109 float scale, bool smooth)
110{
111 SDL_Rect src_rect = {(Sint16) x, (Sint16) y, (Uint16) width, (Uint16) height};
112 SDL_Surface *psrc = crop_rect_from_surface(GET_SURF(source), &src_rect);
113 SDL_Surface *pdest = NULL;
114
115 if (mask) {
116 pdest = mask_surface(psrc, mask->psurface, x - mask_offset_x, y - mask_offset_y);
117 FREESURFACE(psrc);
118 return ctor_sprite(pdest);
119 }
120
121 return ctor_sprite(psrc);
122}
123
124/************************************************************************/
127 struct sprite *create_sprite(int width, int height, struct color *pcolor)
128{
129 SDL_Surface *mypixbuf = NULL;
130
131 fc_assert_ret_val(width > 0, NULL);
132 fc_assert_ret_val(height > 0, NULL);
133 fc_assert_ret_val(pcolor != NULL, NULL);
134
135 if (is_bigendian()) {
136 mypixbuf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
137 0x0000FF00, 0x00FF0000,
138 0xFF000000, 0x000000FF);
139 } else {
140 mypixbuf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
141 0x00FF0000, 0x0000FF00,
142 0x000000FF, 0xFF000000);
143 }
144
145 SDL_FillRect(mypixbuf, NULL,
146 SDL_MapRGBA(mypixbuf->format,
147 pcolor->color->r,
148 pcolor->color->g,
149 pcolor->color->b,
150 pcolor->color->a));
151
152 return ctor_sprite(mypixbuf);
153}
154
155/************************************************************************/
158 void get_sprite_dimensions(struct sprite *sprite, int *width, int *height)
159{
160 *width = GET_SURF(sprite)->w;
161 *height = GET_SURF(sprite)->h;
162}
163
164/************************************************************************/
167 void free_sprite(struct sprite *s)
168{
169 fc_assert_ret(s != NULL);
170 FREESURFACE(GET_SURF_REAL(s));
171 FC_FREE(s);
172}
173
174/************************************************************************/
177 static struct sprite *ctor_sprite(SDL_Surface *surf)
178{
179 struct sprite *result = fc_malloc(sizeof(struct sprite));
180
181 result->psurface = surf;
182
183 return result;
184}
185
186/************************************************************************/
189 struct sprite *load_gfxnumber(int num)
190{
191 /* Not supported in sdl2-client */
192 return NULL;
193}
height
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
pcolor
struct canvas int int int int struct sprite *sprite struct canvas struct color * pcolor
Definition canvas_g.h:56
width
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
incite_cost
char * incite_cost
Definition comments.c:74
fcintl.h
_
#define _(String)
Definition fcintl.h:67
source
static GtkWidget * source
Definition gotodlg.c:58
free_sprite
void free_sprite(struct sprite *s)
Definition sprite.c:278
create_sprite
struct sprite * create_sprite(int width, int height, struct color *pcolor)
Definition sprite.c:84
load_gfxnumber
struct sprite * load_gfxnumber(int num)
Definition sprite.c:526
load_gfxfile
struct sprite * load_gfxfile(const char *filename, bool svgflag)
Definition sprite.c:170
gfx_fileextensions
const char ** gfx_fileextensions(void)
Definition sprite.c:117
mask_surface
SDL_Surface * mask_surface(SDL_Surface *src, SDL_Surface *mask, int mask_offset_x, int mask_offset_y)
Definition graphics.c:262
crop_rect_from_surface
SDL_Surface * crop_rect_from_surface(SDL_Surface *psource, SDL_Rect *prect)
Definition graphics.c:236
convert_surf
SDL_Surface * convert_surf(SDL_Surface *surf_in)
Definition graphics.c:359
FREESURFACE
#define FREESURFACE(ptr)
Definition graphics.h:322
ctor_sprite
static struct sprite * ctor_sprite(SDL_Surface *surf)
Definition sprite.c:177
GET_SURF
#define GET_SURF(m_sprite)
Definition sprite.h:29
GET_SURF_REAL
#define GET_SURF_REAL(m_sprite)
Definition sprite.h:26
colors.h
graphics.h
sprite.h
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
log_error
#define log_error(message,...)
Definition log.h:103
mem.h
FC_FREE
#define FC_FREE(ptr)
Definition mem.h:41
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
mask
struct sprite int int int int struct sprite * mask
Definition sprite_g.h:32
y
struct sprite int int y
Definition sprite_g.h:31
scale
struct sprite int int int int struct sprite int int float scale
Definition sprite_g.h:33
mask_offset_x
struct sprite int int int int struct sprite int mask_offset_x
Definition sprite_g.h:32
x
struct sprite int x
Definition sprite_g.h:31
crop_sprite
crop_sprite
Definition sprite_g.h:30
get_sprite_dimensions
struct sprite int int int int struct sprite int int float bool smooth get_sprite_dimensions
Definition sprite_g.h:36
mask_offset_y
struct sprite int int int int struct sprite int int mask_offset_y
Definition sprite_g.h:32
color
Definition colors.h:21
sprite
Definition sprite.h:22
sprite::psurface
struct SDL_Surface * psurface
Definition sprite.h:21
is_bigendian
static bool is_bigendian(void)
Definition support.h:229