Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-sdl2/canvas.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
canvas.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_ttf.h>
22#else /* SDL2_PLAIN_INCLUDE */
23#include <SDL2/SDL.h>
24#include <SDL2/SDL_ttf.h>
25#endif /* SDL2_PLAIN_INCLUDE */
26
27/* utility */
28#include "log.h"
29#include "mem.h"
30
31/* client/gui-sdl2 */
32#include "colors.h"
33#include "graphics.h"
34#include "gui_main.h"
35#include "gui_string.h"
36#include "sprite.h"
37
38#include "canvas.h"
39
40/**********************************************************************/
43 struct canvas *canvas_create(int width, int height)
44{
45 struct canvas *result = fc_malloc(sizeof(*result));
46
47 result->surf = create_surf(width, height, SDL_SWSURFACE);
48
49 return result;
50}
51
52/**********************************************************************/
56 void canvas_free(struct canvas *store)
57{
58 FREESURFACE(store->surf);
59 free(store);
60}
61
62/**********************************************************************/
65 void canvas_set_zoom(struct canvas *store, float zoom)
66{
67 /* sdl2-client has no zoom support */
68}
69
70/**********************************************************************/
73 bool has_zoom_support(void)
74{
75 return FALSE;
76}
77
78/**********************************************************************/
81 void canvas_mapview_init(struct canvas *store)
82{
83 SDL_SetSurfaceBlendMode(store->surf, SDL_BLENDMODE_NONE);
84}
85
86/**********************************************************************/
89 void canvas_copy(struct canvas *dest, struct canvas *src,
90 int src_x, int src_y, int dest_x, int dest_y, int width,
91 int height)
92{
93 SDL_Rect src_rect = {src_x, src_y, width, height};
94 SDL_Rect dest_rect = {dest_x, dest_y, width, height};
95
96 alphablit(src->surf, &src_rect, dest->surf, &dest_rect, 255);
97}
98
99/**********************************************************************/
102 void canvas_put_sprite(struct canvas *pcanvas,
103 int canvas_x, int canvas_y,
104 struct sprite *sprite,
105 int offset_x, int offset_y, int width, int height)
106{
107 SDL_Rect src = {offset_x, offset_y, width, height};
108 SDL_Rect dst = {canvas_x + offset_x, canvas_y + offset_y, 0, 0};
109
110 alphablit(GET_SURF(sprite), &src, pcanvas->surf, &dst, 255);
111}
112
113/**********************************************************************/
116 void canvas_put_sprite_full(struct canvas *pcanvas,
117 int canvas_x, int canvas_y,
118 struct sprite *sprite)
119{
120 SDL_Rect dst = {canvas_x, canvas_y, 0, 0};
121
122 alphablit(GET_SURF(sprite), NULL, pcanvas->surf, &dst, 255);
123}
124
125/************************************************************************/
128 void canvas_put_sprite_full_scaled(struct canvas *pcanvas,
129 int canvas_x, int canvas_y,
130 int canvas_w, int canvas_h,
131 struct sprite *sprite)
132{
133 /* This should never be called as we have not enabled support
134 * in this client yet. */
135 fc_assert(FALSE);
136}
137
138/**********************************************************************/
142 void canvas_put_sprite_fogged(struct canvas *pcanvas,
143 int canvas_x, int canvas_y,
144 struct sprite *psprite,
145 bool fog, int fog_x, int fog_y)
146{
147 SDL_Rect dst = {canvas_x, canvas_y, 0, 0};
148
149 if (fog) {
150 alphablit(GET_SURF(psprite), NULL, pcanvas->surf, &dst, 160);
151 } else {
152 canvas_put_sprite_full(pcanvas, canvas_x, canvas_y, psprite);
153 }
154}
155
156/**********************************************************************/
159 void canvas_put_rectangle(struct canvas *pcanvas, struct color *pcolor,
160 int canvas_x, int canvas_y, int width, int height)
161{
162 SDL_Rect dst = {canvas_x, canvas_y, width, height};
163
164 SDL_FillRect(pcanvas->surf, &dst, SDL_MapRGBA(pcanvas->surf->format,
165 pcolor->color->r,
166 pcolor->color->g,
167 pcolor->color->b,
168 pcolor->color->a));
169}
170
171/**********************************************************************/
174 void canvas_fill_sprite_area(struct canvas *pcanvas,
175 struct sprite *psprite, struct color *pcolor,
176 int canvas_x, int canvas_y)
177{
178 SDL_Rect dst = {canvas_x, canvas_y,
179 GET_SURF(psprite)->w,
180 GET_SURF(psprite)->h};
181
182 SDL_FillRect(pcanvas->surf, &dst, SDL_MapRGBA(pcanvas->surf->format,
183 pcolor->color->r,
184 pcolor->color->g,
185 pcolor->color->b,
186 pcolor->color->a));
187}
188
189/**********************************************************************/
192 void canvas_put_line(struct canvas *pcanvas, struct color *pcolor,
193 enum line_type ltype, int start_x, int start_y,
194 int dx, int dy)
195{
196 create_line(pcanvas->surf, start_x, start_y, start_x + dx, start_y + dy, pcolor->color);
197}
198
199/**********************************************************************/
202 void canvas_put_curved_line(struct canvas *pcanvas, struct color *pcolor,
203 enum line_type ltype, int start_x, int start_y,
204 int dx, int dy)
205{
206 /* FIXME: Implement curved line drawing. */
207 canvas_put_line(pcanvas, pcolor, ltype, start_x, start_y, dx, dy);
208}
209
210/**********************************************************************/
215 void get_text_size(int *width, int *height,
216 enum client_font font, const char *text)
217{
218 utf8_str *ptext = create_utf8_str(NULL, 0, *client_font_sizes[font]);
219 SDL_Rect size;
220
221 fc_assert(width != NULL || height != NULL);
222
223 copy_chars_to_utf8_str(ptext, text);
224 utf8_str_size(ptext, &size);
225
226 if (width) {
227 *width = size.w;
228 }
229
230 if (height) {
231 *height = size.h;
232 }
233
234 FREEUTF8STR(ptext);
235}
236
237/**********************************************************************/
242 void canvas_put_text(struct canvas *pcanvas, int canvas_x, int canvas_y,
243 enum client_font font, struct color *pcolor,
244 const char *text)
245{
246 SDL_Surface *ptmp;
247 utf8_str *ptext = create_utf8_str(NULL, 0, *client_font_sizes[font]);
248
249 copy_chars_to_utf8_str(ptext, text);
250
251 ptext->fgcol = *pcolor->color;
252 ptext->bgcol = (SDL_Color) {0, 0, 0, 0};
253
254 ptmp = create_text_surf_from_utf8(ptext);
255
256 blit_entire_src(ptmp, pcanvas->surf, canvas_x, canvas_y);
257
258 FREEUTF8STR(ptext);
259 FREESURFACE(ptmp);
260}
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
canvas_put_rectangle
struct canvas int int int int struct sprite *sprite canvas_put_rectangle
Definition canvas_g.h:55
canvas_y
struct canvas int int canvas_y
Definition canvas_g.h:43
canvas_x
struct canvas int canvas_x
Definition canvas_g.h:43
canvas_put_line
struct canvas int int int int struct sprite *sprite struct canvas struct color int int int int height canvas_put_line
Definition canvas_g.h:62
canvas_put_sprite
canvas_put_sprite
Definition canvas_g.h:42
canvas_w
struct canvas int int int canvas_w
Definition canvas_g.h:49
canvas_h
struct canvas int int int int canvas_h
Definition canvas_g.h:49
pcanvas
struct canvas * pcanvas
Definition canvas_g.h:42
canvas_put_sprite_full_scaled
canvas_put_sprite_full_scaled
Definition canvas_g.h:48
canvas_put_text
canvas_put_text
Definition canvas_g.h:80
offset_y
struct canvas int int struct sprite int int offset_y
Definition canvas_g.h:44
offset_x
struct canvas int int struct sprite int offset_x
Definition canvas_g.h:44
width
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
line_type
line_type
Definition canvas_g.h:25
incite_cost
char * incite_cost
Definition comments.c:74
canvas_put_sprite_full
void canvas_put_sprite_full(struct canvas *pcanvas, int canvas_x, int canvas_y, struct sprite *sprite)
Definition canvas.c:144
canvas_set_zoom
void canvas_set_zoom(struct canvas *store, float zoom)
Definition canvas.c:53
canvas_mapview_init
void canvas_mapview_init(struct canvas *store)
Definition canvas.c:69
canvas_fill_sprite_area
void canvas_fill_sprite_area(struct canvas *pcanvas, struct sprite *psprite, struct color *pcolor, int canvas_x, int canvas_y)
Definition canvas.c:211
get_text_size
void get_text_size(int *width, int *height, enum client_font font, const char *text)
Definition canvas.c:341
canvas_free
void canvas_free(struct canvas *store)
Definition canvas.c:44
canvas_put_curved_line
void canvas_put_curved_line(struct canvas *pcanvas, struct color *pcolor, enum line_type ltype, int start_x, int start_y, int dx, int dy)
Definition canvas.c:276
has_zoom_support
bool has_zoom_support(void)
Definition canvas.c:61
canvas_copy
void canvas_copy(struct canvas *dest, struct canvas *src, int src_x, int src_y, int dest_x, int dest_y, int width, int height)
Definition canvas.c:76
canvas_create
struct canvas * canvas_create(int width, int height)
Definition canvas.c:28
canvas_put_sprite_fogged
void canvas_put_sprite_fogged(struct canvas *pcanvas, int canvas_x, int canvas_y, struct sprite *psprite, bool fog, int fog_x, int fog_y)
Definition canvas.c:170
create_line
void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1, SDL_Color *pcolor)
Definition graphics.c:1381
alphablit
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
blit_entire_src
int blit_entire_src(SDL_Surface *psrc, SDL_Surface *pdest, Sint16 dest_x, Sint16 dest_y)
Definition graphics.c:417
create_surf
SDL_Surface * create_surf(int width, int height, Uint32 flags)
Definition graphics.c:351
FREESURFACE
#define FREESURFACE(ptr)
Definition graphics.h:322
client_font_sizes
int * client_font_sizes[FONT_COUNT]
Definition gui_main.c:115
utf8_str_size
void utf8_str_size(utf8_str *pstr, SDL_Rect *fill)
Definition gui_string.c:106
copy_chars_to_utf8_str
utf8_str * copy_chars_to_utf8_str(utf8_str *pstr, const char *pchars)
Definition gui_string.c:251
create_text_surf_from_utf8
SDL_Surface * create_text_surf_from_utf8(utf8_str *pstr)
Definition gui_string.c:425
create_utf8_str
utf8_str * create_utf8_str(char *in_text, size_t n_alloc, Uint16 ptsize)
Definition gui_string.c:206
FREEUTF8STR
#define FREEUTF8STR(pstr)
Definition gui_string.h:93
GET_SURF
#define GET_SURF(m_sprite)
Definition sprite.h:29
gui_string.h
canvas.h
colors.h
graphics.h
gui_main.h
sprite.h
log.h
fc_assert
#define fc_assert(condition)
Definition log.h:176
mem.h
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
size
size_t size
Definition specvec.h:72
canvas
Definition canvas.h:22
canvas::zoom
float zoom
Definition canvas.h:25
canvas::surf
SDL_Surface * surf
Definition canvas.h:27
color
Definition colors.h:21
sprite
Definition sprite.h:22
utf8_str
Definition gui_string.h:52
FALSE
#define FALSE
Definition support.h:47