+ // copy palette
+ for (i = 0; i < zoom_src->format->palette->ncolors; i++)
+ zoom_dst->format->palette->colors[i] =
+ zoom_src->format->palette->colors[i];
+ zoom_dst->format->palette->ncolors = zoom_src->format->palette->ncolors;
+
+ // call the 8 bit transformation routine to do the zooming
+ zoomSurfaceY(zoom_src, zoom_dst);
+ }
+
+ // unlock source surface
+ SDL_UnlockSurface(zoom_src);
+
+ // free temporary surface
+ if (is_converted)
+ SDL_FreeSurface(zoom_src);
+
+ // return destination surface
+ return zoom_dst;
+}
+
+static SDL_Surface *SDLGetOpaqueSurface(SDL_Surface *surface)
+{
+ SDL_Surface *new_surface;
+
+ if (surface == NULL)
+ return NULL;
+
+ if ((new_surface = SDLGetNativeSurface(surface)) == NULL)
+ Error(ERR_EXIT, "SDLGetNativeSurface() failed");
+
+ // remove alpha channel from native non-transparent surface, if defined
+ SDLSetAlpha(new_surface, FALSE, 0);
+
+ // remove transparent color from native non-transparent surface, if defined
+ SDL_SetColorKey(new_surface, UNSET_TRANSPARENT_PIXEL, 0);
+
+ return new_surface;
+}
+
+Bitmap *SDLZoomBitmap(Bitmap *src_bitmap, int dst_width, int dst_height)
+{
+ Bitmap *dst_bitmap = CreateBitmapStruct();
+ SDL_Surface *src_surface = src_bitmap->surface_masked;
+ SDL_Surface *dst_surface;
+
+ dst_width = MAX(1, dst_width); // prevent zero bitmap width
+ dst_height = MAX(1, dst_height); // prevent zero bitmap height
+
+ dst_bitmap->width = dst_width;
+ dst_bitmap->height = dst_height;
+
+ // create zoomed temporary surface from source surface
+ dst_surface = zoomSurface(src_surface, dst_width, dst_height);
+
+ // create native format destination surface from zoomed temporary surface
+ SDLSetNativeSurface(&dst_surface);
+
+ // set color key for zoomed surface from source surface, if defined
+ if (SDLHasColorKey(src_surface))
+ SDL_SetColorKey(dst_surface, SET_TRANSPARENT_PIXEL,
+ SDLGetColorKey(src_surface));
+
+ // create native non-transparent surface for opaque blitting
+ dst_bitmap->surface = SDLGetOpaqueSurface(dst_surface);
+
+ // set native transparent surface for masked blitting
+ dst_bitmap->surface_masked = dst_surface;
+
+ return dst_bitmap;
+}
+
+
+// ============================================================================
+// load image to bitmap
+// ============================================================================
+
+Bitmap *SDLLoadImage(char *filename)
+{
+ Bitmap *new_bitmap = CreateBitmapStruct();
+ SDL_Surface *sdl_image_tmp;
+
+ if (program.headless)
+ {
+ // prevent sanity check warnings at later stage
+ new_bitmap->width = new_bitmap->height = 1;
+
+ return new_bitmap;
+ }
+
+ print_timestamp_init("SDLLoadImage");
+
+ print_timestamp_time(getBaseNamePtr(filename));
+
+ // load image to temporary surface
+ if ((sdl_image_tmp = IMG_Load(filename)) == NULL)
+ Error(ERR_EXIT, "IMG_Load() failed: %s", SDL_GetError());
+
+ print_timestamp_time("IMG_Load");
+
+ UPDATE_BUSY_STATE();
+
+ // create native non-transparent surface for current image
+ if ((new_bitmap->surface = SDLGetOpaqueSurface(sdl_image_tmp)) == NULL)
+ Error(ERR_EXIT, "SDLGetOpaqueSurface() failed");
+
+ print_timestamp_time("SDLGetNativeSurface (opaque)");
+
+ UPDATE_BUSY_STATE();
+
+ // set black pixel to transparent if no alpha channel / transparent color
+ if (!SDLHasAlpha(sdl_image_tmp) &&
+ !SDLHasColorKey(sdl_image_tmp))
+ SDL_SetColorKey(sdl_image_tmp, SET_TRANSPARENT_PIXEL,
+ SDL_MapRGB(sdl_image_tmp->format, 0x00, 0x00, 0x00));
+
+ // create native transparent surface for current image
+ if ((new_bitmap->surface_masked = SDLGetNativeSurface(sdl_image_tmp)) == NULL)
+ Error(ERR_EXIT, "SDLGetNativeSurface() failed");
+
+ print_timestamp_time("SDLGetNativeSurface (masked)");
+
+ UPDATE_BUSY_STATE();
+
+ // free temporary surface
+ SDL_FreeSurface(sdl_image_tmp);
+
+ new_bitmap->width = new_bitmap->surface->w;
+ new_bitmap->height = new_bitmap->surface->h;
+
+ print_timestamp_done("SDLLoadImage");
+
+ return new_bitmap;
+}
+
+
+// ----------------------------------------------------------------------------
+// custom cursor fuctions
+// ----------------------------------------------------------------------------
+
+static SDL_Cursor *create_cursor(struct MouseCursorInfo *cursor_info)
+{
+ return SDL_CreateCursor(cursor_info->data, cursor_info->mask,
+ cursor_info->width, cursor_info->height,
+ cursor_info->hot_x, cursor_info->hot_y);
+}
+
+void SDLSetMouseCursor(struct MouseCursorInfo *cursor_info)
+{
+ static struct MouseCursorInfo *last_cursor_info = NULL;
+ static struct MouseCursorInfo *last_cursor_info2 = NULL;
+ static SDL_Cursor *cursor_default = NULL;
+ static SDL_Cursor *cursor_current = NULL;
+
+ // if invoked for the first time, store the SDL default cursor
+ if (cursor_default == NULL)
+ cursor_default = SDL_GetCursor();
+
+ // only create new cursor if cursor info (custom only) has changed
+ if (cursor_info != NULL && cursor_info != last_cursor_info)
+ {
+ cursor_current = create_cursor(cursor_info);
+ last_cursor_info = cursor_info;
+ }
+
+ // only set new cursor if cursor info (custom or NULL) has changed
+ if (cursor_info != last_cursor_info2)
+ SDL_SetCursor(cursor_info ? cursor_current : cursor_default);
+
+ last_cursor_info2 = cursor_info;
+}
+
+
+// ============================================================================
+// audio functions
+// ============================================================================
+
+void SDLOpenAudio(void)
+{
+ if (program.headless)
+ return;
+
+#if !defined(TARGET_SDL2)
+ if (!strEqual(setup.system.sdl_audiodriver, ARG_DEFAULT))
+ SDL_putenv(getStringCat2("SDL_AUDIODRIVER=", setup.system.sdl_audiodriver));
+#endif
+
+ if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
+ {
+ Error(ERR_WARN, "SDL_InitSubSystem() failed: %s", SDL_GetError());
+ return;
+ }
+
+ if (Mix_OpenAudio(DEFAULT_AUDIO_SAMPLE_RATE, MIX_DEFAULT_FORMAT,
+ AUDIO_NUM_CHANNELS_STEREO,
+ setup.system.audio_fragment_size) < 0)
+ {
+ Error(ERR_WARN, "Mix_OpenAudio() failed: %s", SDL_GetError());
+ return;
+ }
+
+ audio.sound_available = TRUE;
+ audio.music_available = TRUE;
+ audio.loops_available = TRUE;
+ audio.sound_enabled = TRUE;
+
+ // set number of available mixer channels
+ audio.num_channels = Mix_AllocateChannels(NUM_MIXER_CHANNELS);
+ audio.music_channel = MUSIC_CHANNEL;
+ audio.first_sound_channel = FIRST_SOUND_CHANNEL;
+
+ Mixer_InitChannels();
+}
+
+void SDLCloseAudio(void)
+{
+ Mix_HaltMusic();
+ Mix_HaltChannel(-1);
+
+ Mix_CloseAudio();
+ SDL_QuitSubSystem(SDL_INIT_AUDIO);
+}
+
+
+// ============================================================================
+// event functions
+// ============================================================================
+
+void SDLWaitEvent(Event *event)
+{
+ SDL_WaitEvent(event);
+}
+
+void SDLHandleWindowManagerEvent(Event *event)
+{
+#ifdef DEBUG
+#if defined(PLATFORM_WIN32)
+ // experimental drag and drop code
+
+ SDL_SysWMEvent *syswmevent = (SDL_SysWMEvent *)event;
+ SDL_SysWMmsg *syswmmsg = (SDL_SysWMmsg *)(syswmevent->msg);
+
+#if defined(TARGET_SDL2)
+ if (syswmmsg->msg.win.msg == WM_DROPFILES)
+#else
+ if (syswmmsg->msg == WM_DROPFILES)
+#endif
+ {
+#if defined(TARGET_SDL2)
+ HDROP hdrop = (HDROP)syswmmsg->msg.win.wParam;
+#else
+ HDROP hdrop = (HDROP)syswmmsg->wParam;
+#endif
+ int i, num_files;
+
+ printf("::: SDL_SYSWMEVENT:\n");
+
+ num_files = DragQueryFile(hdrop, 0xffffffff, NULL, 0);
+
+ for (i = 0; i < num_files; i++)
+ {
+ int buffer_len = DragQueryFile(hdrop, i, NULL, 0);
+ char buffer[buffer_len + 1];
+
+ DragQueryFile(hdrop, i, buffer, buffer_len + 1);
+
+ printf("::: - '%s'\n", buffer);
+ }
+
+#if defined(TARGET_SDL2)
+ DragFinish((HDROP)syswmmsg->msg.win.wParam);
+#else
+ DragFinish((HDROP)syswmmsg->wParam);
+#endif
+ }
+#endif
+#endif
+}
+
+
+// ============================================================================
+// joystick functions
+// ============================================================================
+
+#if defined(TARGET_SDL2)
+static void *sdl_joystick[MAX_PLAYERS]; // game controller or joystick
+#else
+static SDL_Joystick *sdl_joystick[MAX_PLAYERS]; // only joysticks supported
+#endif
+static int sdl_js_axis_raw[MAX_PLAYERS][2];
+static int sdl_js_axis[MAX_PLAYERS][2];
+static int sdl_js_button[MAX_PLAYERS][2];
+static boolean sdl_is_controller[MAX_PLAYERS];
+
+void SDLClearJoystickState(void)
+{
+ int i, j;
+
+ for (i = 0; i < MAX_PLAYERS; i++)
+ {
+ for (j = 0; j < 2; j++)
+ {
+ sdl_js_axis_raw[i][j] = -1;
+ sdl_js_axis[i][j] = 0;
+ sdl_js_button[i][j] = 0;
+ }
+ }
+}
+
+boolean SDLOpenJoystick(int nr)
+{
+ if (nr < 0 || nr >= MAX_PLAYERS)
+ return FALSE;
+
+#if defined(TARGET_SDL2)
+ sdl_is_controller[nr] = SDL_IsGameController(nr);
+#else
+ sdl_is_controller[nr] = FALSE;
+#endif
+
+#if DEBUG_JOYSTICKS
+ Error(ERR_DEBUG, "opening joystick %d (%s)",
+ nr, (sdl_is_controller[nr] ? "game controller" : "joystick"));
+#endif
+
+#if defined(TARGET_SDL2)
+ if (sdl_is_controller[nr])
+ sdl_joystick[nr] = SDL_GameControllerOpen(nr);
+ else
+ sdl_joystick[nr] = SDL_JoystickOpen(nr);
+#else
+ sdl_joystick[nr] = SDL_JoystickOpen(nr);
+#endif
+
+ return (sdl_joystick[nr] != NULL);
+}
+
+void SDLCloseJoystick(int nr)
+{
+ if (nr < 0 || nr >= MAX_PLAYERS)
+ return;
+
+#if DEBUG_JOYSTICKS
+ Error(ERR_DEBUG, "closing joystick %d", nr);
+#endif
+
+#if defined(TARGET_SDL2)
+ if (sdl_is_controller[nr])
+ SDL_GameControllerClose(sdl_joystick[nr]);
+ else
+ SDL_JoystickClose(sdl_joystick[nr]);
+#else
+ SDL_JoystickClose(sdl_joystick[nr]);
+#endif
+
+ sdl_joystick[nr] = NULL;
+}
+
+boolean SDLCheckJoystickOpened(int nr)
+{
+ if (nr < 0 || nr >= MAX_PLAYERS)
+ return FALSE;
+
+#if defined(TARGET_SDL2)
+ return (sdl_joystick[nr] != NULL ? TRUE : FALSE);
+#else
+ return (SDL_JoystickOpened(nr) ? TRUE : FALSE);
+#endif
+}
+
+static void setJoystickAxis(int nr, int axis_id_raw, int axis_value)
+{
+#if defined(TARGET_SDL2)
+ int axis_id = (axis_id_raw == SDL_CONTROLLER_AXIS_LEFTX ||
+ axis_id_raw == SDL_CONTROLLER_AXIS_RIGHTX ? 0 :
+ axis_id_raw == SDL_CONTROLLER_AXIS_LEFTY ||
+ axis_id_raw == SDL_CONTROLLER_AXIS_RIGHTY ? 1 : -1);
+#else
+ int axis_id = axis_id_raw % 2;
+#endif
+
+ if (nr < 0 || nr >= MAX_PLAYERS)
+ return;
+
+ if (axis_id == -1)
+ return;
+
+ // prevent (slightly jittering, but centered) axis A from resetting axis B
+ if (ABS(axis_value) < JOYSTICK_PERCENT * JOYSTICK_MAX_AXIS_POS / 100 &&
+ axis_id_raw != sdl_js_axis_raw[nr][axis_id])
+ return;
+
+ sdl_js_axis[nr][axis_id] = axis_value;
+ sdl_js_axis_raw[nr][axis_id] = axis_id_raw;
+}
+
+static void setJoystickButton(int nr, int button_id_raw, int button_state)
+{
+#if defined(TARGET_SDL2)
+ int button_id = (button_id_raw == SDL_CONTROLLER_BUTTON_A ||
+ button_id_raw == SDL_CONTROLLER_BUTTON_X ||
+ button_id_raw == SDL_CONTROLLER_BUTTON_LEFTSHOULDER ||
+ button_id_raw == SDL_CONTROLLER_BUTTON_LEFTSTICK ||
+ button_id_raw == SDL_CONTROLLER_BUTTON_RIGHTSTICK ? 0 :
+ button_id_raw == SDL_CONTROLLER_BUTTON_B ||
+ button_id_raw == SDL_CONTROLLER_BUTTON_Y ||
+ button_id_raw == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ? 1 :
+ -1);
+
+ if (button_id_raw == SDL_CONTROLLER_BUTTON_DPAD_LEFT)
+ sdl_js_axis[nr][0] = button_state * JOYSTICK_XLEFT;
+ else if (button_id_raw == SDL_CONTROLLER_BUTTON_DPAD_RIGHT)
+ sdl_js_axis[nr][0] = button_state * JOYSTICK_XRIGHT;
+ else if (button_id_raw == SDL_CONTROLLER_BUTTON_DPAD_UP)
+ sdl_js_axis[nr][1] = button_state * JOYSTICK_YUPPER;
+ else if (button_id_raw == SDL_CONTROLLER_BUTTON_DPAD_DOWN)
+ sdl_js_axis[nr][1] = button_state * JOYSTICK_YLOWER;
+
+ if (button_id_raw == SDL_CONTROLLER_BUTTON_DPAD_LEFT ||
+ button_id_raw == SDL_CONTROLLER_BUTTON_DPAD_RIGHT ||
+ button_id_raw == SDL_CONTROLLER_BUTTON_DPAD_UP ||
+ button_id_raw == SDL_CONTROLLER_BUTTON_DPAD_DOWN)
+ sdl_js_axis_raw[nr][0] = sdl_js_axis_raw[nr][1] = -1;
+#else
+ int button_id = button_id_raw % 2;
+#endif
+
+ if (nr < 0 || nr >= MAX_PLAYERS)
+ return;
+
+ if (button_id == -1)
+ return;
+
+ sdl_js_button[nr][button_id] = button_state;
+}
+
+void HandleJoystickEvent(Event *event)
+{
+ switch(event->type)
+ {
+#if defined(TARGET_SDL2)
+ case SDL_CONTROLLERDEVICEADDED:
+#if DEBUG_JOYSTICKS
+ Error(ERR_DEBUG, "SDL_CONTROLLERDEVICEADDED: device %d added",
+ event->cdevice.which);
+#endif
+ InitJoysticks();
+ break;