29#ifdef SDL2_PLAIN_INCLUDE
33#include <SDL2/SDL_image.h>
34#include <SDL2/SDL_ttf.h>
70 result->
dest_rect = (SDL_Rect){x, y, 0, 0};
198 SDL_Surface *dst, SDL_Rect *dstrect,
199 unsigned char alpha_mod)
203 if (src == NULL || dst == NULL) {
207 SDL_SetSurfaceAlphaMod(src, alpha_mod);
209 ret = SDL_BlitSurface(src, srcrect, dst, dstrect);
212 log_error(
"SDL_BlitSurface() fails: %s", SDL_GetError());
221int screen_blit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Rect *dstrect,
222 unsigned char alpha_mod)
238 prect ? prect->w : psource->w,
239 prect ? prect->h : psource->h,
242 if (
alphablit(psource, prect, new_surf, NULL, 255) != 0) {
261 int mask_offset_x,
int mask_offset_y)
263 SDL_Surface *dest = NULL;
265 Uint32 *src_pixel = NULL;
266 Uint32 *dest_pixel = NULL;
267 Uint32 *mask_pixel = NULL;
268 unsigned char src_alpha, mask_alpha;
276 src_pixel = (Uint32 *)src->pixels;
277 dest_pixel = (Uint32 *)dest->pixels;
279 for (row = 0; row < src->h; row++) {
280 mask_pixel = (Uint32 *)mask->pixels
281 + mask->w * (row + mask_offset_y)
284 for (col = 0; col < src->w; col++) {
285 src_alpha = (*src_pixel & src->format->Amask) >> src->format->Ashift;
286 mask_alpha = (*mask_pixel & mask->format->Amask) >> mask->format->Ashift;
288 *dest_pixel = (*src_pixel & ~src->format->Amask)
289 | (((src_alpha * mask_alpha) / 255) << dest->format->Ashift);
291 src_pixel++; dest_pixel++; mask_pixel++;
313 if ((buf = IMG_Load(fname)) == NULL) {
314 log_error(
_(
"load_surf: Failed to load graphic file %s!"), fname);
330 SDL_Surface *surf = SDL_CreateRGBSurface(flags,
width,
height,
334 pf->Bmask, pf->Amask);
337 log_error(
_(
"Unable to create Sprite (Surface) of size "
338 "%d x %d %d Bits in format %d"),
359 return SDL_ConvertSurface(surf_in,
main_surface->format, 0);
369 SDL_Surface *new_surf;
370 SDL_Color
color = {255, 255, 255, 128};
383 SDL_FillRect(new_surf, NULL,
388 SDL_SetSurfaceAlphaMod(new_surf,
pcolor->a);
402 SDL_Rect _dstrect = *dstrect;
404 return SDL_FillRect(surf, &_dstrect, SDL_MapRGBA(surf->format, 0, 0, 0, 0));
406 return SDL_FillRect(surf, NULL, SDL_MapRGBA(surf->format, 0, 0, 0, 0));
416 Sint16 dest_x, Sint16 dest_y)
418 SDL_Rect
dest_rect = { dest_x, dest_y, 0, 0 };
434 switch (surf->format->BytesPerPixel) {
436 return *(Uint8 *) ((Uint8 *) surf->pixels + y * surf->pitch + x);
439 return *((Uint16 *)surf->pixels + y * surf->pitch /
sizeof(Uint16) + x);
445 (Uint8 *) surf->pixels + y * surf->pitch + x * 3;
448 return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
450 return ptr[0] | ptr[1] << 8 | ptr[2] << 16;
454 return *((Uint32 *)surf->pixels + y * surf->pitch /
sizeof(Uint32) + x);
472 switch (surf->format->BytesPerPixel) {
474 return *((Uint8 *)surf->pixels);
477 return *((Uint16 *)surf->pixels);
482 return (((Uint8 *)surf->pixels)[0] << 16)
483 | (((Uint8 *)surf->pixels)[1] << 8)
484 | ((Uint8 *)surf->pixels)[2];
486 return ((Uint8 *)surf->pixels)[0]
487 | (((Uint8 *)surf->pixels)[1] << 8)
488 | (((Uint8 *)surf->pixels)[2] << 16);
492 return *((Uint32 *)surf->pixels);
517 if (SDL_WasInit(SDL_INIT_AUDIO)) {
518 error = (SDL_InitSubSystem(flags) < 0);
520 error = (SDL_Init(flags) < 0);
523 log_fatal(
_(
"Unable to initialize SDL2 library: %s"), SDL_GetError());
530 if (TTF_Init() < 0) {
531 log_fatal(
_(
"Unable to initialize SDL2_ttf library: %s"), SDL_GetError());
562 0x0000FF00, 0x00FF0000,
563 0xFF000000, 0x000000FF);
565 0x0000FF00, 0x00FF0000,
566 0xFF000000, 0x000000FF);
569 0x00FF0000, 0x0000FF00,
570 0x000000FF, 0xFF000000);
572 0x00FF0000, 0x0000FF00,
573 0x000000FF, 0xFF000000);
577 log_fatal(
_(
"Failed to create RGB surface: %s"), SDL_GetError());
583 flags = SDL_RENDERER_SOFTWARE;
591 log_fatal(
_(
"Failed to create renderer: %s"), SDL_GetError());
596 SDL_PIXELFORMAT_ARGB8888,
597 SDL_TEXTUREACCESS_STREAMING,
601 log_fatal(
_(
"Failed to create texture: %s"), SDL_GetError());
637 SDL_WINDOWPOS_UNDEFINED,
638 SDL_WINDOWPOS_UNDEFINED,
643 log_fatal(
_(
"Failed to create main window: %s"), SDL_GetError());
647 if (flags_in & SDL_WINDOW_FULLSCREEN) {
648 SDL_DisplayMode mode;
651 SDL_SetWindowFullscreen(
main_data.
screen, SDL_WINDOW_FULLSCREEN_DESKTOP);
699#define MASK565 0xf7de
700#define MASK555 0xfbde
703#define BLEND16_50( d, s , mask ) \
704 (((( s & mask ) + ( d & mask )) >> 1) + ( s & d & ( ~mask & 0xffff)))
706#define BLEND2x16_50( d, s , mask ) \
707 (((( s & (mask | mask << 16)) + ( d & ( mask | mask << 16 ))) >> 1) + \
708 ( s & d & ( ~(mask | mask << 16))))
716 register Uint32 A =
pcolor->a;
717 register Uint32 dSIMD1, dSIMD2;
718 register Uint32 sSIMD1, sSIMD2 = SDL_MapRGB(surf->format,
721 Uint32 y, end, A_Dst, A_Mask = surf->format->Amask;
722 Uint32 *start, *pixel;
724 sSIMD1 = sSIMD2 & 0x00FF00FF;
729 end = surf->w * surf->h;
730 pixel = (Uint32 *) surf->pixels;
735 A_Dst = dSIMD2 & A_Mask;
736 *pixel++ = ((((sSIMD2 & 0x00fefefe) + (dSIMD2 & 0x00fefefe)) >> 1)
737 + (sSIMD2 & dSIMD2 & 0x00010101)) | A_Dst;
741 sSIMD2 = sSIMD2 >> 8 | sSIMD2 << 8;
745 A_Dst = dSIMD2 & A_Mask;
746 dSIMD1 = dSIMD2 & 0x00FF00FF;
747 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
748 dSIMD1 &= 0x00FF00FF;
750 dSIMD2 += (((sSIMD2 << 8) & 0xFF00) - dSIMD2) * A >> 8;
752 *pixel++ = dSIMD1 | dSIMD2 | A_Dst;
755 A_Dst = dSIMD1 & A_Mask;
756 dSIMD1 &= 0x00FF00FF;
757 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
758 dSIMD1 &= 0x00FF00FF;
760 dSIMD2 = ((*pixel & 0xFF00) >> 8)| ((pixel[1] & 0xFF00) << 8);
761 dSIMD2 += (sSIMD2 - dSIMD2) * A >> 8;
762 dSIMD2 &= 0x00FF00FF;
764 *pixel++ = dSIMD1 | ((dSIMD2 << 8) & 0xFF00) | A_Dst;
767 A_Dst = dSIMD1 & A_Mask;
768 dSIMD1 &= 0x00FF00FF;
769 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
770 dSIMD1 &= 0x00FF00FF;
772 *pixel++ = dSIMD1 | ((dSIMD2 >> 8) & 0xFF00) | A_Dst;
778 prect->w += prect->x;
780 }
else if (prect->x >= surf->w - prect->w) {
781 prect->w = surf->w - prect->x;
785 prect->h += prect->y;
787 }
else if (prect->y >= surf->h - prect->h) {
788 prect->h = surf->h - prect->y;
791 start = pixel = (Uint32 *) surf->pixels +
792 (prect->y * (surf->pitch >> 2)) + prect->x;
801 A_Dst = dSIMD2 & A_Mask;
802 *pixel++ = ((((sSIMD2 & 0x00fefefe) + (dSIMD2 & 0x00fefefe)) >> 1)
803 + (sSIMD2 & dSIMD2 & 0x00010101)) | A_Dst;
805 pixel = start + (surf->pitch >> 2);
813 sSIMD2 = sSIMD2 >> 8 | sSIMD2 << 8;
819 A_Dst = dSIMD2 & A_Mask;
820 dSIMD1 = dSIMD2 & 0x00FF00FF;
821 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
822 dSIMD1 &= 0x00FF00FF;
824 dSIMD2 += (((sSIMD2 << 8) & 0xFF00) - dSIMD2) * A >> 8;
826 *pixel++ = dSIMD1 | dSIMD2 | A_Dst;
829 A_Dst = dSIMD1 & A_Mask;
830 dSIMD1 &= 0x00FF00FF;
831 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
832 dSIMD1 &= 0x00FF00FF;
834 dSIMD2 = ((*pixel & 0xFF00) >> 8)| ((pixel[1] & 0xFF00) << 8);
835 dSIMD2 += (sSIMD2 - dSIMD2) * A >> 8;
836 dSIMD2 &= 0x00FF00FF;
838 *pixel++ = dSIMD1 | ((dSIMD2 << 8) & 0xFF00) | A_Dst;
841 A_Dst = dSIMD1 & A_Mask;
842 dSIMD1 &= 0x00FF00FF;
843 dSIMD1 += (sSIMD1 - dSIMD1) * A >> 8;
844 dSIMD1 &= 0x00FF00FF;
846 *pixel++ = dSIMD1 | ((dSIMD2 >> 8) & 0xFF00) | A_Dst;
849 pixel = start + (surf->pitch >> 2);
866 if (prect && (prect->x < - prect->w || prect->x >= surf->w
867 || prect->y < - prect->h || prect->y >= surf->h)) {
872 return SDL_FillRect(surf, prect,
888 int ww = prect->w, hh = prect->h;
910 if (ww <= 0 || hh <= 0) {
925 return ((x >= rect.x) && (x < rect.x + rect.w)
926 && (y >= rect.y) && (y < rect.y + rect.h));
938 Uint16 minX, maxX, minY, maxY;
949 if (SDL_GetColorKey(surf, &colorkey) < 0) {
951 mask = surf->format->Amask;
959 switch (surf->format->BytesPerPixel) {
962 Uint8 *pixel = (Uint8 *)surf->pixels;
963 Uint8 *start = pixel;
970 if (*pixel != colorkey) {
986 pixel = start + surf->pitch;
994 pixel = (Uint8 *)((Uint8 *)surf->pixels + (y * surf->pitch) + x);
998 if (*pixel != colorkey) {
1014 pixel = start - surf->pitch;
1021 Uint16 *pixel = (Uint16 *)surf->pixels;
1022 Uint16 *start = pixel;
1030 if (*pixel != colorkey) {
1046 pixel = start + surf->pitch / 2;
1054 pixel = ((Uint16 *)surf->pixels + (y * surf->pitch / 2) + x);
1058 if (*pixel != colorkey) {
1074 pixel = start - surf->pitch / 2;
1081 Uint8 *pixel = (Uint8 *)surf->pixels;
1082 Uint8 *start = pixel;
1092 color = (pixel[0] << 16 | pixel[1] << 8 | pixel[2]);
1094 color = (pixel[0] | pixel[1] << 8 | pixel[2] << 16);
1096 if (
color != colorkey) {
1112 pixel = start + surf->pitch / 3;
1120 pixel = (Uint8 *)((Uint8 *)surf->pixels + (y * surf->pitch) + x * 3);
1125 color = (pixel[0] << 16 | pixel[1] << 8 | pixel[2]);
1127 color = (pixel[0] | pixel[1] << 8 | pixel[2] << 16);
1129 if (
color != colorkey) {
1145 pixel = start - surf->pitch / 3;
1152 Uint32 *pixel = (Uint32 *)surf->pixels;
1153 Uint32 *start = pixel;
1161 if (((*pixel) & mask) != colorkey) {
1177 pixel = start + surf->pitch / 4;
1185 pixel = ((Uint32 *)surf->pixels + (y * surf->pitch / 4) + x);
1189 if (((*pixel) & mask) != colorkey) {
1205 pixel = start - surf->pitch / 4;
1215 src.w = maxX - minX + 1;
1216 src.h = maxY - minY + 1;
1235 Uint16 new_height,
int smooth)
1241 return zoomSurface((SDL_Surface*)psrc,
1242 (
double)new_width / psrc->w,
1243 (
double)new_height / psrc->h,
1255 Uint16 new_width, Uint16 new_height,
1256 int smooth,
bool scale_up,
1257 bool absolute_dimensions)
1259 SDL_Surface *tmp_surface, *result;
1265 if (!(!scale_up && ((new_width >= psrc->w) && (new_height >= psrc->h)))) {
1266 if ((new_width - psrc->w) <= (new_height - psrc->h)) {
1268 tmp_surface = zoomSurface((SDL_Surface*)psrc,
1269 (
double)new_width / psrc->w,
1270 (
double)new_width / psrc->w,
1274 tmp_surface = zoomSurface((SDL_Surface*)psrc,
1275 (
double)new_height / psrc->h,
1276 (
double)new_height / psrc->h,
1280 tmp_surface = zoomSurface((SDL_Surface*)psrc,
1286 if (absolute_dimensions) {
1288 (new_width - tmp_surface->w) / 2,
1289 (new_height - tmp_surface->h) / 2,
1293 result =
create_surf(new_width, new_height, SDL_SWSURFACE);
1294 alphablit(tmp_surface, NULL, result, &area, 255);
1297 result = tmp_surface;
1310 dst = SDL_CreateRGBSurface(0, src->w, src->h, src->format->BitsPerPixel,
1311 src->format->Rmask, src->format->Gmask,
1312 src->format->Bmask, src->format->Amask);
1314 SDL_BlitSurface(src, NULL, dst, NULL);
1352 SDL_Rect tmp,dst = { left, top, 0, 0 };
1376void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1,
1379 int xl = x0 < x1 ? x0 : x1;
1380 int xr = x0 < x1 ? x1 : x0;
1381 int yt = y0 < y1 ? y0 : y1;
1382 int yb = y0 < y1 ? y1 : y0;
1383 int w = (xr - xl) + 1;
1384 int h = (yb - yt) + 1;
1386 SDL_Rect dst = { xl, yt, w, h };
1388 struct color gsdl2_color;
1389 int l =
MAX((xr - xl) + 1, (yb - yt) + 1);
1398 gsdl2_color.
color = pcol;
1404 if ((x0 <= x1 && y0 <= y1)
1405 || (x1 <= x0 && y1 <= y0)) {
1406 for (i = 0; i < l; i++) {
1407 int cx = (xr - xl) * i / l;
1408 int cy = (yb - yt) * i / l;
1414 for (i = 0; i < l; i++) {
1415 int cx = (xr - xl) * i / l;
1416 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)
Uint32 getpixel(SDL_Surface *surf, int x, int y)
bool set_video_mode(unsigned width, unsigned height, unsigned flags_in)
SDL_Rect get_smaller_surface_rect(SDL_Surface *surf)
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)