Freeciv-3.1
Loading...
Searching...
No Matches
graphics.c
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.c - 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#ifdef HAVE_CONFIG_H
25#include <fc_config.h>
26#endif
27
28/* SDL2 */
29#ifdef SDL2_PLAIN_INCLUDE
30#include <SDL_image.h>
31#include <SDL_syswm.h>
32#include <SDL_ttf.h>
33#else /* SDL2_PLAIN_INCLUDE */
34#include <SDL2/SDL_image.h>
35#include <SDL2/SDL_syswm.h>
36#include <SDL2/SDL_ttf.h>
37#endif /* SDL2_PLAIN_INCLUDE */
38
39/* utility */
40#include "fcintl.h"
41#include "log.h"
42
43/* client */
44#include "tilespec.h"
45
46/* gui-sdl2 */
47#include "colors.h"
48#include "gui_tilespec.h"
49#include "mapview.h"
50#include "themebackgrounds.h"
51#include "themespec.h"
52
53#include "graphics.h"
54
55/* ------------------------------ */
56
58
59static SDL_Surface *main_surface = NULL;
60
61static bool render_dirty = TRUE;
62
63/**********************************************************************/
66struct gui_layer *gui_layer_new(int x, int y, SDL_Surface *surface)
67{
68 struct gui_layer *result;
69
70 result = fc_calloc(1, sizeof(struct gui_layer));
71
72 result->dest_rect = (SDL_Rect){x, y, 0, 0};
73 result->surface = surface;
74
75 return result;
76}
77
78/**********************************************************************/
82{
83 FREESURFACE((*gui_layer)->surface);
85}
86
87/**********************************************************************/
90struct gui_layer *get_gui_layer(SDL_Surface *surface)
91{
92 int i = 0;
93
94 while ((i < main_data.guis_count) && main_data.guis[i]) {
95 if (main_data.guis[i]->surface == surface) {
96 return main_data.guis[i];
97 }
98 i++;
99 }
100
101 return NULL;
102}
103
104/**********************************************************************/
113{
114 struct gui_layer *gui_layer = NULL;
115 SDL_Surface *buffer;
116
117 buffer = create_surf(width, height, SDL_SWSURFACE);
118 gui_layer = gui_layer_new(0, 0, buffer);
119
120 /* add to buffers array */
121 if (main_data.guis) {
122 int i;
123
124 /* find NULL element */
125 for (i = 0; i < main_data.guis_count; i++) {
126 if (!main_data.guis[i]) {
128 return gui_layer;
129 }
130 }
133 main_data.guis_count * sizeof(struct gui_layer *));
135 } else {
136 main_data.guis = fc_calloc(1, sizeof(struct gui_layer *));
139 }
140
141 return gui_layer;
142}
143
144/**********************************************************************/
149{
150 int i;
151
152 for (i = 0; i < main_data.guis_count - 1; i++) {
153 if (main_data.guis[i] && (main_data.guis[i] == gui_layer)) {
155 main_data.guis[i] = main_data.guis[i + 1];
156 main_data.guis[i + 1] = NULL;
157 } else {
158 if (!main_data.guis[i]) {
159 main_data.guis[i] = main_data.guis[i + 1];
160 main_data.guis[i + 1] = NULL;
161 }
162 }
163 }
164
167 }
168}
169
170/**********************************************************************/
174 SDL_Rect *dest_rect)
175{
176 if (gui_layer) {
179 }
180}
181
182/**********************************************************************/
186 SDL_Rect *dest_rect)
187{
188 if (gui_layer) {
191 }
192}
193
194/* ============ Freeciv sdl graphics function =========== */
195
196/**********************************************************************/
199int alphablit(SDL_Surface *src, SDL_Rect *srcrect,
200 SDL_Surface *dst, SDL_Rect *dstrect,
201 unsigned char alpha_mod)
202{
203 int ret;
204
205 if (src == NULL || dst == NULL) {
206 return 1;
207 }
208
209 SDL_SetSurfaceAlphaMod(src, alpha_mod);
210
211 ret = SDL_BlitSurface(src, srcrect, dst, dstrect);
212
213 if (ret) {
214 log_error("SDL_BlitSurface() fails: %s", SDL_GetError());
215 }
216
217 return ret;
218}
219
220/**********************************************************************/
223int screen_blit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Rect *dstrect,
224 unsigned char alpha_mod)
225{
227
228 return alphablit(src, srcrect, main_surface, dstrect, alpha_mod);
229}
230
231/**********************************************************************/
236SDL_Surface *crop_rect_from_surface(SDL_Surface *psource,
237 SDL_Rect *prect)
238{
239 SDL_Surface *new_surf = create_surf_with_format(psource->format,
240 prect ? prect->w : psource->w,
241 prect ? prect->h : psource->h,
242 SDL_SWSURFACE);
243
244 if (alphablit(psource, prect, new_surf, NULL, 255) != 0) {
245 FREESURFACE(new_surf);
246
247 return NULL;
248 }
249
250 return new_surf;
251}
252
253/**********************************************************************/
262SDL_Surface *mask_surface(SDL_Surface *src, SDL_Surface *mask,
263 int mask_offset_x, int mask_offset_y)
264{
265 SDL_Surface *dest = NULL;
266 int row, col;
267 Uint32 *src_pixel = NULL;
268 Uint32 *dest_pixel = NULL;
269 Uint32 *mask_pixel = NULL;
270 unsigned char src_alpha, mask_alpha;
271
272 dest = copy_surface(src);
273
274 lock_surf(src);
275 lock_surf(mask);
276 lock_surf(dest);
277
278 src_pixel = (Uint32 *)src->pixels;
279 dest_pixel = (Uint32 *)dest->pixels;
280
281 for (row = 0; row < src->h; row++) {
282 mask_pixel = (Uint32 *)mask->pixels
283 + mask->w * (row + mask_offset_y)
284 + mask_offset_x;
285
286 for (col = 0; col < src->w; col++) {
287 src_alpha = (*src_pixel & src->format->Amask) >> src->format->Ashift;
288 mask_alpha = (*mask_pixel & mask->format->Amask) >> mask->format->Ashift;
289
290 *dest_pixel = (*src_pixel & ~src->format->Amask)
291 | (((src_alpha * mask_alpha) / 255) << dest->format->Ashift);
292
293 src_pixel++; dest_pixel++; mask_pixel++;
294 }
295 }
296
297 unlock_surf(dest);
298 unlock_surf(mask);
299 unlock_surf(src);
300
301 return dest;
302}
303
304/**********************************************************************/
307SDL_Surface *load_surf(const char *fname)
308{
309 SDL_Surface *buf;
310
311 if (!fname) {
312 return NULL;
313 }
314
315 if ((buf = IMG_Load(fname)) == NULL) {
316 log_error(_("load_surf: Failed to load graphic file %s!"), fname);
317
318 return NULL;
319 }
320
321 return buf;
322}
323
324/**********************************************************************/
328SDL_Surface *create_surf_with_format(SDL_PixelFormat *pf,
329 int width, int height,
330 Uint32 flags)
331{
332 SDL_Surface *surf = SDL_CreateRGBSurface(flags, width, height,
333 pf->BitsPerPixel,
334 pf->Rmask,
335 pf->Gmask,
336 pf->Bmask, pf->Amask);
337
338 if (surf == NULL) {
339 log_error(_("Unable to create Sprite (Surface) of size "
340 "%d x %d %d Bits in format %d"),
341 width, height, pf->BitsPerPixel, flags);
342 return NULL;
343 }
344
345 return surf;
346}
347
348/**********************************************************************/
351SDL_Surface *create_surf(int width, int height, Uint32 flags)
352{
353 return create_surf_with_format(main_surface->format, width, height, flags);
354}
355
356/**********************************************************************/
359SDL_Surface *convert_surf(SDL_Surface *surf_in)
360{
361 return SDL_ConvertSurface(surf_in, main_surface->format, 0);
362}
363
364/**********************************************************************/
368SDL_Surface *create_filled_surface(Uint16 w, Uint16 h, Uint32 flags,
369 SDL_Color *pcolor)
370{
371 SDL_Surface *new_surf;
372 SDL_Color color = {255, 255, 255, 128};
373
374 new_surf = create_surf(w, h, flags);
375
376 if (!new_surf) {
377 return NULL;
378 }
379
380 if (!pcolor) {
381 /* pcolor->unused == ALPHA */
382 pcolor = &color;
383 }
384
385 SDL_FillRect(new_surf, NULL,
386 SDL_MapRGBA(new_surf->format, pcolor->r, pcolor->g, pcolor->b,
387 pcolor->a));
388
389 if (pcolor->a != 255) {
390 SDL_SetSurfaceAlphaMod(new_surf, pcolor->a);
391 }
392
393 return new_surf;
394}
395
396/**********************************************************************/
400int clear_surface(SDL_Surface *surf, SDL_Rect *dstrect)
401{
402 /* SDL_FillRect might change the rectangle, so we create a copy */
403 if (dstrect) {
404 SDL_Rect _dstrect = *dstrect;
405
406 return SDL_FillRect(surf, &_dstrect, SDL_MapRGBA(surf->format, 0, 0, 0, 0));
407 } else {
408 return SDL_FillRect(surf, NULL, SDL_MapRGBA(surf->format, 0, 0, 0, 0));
409 }
410}
411
412/**********************************************************************/
417int blit_entire_src(SDL_Surface *psrc, SDL_Surface *pdest,
418 Sint16 dest_x, Sint16 dest_y)
419{
420 SDL_Rect dest_rect = { dest_x, dest_y, 0, 0 };
421
422 return alphablit(psrc, NULL, pdest, &dest_rect, 255);
423}
424
425/**********************************************************************/
430Uint32 getpixel(SDL_Surface *surf, Sint16 x, Sint16 y)
431{
432 if (!surf) {
433 return 0x0;
434 }
435
436 switch (surf->format->BytesPerPixel) {
437 case 1:
438 return *(Uint8 *) ((Uint8 *) surf->pixels + y * surf->pitch + x);
439
440 case 2:
441 return *((Uint16 *)surf->pixels + y * surf->pitch / sizeof(Uint16) + x);
442
443 case 3:
444 {
445 /* Here ptr is the address to the pixel we want to retrieve */
446 Uint8 *ptr =
447 (Uint8 *) surf->pixels + y * surf->pitch + x * 3;
448
449 if (is_bigendian()) {
450 return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
451 } else {
452 return ptr[0] | ptr[1] << 8 | ptr[2] << 16;
453 }
454 }
455 case 4:
456 return *((Uint32 *)surf->pixels + y * surf->pitch / sizeof(Uint32) + x);
457
458 default:
459 return 0; /* shouldn't happen, but avoids warnings */
460 }
461}
462
463/**********************************************************************/
468Uint32 get_first_pixel(SDL_Surface *surf)
469{
470 if (!surf) {
471 return 0;
472 }
473
474 switch (surf->format->BytesPerPixel) {
475 case 1:
476 return *((Uint8 *)surf->pixels);
477
478 case 2:
479 return *((Uint16 *)surf->pixels);
480
481 case 3:
482 {
483 if (is_bigendian()) {
484 return (((Uint8 *)surf->pixels)[0] << 16)
485 | (((Uint8 *)surf->pixels)[1] << 8)
486 | ((Uint8 *)surf->pixels)[2];
487 } else {
488 return ((Uint8 *)surf->pixels)[0]
489 | (((Uint8 *)surf->pixels)[1] << 8)
490 | (((Uint8 *)surf->pixels)[2] << 16);
491 }
492 }
493 case 4:
494 return *((Uint32 *)surf->pixels);
495
496 default:
497 return 0; /* shouldn't happen, but avoids warnings */
498 }
499}
500
501/* ===================================================================== */
502
503/**********************************************************************/
506void init_sdl(int flags)
507{
508 bool error;
509
510 main_data.screen = NULL;
511 main_data.guis = NULL;
512 main_data.gui = NULL;
513 main_data.map = NULL;
514 main_data.dummy = NULL; /* Can't create yet -- hope we don't need it */
515 main_data.renderer = NULL;
518
519 if (SDL_WasInit(SDL_INIT_AUDIO)) {
520 error = (SDL_InitSubSystem(flags) < 0);
521 } else {
522 error = (SDL_Init(flags) < 0);
523 }
524 if (error) {
525 log_fatal(_("Unable to initialize SDL2 library: %s"), SDL_GetError());
526 exit(EXIT_FAILURE);
527 }
528
529 atexit(SDL_Quit);
530
531 /* Initialize the TTF library */
532 if (TTF_Init() < 0) {
533 log_fatal(_("Unable to initialize SDL2_ttf library: %s"), SDL_GetError());
534 exit(EXIT_FAILURE);
535 }
536
537 atexit(TTF_Quit);
538}
539
540/**********************************************************************/
543static void free_surfaces(void)
544{
545 if (main_data.renderer != NULL) {
546 SDL_DestroyRenderer(main_data.renderer);
547 main_data.renderer = NULL;
548
550 }
551}
552
553/**********************************************************************/
557{
558 unsigned flags;
559
561
562 if (is_bigendian()) {
563 main_surface = SDL_CreateRGBSurface(0, width, height, 32,
564 0x0000FF00, 0x00FF0000,
565 0xFF000000, 0x000000FF);
566 main_data.map = SDL_CreateRGBSurface(0, width, height, 32,
567 0x0000FF00, 0x00FF0000,
568 0xFF000000, 0x000000FF);
569 } else {
570 main_surface = SDL_CreateRGBSurface(0, width, height, 32,
571 0x00FF0000, 0x0000FF00,
572 0x000000FF, 0xFF000000);
573 main_data.map = SDL_CreateRGBSurface(0, width, height, 32,
574 0x00FF0000, 0x0000FF00,
575 0x000000FF, 0xFF000000);
576 }
577
578 if (main_surface == NULL || main_data.map == NULL) {
579 log_fatal(_("Failed to create RGB surface: %s"), SDL_GetError());
580 return FALSE;
581 }
582
585 flags = SDL_RENDERER_SOFTWARE;
586 } else {
587 flags = 0;
588 }
589
590 main_data.renderer = SDL_CreateRenderer(main_data.screen, -1, flags);
591
592 if (main_data.renderer == NULL) {
593 log_fatal(_("Failed to create renderer: %s"), SDL_GetError());
594 return FALSE;
595 }
596
597 main_data.maintext = SDL_CreateTexture(main_data.renderer,
598 SDL_PIXELFORMAT_ARGB8888,
599 SDL_TEXTUREACCESS_STREAMING,
600 width, height);
601
602 if (main_data.maintext == NULL) {
603 log_fatal(_("Failed to create texture: %s"), SDL_GetError());
604 return FALSE;
605 }
606
607 if (main_data.gui) {
609 main_data.gui->surface = create_surf(width, height, SDL_SWSURFACE);
610 } else {
612 }
613
615
616 return TRUE;
617}
618
619/**********************************************************************/
622void quit_sdl(void)
623{
628 SDL_DestroyTexture(main_data.maintext);
629
631}
632
633/**********************************************************************/
636bool set_video_mode(unsigned width, unsigned height, unsigned flags_in)
637{
638 main_data.screen = SDL_CreateWindow(_("SDL2 Client for Freeciv"),
639 SDL_WINDOWPOS_UNDEFINED,
640 SDL_WINDOWPOS_UNDEFINED,
641 width, height,
642 0);
643
644 if (main_data.screen == NULL) {
645 log_fatal(_("Failed to create main window: %s"), SDL_GetError());
646 return FALSE;
647 }
648
649 if (flags_in & SDL_WINDOW_FULLSCREEN) {
650 SDL_DisplayMode mode;
651
652 /* Use SDL_WINDOW_FULLSCREEN_DESKTOP instead of real SDL_WINDOW_FULLSCREEN */
653 SDL_SetWindowFullscreen(main_data.screen, SDL_WINDOW_FULLSCREEN_DESKTOP);
654 SDL_GetWindowDisplayMode(main_data.screen, &mode);
655 width = mode.w;
656 height = mode.h;
657 }
658
660 return FALSE;
661 }
662
663 return TRUE;
664}
665
666/**********************************************************************/
670{
671 if (render_dirty) {
672 SDL_UpdateTexture(main_data.maintext, NULL,
673 main_surface->pixels, main_surface->pitch);
674 SDL_RenderClear(main_data.renderer);
675 SDL_RenderCopy(main_data.renderer, main_data.maintext, NULL, NULL);
676 SDL_RenderPresent(main_data.renderer);
677
679 }
680}
681
682/**********************************************************************/
686{
687 return main_surface->w;
688}
689
690/**********************************************************************/
694{
695 return main_surface->h;
696}
697
698/**************************************************************************
699 Fill Rect with RGBA color
700**************************************************************************/
701#define MASK565 0xf7de
702#define MASK555 0xfbde
703
704/* 50% alpha (128) */
705#define BLEND16_50( d, s , mask ) \
706 (((( s & mask ) + ( d & mask )) >> 1) + ( s & d & ( ~mask & 0xffff)))
707
708#define BLEND2x16_50( d, s , mask ) \
709 (((( s & (mask | mask << 16)) + ( d & ( mask | mask << 16 ))) >> 1) + \
710 ( s & d & ( ~(mask | mask << 16))))
711
712/**********************************************************************/
715static int __FillRectAlpha8888_32bit(SDL_Surface *surf, SDL_Rect *prect,
716 SDL_Color *pcolor)
717{
718 register Uint32 A = pcolor->a;
719 register Uint32 dSIMD1, dSIMD2;
720 register Uint32 sSIMD1, sSIMD2 = SDL_MapRGB(surf->format,
721 pcolor->r, pcolor->g,
722 pcolor->b);
723 Uint32 y, end, A_Dst, A_Mask = surf->format->Amask;
724 Uint32 *start, *pixel;
725
726 sSIMD1 = sSIMD2 & 0x00FF00FF;
727
728 lock_surf(surf);
729
730 if (prect == NULL) {
731 end = surf->w * surf->h;
732 pixel = (Uint32 *) surf->pixels;
733 if (A == 128) { /* 50% A */
735 {
736 dSIMD2 = *pixel;
737 A_Dst = dSIMD2 & A_Mask;
738 *pixel++ = ((((sSIMD2 & 0x00fefefe) + (dSIMD2 & 0x00fefefe)) >> 1)
739 + (sSIMD2 & dSIMD2 & 0x00010101)) | A_Dst;
740 }, end);
741 } else {
742 sSIMD2 &= 0xFF00;
743 sSIMD2 = sSIMD2 >> 8 | sSIMD2 << 8;
745 {
746 dSIMD2 = *pixel;
747 A_Dst = dSIMD2 & A_Mask;
748 dSIMD1 = dSIMD2 & 0x00FF00FF;
749 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
750 dSIMD1 &= 0x00FF00FF;
751 dSIMD2 &= 0xFF00;
752 dSIMD2 += (((sSIMD2 << 8) & 0xFF00) - dSIMD2) * A >> 8;
753 dSIMD2 &= 0xFF00;
754 *pixel++ = dSIMD1 | dSIMD2 | A_Dst;
755 }, {
756 dSIMD1 = *pixel;
757 A_Dst = dSIMD1 & A_Mask;
758 dSIMD1 &= 0x00FF00FF;
759 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
760 dSIMD1 &= 0x00FF00FF;
761
762 dSIMD2 = ((*pixel & 0xFF00) >> 8)| ((pixel[1] & 0xFF00) << 8);
763 dSIMD2 += (sSIMD2 - dSIMD2) * A >> 8;
764 dSIMD2 &= 0x00FF00FF;
765
766 *pixel++ = dSIMD1 | ((dSIMD2 << 8) & 0xFF00) | A_Dst;
767
768 dSIMD1 = *pixel;
769 A_Dst = dSIMD1 & A_Mask;
770 dSIMD1 &= 0x00FF00FF;
771 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
772 dSIMD1 &= 0x00FF00FF;
773
774 *pixel++ = dSIMD1 | ((dSIMD2 >> 8) & 0xFF00) | A_Dst;
775 }, end);
776 }
777 } else {
778 /* Correct prect size */
779 if (prect->x < 0) {
780 prect->w += prect->x;
781 prect->x = 0;
782 } else if (prect->x >= surf->w - prect->w) {
783 prect->w = surf->w - prect->x;
784 }
785
786 if (prect->y < 0) {
787 prect->h += prect->y;
788 prect->y = 0;
789 } else if (prect->y >= surf->h - prect->h) {
790 prect->h = surf->h - prect->y;
791 }
792
793 start = pixel = (Uint32 *) surf->pixels +
794 (prect->y * (surf->pitch >> 2)) + prect->x;
795
796 if (A == 128) { /* 50% A */
797 y = prect->h;
798 end = prect->w;
799 while (y--) {
801 {
802 dSIMD2 = *pixel;
803 A_Dst = dSIMD2 & A_Mask;
804 *pixel++ = ((((sSIMD2 & 0x00fefefe) + (dSIMD2 & 0x00fefefe)) >> 1)
805 + (sSIMD2 & dSIMD2 & 0x00010101)) | A_Dst;
806 }, end);
807 pixel = start + (surf->pitch >> 2);
808 start = pixel;
809 }
810 } else {
811 y = prect->h;
812 end = prect->w;
813
814 sSIMD2 &= 0xFF00;
815 sSIMD2 = sSIMD2 >> 8 | sSIMD2 << 8;
816
817 while (y--) {
819 {
820 dSIMD2 = *pixel;
821 A_Dst = dSIMD2 & A_Mask;
822 dSIMD1 = dSIMD2 & 0x00FF00FF;
823 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
824 dSIMD1 &= 0x00FF00FF;
825 dSIMD2 &= 0xFF00;
826 dSIMD2 += (((sSIMD2 << 8) & 0xFF00) - dSIMD2) * A >> 8;
827 dSIMD2 &= 0xFF00;
828 *pixel++ = dSIMD1 | dSIMD2 | A_Dst;
829 }, {
830 dSIMD1 = *pixel;
831 A_Dst = dSIMD1 & A_Mask;
832 dSIMD1 &= 0x00FF00FF;
833 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
834 dSIMD1 &= 0x00FF00FF;
835
836 dSIMD2 = ((*pixel & 0xFF00) >> 8)| ((pixel[1] & 0xFF00) << 8);
837 dSIMD2 += (sSIMD2 - dSIMD2) * A >> 8;
838 dSIMD2 &= 0x00FF00FF;
839
840 *pixel++ = dSIMD1 | ((dSIMD2 << 8) & 0xFF00) | A_Dst;
841
842 dSIMD1 = *pixel;
843 A_Dst = dSIMD1 & A_Mask;
844 dSIMD1 &= 0x00FF00FF;
845 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
846 dSIMD1 &= 0x00FF00FF;
847
848 *pixel++ = dSIMD1 | ((dSIMD2 >> 8) & 0xFF00) | A_Dst;
849 }, end);
850
851 pixel = start + (surf->pitch >> 2);
852 start = pixel;
853 } /* while */
854 }
855 }
856
857 unlock_surf(surf);
858
859 return 0;
860}
861
862/**********************************************************************/
865int fill_rect_alpha(SDL_Surface *surf, SDL_Rect *prect,
866 SDL_Color *pcolor)
867{
868 if (prect && (prect->x < - prect->w || prect->x >= surf->w
869 || prect->y < - prect->h || prect->y >= surf->h)) {
870 return -2;
871 }
872
873 if (pcolor->a == 255) {
874 return SDL_FillRect(surf, prect,
875 SDL_MapRGB(surf->format, pcolor->r, pcolor->g, pcolor->b));
876 }
877
878 if (!pcolor->a) {
879 return -3;
880 }
881
882 return __FillRectAlpha8888_32bit(surf, prect, pcolor);
883}
884
885/**********************************************************************/
888bool correct_rect_region(SDL_Rect *prect)
889{
890 int ww = prect->w, hh = prect->h;
891
892 if (prect->x < 0) {
893 ww += prect->x;
894 prect->x = 0;
895 }
896
897 if (prect->y < 0) {
898 hh += prect->y;
899 prect->y = 0;
900 }
901
902 if (prect->x + ww > main_window_width()) {
903 ww = main_window_width() - prect->x;
904 }
905
906 if (prect->y + hh > main_window_height()) {
907 hh = main_window_height() - prect->y;
908 }
909
910 /* End Correction */
911
912 if (ww <= 0 || hh <= 0) {
913 return FALSE; /* surprise :) */
914 } else {
915 prect->w = ww;
916 prect->h = hh;
917 }
918
919 return TRUE;
920}
921
922/**********************************************************************/
925bool is_in_rect_area(int x, int y, SDL_Rect rect)
926{
927 return ((x >= rect.x) && (x < rect.x + rect.w)
928 && (y >= rect.y) && (y < rect.y + rect.h));
929}
930
931/* ===================================================================== */
932
933/**********************************************************************/
936SDL_Rect get_smaller_surface_rect(SDL_Surface *surf)
937{
938 SDL_Rect src;
939 int w, h, x, y;
940 Uint16 minX, maxX, minY, maxY;
941 Uint32 colorkey;
942 Uint32 mask;
943
944 fc_assert(surf != NULL);
945
946 minX = surf->w;
947 maxX = 0;
948 minY = surf->h;
949 maxY = 0;
950
951 if (SDL_GetColorKey(surf, &colorkey) < 0) {
952 /* Use alpha instead of colorkey */
953 mask = surf->format->Amask;
954 colorkey = 0;
955 } else {
956 mask = 0xffffffff;
957 }
958
959 lock_surf(surf);
960
961 switch (surf->format->BytesPerPixel) {
962 case 1:
963 {
964 Uint8 *pixel = (Uint8 *)surf->pixels;
965 Uint8 *start = pixel;
966 x = 0;
967 y = 0;
968 w = surf->w;
969 h = surf->h;
970 while (h--) {
971 do {
972 if (*pixel != colorkey) {
973 if (minY > y) {
974 minY = y;
975 }
976
977 if (minX > x) {
978 minX = x;
979 }
980 break;
981 }
982 pixel++;
983 x++;
984 } while (--w > 0);
985 w = surf->w;
986 x = 0;
987 y++;
988 pixel = start + surf->pitch;
989 start = pixel;
990 }
991
992 w = surf->w;
993 h = surf->h;
994 x = w - 1;
995 y = h - 1;
996 pixel = (Uint8 *)((Uint8 *)surf->pixels + (y * surf->pitch) + x);
997 start = pixel;
998 while (h--) {
999 do {
1000 if (*pixel != colorkey) {
1001 if (maxY < y) {
1002 maxY = y;
1003 }
1004
1005 if (maxX < x) {
1006 maxX = x;
1007 }
1008 break;
1009 }
1010 pixel--;
1011 x--;
1012 } while (--w > 0);
1013 w = surf->w;
1014 x = w - 1;
1015 y--;
1016 pixel = start - surf->pitch;
1017 start = pixel;
1018 }
1019 break;
1020 }
1021 case 2:
1022 {
1023 Uint16 *pixel = (Uint16 *)surf->pixels;
1024 Uint16 *start = pixel;
1025
1026 x = 0;
1027 y = 0;
1028 w = surf->w;
1029 h = surf->h;
1030 while (h--) {
1031 do {
1032 if (*pixel != colorkey) {
1033 if (minY > y) {
1034 minY = y;
1035 }
1036
1037 if (minX > x) {
1038 minX = x;
1039 }
1040 break;
1041 }
1042 pixel++;
1043 x++;
1044 } while (--w > 0);
1045 w = surf->w;
1046 x = 0;
1047 y++;
1048 pixel = start + surf->pitch / 2;
1049 start = pixel;
1050 }
1051
1052 w = surf->w;
1053 h = surf->h;
1054 x = w - 1;
1055 y = h - 1;
1056 pixel = ((Uint16 *)surf->pixels + (y * surf->pitch / 2) + x);
1057 start = pixel;
1058 while (h--) {
1059 do {
1060 if (*pixel != colorkey) {
1061 if (maxY < y) {
1062 maxY = y;
1063 }
1064
1065 if (maxX < x) {
1066 maxX = x;
1067 }
1068 break;
1069 }
1070 pixel--;
1071 x--;
1072 } while (--w > 0);
1073 w = surf->w;
1074 x = w - 1;
1075 y--;
1076 pixel = start - surf->pitch / 2;
1077 start = pixel;
1078 }
1079 break;
1080 }
1081 case 3:
1082 {
1083 Uint8 *pixel = (Uint8 *)surf->pixels;
1084 Uint8 *start = pixel;
1085 Uint32 color;
1086
1087 x = 0;
1088 y = 0;
1089 w = surf->w;
1090 h = surf->h;
1091 while (h--) {
1092 do {
1093 if (is_bigendian()) {
1094 color = (pixel[0] << 16 | pixel[1] << 8 | pixel[2]);
1095 } else {
1096 color = (pixel[0] | pixel[1] << 8 | pixel[2] << 16);
1097 }
1098 if (color != colorkey) {
1099 if (minY > y) {
1100 minY = y;
1101 }
1102
1103 if (minX > x) {
1104 minX = x;
1105 }
1106 break;
1107 }
1108 pixel += 3;
1109 x++;
1110 } while (--w > 0);
1111 w = surf->w;
1112 x = 0;
1113 y++;
1114 pixel = start + surf->pitch / 3;
1115 start = pixel;
1116 }
1117
1118 w = surf->w;
1119 h = surf->h;
1120 x = w - 1;
1121 y = h - 1;
1122 pixel = (Uint8 *)((Uint8 *)surf->pixels + (y * surf->pitch) + x * 3);
1123 start = pixel;
1124 while (h--) {
1125 do {
1126 if (is_bigendian()) {
1127 color = (pixel[0] << 16 | pixel[1] << 8 | pixel[2]);
1128 } else {
1129 color = (pixel[0] | pixel[1] << 8 | pixel[2] << 16);
1130 }
1131 if (color != colorkey) {
1132 if (maxY < y) {
1133 maxY = y;
1134 }
1135
1136 if (maxX < x) {
1137 maxX = x;
1138 }
1139 break;
1140 }
1141 pixel -= 3;
1142 x--;
1143 } while (--w > 0);
1144 w = surf->w;
1145 x = w - 1;
1146 y--;
1147 pixel = start - surf->pitch / 3;
1148 start = pixel;
1149 }
1150 break;
1151 }
1152 case 4:
1153 {
1154 Uint32 *pixel = (Uint32 *)surf->pixels;
1155 Uint32 *start = pixel;
1156
1157 x = 0;
1158 y = 0;
1159 w = surf->w;
1160 h = surf->h;
1161 while (h--) {
1162 do {
1163 if (((*pixel) & mask) != colorkey) {
1164 if (minY > y) {
1165 minY = y;
1166 }
1167
1168 if (minX > x) {
1169 minX = x;
1170 }
1171 break;
1172 }
1173 pixel++;
1174 x++;
1175 } while (--w > 0);
1176 w = surf->w;
1177 x = 0;
1178 y++;
1179 pixel = start + surf->pitch / 4;
1180 start = pixel;
1181 }
1182
1183 w = surf->w;
1184 h = surf->h;
1185 x = w - 1;
1186 y = h - 1;
1187 pixel = ((Uint32 *)surf->pixels + (y * surf->pitch / 4) + x);
1188 start = pixel;
1189 while (h--) {
1190 do {
1191 if (((*pixel) & mask) != colorkey) {
1192 if (maxY < y) {
1193 maxY = y;
1194 }
1195
1196 if (maxX < x) {
1197 maxX = x;
1198 }
1199 break;
1200 }
1201 pixel--;
1202 x--;
1203 } while (--w > 0);
1204 w = surf->w;
1205 x = w - 1;
1206 y--;
1207 pixel = start - surf->pitch / 4;
1208 start = pixel;
1209 }
1210 break;
1211 }
1212 }
1213
1214 unlock_surf(surf);
1215 src.x = minX;
1216 src.y = minY;
1217 src.w = maxX - minX + 1;
1218 src.h = maxY - minY + 1;
1219
1220 return src;
1221}
1222
1223/**********************************************************************/
1226SDL_Surface *crop_visible_part_from_surface(SDL_Surface *psrc)
1227{
1228 SDL_Rect src = get_smaller_surface_rect(psrc);
1229
1230 return crop_rect_from_surface(psrc, &src);
1231}
1232
1233/**********************************************************************/
1236SDL_Surface *resize_surface(const SDL_Surface *psrc, Uint16 new_width,
1237 Uint16 new_height, int smooth)
1238{
1239 if (psrc == NULL) {
1240 return NULL;
1241 }
1242
1243 return zoomSurface((SDL_Surface*)psrc,
1244 (double)new_width / psrc->w,
1245 (double)new_height / psrc->h,
1246 smooth);
1247}
1248
1249/**********************************************************************/
1256SDL_Surface *resize_surface_box(const SDL_Surface *psrc,
1257 Uint16 new_width, Uint16 new_height,
1258 int smooth, bool scale_up,
1259 bool absolute_dimensions)
1260{
1261 SDL_Surface *tmp_surface, *result;
1262
1263 if (psrc == NULL) {
1264 return NULL;
1265 }
1266
1267 if (!(!scale_up && ((new_width >= psrc->w) && (new_height >= psrc->h)))) {
1268 if ((new_width - psrc->w) <= (new_height - psrc->h)) {
1269 /* horizontal limit */
1270 tmp_surface = zoomSurface((SDL_Surface*)psrc,
1271 (double)new_width / psrc->w,
1272 (double)new_width / psrc->w,
1273 smooth);
1274 } else {
1275 /* vertical limit */
1276 tmp_surface = zoomSurface((SDL_Surface*)psrc,
1277 (double)new_height / psrc->h,
1278 (double)new_height / psrc->h,
1279 smooth);
1280 }
1281 } else {
1282 tmp_surface = zoomSurface((SDL_Surface*)psrc,
1283 1.0,
1284 1.0,
1285 smooth);
1286 }
1287
1288 if (absolute_dimensions) {
1289 SDL_Rect area = {
1290 (new_width - tmp_surface->w) / 2,
1291 (new_height - tmp_surface->h) / 2,
1292 0, 0
1293 };
1294
1295 result = create_surf(new_width, new_height, SDL_SWSURFACE);
1296 alphablit(tmp_surface, NULL, result, &area, 255);
1297 FREESURFACE(tmp_surface);
1298 } else {
1299 result = tmp_surface;
1300 }
1301
1302 return result;
1303}
1304
1305/**********************************************************************/
1308SDL_Surface *copy_surface(SDL_Surface *src)
1309{
1310 SDL_Surface *dst;
1311
1312 dst = SDL_CreateRGBSurface(0, src->w, src->h, src->format->BitsPerPixel,
1313 src->format->Rmask, src->format->Gmask,
1314 src->format->Bmask, src->format->Amask);
1315
1316 SDL_BlitSurface(src, NULL, dst, NULL);
1317
1318 return dst;
1319}
1320
1321/* ============ Freeciv game graphics function =========== */
1322
1323/**********************************************************************/
1326bool is_view_supported(enum ts_type type)
1327{
1328 switch (type) {
1329 case TS_ISOMETRIC:
1330 case TS_OVERHEAD:
1331 return TRUE;
1332 }
1333
1334 return FALSE;
1335}
1336
1337/**********************************************************************/
1340void tileset_type_set(enum ts_type type)
1341{
1342}
1343
1344/**********************************************************************/
1347void create_frame(SDL_Surface *dest, Sint16 left, Sint16 top,
1348 Sint16 width, Sint16 height,
1349 SDL_Color *pcolor)
1350{
1351 struct color gsdl2_color = { .color = pcolor };
1352 struct sprite *vertical = create_sprite(1, height, &gsdl2_color);
1353 struct sprite *horizontal = create_sprite(width, 1, &gsdl2_color);
1354 SDL_Rect tmp,dst = { left, top, 0, 0 };
1355
1356 tmp = dst;
1357 alphablit(vertical->psurface, NULL, dest, &tmp, 255);
1358
1359 dst.x += width - 1;
1360 tmp = dst;
1361 alphablit(vertical->psurface, NULL, dest, &tmp, 255);
1362
1363 dst.x = left;
1364 tmp = dst;
1365 alphablit(horizontal->psurface, NULL, dest, &tmp, 255);
1366
1367 dst.y += height - 1;
1368 tmp = dst;
1369 alphablit(horizontal->psurface, NULL, dest, &tmp, 255);
1370
1371 free_sprite(horizontal);
1372 free_sprite(vertical);
1373}
1374
1375/**********************************************************************/
1378void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1,
1379 SDL_Color *pcolor)
1380{
1381 int xl = x0 < x1 ? x0 : x1;
1382 int xr = x0 < x1 ? x1 : x0;
1383 int yt = y0 < y1 ? y0 : y1;
1384 int yb = y0 < y1 ? y1 : y0;
1385 int w = (xr - xl) + 1;
1386 int h = (yb - yt) + 1;
1387 struct sprite *spr;
1388 SDL_Rect dst = { xl, yt, w, h };
1389 SDL_Color *pcol;
1390 struct color gsdl2_color;
1391 int l = MAX((xr - xl) + 1, (yb - yt) + 1);
1392 int i;
1393
1394 pcol = fc_malloc(sizeof(pcol));
1395 pcol->r = pcolor->r;
1396 pcol->g = pcolor->g;
1397 pcol->b = pcolor->b;
1398 pcol->a = 0; /* Fill with transparency */
1399
1400 gsdl2_color.color = pcol;
1401 spr = create_sprite(w, h, &gsdl2_color);
1402
1403 lock_surf(spr->psurface);
1404
1405 /* Set off transparency from pixels belonging to the line */
1406 if ((x0 <= x1 && y0 <= y1)
1407 || (x1 <= x0 && y1 <= y0)) {
1408 for (i = 0; i < l; i++) {
1409 int cx = (xr - xl) * i / l;
1410 int cy = (yb - yt) * i / l;
1411
1412 *((Uint32 *)spr->psurface->pixels + spr->psurface->w * cy + cx)
1413 |= (pcolor->a << spr->psurface->format->Ashift);
1414 }
1415 } else {
1416 for (i = 0; i < l; i++) {
1417 int cx = (xr - xl) * i / l;
1418 int cy = yb - yt - (yb - yt) * i / l;
1419
1420 *((Uint32 *)spr->psurface->pixels + spr->psurface->w * cy + cx)
1421 |= (pcolor->a << spr->psurface->format->Ashift);
1422 }
1423 }
1424
1425 unlock_surf(spr->psurface);
1426
1427 alphablit(spr->psurface, NULL, dest, &dst, 255);
1428
1429 free_sprite(spr);
1430 free(pcol);
1431}
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 int int int width
Definition canvas_g.h:44
#define _(String)
Definition fcintl.h:67
bool is_view_supported(enum ts_type type)
Definition graphics.c:53
void tileset_type_set(enum ts_type type)
Definition graphics.c:67
GType type
Definition repodlgs.c:1312
void free_sprite(struct sprite *s)
Definition sprite.c:278
SDL_Surface * resize_surface(const SDL_Surface *psrc, Uint16 new_width, Uint16 new_height, int smooth)
Definition graphics.c:1236
bool set_video_mode(unsigned width, unsigned height, unsigned flags_in)
Definition graphics.c:636
SDL_Rect get_smaller_surface_rect(SDL_Surface *surf)
Definition graphics.c:936
Uint32 getpixel(SDL_Surface *surf, Sint16 x, Sint16 y)
Definition graphics.c:430
bool is_in_rect_area(int x, int y, SDL_Rect rect)
Definition graphics.c:925
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 init_sdl(int flags)
Definition graphics.c:506
void update_main_screen(void)
Definition graphics.c:669
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:1378
SDL_Surface * copy_surface(SDL_Surface *src)
Definition graphics.c:1308
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:1226
SDL_Surface * crop_rect_from_surface(SDL_Surface *psource, SDL_Rect *prect)
Definition graphics.c:236
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
struct gui_layer * get_gui_layer(SDL_Surface *surface)
Definition graphics.c:90
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
static SDL_Surface * main_surface
Definition graphics.c:59
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
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
static int __FillRectAlpha8888_32bit(SDL_Surface *surf, SDL_Rect *prect, SDL_Color *pcolor)
Definition graphics.c:715
void create_frame(SDL_Surface *dest, Sint16 left, Sint16 top, Sint16 width, Sint16 height, SDL_Color *pcolor)
Definition graphics.c:1347
static void free_surfaces(void)
Definition graphics.c:543
bool create_surfaces(int width, int height)
Definition graphics.c:556
int main_window_height(void)
Definition graphics.c:693
static bool render_dirty
Definition graphics.c:61
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:1256
#define FREESURFACE(ptr)
Definition graphics.h:322
#define unlock_surf(surf)
Definition graphics.h:344
#define lock_surf(surf)
Definition graphics.h:333
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment, double_pixel_copy_increment, width)
Definition graphics.h:98
#define DUFFS_LOOP8(pixel_copy_increment, width)
Definition graphics.h:60
#define DUFFS_LOOP4(pixel_copy_increment, width)
Definition graphics.h:83
Uint32 sdl2_client_flags
Definition gui_main.c:97
#define CF_SWRENDERER
Definition gui_main.h:61
#define fc_assert(condition)
Definition log.h:176
#define log_fatal(message,...)
Definition log.h:100
#define log_error(message,...)
Definition log.h:103
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_realloc(ptr, sz)
Definition mem.h:36
#define fc_malloc(sz)
Definition mem.h:34
struct client_options gui_options
Definition options.c:71
#define MAX(x, y)
Definition shared.h:54
create_sprite
Definition sprite_g.h:30
bool gui_sdl2_swrenderer
Definition options.h:378
Definition colors.h:20
GdkRGBA color
Definition colors.h:21
SDL_Surface * surface
Definition graphics.h:229
SDL_Rect dest_rect
Definition graphics.h:228
int guis_count
Definition graphics.h:207
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
struct gui_layer ** guis
Definition graphics.h:216
SDL_Window * screen
Definition graphics.h:209
SDL_Surface * map
Definition graphics.h:210
struct SDL_Surface * psurface
Definition sprite.h:21
static bool is_bigendian(void)
Definition support.h:207
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47