Freeciv-3.2: /fast/freeciv/nightly/S3_2/client/gui-sdl3/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/* SDL3 */
19#include <SDL3/SDL.h>
20#include <SDL3_image/SDL_image.h>
21
22/* utility */
23#include "fcintl.h"
24#include "log.h"
25#include "mem.h"
26
27/* client/gui-sdl3 */
28#include "colors.h"
29#include "graphics.h"
30
31#include "sprite.h"
32
33static struct sprite *ctor_sprite(SDL_Surface *surf);
34
35/************************************************************************/
40 const char **gfx_fileextensions(void)
41{
42 static const char *ext[] = {
43 "png",
44 "xpm",
45 NULL
46 };
47
48 return ext;
49}
50
51/************************************************************************/
56 struct sprite *load_gfxfile(const char *filename, bool svgflag)
57{
58 SDL_Surface *pbuf = NULL;
59
60 if ((pbuf = IMG_Load(filename)) == NULL) {
61 log_error(_("load_gfxfile: Unable to load graphic file %s!"), filename);
62 return NULL; /* Should I use abort() ? */
63 }
64
65 if (pbuf->format->palette != NULL) {
66 SDL_Surface *pnew = convert_surf(pbuf);
67
68 FREESURFACE(pbuf);
69 pbuf = pnew;
70 }
71
72 return ctor_sprite(pbuf);
73}
74
75/************************************************************************/
100 struct sprite *crop_sprite(struct sprite *source,
101 int x, int y, int width, int height,
102 struct sprite *mask,
103 int mask_offset_x, int mask_offset_y,
104 float scale, bool smooth)
105{
106 SDL_Rect src_rect = {(Sint16) x, (Sint16) y, (Uint16) width, (Uint16) height};
107 SDL_Surface *psrc = crop_rect_from_surface(GET_SURF(source), &src_rect);
108 SDL_Surface *pdest = NULL;
109
110 if (mask) {
111 pdest = mask_surface(psrc, mask->psurface, x - mask_offset_x, y - mask_offset_y);
112 FREESURFACE(psrc);
113 return ctor_sprite(pdest);
114 }
115
116 return ctor_sprite(psrc);
117}
118
119/************************************************************************/
122 struct sprite *create_sprite(int width, int height, struct color *pcolor)
123{
124 SDL_Surface *mypixbuf = NULL;
125
126 fc_assert_ret_val(width > 0, NULL);
127 fc_assert_ret_val(height > 0, NULL);
128 fc_assert_ret_val(pcolor != NULL, NULL);
129
130 mypixbuf = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGBA8888);
131
132 SDL_FillSurfaceRect(mypixbuf, NULL,
133 SDL_MapRGBA(mypixbuf->format,
134 pcolor->color->r,
135 pcolor->color->g,
136 pcolor->color->b,
137 pcolor->color->a));
138
139 return ctor_sprite(mypixbuf);
140}
141
142/************************************************************************/
145 void get_sprite_dimensions(struct sprite *sprite, int *width, int *height)
146{
147 *width = GET_SURF(sprite)->w;
148 *height = GET_SURF(sprite)->h;
149}
150
151/************************************************************************/
154 void free_sprite(struct sprite *s)
155{
156 fc_assert_ret(s != NULL);
157 FREESURFACE(GET_SURF_REAL(s));
158 FC_FREE(s);
159}
160
161/************************************************************************/
164 static struct sprite *ctor_sprite(SDL_Surface *surf)
165{
166 struct sprite *result = fc_malloc(sizeof(struct sprite));
167
168 result->psurface = surf;
169
170 return result;
171}
172
173/************************************************************************/
176 struct sprite *load_gfxnumber(int num)
177{
178 /* Not supported in sdl3-client */
179 return NULL;
180}
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