Freeciv-3.1
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/**********************************************************************/
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/**********************************************************************/
56void canvas_free(struct canvas *store)
57{
58 FREESURFACE(store->surf);
59 free(store);
60}
61
62/**********************************************************************/
65void canvas_set_zoom(struct canvas *store, float zoom)
66{
67 /* sdl2-client has no zoom support */
68}
69
70/**********************************************************************/
74{
75 return FALSE;
76}
77
78/**********************************************************************/
81void canvas_mapview_init(struct canvas *store)
82{
83 SDL_SetSurfaceBlendMode(store->surf, SDL_BLENDMODE_NONE);
84}
85
86/**********************************************************************/
89void 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/**********************************************************************/
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/**********************************************************************/
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/**********************************************************************/
130 int canvas_x, int canvas_y,
131 struct sprite *psprite,
132 bool fog, int fog_x, int fog_y)
133{
134 SDL_Rect dst = {canvas_x, canvas_y, 0, 0};
135
136 if (fog) {
137 alphablit(GET_SURF(psprite), NULL, pcanvas->surf, &dst, 160);
138 } else {
140 }
141}
142
143/**********************************************************************/
147 int canvas_x, int canvas_y, int width, int height)
148{
149 SDL_Rect dst = {canvas_x, canvas_y, width, height};
150
151 SDL_FillRect(pcanvas->surf, &dst, SDL_MapRGBA(pcanvas->surf->format,
152 pcolor->color->r,
153 pcolor->color->g,
154 pcolor->color->b,
155 pcolor->color->a));
156}
157
158/**********************************************************************/
162 struct sprite *psprite, struct color *pcolor,
163 int canvas_x, int canvas_y)
164{
165 SDL_Rect dst = {canvas_x, canvas_y,
166 GET_SURF(psprite)->w,
167 GET_SURF(psprite)->h};
168
169 SDL_FillRect(pcanvas->surf, &dst, SDL_MapRGBA(pcanvas->surf->format,
170 pcolor->color->r,
171 pcolor->color->g,
172 pcolor->color->b,
173 pcolor->color->a));
174}
175
176/**********************************************************************/
180 enum line_type ltype, int start_x, int start_y,
181 int dx, int dy)
182{
183 create_line(pcanvas->surf, start_x, start_y, start_x + dx, start_y + dy, pcolor->color);
184}
185
186/**********************************************************************/
190 enum line_type ltype, int start_x, int start_y,
191 int dx, int dy)
192{
193 /* FIXME: Implement curved line drawing. */
194 canvas_put_line(pcanvas, pcolor, ltype, start_x, start_y, dx, dy);
195}
196
197/**********************************************************************/
202void get_text_size(int *width, int *height,
203 enum client_font font, const char *text)
204{
205 utf8_str *ptext = create_utf8_str(NULL, 0, *client_font_sizes[font]);
206 SDL_Rect size;
207
208 fc_assert(width != NULL || height != NULL);
209
210 copy_chars_to_utf8_str(ptext, text);
211 utf8_str_size(ptext, &size);
212
213 if (width) {
214 *width = size.w;
215 }
216
217 if (height) {
218 *height = size.h;
219 }
220
221 FREEUTF8STR(ptext);
222}
223
224/**********************************************************************/
230 enum client_font font, struct color *pcolor,
231 const char *text)
232{
233 SDL_Surface *ptmp;
234 utf8_str *ptext = create_utf8_str(NULL, 0, *client_font_sizes[font]);
235
236 copy_chars_to_utf8_str(ptext, text);
237
238 ptext->fgcol = *pcolor->color;
239 ptext->bgcol = (SDL_Color) {0, 0, 0, 0};
240
241 ptmp = create_text_surf_from_utf8(ptext);
242
244
245 FREEUTF8STR(ptext);
246 FREESURFACE(ptmp);
247}
struct canvas int int struct sprite bool int int fog_y canvas_fill_sprite_area
Definition canvas_g.h:55
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 bool int fog_x
Definition canvas_g.h:51
struct canvas int int canvas_y
Definition canvas_g.h:43
canvas_put_sprite_fogged
Definition canvas_g.h:48
struct canvas int canvas_x
Definition canvas_g.h:43
struct canvas int int struct sprite bool int int fog_y struct canvas struct sprite struct color int int canvas_y canvas_put_curved_line
Definition canvas_g.h:63
canvas_put_sprite
Definition canvas_g.h:42
struct canvas int int struct sprite bool int int fog_y struct canvas struct sprite struct color int int canvas_y struct canvas struct color enum line_type ltype int start_x int start_y int dx int dy enum client_font
Definition canvas_g.h:69
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
struct canvas * pcanvas
Definition canvas_g.h:42
struct canvas int int struct sprite * psprite
Definition canvas_g.h:50
canvas_put_text
Definition canvas_g.h:77
struct canvas int int struct sprite int int offset_y
Definition canvas_g.h:44
struct canvas int int struct sprite int offset_x
Definition canvas_g.h:44
line_type
Definition canvas_g.h:25
struct canvas int int struct sprite bool fog
Definition canvas_g.h:51
void canvas_put_rectangle(struct canvas *pcanvas, struct color *pcolor, int canvas_x, int canvas_y, int width, int height)
Definition canvas.c:176
void canvas_put_sprite_full(struct canvas *pcanvas, int canvas_x, int canvas_y, struct sprite *sprite)
Definition canvas.c:148
void canvas_set_zoom(struct canvas *store, float zoom)
Definition canvas.c:53
void canvas_mapview_init(struct canvas *store)
Definition canvas.c:69
void get_text_size(int *width, int *height, enum client_font font, const char *text)
Definition canvas.c:347
void canvas_free(struct canvas *store)
Definition canvas.c:44
bool has_zoom_support(void)
Definition canvas.c:61
void canvas_put_line(struct canvas *pcanvas, struct color *pcolor, enum line_type ltype, int start_x, int start_y, int dx, int dy)
Definition canvas.c:223
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
struct canvas * canvas_create(int width, int height)
Definition canvas.c:28
void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1, SDL_Color *pcolor)
Definition graphics.c:1378
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
int blit_entire_src(SDL_Surface *psrc, SDL_Surface *pdest, Sint16 dest_x, Sint16 dest_y)
Definition graphics.c:417
SDL_Surface * create_surf(int width, int height, Uint32 flags)
Definition graphics.c:351
#define FREESURFACE(ptr)
Definition graphics.h:322
int * client_font_sizes[FONT_COUNT]
Definition gui_main.c:111
#define GET_SURF(m_sprite)
Definition sprite.h:29
void utf8_str_size(utf8_str *pstr, SDL_Rect *fill)
Definition gui_string.c:106
utf8_str * copy_chars_to_utf8_str(utf8_str *pstr, const char *pchars)
Definition gui_string.c:251
SDL_Surface * create_text_surf_from_utf8(utf8_str *pstr)
Definition gui_string.c:425
utf8_str * create_utf8_str(char *in_text, size_t n_alloc, Uint16 ptsize)
Definition gui_string.c:206
#define FREEUTF8STR(pstr)
Definition gui_string.h:92
#define fc_assert(condition)
Definition log.h:176
#define fc_malloc(sz)
Definition mem.h:34
size_t size
Definition specvec.h:72
float zoom
Definition canvas.h:24
SDL_Surface * surf
Definition canvas.h:27
Definition colors.h:20
SDL_Color bgcol
Definition gui_string.h:58
SDL_Color fgcol
Definition gui_string.h:57
#define FALSE
Definition support.h:47