Freeciv-3.2
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
60
61static bool render_dirty = TRUE;
62
63/**********************************************************************/
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/**********************************************************************/
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
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/**********************************************************************/
175{
176 if (gui_layer) {
179 }
180}
181
182/**********************************************************************/
187{
188 if (gui_layer) {
191 }
192}
193
194/* ============ Freeciv sdl graphics function =========== */
195
196/**********************************************************************/
201 unsigned char alpha_mod)
202{
203 int ret;
204
205 if (src == NULL || dst == NULL) {
206 return 1;
207 }
208
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/**********************************************************************/
224 unsigned char alpha_mod)
225{
227
229}
230
231/**********************************************************************/
238{
240 prect ? prect->w : psource->w,
241 prect ? prect->h : psource->h,
243
244 if (alphablit(psource, prect, new_surf, NULL, 255) != 0) {
246
247 return NULL;
248 }
249
250 return new_surf;
251}
252
253/**********************************************************************/
264{
265 SDL_Surface *dest = NULL;
266 int row, col;
270 unsigned char src_alpha, mask_alpha;
271
272 dest = copy_surface(src);
273
274 lock_surf(src);
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)
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
294 }
295 }
296
297 unlock_surf(dest);
299 unlock_surf(src);
300
301 return dest;
302}
303
304/**********************************************************************/
308{
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/**********************************************************************/
329 int width, int height,
330 Uint32 flags)
331{
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/**********************************************************************/
352{
353 return create_surf_with_format(main_surface->format, width, height, flags);
354}
355
356/**********************************************************************/
363
364/**********************************************************************/
370{
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
386 SDL_MapRGBA(new_surf->format, pcolor->r, pcolor->g, pcolor->b,
387 pcolor->a));
388
389 if (pcolor->a != 255) {
391 }
392
393 return new_surf;
394}
395
396/**********************************************************************/
401{
402 /* SDL_FillRect might change the rectangle, so we create a copy */
403 if (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/**********************************************************************/
424
425/**********************************************************************/
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/**********************************************************************/
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
514 main_data.dummy = NULL; /* Can't create yet -- hope we don't need it */
518
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());
527 }
528
530
531 /* Initialize the TTF library */
532 if (TTF_Init() < 0) {
533 log_fatal(_("Unable to initialize SDL2_ttf library: %s"), SDL_GetError());
535 }
536
538}
539
540/**********************************************************************/
543static void free_surfaces(void)
544{
545 if (main_data.renderer != NULL) {
548
550 }
551}
552
553/**********************************************************************/
557{
558 unsigned flags;
559
561
562 if (is_bigendian()) {
564 0x0000FF00, 0x00FF0000,
565 0xFF000000, 0x000000FF);
567 0x0000FF00, 0x00FF0000,
568 0xFF000000, 0x000000FF);
569 } else {
571 0x00FF0000, 0x0000FF00,
572 0x000000FF, 0xFF000000);
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
591
592 if (main_data.renderer == NULL) {
593 log_fatal(_("Failed to create renderer: %s"), SDL_GetError());
594 return FALSE;
595 }
596
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) {
610 } else {
612 }
613
615
616 return TRUE;
617}
618
619/**********************************************************************/
632
633/**********************************************************************/
636bool set_video_mode(unsigned width, unsigned height, unsigned flags_in)
637{
638 main_data.screen = SDL_CreateWindow(_("SDL2 Client for Freeciv"),
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
650 SDL_DisplayMode mode;
651
652 /* Use SDL_WINDOW_FULLSCREEN_DESKTOP instead of real SDL_WINDOW_FULLSCREEN */
655 width = mode.w;
656 height = mode.h;
657 }
658
660 return FALSE;
661 }
662
663 return TRUE;
664}
665
666/**********************************************************************/
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/**********************************************************************/
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/**********************************************************************/
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
883}
884
885/**********************************************************************/
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, const 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/**********************************************************************/
937{
938 int w, h, x, y;
939 Uint16 minX, maxX, minY, maxY;
941 Uint32 mask;
942
943 fc_assert(surf != NULL);
944 fc_assert(rect != 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
967 x = 0;
968 y = 0;
969 w = surf->w;
970 h = surf->h;
971 while (h--) {
972 do {
973 if (*pixel != colorkey) {
974 if (minY > y) {
975 minY = y;
976 }
977
978 if (minX > x) {
979 minX = x;
980 }
981 break;
982 }
983 pixel++;
984 x++;
985 } while (--w > 0);
986 w = surf->w;
987 x = 0;
988 y++;
989 pixel = start + surf->pitch;
990 start = pixel;
991 }
992
993 w = surf->w;
994 h = surf->h;
995 x = w - 1;
996 y = h - 1;
997 pixel = (Uint8 *)((Uint8 *)surf->pixels + (y * surf->pitch) + x);
998 start = pixel;
999 while (h--) {
1000 do {
1001 if (*pixel != colorkey) {
1002 if (maxY < y) {
1003 maxY = y;
1004 }
1005
1006 if (maxX < x) {
1007 maxX = x;
1008 }
1009 break;
1010 }
1011 pixel--;
1012 x--;
1013 } while (--w > 0);
1014 w = surf->w;
1015 x = w - 1;
1016 y--;
1017 pixel = start - surf->pitch;
1018 start = pixel;
1019 }
1020 break;
1021 }
1022 case 2:
1023 {
1024 Uint16 *pixel = (Uint16 *)surf->pixels;
1025 Uint16 *start = pixel;
1026
1027 x = 0;
1028 y = 0;
1029 w = surf->w;
1030 h = surf->h;
1031 while (h--) {
1032 do {
1033 if (*pixel != colorkey) {
1034 if (minY > y) {
1035 minY = y;
1036 }
1037
1038 if (minX > x) {
1039 minX = x;
1040 }
1041 break;
1042 }
1043 pixel++;
1044 x++;
1045 } while (--w > 0);
1046 w = surf->w;
1047 x = 0;
1048 y++;
1049 pixel = start + surf->pitch / 2;
1050 start = pixel;
1051 }
1052
1053 w = surf->w;
1054 h = surf->h;
1055 x = w - 1;
1056 y = h - 1;
1057 pixel = ((Uint16 *)surf->pixels + (y * surf->pitch / 2) + x);
1058 start = pixel;
1059 while (h--) {
1060 do {
1061 if (*pixel != colorkey) {
1062 if (maxY < y) {
1063 maxY = y;
1064 }
1065
1066 if (maxX < x) {
1067 maxX = x;
1068 }
1069 break;
1070 }
1071 pixel--;
1072 x--;
1073 } while (--w > 0);
1074 w = surf->w;
1075 x = w - 1;
1076 y--;
1077 pixel = start - surf->pitch / 2;
1078 start = pixel;
1079 }
1080 break;
1081 }
1082 case 3:
1083 {
1084 Uint8 *pixel = (Uint8 *)surf->pixels;
1085 Uint8 *start = pixel;
1086 Uint32 color;
1087
1088 x = 0;
1089 y = 0;
1090 w = surf->w;
1091 h = surf->h;
1092 while (h--) {
1093 do {
1094 if (is_bigendian()) {
1095 color = (pixel[0] << 16 | pixel[1] << 8 | pixel[2]);
1096 } else {
1097 color = (pixel[0] | pixel[1] << 8 | pixel[2] << 16);
1098 }
1099 if (color != colorkey) {
1100 if (minY > y) {
1101 minY = y;
1102 }
1103
1104 if (minX > x) {
1105 minX = x;
1106 }
1107 break;
1108 }
1109 pixel += 3;
1110 x++;
1111 } while (--w > 0);
1112 w = surf->w;
1113 x = 0;
1114 y++;
1115 pixel = start + surf->pitch / 3;
1116 start = pixel;
1117 }
1118
1119 w = surf->w;
1120 h = surf->h;
1121 x = w - 1;
1122 y = h - 1;
1123 pixel = (Uint8 *)((Uint8 *)surf->pixels + (y * surf->pitch) + x * 3);
1124 start = pixel;
1125 while (h--) {
1126 do {
1127 if (is_bigendian()) {
1128 color = (pixel[0] << 16 | pixel[1] << 8 | pixel[2]);
1129 } else {
1130 color = (pixel[0] | pixel[1] << 8 | pixel[2] << 16);
1131 }
1132 if (color != colorkey) {
1133 if (maxY < y) {
1134 maxY = y;
1135 }
1136
1137 if (maxX < x) {
1138 maxX = x;
1139 }
1140 break;
1141 }
1142 pixel -= 3;
1143 x--;
1144 } while (--w > 0);
1145 w = surf->w;
1146 x = w - 1;
1147 y--;
1148 pixel = start - surf->pitch / 3;
1149 start = pixel;
1150 }
1151 break;
1152 }
1153 case 4:
1154 {
1155 Uint32 *pixel = (Uint32 *)surf->pixels;
1156 Uint32 *start = pixel;
1157
1158 x = 0;
1159 y = 0;
1160 w = surf->w;
1161 h = surf->h;
1162 while (h--) {
1163 do {
1164 if (((*pixel) & mask) != colorkey) {
1165 if (minY > y) {
1166 minY = y;
1167 }
1168
1169 if (minX > x) {
1170 minX = x;
1171 }
1172 break;
1173 }
1174 pixel++;
1175 x++;
1176 } while (--w > 0);
1177 w = surf->w;
1178 x = 0;
1179 y++;
1180 pixel = start + surf->pitch / 4;
1181 start = pixel;
1182 }
1183
1184 w = surf->w;
1185 h = surf->h;
1186 x = w - 1;
1187 y = h - 1;
1188 pixel = ((Uint32 *)surf->pixels + (y * surf->pitch / 4) + x);
1189 start = pixel;
1190 while (h--) {
1191 do {
1192 if (((*pixel) & mask) != colorkey) {
1193 if (maxY < y) {
1194 maxY = y;
1195 }
1196
1197 if (maxX < x) {
1198 maxX = x;
1199 }
1200 break;
1201 }
1202 pixel--;
1203 x--;
1204 } while (--w > 0);
1205 w = surf->w;
1206 x = w - 1;
1207 y--;
1208 pixel = start - surf->pitch / 4;
1209 start = pixel;
1210 }
1211 break;
1212 }
1213 }
1214
1215 unlock_surf(surf);
1216 rect->x = minX;
1217 rect->y = minY;
1218 rect->w = maxX - minX + 1;
1219 rect->h = maxY - minY + 1;
1220}
1221
1222/**********************************************************************/
1233
1234/**********************************************************************/
1239{
1240 if (psrc == NULL) {
1241 return NULL;
1242 }
1243
1244 return zoomSurface((SDL_Surface*)psrc,
1245 (double)new_width / psrc->w,
1246 (double)new_height / psrc->h,
1247 smooth);
1248}
1249
1250/**********************************************************************/
1259 int smooth, bool scale_up,
1261{
1262 SDL_Surface *tmp_surface, *result;
1263
1264 if (psrc == NULL) {
1265 return NULL;
1266 }
1267
1268 if (!(!scale_up && ((new_width >= psrc->w) && (new_height >= psrc->h)))) {
1269 if ((new_width - psrc->w) <= (new_height - psrc->h)) {
1270 /* horizontal limit */
1272 (double)new_width / psrc->w,
1273 (double)new_width / psrc->w,
1274 smooth);
1275 } else {
1276 /* vertical limit */
1278 (double)new_height / psrc->h,
1279 (double)new_height / psrc->h,
1280 smooth);
1281 }
1282 } else {
1284 1.0,
1285 1.0,
1286 smooth);
1287 }
1288
1289 if (absolute_dimensions) {
1290 SDL_Rect area = {
1291 (new_width - tmp_surface->w) / 2,
1292 (new_height - tmp_surface->h) / 2,
1293 0, 0
1294 };
1295
1297 alphablit(tmp_surface, NULL, result, &area, 255);
1299 } else {
1300 result = tmp_surface;
1301 }
1302
1303 return result;
1304}
1305
1306/**********************************************************************/
1310{
1311 SDL_Surface *dst;
1312
1313 dst = SDL_CreateRGBSurface(0, src->w, src->h, src->format->BitsPerPixel,
1314 src->format->Rmask, src->format->Gmask,
1315 src->format->Bmask, src->format->Amask);
1316
1317 SDL_BlitSurface(src, NULL, dst, NULL);
1318
1319 return dst;
1320}
1321
1322/* ============ Freeciv game graphics function =========== */
1323
1324/**********************************************************************/
1328{
1329 switch (type) {
1330 case TS_ISOMETRIC:
1331 case TS_OVERHEAD:
1332 return TRUE;
1333 }
1334
1335 return FALSE;
1336}
1337
1338/**********************************************************************/
1342{
1343}
1344
1345/**********************************************************************/
1351{
1352 struct color gsdl2_color = { .color = pcolor };
1355 SDL_Rect tmp, dst = { .x = left, .y = top, .w = 0, .h = 0 };
1356
1357 tmp = dst;
1358 alphablit(vertical->psurface, NULL, dest, &tmp, 255);
1359
1360 dst.x += width - 1;
1361 tmp = dst;
1362 alphablit(vertical->psurface, NULL, dest, &tmp, 255);
1363
1364 dst.x = left;
1365 tmp = dst;
1366 alphablit(horizontal->psurface, NULL, dest, &tmp, 255);
1367
1368 dst.y += height - 1;
1369 tmp = dst;
1370 alphablit(horizontal->psurface, NULL, dest, &tmp, 255);
1371
1374}
1375
1376/**********************************************************************/
1381{
1382 int xl = x0 < x1 ? x0 : x1;
1383 int xr = x0 < x1 ? x1 : x0;
1384 int yt = y0 < y1 ? y0 : y1;
1385 int yb = y0 < y1 ? y1 : y0;
1386 int w = (xr - xl) + 1;
1387 int h = (yb - yt) + 1;
1388 struct sprite *spr;
1389 SDL_Rect dst = { xl, yt, w, h };
1390 SDL_Color *pcol;
1391 struct color gsdl2_color;
1392 int l = MAX((xr - xl) + 1, (yb - yt) + 1);
1393 int i;
1394
1395 pcol = fc_malloc(sizeof(pcol));
1396 pcol->r = pcolor->r;
1397 pcol->g = pcolor->g;
1398 pcol->b = pcolor->b;
1399 pcol->a = 0; /* Fill with transparency */
1400
1401 gsdl2_color.color = pcol;
1402 spr = create_sprite(w, h, &gsdl2_color);
1403
1404 lock_surf(spr->psurface);
1405
1406 /* Set off transparency from pixels belonging to the line */
1407 if ((x0 <= x1 && y0 <= y1)
1408 || (x1 <= x0 && y1 <= y0)) {
1409 for (i = 0; i < l; i++) {
1410 int cx = (xr - xl) * i / l;
1411 int cy = (yb - yt) * i / l;
1412
1413 *((Uint32 *)spr->psurface->pixels + spr->psurface->w * cy + cx)
1414 |= (pcolor->a << spr->psurface->format->Ashift);
1415 }
1416 } else {
1417 for (i = 0; i < l; i++) {
1418 int cx = (xr - xl) * i / l;
1419 int cy = yb - yt - (yb - yt) * i / l;
1420
1421 *((Uint32 *)spr->psurface->pixels + spr->psurface->w * cy + cx)
1422 |= (pcolor->a << spr->psurface->format->Ashift);
1423 }
1424 }
1425
1426 unlock_surf(spr->psurface);
1427
1428 alphablit(spr->psurface, NULL, dest, &dst, 255);
1429
1431 free(pcol);
1432}
SDL_Surface * zoomSurface(SDL_Surface *src, double zoomx, double zoomy, int smooth)
Zoom a surface by independent horizontal and vertical factors with optional smoothing.
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:75
#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:1313
void free_sprite(struct sprite *s)
Definition sprite.c:278
struct sprite * create_sprite(int width, int height, struct color *pcolor)
Definition sprite.c:84
SDL_Surface * resize_surface(const SDL_Surface *psrc, Uint16 new_width, Uint16 new_height, int smooth)
Definition graphics.c:1237
bool set_video_mode(unsigned width, unsigned height, unsigned flags_in)
Definition graphics.c:636
Uint32 getpixel(SDL_Surface *surf, Sint16 x, Sint16 y)
Definition graphics.c:430
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:1379
SDL_Surface * copy_surface(SDL_Surface *src)
Definition graphics.c:1309
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
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
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
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
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
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:1348
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:1257
#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:101
#define CF_SWRENDERER
Definition gui_main.h:64
#define GUI_SDL_OPTION(optname)
Definition gui_main.h:37
#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
#define MAX(x, y)
Definition shared.h:54
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
Definition colors.h:21
GdkRGBA color
Definition colors.h:22
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
static bool is_bigendian(void)
Definition support.h:235
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47