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