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)
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 #ifdef SDL2_PLAIN_INCLUDE
21 #include <SDL_image.h>
22 #else /* SDL2_PLAIN_INCLUDE */
24 #include <SDL2/SDL_image.h>
25 #endif /* SDL2_PLAIN_INCLUDE */
38 static struct sprite
*ctor_sprite(SDL_Surface
*pSurface
);
40 /****************************************************************************
41 Return a NULL-terminated, permanently allocated array of possible
42 graphics types extensions. Extensions listed first will be checked
44 ****************************************************************************/
45 const char **gfx_fileextensions(void)
47 static const char *ext
[] = {
56 /****************************************************************************
57 Load the given graphics file into a sprite. This function loads an
58 entire image file, which may later be broken up into individual sprites
60 ****************************************************************************/
61 struct sprite
*load_gfxfile(const char *filename
)
63 SDL_Surface
*pbuf
= NULL
;
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() ? */
70 if (pbuf
->format
->palette
!= NULL
) {
71 SDL_Surface
*pnew
= convert_surf(pbuf
);
77 return ctor_sprite(pbuf
);
80 /****************************************************************************
81 Create a new sprite by cropping and taking only the given portion of
84 source gives the sprite that is to be cropped.
86 x,y, width, height gives the rectangle to be cropped. The pixel at
87 position of the source sprite will be at (0,0) in the new sprite, and
88 the new sprite will have dimensions (width, height).
90 mask gives an additional mask to be used for clipping the new
91 sprite. Only the transparency value of the mask is used in
92 crop_sprite. The formula is: dest_trans = src_trans *
93 mask_trans. Note that because the transparency is expressed as an
94 integer it is common to divide it by 256 afterwards.
96 mask_offset_x, mask_offset_y is the offset of the mask relative to the
97 origin of the source image. The pixel at (mask_offset_x,mask_offset_y)
98 in the mask image will be used to clip pixel (0,0) in the source image
99 which is pixel (-x,-y) in the new image.
101 scale gives scale of new tileset
102 smooth means if scaling might be bilinear, if set to false use nearest
104 ****************************************************************************/
105 struct sprite
*crop_sprite(struct sprite
*source
,
106 int x
, int y
, int width
, int height
,
108 int mask_offset_x
, int mask_offset_y
,
109 float scale
, bool smooth
)
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
;
116 pDest
= mask_surface(pSrc
, mask
->psurface
, x
- mask_offset_x
, y
- mask_offset_y
);
118 return ctor_sprite(pDest
);
121 return ctor_sprite(pSrc
);
124 /****************************************************************************
125 Create a sprite with the given height, width and color.
126 ****************************************************************************/
127 struct sprite
*create_sprite(int width
, int height
, struct color
*pcolor
)
129 SDL_Surface
*mypixbuf
= NULL
;
131 fc_assert_ret_val(width
> 0, NULL
);
132 fc_assert_ret_val(height
> 0, NULL
);
133 fc_assert_ret_val(pcolor
!= NULL
, NULL
);
135 mypixbuf
= SDL_CreateRGBSurface(SDL_SWSURFACE
, width
, height
, 32,
136 #if SDL_BYTEORDER != SDL_LIL_ENDIAN
137 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF
139 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000
143 SDL_FillRect(mypixbuf
, NULL
,
144 SDL_MapRGBA(mypixbuf
->format
,
150 return ctor_sprite(mypixbuf
);
153 /****************************************************************************
154 Find the dimensions of the sprite.
155 ****************************************************************************/
156 void get_sprite_dimensions(struct sprite
*sprite
, int *width
, int *height
)
158 *width
= GET_SURF(sprite
)->w
;
159 *height
= GET_SURF(sprite
)->h
;
162 /****************************************************************************
163 Free a sprite and all associated image data.
164 ****************************************************************************/
165 void free_sprite(struct sprite
*s
)
167 fc_assert_ret(s
!= NULL
);
168 FREESURFACE(GET_SURF_REAL(s
));
172 /**************************************************************************
173 Create a sprite struct and fill it with SDL_Surface pointer
174 **************************************************************************/
175 static struct sprite
*ctor_sprite(SDL_Surface
*pSurface
)
177 struct sprite
*result
= fc_malloc(sizeof(struct sprite
));
179 result
->psurface
= pSurface
;