+void SDLSetWindowTitle()
+{
+#if defined(TARGET_SDL2)
+ SDL_SetWindowTitle(sdl_window, program.window_title);
+#else
+ SDL_WM_SetCaption(program.window_title, program.window_title);
+#endif
+}
+
+#if defined(TARGET_SDL2)
+void SDLSetWindowScaling(int window_scaling_percent)
+{
+ if (sdl_window == NULL)
+ return;
+
+ float window_scaling_factor = (float)window_scaling_percent / 100;
+ int new_window_width = (int)(window_scaling_factor * video.width);
+ int new_window_height = (int)(window_scaling_factor * video.height);
+
+ SDL_SetWindowSize(sdl_window, new_window_width, new_window_height);
+
+ video.window_scaling_percent = window_scaling_percent;
+ video.window_width = new_window_width;
+ video.window_height = new_window_height;
+
+ SetWindowTitle();
+}
+
+void SDLSetWindowScalingQuality(char *window_scaling_quality)
+{
+ if (sdl_texture == NULL)
+ return;
+
+ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, window_scaling_quality);
+
+ SDL_Texture *new_texture = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_STREAMING,
+ video.width, video.height);
+
+ if (new_texture != NULL)
+ {
+ SDL_DestroyTexture(sdl_texture);
+
+ sdl_texture = new_texture;
+
+ SDLRedrawWindow();
+ }
+
+ video.window_scaling_quality = window_scaling_quality;
+}
+
+void SDLSetWindowFullscreen(boolean fullscreen)
+{
+ if (sdl_window == NULL)
+ return;
+
+#if USE_DESKTOP_FULLSCREEN
+ int flags = (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
+#else
+ int flags = (fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
+#endif
+
+ if (SDL_SetWindowFullscreen(sdl_window, flags) == 0)
+ video.fullscreen_enabled = fullscreen_enabled = fullscreen;
+
+ // if screen size was changed in fullscreen mode, correct desktop window size
+ if (!fullscreen && video.fullscreen_initial)
+ {
+ SDLSetWindowScaling(setup.window_scaling_percent);
+ SDL_SetWindowPosition(sdl_window,
+ SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
+
+ video.fullscreen_initial = FALSE;
+ }
+}
+
+void SDLRedrawWindow()
+{
+ UpdateScreen(NULL);
+}
+#endif
+
+void SDLCreateBitmapContent(Bitmap *bitmap, int width, int height,
+ int depth)
+{
+ SDL_Surface *surface =
+ SDL_CreateRGBSurface(SURFACE_FLAGS, width, height, depth, 0,0,0, 0);
+
+ if (surface == NULL)
+ Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
+
+ SDLSetNativeSurface(&surface);
+
+ bitmap->surface = surface;
+}
+
+void SDLFreeBitmapPointers(Bitmap *bitmap)
+{
+ if (bitmap->surface)
+ SDL_FreeSurface(bitmap->surface);
+ if (bitmap->surface_masked)
+ SDL_FreeSurface(bitmap->surface_masked);
+
+ bitmap->surface = NULL;
+ bitmap->surface_masked = NULL;
+
+#if defined(TARGET_SDL2)
+ if (bitmap->texture)
+ SDL_DestroyTexture(bitmap->texture);
+ if (bitmap->texture_masked)
+ SDL_DestroyTexture(bitmap->texture_masked);
+
+ bitmap->texture = NULL;
+ bitmap->texture_masked = NULL;
+#endif
+}
+
+void SDLCopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
+ int src_x, int src_y, int width, int height,
+ int dst_x, int dst_y, int mask_mode)
+{
+ Bitmap *real_dst_bitmap = (dst_bitmap == window ? backbuffer : dst_bitmap);
+ SDL_Rect src_rect, dst_rect;
+
+ if (src_bitmap == backbuffer)
+ {
+ src_x += video_xoffset;
+ src_y += video_yoffset;
+ }
+
+ src_rect.x = src_x;
+ src_rect.y = src_y;
+ src_rect.w = width;
+ src_rect.h = height;
+
+ if (dst_bitmap == backbuffer || dst_bitmap == window)
+ {
+ dst_x += video_xoffset;
+ dst_y += video_yoffset;
+ }
+
+ dst_rect.x = dst_x;
+ dst_rect.y = dst_y;
+ dst_rect.w = width;
+ dst_rect.h = height;
+
+ // if (src_bitmap != backbuffer || dst_bitmap != window)
+ if (!(src_bitmap == backbuffer && dst_bitmap == window))
+ SDL_BlitSurface((mask_mode == BLIT_MASKED ?
+ src_bitmap->surface_masked : src_bitmap->surface),
+ &src_rect, real_dst_bitmap->surface, &dst_rect);
+
+#if defined(TARGET_SDL2)
+ if (dst_bitmap == window)
+ {
+ // SDL_UpdateWindowSurface(sdl_window);
+ // SDL_UpdateWindowSurfaceRects(sdl_window, &dst_rect, 1);
+ UpdateScreen(&dst_rect);
+ }
+#else
+ if (dst_bitmap == window)
+ {
+ // SDL_UpdateRect(backbuffer->surface, dst_x, dst_y, width, height);
+ UpdateScreen(&dst_rect);
+ }
+#endif
+}
+
+void SDLBlitTexture(Bitmap *bitmap,
+ int src_x, int src_y, int width, int height,
+ int dst_x, int dst_y, int mask_mode)
+{
+#if defined(TARGET_SDL2)
+#if USE_RENDERER
+ SDL_Texture *texture;
+ SDL_Rect src_rect;
+ SDL_Rect dst_rect;
+
+ texture =
+ (mask_mode == BLIT_MASKED ? bitmap->texture_masked : bitmap->texture);
+
+ if (texture == NULL)
+ return;
+
+ src_rect.x = src_x;
+ src_rect.y = src_y;
+ src_rect.w = width;
+ src_rect.h = height;
+
+ dst_rect.x = dst_x;
+ dst_rect.y = dst_y;
+ dst_rect.w = width;
+ dst_rect.h = height;
+
+ SDL_RenderCopy(sdl_renderer, texture, &src_rect, &dst_rect);
+#endif
+#endif
+}
+
+void SDLFillRectangle(Bitmap *dst_bitmap, int x, int y, int width, int height,
+ Uint32 color)
+{
+ Bitmap *real_dst_bitmap = (dst_bitmap == window ? backbuffer : dst_bitmap);
+ SDL_Rect rect;
+
+ if (dst_bitmap == backbuffer || dst_bitmap == window)
+ {
+ x += video_xoffset;
+ y += video_yoffset;
+ }
+
+ rect.x = x;
+ rect.y = y;
+ rect.w = width;
+ rect.h = height;
+
+ SDL_FillRect(real_dst_bitmap->surface, &rect, color);
+
+#if defined(TARGET_SDL2)
+ if (dst_bitmap == window)
+ {
+ // SDL_UpdateWindowSurface(sdl_window);
+ // SDL_UpdateWindowSurfaceRects(sdl_window, &rect, 1);
+ UpdateScreen(&rect);
+ }
+#else
+ if (dst_bitmap == window)
+ {
+ // SDL_UpdateRect(backbuffer->surface, x, y, width, height);
+ UpdateScreen(&rect);
+ }
+#endif
+}
+
+void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
+ int fade_mode, int fade_delay, int post_delay,
+ void (*draw_border_function)(void))
+{
+ SDL_Surface *surface_source = gfx.fade_bitmap_source->surface;
+ SDL_Surface *surface_target = gfx.fade_bitmap_target->surface;
+ SDL_Surface *surface_black = gfx.fade_bitmap_black->surface;
+ SDL_Surface *surface_screen = backbuffer->surface;
+ SDL_Surface *surface_cross = (bitmap_cross ? bitmap_cross->surface : NULL);
+ SDL_Rect src_rect, dst_rect;
+ SDL_Rect dst_rect2;
+ int src_x = x, src_y = y;
+ int dst_x = x, dst_y = y;
+ unsigned int time_last, time_current;
+
+ // store function for drawing global masked border
+ void (*draw_global_border_function)(int) = gfx.draw_global_border_function;
+
+ // deactivate drawing of global border while fading, if needed
+ if (draw_border_function == NULL)
+ gfx.draw_global_border_function = NULL;
+
+ src_rect.x = src_x;
+ src_rect.y = src_y;
+ src_rect.w = width;
+ src_rect.h = height;
+
+ dst_x += video_xoffset;
+ dst_y += video_yoffset;
+
+ dst_rect.x = dst_x;
+ dst_rect.y = dst_y;
+ dst_rect.w = width; /* (ignored) */
+ dst_rect.h = height; /* (ignored) */
+
+ dst_rect2 = dst_rect;
+
+ /* copy source and target surfaces to temporary surfaces for fading */
+ if (fade_mode & FADE_TYPE_TRANSFORM)
+ {
+ SDL_BlitSurface(surface_cross, &src_rect, surface_source, &src_rect);
+ SDL_BlitSurface(surface_screen, &dst_rect, surface_target, &src_rect);
+ }
+ else if (fade_mode & FADE_TYPE_FADE_IN)
+ {
+ SDL_BlitSurface(surface_black, &src_rect, surface_source, &src_rect);
+ SDL_BlitSurface(surface_screen, &dst_rect, surface_target, &src_rect);
+ }
+ else /* FADE_TYPE_FADE_OUT */
+ {
+ SDL_BlitSurface(surface_screen, &dst_rect, surface_source, &src_rect);
+ SDL_BlitSurface(surface_black, &src_rect, surface_target, &src_rect);
+ }
+
+ time_current = SDL_GetTicks();
+
+ if (fade_mode == FADE_MODE_MELT)
+ {
+ boolean done = FALSE;
+ int melt_pixels = 2;
+ int melt_columns = width / melt_pixels;
+ int ypos[melt_columns];
+ int max_steps = height / 8 + 32;
+ int steps_done = 0;
+ float steps = 0;
+ int i;
+
+ SDL_BlitSurface(surface_source, &src_rect, surface_screen, &dst_rect);
+#if defined(TARGET_SDL2)
+ SDL_SetSurfaceBlendMode(surface_target, SDL_BLENDMODE_NONE);
+#else
+ SDL_SetAlpha(surface_target, 0, 0); /* disable alpha blending */
+#endif
+
+ ypos[0] = -GetSimpleRandom(16);
+
+ for (i = 1 ; i < melt_columns; i++)
+ {
+ int r = GetSimpleRandom(3) - 1; /* randomly choose from { -1, 0, -1 } */
+
+ ypos[i] = ypos[i - 1] + r;
+
+ if (ypos[i] > 0)
+ ypos[i] = 0;
+ else
+ if (ypos[i] == -16)
+ ypos[i] = -15;
+ }
+
+ while (!done)
+ {
+ int steps_final;
+
+ time_last = time_current;
+ time_current = SDL_GetTicks();
+ steps += max_steps * ((float)(time_current - time_last) / fade_delay);
+ steps_final = MIN(MAX(0, steps), max_steps);
+
+ steps_done++;
+
+ done = (steps_done >= steps_final);
+
+ for (i = 0 ; i < melt_columns; i++)
+ {
+ if (ypos[i] < 0)
+ {
+ ypos[i]++;
+
+ done = FALSE;
+ }
+ else if (ypos[i] < height)
+ {
+ int y1 = 16;
+ int y2 = 8;
+ int y3 = 8;
+ int dy = (ypos[i] < y1) ? ypos[i] + 1 : y2 + GetSimpleRandom(y3);
+
+ if (ypos[i] + dy >= height)
+ dy = height - ypos[i];
+
+ /* copy part of (appearing) target surface to upper area */
+ src_rect.x = src_x + i * melt_pixels;
+ // src_rect.y = src_y + ypos[i];
+ src_rect.y = src_y;
+ src_rect.w = melt_pixels;
+ // src_rect.h = dy;
+ src_rect.h = ypos[i] + dy;
+
+ dst_rect.x = dst_x + i * melt_pixels;
+ // dst_rect.y = dst_y + ypos[i];
+ dst_rect.y = dst_y;
+
+ if (steps_done >= steps_final)
+ SDL_BlitSurface(surface_target, &src_rect,
+ surface_screen, &dst_rect);
+
+ ypos[i] += dy;
+
+ /* copy part of (disappearing) source surface to lower area */
+ src_rect.x = src_x + i * melt_pixels;
+ src_rect.y = src_y;
+ src_rect.w = melt_pixels;
+ src_rect.h = height - ypos[i];
+
+ dst_rect.x = dst_x + i * melt_pixels;
+ dst_rect.y = dst_y + ypos[i];
+
+ if (steps_done >= steps_final)
+ SDL_BlitSurface(surface_source, &src_rect,
+ surface_screen, &dst_rect);
+
+ done = FALSE;
+ }
+ else
+ {
+ src_rect.x = src_x + i * melt_pixels;
+ src_rect.y = src_y;
+ src_rect.w = melt_pixels;
+ src_rect.h = height;
+
+ dst_rect.x = dst_x + i * melt_pixels;
+ dst_rect.y = dst_y;
+
+ if (steps_done >= steps_final)
+ SDL_BlitSurface(surface_target, &src_rect,
+ surface_screen, &dst_rect);
+ }
+ }
+
+ if (steps_done >= steps_final)
+ {
+ if (draw_border_function != NULL)
+ draw_border_function();
+
+ UpdateScreen(&dst_rect2);
+ }
+ }
+ }
+ else if (fade_mode == FADE_MODE_CURTAIN)
+ {
+ float xx;
+ int xx_final;
+ int xx_size = width / 2;
+
+ SDL_BlitSurface(surface_target, &src_rect, surface_screen, &dst_rect);
+#if defined(TARGET_SDL2)
+ SDL_SetSurfaceBlendMode(surface_source, SDL_BLENDMODE_NONE);
+#else
+ SDL_SetAlpha(surface_source, 0, 0); /* disable alpha blending */
+#endif
+
+ for (xx = 0; xx < xx_size;)
+ {
+ time_last = time_current;
+ time_current = SDL_GetTicks();
+ xx += xx_size * ((float)(time_current - time_last) / fade_delay);
+ xx_final = MIN(MAX(0, xx), xx_size);
+
+ src_rect.x = src_x;
+ src_rect.y = src_y;
+ src_rect.w = width;
+ src_rect.h = height;
+
+ dst_rect.x = dst_x;
+ dst_rect.y = dst_y;
+
+ /* draw new (target) image to screen buffer */
+ SDL_BlitSurface(surface_target, &src_rect, surface_screen, &dst_rect);
+
+ if (xx_final < xx_size)
+ {
+ src_rect.w = xx_size - xx_final;
+ src_rect.h = height;
+
+ /* draw old (source) image to screen buffer (left side) */
+
+ src_rect.x = src_x + xx_final;
+ dst_rect.x = dst_x;
+
+ SDL_BlitSurface(surface_source, &src_rect, surface_screen, &dst_rect);
+
+ /* draw old (source) image to screen buffer (right side) */
+
+ src_rect.x = src_x + xx_size;
+ dst_rect.x = dst_x + xx_size + xx_final;
+
+ SDL_BlitSurface(surface_source, &src_rect, surface_screen, &dst_rect);
+ }
+
+ if (draw_border_function != NULL)
+ draw_border_function();