29#ifdef SDL2_PLAIN_INCLUDE
34#include <SDL2/SDL_image.h>
35#include <SDL2/SDL_syswm.h>
36#include <SDL2/SDL_ttf.h>
72 result->
dest_rect = (SDL_Rect){x, y, 0, 0};
200 SDL_Surface *dst, SDL_Rect *dstrect,
201 unsigned char alpha_mod)
205 if (src == NULL || dst == NULL) {
209 SDL_SetSurfaceAlphaMod(src, alpha_mod);
211 ret = SDL_BlitSurface(src, srcrect, dst, dstrect);
214 log_error(
"SDL_BlitSurface() fails: %s", SDL_GetError());
223int screen_blit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Rect *dstrect,
224 unsigned char alpha_mod)
240 prect ? prect->w : psource->w,
241 prect ? prect->h : psource->h,
244 if (
alphablit(psource, prect, new_surf, NULL, 255) != 0) {
263 int mask_offset_x,
int mask_offset_y)
265 SDL_Surface *dest = NULL;
267 Uint32 *src_pixel = NULL;
268 Uint32 *dest_pixel = NULL;
269 Uint32 *mask_pixel = NULL;
270 unsigned char src_alpha, mask_alpha;
278 src_pixel = (Uint32 *)src->pixels;
279 dest_pixel = (Uint32 *)dest->pixels;
281 for (row = 0; row < src->h; row++) {
282 mask_pixel = (Uint32 *)mask->pixels
283 + mask->w * (row + mask_offset_y)
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;
290 *dest_pixel = (*src_pixel & ~src->format->Amask)
291 | (((src_alpha * mask_alpha) / 255) << dest->format->Ashift);
293 src_pixel++; dest_pixel++; mask_pixel++;
315 if ((buf = IMG_Load(fname)) == NULL) {
316 log_error(
_(
"load_surf: Failed to load graphic file %s!"), fname);
332 SDL_Surface *surf = SDL_CreateRGBSurface(flags,
width,
height,
336 pf->Bmask, pf->Amask);
339 log_error(
_(
"Unable to create Sprite (Surface) of size "
340 "%d x %d %d Bits in format %d"),
361 return SDL_ConvertSurface(surf_in,
main_surface->format, 0);
371 SDL_Surface *new_surf;
372 SDL_Color
color = {255, 255, 255, 128};
385 SDL_FillRect(new_surf, NULL,
390 SDL_SetSurfaceAlphaMod(new_surf,
pcolor->a);
404 SDL_Rect _dstrect = *dstrect;
406 return SDL_FillRect(surf, &_dstrect, SDL_MapRGBA(surf->format, 0, 0, 0, 0));
408 return SDL_FillRect(surf, NULL, SDL_MapRGBA(surf->format, 0, 0, 0, 0));
418 Sint16 dest_x, Sint16 dest_y)
420 SDL_Rect
dest_rect = { dest_x, dest_y, 0, 0 };
430Uint32
getpixel(SDL_Surface *surf, Sint16 x, Sint16 y)
436 switch (surf->format->BytesPerPixel) {
438 return *(Uint8 *) ((Uint8 *) surf->pixels + y * surf->pitch + x);
441 return *((Uint16 *)surf->pixels + y * surf->pitch /
sizeof(Uint16) + x);
447 (Uint8 *) surf->pixels + y * surf->pitch + x * 3;
450 return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
452 return ptr[0] | ptr[1] << 8 | ptr[2] << 16;
456 return *((Uint32 *)surf->pixels + y * surf->pitch /
sizeof(Uint32) + x);
474 switch (surf->format->BytesPerPixel) {
476 return *((Uint8 *)surf->pixels);
479 return *((Uint16 *)surf->pixels);
484 return (((Uint8 *)surf->pixels)[0] << 16)
485 | (((Uint8 *)surf->pixels)[1] << 8)
486 | ((Uint8 *)surf->pixels)[2];
488 return ((Uint8 *)surf->pixels)[0]
489 | (((Uint8 *)surf->pixels)[1] << 8)
490 | (((Uint8 *)surf->pixels)[2] << 16);
494 return *((Uint32 *)surf->pixels);
519 if (SDL_WasInit(SDL_INIT_AUDIO)) {
520 error = (SDL_InitSubSystem(flags) < 0);
522 error = (SDL_Init(flags) < 0);
525 log_fatal(
_(
"Unable to initialize SDL2 library: %s"), SDL_GetError());
532 if (TTF_Init() < 0) {
533 log_fatal(
_(
"Unable to initialize SDL2_ttf library: %s"), SDL_GetError());
564 0x0000FF00, 0x00FF0000,
565 0xFF000000, 0x000000FF);
567 0x0000FF00, 0x00FF0000,
568 0xFF000000, 0x000000FF);
571 0x00FF0000, 0x0000FF00,
572 0x000000FF, 0xFF000000);
574 0x00FF0000, 0x0000FF00,
575 0x000000FF, 0xFF000000);
579 log_fatal(
_(
"Failed to create RGB surface: %s"), SDL_GetError());
585 flags = SDL_RENDERER_SOFTWARE;
593 log_fatal(
_(
"Failed to create renderer: %s"), SDL_GetError());
598 SDL_PIXELFORMAT_ARGB8888,
599 SDL_TEXTUREACCESS_STREAMING,
603 log_fatal(
_(
"Failed to create texture: %s"), SDL_GetError());
639 SDL_WINDOWPOS_UNDEFINED,
640 SDL_WINDOWPOS_UNDEFINED,
645 log_fatal(
_(
"Failed to create main window: %s"), SDL_GetError());
649 if (flags_in & SDL_WINDOW_FULLSCREEN) {
650 SDL_DisplayMode mode;
653 SDL_SetWindowFullscreen(
main_data.
screen, SDL_WINDOW_FULLSCREEN_DESKTOP);
701#define MASK565 0xf7de
702#define MASK555 0xfbde
705#define BLEND16_50( d, s , mask ) \
706 (((( s & mask ) + ( d & mask )) >> 1) + ( s & d & ( ~mask & 0xffff)))
708#define BLEND2x16_50( d, s , mask ) \
709 (((( s & (mask | mask << 16)) + ( d & ( mask | mask << 16 ))) >> 1) + \
710 ( s & d & ( ~(mask | mask << 16))))
718 register Uint32 A =
pcolor->a;
719 register Uint32 dSIMD1, dSIMD2;
720 register Uint32 sSIMD1, sSIMD2 = SDL_MapRGB(surf->format,
723 Uint32 y, end, A_Dst, A_Mask = surf->format->Amask;
724 Uint32 *start, *pixel;
726 sSIMD1 = sSIMD2 & 0x00FF00FF;
731 end = surf->w * surf->h;
732 pixel = (Uint32 *) surf->pixels;
737 A_Dst = dSIMD2 & A_Mask;
738 *pixel++ = ((((sSIMD2 & 0x00fefefe) + (dSIMD2 & 0x00fefefe)) >> 1)
739 + (sSIMD2 & dSIMD2 & 0x00010101)) | A_Dst;
743 sSIMD2 = sSIMD2 >> 8 | sSIMD2 << 8;
747 A_Dst = dSIMD2 & A_Mask;
748 dSIMD1 = dSIMD2 & 0x00FF00FF;
749 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
750 dSIMD1 &= 0x00FF00FF;
752 dSIMD2 += (((sSIMD2 << 8) & 0xFF00) - dSIMD2) * A >> 8;
754 *pixel++ = dSIMD1 | dSIMD2 | A_Dst;
757 A_Dst = dSIMD1 & A_Mask;
758 dSIMD1 &= 0x00FF00FF;
759 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
760 dSIMD1 &= 0x00FF00FF;
762 dSIMD2 = ((*pixel & 0xFF00) >> 8)| ((pixel[1] & 0xFF00) << 8);
763 dSIMD2 += (sSIMD2 - dSIMD2) * A >> 8;
764 dSIMD2 &= 0x00FF00FF;
766 *pixel++ = dSIMD1 | ((dSIMD2 << 8) & 0xFF00) | A_Dst;
769 A_Dst = dSIMD1 & A_Mask;
770 dSIMD1 &= 0x00FF00FF;
771 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
772 dSIMD1 &= 0x00FF00FF;
774 *pixel++ = dSIMD1 | ((dSIMD2 >> 8) & 0xFF00) | A_Dst;
780 prect->w += prect->x;
782 }
else if (prect->x >= surf->w - prect->w) {
783 prect->w = surf->w - prect->x;
787 prect->h += prect->y;
789 }
else if (prect->y >= surf->h - prect->h) {
790 prect->h = surf->h - prect->y;
793 start = pixel = (Uint32 *) surf->pixels +
794 (prect->y * (surf->pitch >> 2)) + prect->x;
803 A_Dst = dSIMD2 & A_Mask;
804 *pixel++ = ((((sSIMD2 & 0x00fefefe) + (dSIMD2 & 0x00fefefe)) >> 1)
805 + (sSIMD2 & dSIMD2 & 0x00010101)) | A_Dst;
807 pixel = start + (surf->pitch >> 2);
815 sSIMD2 = sSIMD2 >> 8 | sSIMD2 << 8;
821 A_Dst = dSIMD2 & A_Mask;
822 dSIMD1 = dSIMD2 & 0x00FF00FF;
823 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
824 dSIMD1 &= 0x00FF00FF;
826 dSIMD2 += (((sSIMD2 << 8) & 0xFF00) - dSIMD2) * A >> 8;
828 *pixel++ = dSIMD1 | dSIMD2 | A_Dst;
831 A_Dst = dSIMD1 & A_Mask;
832 dSIMD1 &= 0x00FF00FF;
833 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
834 dSIMD1 &= 0x00FF00FF;
836 dSIMD2 = ((*pixel & 0xFF00) >> 8)| ((pixel[1] & 0xFF00) << 8);
837 dSIMD2 += (sSIMD2 - dSIMD2) * A >> 8;
838 dSIMD2 &= 0x00FF00FF;
840 *pixel++ = dSIMD1 | ((dSIMD2 << 8) & 0xFF00) | A_Dst;
843 A_Dst = dSIMD1 & A_Mask;
844 dSIMD1 &= 0x00FF00FF;
845 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
846 dSIMD1 &= 0x00FF00FF;
848 *pixel++ = dSIMD1 | ((dSIMD2 >> 8) & 0xFF00) | A_Dst;
851 pixel = start + (surf->pitch >> 2);
868 if (prect && (prect->x < - prect->w || prect->x >= surf->w
869 || prect->y < - prect->h || prect->y >= surf->h)) {
874 return SDL_FillRect(surf, prect,
890 int ww = prect->w, hh = prect->h;
912 if (ww <= 0 || hh <= 0) {
927 return ((x >= rect.x) && (x < rect.x + rect.w)
928 && (y >= rect.y) && (y < rect.y + rect.h));
940 Uint16 minX, maxX, minY, maxY;
951 if (SDL_GetColorKey(surf, &colorkey) < 0) {
953 mask = surf->format->Amask;
961 switch (surf->format->BytesPerPixel) {
964 Uint8 *pixel = (Uint8 *)surf->pixels;
965 Uint8 *start = pixel;
972 if (*pixel != colorkey) {
988 pixel = start + surf->pitch;
996 pixel = (Uint8 *)((Uint8 *)surf->pixels + (y * surf->pitch) + x);
1000 if (*pixel != colorkey) {
1016 pixel = start - surf->pitch;
1023 Uint16 *pixel = (Uint16 *)surf->pixels;
1024 Uint16 *start = pixel;
1032 if (*pixel != colorkey) {
1048 pixel = start + surf->pitch / 2;
1056 pixel = ((Uint16 *)surf->pixels + (y * surf->pitch / 2) + x);
1060 if (*pixel != colorkey) {
1076 pixel = start - surf->pitch / 2;
1083 Uint8 *pixel = (Uint8 *)surf->pixels;
1084 Uint8 *start = pixel;
1094 color = (pixel[0] << 16 | pixel[1] << 8 | pixel[2]);
1096 color = (pixel[0] | pixel[1] << 8 | pixel[2] << 16);
1098 if (
color != colorkey) {
1114 pixel = start + surf->pitch / 3;
1122 pixel = (Uint8 *)((Uint8 *)surf->pixels + (y * surf->pitch) + x * 3);
1127 color = (pixel[0] << 16 | pixel[1] << 8 | pixel[2]);
1129 color = (pixel[0] | pixel[1] << 8 | pixel[2] << 16);
1131 if (
color != colorkey) {
1147 pixel = start - surf->pitch / 3;
1154 Uint32 *pixel = (Uint32 *)surf->pixels;
1155 Uint32 *start = pixel;
1163 if (((*pixel) & mask) != colorkey) {
1179 pixel = start + surf->pitch / 4;
1187 pixel = ((Uint32 *)surf->pixels + (y * surf->pitch / 4) + x);
1191 if (((*pixel) & mask) != colorkey) {
1207 pixel = start - surf->pitch / 4;
1217 src.w = maxX - minX + 1;
1218 src.h = maxY - minY + 1;
1237 Uint16 new_height,
int smooth)
1243 return zoomSurface((SDL_Surface*)psrc,
1244 (
double)new_width / psrc->w,
1245 (
double)new_height / psrc->h,
1257 Uint16 new_width, Uint16 new_height,
1258 int smooth,
bool scale_up,
1259 bool absolute_dimensions)
1261 SDL_Surface *tmp_surface, *result;
1267 if (!(!scale_up && ((new_width >= psrc->w) && (new_height >= psrc->h)))) {
1268 if ((new_width - psrc->w) <= (new_height - psrc->h)) {
1270 tmp_surface = zoomSurface((SDL_Surface*)psrc,
1271 (
double)new_width / psrc->w,
1272 (
double)new_width / psrc->w,
1276 tmp_surface = zoomSurface((SDL_Surface*)psrc,
1277 (
double)new_height / psrc->h,
1278 (
double)new_height / psrc->h,
1282 tmp_surface = zoomSurface((SDL_Surface*)psrc,
1288 if (absolute_dimensions) {
1290 (new_width - tmp_surface->w) / 2,
1291 (new_height - tmp_surface->h) / 2,
1295 result =
create_surf(new_width, new_height, SDL_SWSURFACE);
1296 alphablit(tmp_surface, NULL, result, &area, 255);
1299 result = tmp_surface;
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);
1316 SDL_BlitSurface(src, NULL, dst, NULL);
1354 SDL_Rect tmp,dst = { left, top, 0, 0 };
1378void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1,
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;
1388 SDL_Rect dst = { xl, yt, w, h };
1390 struct color gsdl2_color;
1391 int l =
MAX((xr - xl) + 1, (yb - yt) + 1);
1400 gsdl2_color.
color = pcol;
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;
1416 for (i = 0; i < l; i++) {
1417 int cx = (xr - xl) * i / l;
1418 int cy = yb - yt - (yb - yt) * i / l;
struct canvas int int struct sprite int int int int height
struct canvas int int struct sprite bool int int fog_y struct canvas struct sprite struct color * pcolor
struct canvas int int struct sprite int int int width
bool is_view_supported(enum ts_type type)
void tileset_type_set(enum ts_type type)
void free_sprite(struct sprite *s)
SDL_Surface * resize_surface(const SDL_Surface *psrc, Uint16 new_width, Uint16 new_height, int smooth)
bool set_video_mode(unsigned width, unsigned height, unsigned flags_in)
SDL_Rect get_smaller_surface_rect(SDL_Surface *surf)
Uint32 getpixel(SDL_Surface *surf, Sint16 x, Sint16 y)
bool is_in_rect_area(int x, int y, SDL_Rect rect)
int main_window_width(void)
void layer_rect_to_screen_rect(struct gui_layer *gui_layer, SDL_Rect *dest_rect)
SDL_Surface * load_surf(const char *fname)
SDL_Surface * create_surf_with_format(SDL_PixelFormat *pf, int width, int height, Uint32 flags)
void update_main_screen(void)
void remove_gui_layer(struct gui_layer *gui_layer)
SDL_Surface * create_filled_surface(Uint16 w, Uint16 h, Uint32 flags, SDL_Color *pcolor)
int fill_rect_alpha(SDL_Surface *surf, SDL_Rect *prect, SDL_Color *pcolor)
void gui_layer_destroy(struct gui_layer **gui_layer)
void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1, SDL_Color *pcolor)
SDL_Surface * copy_surface(SDL_Surface *src)
SDL_Surface * mask_surface(SDL_Surface *src, SDL_Surface *mask, int mask_offset_x, int mask_offset_y)
struct gui_layer * gui_layer_new(int x, int y, SDL_Surface *surface)
SDL_Surface * crop_visible_part_from_surface(SDL_Surface *psrc)
SDL_Surface * crop_rect_from_surface(SDL_Surface *psource, SDL_Rect *prect)
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
struct gui_layer * get_gui_layer(SDL_Surface *surface)
int screen_blit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Rect *dstrect, unsigned char alpha_mod)
struct sdl2_data main_data
static SDL_Surface * main_surface
int blit_entire_src(SDL_Surface *psrc, SDL_Surface *pdest, Sint16 dest_x, Sint16 dest_y)
Uint32 get_first_pixel(SDL_Surface *surf)
SDL_Surface * convert_surf(SDL_Surface *surf_in)
SDL_Surface * create_surf(int width, int height, Uint32 flags)
struct gui_layer * add_gui_layer(int width, int height)
int clear_surface(SDL_Surface *surf, SDL_Rect *dstrect)
bool correct_rect_region(SDL_Rect *prect)
void screen_rect_to_layer_rect(struct gui_layer *gui_layer, SDL_Rect *dest_rect)
static int __FillRectAlpha8888_32bit(SDL_Surface *surf, SDL_Rect *prect, SDL_Color *pcolor)
void create_frame(SDL_Surface *dest, Sint16 left, Sint16 top, Sint16 width, Sint16 height, SDL_Color *pcolor)
static void free_surfaces(void)
bool create_surfaces(int width, int height)
int main_window_height(void)
SDL_Surface * resize_surface_box(const SDL_Surface *psrc, Uint16 new_width, Uint16 new_height, int smooth, bool scale_up, bool absolute_dimensions)
#define unlock_surf(surf)
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment, double_pixel_copy_increment, width)
#define DUFFS_LOOP8(pixel_copy_increment, width)
#define DUFFS_LOOP4(pixel_copy_increment, width)
#define fc_assert(condition)
#define log_fatal(message,...)
#define log_error(message,...)
#define fc_calloc(n, esz)
#define fc_realloc(ptr, sz)
struct client_options gui_options
struct SDL_Surface * psurface
static bool is_bigendian(void)