Freeciv-3.2: /fast/freeciv/nightly/S3_2/client/gui-sdl3/graphics.h Source File

Freeciv-3.2

Loading...
Searching...
No Matches
graphics.h
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
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/***********************************************************************
15 graphics.h - description
16 -------------------
17 begin : Mon Jul 1 2002
18 copyright : (C) 2000 by Michael Speck
19 : (C) 2002 by Rafał Bursig
20 email : Michael Speck <kulkanie@gmx.net>
21 : Rafał Bursig <bursig@poczta.fm>
22***********************************************************************/
23
24#ifndef FC__GRAPHICS_H
25#define FC__GRAPHICS_H
26
27/* SDL3 */
28#include "SDL3_gfx/SDL3_rotozoom.h"
29
30/* client */
31#include "graphics_g.h"
32
33/* gui-sdl3 */
34#include "canvas.h"
35#include "gui_main.h"
36
37#define RECT_LIMIT 80
38
39/* DUFFS LOOPs come from SDL lib (LGPL) */
40/* DUFFS_LOOP_DOUBLE2 and DUFFS_LOOP_QUATRO2 was created by Rafal Bursig under GPL */
41
42/* This is a very useful loop for optimizing blitters */
43#if defined(_MSC_VER) && (_MSC_VER == 1300)
44/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
45#else
46#define USE_DUFFS_LOOP
47#endif
48
49#ifdef USE_DUFFS_LOOP
50
51/* 8-times unrolled loop */
52#define DUFFS_LOOP8(pixel_copy_increment, width) \
53{ int n = (width + 7) / 8; \
54 switch (width & 7) { \
55 case 0: do { pixel_copy_increment; \
56 fc__fallthrough; \
57 case 7: pixel_copy_increment; \
58 fc__fallthrough; \
59 case 6: pixel_copy_increment; \
60 fc__fallthrough; \
61 case 5: pixel_copy_increment; \
62 fc__fallthrough; \
63 case 4: pixel_copy_increment; \
64 fc__fallthrough; \
65 case 3: pixel_copy_increment; \
66 fc__fallthrough; \
67 case 2: pixel_copy_increment; \
68 fc__fallthrough; \
69 case 1: pixel_copy_increment; \
70 } while ( --n > 0 ); \
71 } \
72}
73
74/* 4-times unrolled loop */
75#define DUFFS_LOOP4(pixel_copy_increment, width) \
76{ int n = (width + 3) / 4; \
77 switch (width & 3) { \
78 case 0: do { pixel_copy_increment; \
79 fc__fallthrough; \
80 case 3: pixel_copy_increment; \
81 fc__fallthrough; \
82 case 2: pixel_copy_increment; \
83 fc__fallthrough; \
84 case 1: pixel_copy_increment; \
85 } while ( --n > 0 ); \
86 } \
87}
88
89/* 2 - times unrolled loop */
90#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment, \
91 double_pixel_copy_increment, width) \
92{ int n, w = width; \
93 if ( w & 1 ) { \
94 pixel_copy_increment; \
95 w--; \
96 } \
97 if ( w > 0 ) { \
98 n = ( w + 2 ) / 4; \
99 switch ( w & 2 ) { \
100 case 0: do { double_pixel_copy_increment; \
101 fc__fallthrough; \
102 case 2: double_pixel_copy_increment; \
103 } while ( --n > 0 ); \
104 } \
105 } \
106}
107
108/* 2 - times unrolled loop 4 pixels */
109#define DUFFS_LOOP_QUATRO2(pixel_copy_increment, \
110 double_pixel_copy_increment, \
111 quatro_pixel_copy_increment, width) \
112{ int n, w = width; \
113 if (w & 1) { \
114 pixel_copy_increment; \
115 w--; \
116 } \
117 if (w & 2) { \
118 double_pixel_copy_increment; \
119 w -= 2; \
120 } \
121 if ( w > 0 ) { \
122 n = ( w + 7 ) / 8; \
123 switch ( w & 4 ) { \
124 case 0: do { quatro_pixel_copy_increment; \
125 case 4: quatro_pixel_copy_increment; \
126 } while ( --n > 0 ); \
127 } \
128 } \
129}
130
131/* Use the 8-times version of the loop by default */
132#define DUFFS_LOOP(pixel_copy_increment, width) \
133 DUFFS_LOOP8(pixel_copy_increment, width)
134
135#else
136
137/* Don't use Duff's device to unroll loops */
138#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment, \
139 double_pixel_copy_increment, width) \
140{ int n = width; \
141 if ( n & 1 ) { \
142 pixel_copy_increment; \
143 n--; \
144 } \
145 for (; n > 0; --n) { \
146 double_pixel_copy_increment; \
147 n--; \
148 } \
149}
150
151/* Don't use Duff's device to unroll loops */
152#define DUFFS_LOOP_QUATRO2(pixel_copy_increment, \
153 double_pixel_copy_increment, \
154 quatro_pixel_copy_increment, width) \
155{ int n = width; \
156 if (n & 1) { \
157 pixel_copy_increment; \
158 n--; \
159 } \
160 if (n & 2) { \
161 double_pixel_copy_increment; \
162 n -= 2; \
163 } \
164 for (; n > 0; --n) { \
165 quatro_pixel_copy_increment; \
166 n -= 3; \
167 } \
168}
169
170/* Don't use Duff's device to unroll loops */
171#define DUFFS_LOOP(pixel_copy_increment, width) \
172{ int n; \
173 for ( n=width; n > 0; --n ) { \
174 pixel_copy_increment; \
175 } \
176}
177
178#define DUFFS_LOOP8(pixel_copy_increment, width) \
179 DUFFS_LOOP(pixel_copy_increment, width)
180#define DUFFS_LOOP4(pixel_copy_increment, width) \
181 DUFFS_LOOP(pixel_copy_increment, width)
182
183#endif /* USE_DUFFS_LOOP */
184
185/* Shrink surface on 320x240 screen */
186#ifdef SMALL_SCREEN
187#define DEFAULT_ZOOM 0.5
188#define adj_surf(surf) zoomSurface((surf), DEFAULT_ZOOM, DEFAULT_ZOOM, 0)
189#else
190#define DEFAULT_ZOOM 1.0
191/* Cannot return the original as callers free what we return */
192#define adj_surf(surf) copy_surface(surf)
193#endif
194
195struct gui_layer;
196
197 struct sdl3_data {
198 int rects_count; /* Update rect. array counter */
199 int guis_count; /* Gui buffers array counter */
200 SDL_Rect rects[RECT_LIMIT]; /* Update rect. list */
201 SDL_Window *screen; /* Main screen buffer */
202 SDL_Surface *map; /* Map buffer */
203 SDL_Surface *dummy; /* Dummy surface for missing sprites */
204 SDL_Texture *maintext;
205 SDL_Renderer *renderer;
206 struct canvas map_canvas;
207 struct gui_layer *gui; /* Gui buffer */
208 struct gui_layer **guis; /* Gui buffers used by sdl3-client widgets window menager */
209 SDL_Event event; /* Main event struct */
210};
211
212extern struct sdl3_data main_data;
213
214/* GUI layer */
215/* A gui_layer is a surface with its own origin. Each widget belongs
216 * to a gui_layer. gui_layers are stored in an array main_data.guis
217 * (a "window manager"). */
218
219struct gui_layer {
220 SDL_Rect dest_rect; /* origin: only x and y are used */
221 SDL_Surface *surface;
222};
223
224struct gui_layer *gui_layer_new(int x, int y, SDL_Surface *surface);
225void gui_layer_destroy(struct gui_layer **gui_layer);
226
227struct gui_layer *get_gui_layer(SDL_Surface *surface);
228
229struct gui_layer *add_gui_layer(int width, int height);
230void remove_gui_layer(struct gui_layer *gui_layer);
231
232void screen_rect_to_layer_rect(struct gui_layer *gui_layer, SDL_Rect *dest_rect);
233void layer_rect_to_screen_rect(struct gui_layer *gui_layer, SDL_Rect *dest_rect);
234
235/* ---------- */
236
237int alphablit(SDL_Surface *src, SDL_Rect *srcrect,
238 SDL_Surface *dst, SDL_Rect *dstrect,
239 unsigned char alpha_mod);
240int screen_blit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Rect *dstrect,
241 unsigned char alpha_mod);
242
243 SDL_Surface *load_surf(const char *fname);
244
245 SDL_Surface *create_surf_with_format(SDL_PixelFormat *pf,
246 int width, int height);
247 SDL_Surface *create_surf(int width, int height, Uint32 flags);
248 SDL_Surface *convert_surf(SDL_Surface *surf_in);
249
250 SDL_Surface *create_filled_surface(Uint16 w, Uint16 h, Uint32 flags,
251 SDL_Color *pcolor);
252
253 SDL_Surface *crop_rect_from_surface(SDL_Surface *psource,
254 SDL_Rect *prect);
255
256 SDL_Surface *mask_surface(SDL_Surface *src, SDL_Surface *mask,
257 int mask_offset_x, int mask_offset_y);
258
259 SDL_Surface *copy_surface(SDL_Surface *src);
260
261int blit_entire_src(SDL_Surface *psrc,
262 SDL_Surface *pdest, Sint16 dest_x, Sint16 dest_y);
263
264 Uint32 getpixel(SDL_Surface *surf, Sint16 x, Sint16 y);
265 Uint32 get_first_pixel(SDL_Surface *surf);
266
267void create_frame(SDL_Surface *dest, Sint16 left, Sint16 top,
268 Sint16 right, Sint16 bottom,
269 SDL_Color *pcolor);
270
271void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1,
272 SDL_Color *pcolor);
273
274/* SDL */
275void init_sdl(int f);
276void quit_sdl(void);
277bool set_video_mode(unsigned width, unsigned height, unsigned flags);
278bool create_surfaces(int width, int height);
279
280void update_main_screen(void);
281
282int main_window_width(void);
283int main_window_height(void);
284
285/* Rect */
286bool correct_rect_region(SDL_Rect *prect);
287bool is_in_rect_area(int x, int y, const SDL_Rect *rect);
288
289int fill_rect_alpha(SDL_Surface *surf, SDL_Rect *prect,
290 SDL_Color *pcolor);
291
292int clear_surface(SDL_Surface *surf, SDL_Rect *dstrect);
293
294/* ================================================================= */
295
296 SDL_Surface *resize_surface(const SDL_Surface *psrc, Uint16 new_width,
297 Uint16 new_height, int smooth);
298
299 SDL_Surface *resize_surface_box(const SDL_Surface *psrc,
300 Uint16 new_width, Uint16 new_height,
301 int smooth, bool scale_up,
302 bool absolute_dimensions);
303
304 SDL_Surface *crop_visible_part_from_surface(SDL_Surface *psrc);
305void get_smaller_surface_rect(SDL_Surface *surf, SDL_Rect *rect);
306
307#define map_rgba(format, color) \
308 SDL_MapRGBA(format, (color).r, (color).g, (color).b, (color).a)
309
310#define crop_rect_from_screen(rect) \
311 crop_rect_from_surface(main_data.screen, &rect)
312
313/* Free surface with check and clear pointer */
314#define FREESURFACE(ptr) \
315do { \
316 if (ptr) { \
317 SDL_DestroySurface(ptr); \
318 ptr = NULL; \
319 } \
320} while (FALSE)
321
322/*
323 * Lock surface
324 */
325#define lock_surf(surf) \
326do { \
327 if (SDL_MUSTLOCK(surf)) { \
328 SDL_LockSurface(surf); \
329 } \
330} while (FALSE)
331
332
333/*
334 * Unlock surface
335 */
336#define unlock_surf(surf) \
337do { \
338 if (SDL_MUSTLOCK(surf)) { \
339 SDL_UnlockSurface(surf); \
340 } \
341} while (FALSE)
342
343/*
344 * Lock screen surface
345 */
346#define lock_screen() lock_surf(main_data.screen)
347
348/*
349 * Unlock screen surface
350 */
351#define unlock_screen() unlock_surf(main_data.screen)
352
353#define putpixel(surf, x, y, pixel) \
354do { \
355 Uint8 *buf_ptr = ((Uint8 *)surf->pixels + (y * surf->pitch)); \
356 switch (surf->format->BytesPerPixel) { \
357 case 1: \
358 buf_ptr += x; \
359 *(Uint8 *)buf_ptr = pixel; \
360 break; \
361 case 2: \
362 buf_ptr += x << 1; \
363 *((Uint16 *)buf_ptr) = pixel; \
364 break; \
365 case 3: \
366 buf_ptr += (x << 1) + x; \
367 if (is_bigendian()) { \
368 buf_ptr[0] = (pixel >> 16) & 0xff; \
369 buf_ptr[1] = (pixel >> 8) & 0xff; \
370 buf_ptr[2] = pixel & 0xff; \
371 } else { \
372 buf_ptr[0] = pixel & 0xff; \
373 buf_ptr[1] = (pixel >> 8) & 0xff; \
374 buf_ptr[2] = (pixel >> 16) & 0xff; \
375 } \
376 break; \
377 case 4: \
378 buf_ptr += x << 2; \
379 *((Uint32 *)buf_ptr) = pixel; \
380 break; \
381 } \
382} while (FALSE)
383
384/* Blend the RGB values of two pixels based on a source alpha value */
385#define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB) \
386do { \
387 dR = (((sR-dR)*(A))>>8)+dR; \
388 dG = (((sG-dG)*(A))>>8)+dG; \
389 dB = (((sB-dB)*(A))>>8)+dB; \
390} while (FALSE)
391
392#define ALPHA_BLEND128(sR, sG, sB, dR, dG, dB) \
393do { \
394 Uint32 __Src = (sR << 16 | sG << 8 | sB); \
395 Uint32 __Dst = (dR << 16 | dG << 8 | dB); \
396 __Dst = ((((__Src & 0x00fefefe) + (__Dst & 0x00fefefe)) >> 1) \
397 + (__Src & __Dst & 0x00010101)) | 0xFF000000; \
398 dR = (__Dst >> 16) & 0xFF; \
399 dG = (__Dst >> 8 ) & 0xFF; \
400 dB = (__Dst ) & 0xFF; \
401} while (FALSE)
402
403#endif /* FC__GRAPHICS_H */
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
graphics_g.h
resize_surface
SDL_Surface * resize_surface(const SDL_Surface *psrc, Uint16 new_width, Uint16 new_height, int smooth)
Definition graphics.c:1237
getpixel
Uint32 getpixel(SDL_Surface *surf, Sint16 x, Sint16 y)
Definition graphics.c:430
main_window_width
int main_window_width(void)
Definition graphics.c:685
layer_rect_to_screen_rect
void layer_rect_to_screen_rect(struct gui_layer *gui_layer, SDL_Rect *dest_rect)
Definition graphics.c:185
load_surf
SDL_Surface * load_surf(const char *fname)
Definition graphics.c:307
create_surf_with_format
SDL_Surface * create_surf_with_format(SDL_PixelFormat *pf, int width, int height, Uint32 flags)
Definition graphics.c:328
update_main_screen
void update_main_screen(void)
Definition graphics.c:669
remove_gui_layer
void remove_gui_layer(struct gui_layer *gui_layer)
Definition graphics.c:148
create_filled_surface
SDL_Surface * create_filled_surface(Uint16 w, Uint16 h, Uint32 flags, SDL_Color *pcolor)
Definition graphics.c:368
fill_rect_alpha
int fill_rect_alpha(SDL_Surface *surf, SDL_Rect *prect, SDL_Color *pcolor)
Definition graphics.c:865
gui_layer_destroy
void gui_layer_destroy(struct gui_layer **gui_layer)
Definition graphics.c:81
create_line
void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1, SDL_Color *pcolor)
Definition graphics.c:1379
copy_surface
SDL_Surface * copy_surface(SDL_Surface *src)
Definition graphics.c:1309
set_video_mode
bool set_video_mode(unsigned width, unsigned height, unsigned flags)
Definition graphics.c:636
mask_surface
SDL_Surface * mask_surface(SDL_Surface *src, SDL_Surface *mask, int mask_offset_x, int mask_offset_y)
Definition graphics.c:262
gui_layer_new
struct gui_layer * gui_layer_new(int x, int y, SDL_Surface *surface)
Definition graphics.c:66
quit_sdl
void quit_sdl(void)
Definition graphics.c:622
crop_visible_part_from_surface
SDL_Surface * crop_visible_part_from_surface(SDL_Surface *psrc)
Definition graphics.c:1225
crop_rect_from_surface
SDL_Surface * crop_rect_from_surface(SDL_Surface *psource, SDL_Rect *prect)
Definition graphics.c:236
init_sdl
void init_sdl(int f)
Definition graphics.c:506
alphablit
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
create_frame
void create_frame(SDL_Surface *dest, Sint16 left, Sint16 top, Sint16 right, Sint16 bottom, SDL_Color *pcolor)
Definition graphics.c:1348
get_gui_layer
struct gui_layer * get_gui_layer(SDL_Surface *surface)
Definition graphics.c:90
is_in_rect_area
bool is_in_rect_area(int x, int y, const SDL_Rect *rect)
Definition graphics.c:925
screen_blit
int screen_blit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:223
main_data
struct sdl2_data main_data
Definition graphics.c:57
blit_entire_src
int blit_entire_src(SDL_Surface *psrc, SDL_Surface *pdest, Sint16 dest_x, Sint16 dest_y)
Definition graphics.c:417
get_first_pixel
Uint32 get_first_pixel(SDL_Surface *surf)
Definition graphics.c:468
convert_surf
SDL_Surface * convert_surf(SDL_Surface *surf_in)
Definition graphics.c:359
create_surf
SDL_Surface * create_surf(int width, int height, Uint32 flags)
Definition graphics.c:351
get_smaller_surface_rect
void get_smaller_surface_rect(SDL_Surface *surf, SDL_Rect *rect)
Definition graphics.c:936
add_gui_layer
struct gui_layer * add_gui_layer(int width, int height)
Definition graphics.c:112
clear_surface
int clear_surface(SDL_Surface *surf, SDL_Rect *dstrect)
Definition graphics.c:400
correct_rect_region
bool correct_rect_region(SDL_Rect *prect)
Definition graphics.c:888
screen_rect_to_layer_rect
void screen_rect_to_layer_rect(struct gui_layer *gui_layer, SDL_Rect *dest_rect)
Definition graphics.c:173
create_surfaces
bool create_surfaces(int width, int height)
Definition graphics.c:556
main_window_height
int main_window_height(void)
Definition graphics.c:693
resize_surface_box
SDL_Surface * resize_surface_box(const SDL_Surface *psrc, Uint16 new_width, Uint16 new_height, int smooth, bool scale_up, bool absolute_dimensions)
Definition graphics.c:1257
RECT_LIMIT
#define RECT_LIMIT
Definition graphics.h:37
canvas.h
gui_main.h
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
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
mask_offset_y
struct sprite int int int int struct sprite int int mask_offset_y
Definition sprite_g.h:32
canvas
Definition canvas.h:22
gui_layer
Definition graphics.h:227
gui_layer::surface
SDL_Surface * surface
Definition graphics.h:229
gui_layer::dest_rect
SDL_Rect dest_rect
Definition graphics.h:228
sdl3_data
Definition graphics.h:197
sdl3_data::guis
struct gui_layer ** guis
Definition graphics.h:208
sdl3_data::guis_count
int guis_count
Definition graphics.h:199
sdl3_data::map_canvas
struct canvas map_canvas
Definition graphics.h:206
sdl3_data::rects
SDL_Rect rects[RECT_LIMIT]
Definition graphics.h:200
sdl3_data::renderer
SDL_Renderer * renderer
Definition graphics.h:205
sdl3_data::event
SDL_Event event
Definition graphics.h:209
sdl3_data::screen
SDL_Window * screen
Definition graphics.h:201
sdl3_data::map
SDL_Surface * map
Definition graphics.h:202
sdl3_data::maintext
SDL_Texture * maintext
Definition graphics.h:204
sdl3_data::gui
struct gui_layer * gui
Definition graphics.h:207
sdl3_data::rects_count
int rects_count
Definition graphics.h:198
sdl3_data::dummy
SDL_Surface * dummy
Definition graphics.h:203