From e4868b4b41aea2681264a3926a93e9a1c881acab Mon Sep 17 00:00:00 2001 From: AliceLR Date: Sun, 21 Jan 2024 02:54:58 -0700 Subject: [PATCH 01/44] FIXME Initial SDL3 support. --- Makefile | 18 +++ config.sh | 10 +- src/SDLmzx.h | 224 ++++++++++++++++++++++++++++++-- src/audio/audio_struct.h | 8 +- src/audio/audio_wav.c | 18 +-- src/event_sdl.c | 273 ++++++++++++++++++++++++--------------- src/graphics.c | 9 +- src/platform_sdl.c | 11 +- src/render_gl.h | 18 +++ src/render_glsl.c | 4 + src/render_sdl.c | 224 +++++++++++++++++++++++--------- src/render_sdl.h | 5 +- src/render_sdlaccel.c | 31 ++++- src/render_softscale.c | 16 ++- src/thread_sdl.h | 40 +++--- 15 files changed, 685 insertions(+), 224 deletions(-) diff --git a/Makefile b/Makefile index a1cad143a..0a751e82a 100644 --- a/Makefile +++ b/Makefile @@ -83,6 +83,24 @@ include arch/compat.inc ifneq (${BUILD_SDL},) +# +# SDL 3 +# +ifeq (${BUILD_SDL},3) +# Check SDL_PREFIX and PREFIX for lib/pkgconfig/sdl3.pc. +ifneq ($(and ${SDL_PREFIX},$(wildcard ${SDL_PREFIX}/lib/pkgconfig/sdl3.pc)),) +SDL_PKGCONFIG ?= --with-path=${SDL_PREFIX}/lib/pkgconfig +else +ifneq ($(wildcard ${PREFIX}/lib/pkgconfig/sdl3.pc),) +SDL_PKGCONFIG ?= --with-path=${PREFIX}/lib/pkgconfig +endif +endif + +SDL_PREFIX := $(shell pkg-config ${SDL_PKGCONFIG} sdl3 --variable=prefix) +SDL_CFLAGS ?= $(shell pkg-config ${SDL_PKGCONFIG} sdl3 --cflags) +SDL_LDFLAGS ?= $(shell pkg-config ${SDL_PKGCONFIG} sdl3 --libs) +endif # SDL3 + # # SDL 2 # diff --git a/config.sh b/config.sh index e6df9f808..c3f73aff9 100755 --- a/config.sh +++ b/config.sh @@ -67,6 +67,7 @@ usage() { echo echo "Platform-dependent options:" echo " --enable-sdl Enable SDL backend, typically SDL2 (default)." + echo " --enable-sdl3 Enable SDL3 backend (any version)." echo " --enable-sdl2 Enable SDL2 backend (any version)." echo " --enable-sdl1 Enable SDL1 backend (1.2.x)." echo " --disable-sdl Disable SDL dependencies and features." @@ -441,6 +442,7 @@ while [ "$1" != "" ]; do [ "$1" = "--enable-sdl" ] && SDL="true" [ "$1" = "--enable-sdl1" ] && SDL="1" [ "$1" = "--enable-sdl2" ] && SDL="2" + [ "$1" = "--enable-sdl3" ] && SDL="3" [ "$1" = "--enable-egl" ] && EGL="true" [ "$1" = "--disable-egl" ] && EGL="false" @@ -808,7 +810,7 @@ else # # If no specific version of SDL is selected, select SDL2. # - if [ "$SDL" != "1" ] && [ "$SDL" != "2" ]; then + if [ "$SDL" != "1" ] && [ "$SDL" != "2" ] && [ "$SDL" != "3" ]; then SDL="2" fi echo "Enabling SDL $SDL.x." @@ -1112,7 +1114,7 @@ fi # Force-disable the softscale renderer for SDL 1.2 (requires SDL_Renderer). # if [ "$SDL" = "1" ] && [ "$SOFTSCALE" = "true" ]; then - echo "Force-disabling softscale renderer (requires SDL 2)." + echo "Force-disabling softscale renderer (requires SDL 2+)." SOFTSCALE="false" SDLACCEL="false" fi @@ -1519,7 +1521,7 @@ else fi # -# Softscale renderer (SDL 2) +# Softscale renderer (SDL 2+) # if [ "$SOFTSCALE" = "true" ]; then echo "Softscale renderer enabled." @@ -1899,7 +1901,7 @@ else fi # -# SDL_GameControllerDB, if enabled. This depends on SDL 2. +# SDL_GameControllerDB, if enabled. This depends on SDL 2+. # if [ "$SDL" != "false" ] && [ "$SDL" -ge "2" ] && \ [ "$GAMECONTROLLERDB" = "true" ]; then diff --git a/src/SDLmzx.h b/src/SDLmzx.h index c802ed9a1..1104b1c09 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -1,6 +1,7 @@ /* MegaZeux * * Copyright (C) 2007 Alistair John Strachan + * Copyright (C) 2024 Alice Rowan * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -17,11 +18,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -// Split from compat.h because it's only needed in one place and it was making -// the build process much slower. - -#ifndef __COMPAT_SDL_H -#define __COMPAT_SDL_H +#ifndef __SDLMZX_H +#define __SDLMZX_H #include "compat.h" @@ -29,10 +27,19 @@ __M_BEGIN_DECLS #if defined(CONFIG_SDL) +/* TODO: SDL3 hack so the dso code doesn't need to be overhauled (again). */ +#define SDL_FUNCTION_POINTER_IS_VOID_POINTER + +#if CONFIG_SDL == 3 +#include +#else #include #if defined(_WIN32) || defined(CONFIG_X11) #include #endif +#endif + +/* SDL1 backwards compatibility for SDL2 ************************************/ #if !SDL_VERSION_ATLEAST(2,0,0) @@ -41,6 +48,14 @@ __M_BEGIN_DECLS // Data types +struct SDL_DisplayMode +{ + Uint32 format; + int w; + int h; + int refresh_rate; +}; + typedef SDLKey SDL_Keycode; typedef int (*SDL_ThreadFunction)(void *); typedef Uint32 SDL_threadID; @@ -137,7 +152,7 @@ static inline HWND SDL_GetWindowProperty_HWND(SDL_Window *window) } #endif /* _WIN32 */ -#else /* SDL_VERSION_ATLEAST(2,0,0) */ +#elif !SDL_VERSION_ATLEAST(3,0,0) #ifdef _WIN32 static inline HWND SDL_GetWindowProperty_HWND(SDL_Window *window) @@ -149,10 +164,203 @@ static inline HWND SDL_GetWindowProperty_HWND(SDL_Window *window) } #endif /* _WIN32 */ -#endif /* SDL_VERSION_ATLEAST(2,0,0) */ +#else /* SDL_VERSION_ATLEAST(3,0,0) */ + +#ifdef _WIN32 +static inline void *SDL_GetWindowProperty_HWND(SDL_Window *window) +{ + return SDL_GetProperty(SDL_GetWindowProperties(window), + SDL_PROPERTY_WINDOW_WIN32_HWND_POINTER, NULL); +} +#endif /* _WIN32 */ + +#endif /* SDL_VERSION_ATLEAST(3,0,0) */ + + +/* SDL 1/2 backwards compatibility for SDL3 *********************************/ + +/** + * SDL_audio.h + */ +#if !SDL_VERSION_ATLEAST(3,0,0) +#define SDL_AUDIO_U8 AUDIO_U8 +#define SDL_AUDIO_S8 AUDIO_S8 +#define SDL_AUDIO_S16 AUDIO_S16SYS +#define SDL_AUDIO_S16LE AUDIO_S16LSB +#define SDL_AUDIO_S16BE AUDIO_S16MSB +#else +/* SDL3 defines SDL_FreeWAV to an error type, but MZX needs to keep it to + * transparently support older versions. */ +#undef SDL_FreeWAV +#define SDL_FreeWAV(w) SDL_free(w) +#endif + +/** + * SDL_event.h + */ +#if !SDL_VERSION_ATLEAST(3,0,0) +#define SDL_EVENT_GAMEPAD_AXIS_MOTION SDL_CONTROLLERAXISMOTION +#define SDL_EVENT_JOYSTICK_ADDED SDL_JOYDEVICEADDED +#define SDL_EVENT_JOYSTICK_REMOVED SDL_JOYDEVICEREMOVED +#define SDL_EVENT_JOYSTICK_AXIS_MOTION SDL_JOYAXISMOTION +#define SDL_EVENT_JOYSTICK_BUTTON_DOWN SDL_JOYBUTTONDOWN +#define SDL_EVENT_JOYSTICK_BUTTON_UP SDL_JOYBUTTONUP +#define SDL_EVENT_JOYSTICK_HAT_MOTION SDL_JOYHATMOTION +#define SDL_EVENT_KEY_DOWN SDL_KEYDOWN +#define SDL_EVENT_KEY_UP SDL_KEYUP +#define SDL_EVENT_MOUSE_BUTTON_DOWN SDL_MOUSEBUTTONDOWN +#define SDL_EVENT_MOUSE_BUTTON_UP SDL_MOUSEBUTTONUP +#define SDL_EVENT_MOUSE_MOTION SDL_MOUSEMOTION +#define SDL_EVENT_MOUSE_WHEEL SDL_MOUSEWHEEL +#define SDL_EVENT_TEXT_INPUT SDL_TEXTINPUT +#define SDL_EVENT_QUIT SDL_QUIT +#define SDL_EVENT_FIRST SDL_FIRSTEVENT +#define SDL_EVENT_LAST SDL_LASTEVENT +#endif + +/** + * SDL_gamepad.h (formerly SDL_gamecontroller.h) + */ +#if !SDL_VERSION_ATLEAST(3,0,0) && SDL_VERSION_ATLEAST(2,0,0) +typedef SDL_GameController SDL_Gamepad; +typedef SDL_GameControllerAxis SDL_GamepadAxis; +typedef SDL_GameControllerButton SDL_GamepadButton; +#define SDL_INIT_GAMEPAD SDL_INIT_GAMECONTROLLER +#define SDL_GAMEPAD_AXIS_INVALID SDL_CONTROLLER_AXIS_INVALID +#define SDL_GAMEPAD_AXIS_LEFTX SDL_CONTROLLER_AXIS_LEFTX +#define SDL_GAMEPAD_AXIS_LEFTY SDL_CONTROLLER_AXIS_LEFTY +#define SDL_GAMEPAD_AXIS_RIGHTX SDL_CONTROLLER_AXIS_RIGHTX +#define SDL_GAMEPAD_AXIS_RIGHTY SDL_CONTROLLER_AXIS_RIGHTY +#define SDL_GAMEPAD_AXIS_LEFT_TRIGGER SDL_CONTROLLER_AXIS_TRIGGERLEFT +#define SDL_GAMEPAD_AXIS_RIGHT_TRIGGER SDL_CONTROLLER_AXIS_TRIGGERRIGHT +#define SDL_GAMEPAD_AXIS_MAX SDL_CONTROLLER_AXIS_MAX +#define SDL_GAMEPAD_BUTTON_INVALID SDL_CONTROLLER_BUTTON_INVALID +#define SDL_GAMEPAD_BUTTON_SOUTH SDL_CONTROLLER_BUTTON_A +#define SDL_GAMEPAD_BUTTON_EAST SDL_CONTROLLER_BUTTON_B +#define SDL_GAMEPAD_BUTTON_WEST SDL_CONTROLLER_BUTTON_X +#define SDL_GAMEPAD_BUTTON_NORTH SDL_CONTROLLER_BUTTON_Y +#define SDL_GAMEPAD_BUTTON_BACK SDL_CONTROLLER_BUTTON_BACK +#define SDL_GAMEPAD_BUTTON_GUIDE SDL_CONTROLLER_BUTTON_GUIDE +#define SDL_GAMEPAD_BUTTON_START SDL_CONTROLLER_BUTTON_START +#define SDL_GAMEPAD_BUTTON_LEFT_STICK SDL_CONTROLLER_BUTTON_LEFT_STICK +#define SDL_GAMEPAD_BUTTON_RIGHT_STICK SDL_CONTROLLER_BUTTON_RIGHT_STICK +#define SDL_GAMEPAD_BUTTON_LEFT_SHOULDER SDL_CONTROLLER_BUTTON_LEFTSHOULDER +#define SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER SDL_CONTROLLER_BUTTON_RIGHTSHOULDER +#define SDL_GAMEPAD_BUTTON_DPAD_UP SDL_CONTROLLER_BUTTON_DPAD_UP +#define SDL_GAMEPAD_BUTTON_DPAD_DOWN SDL_CONTROLLER_BUTTON_DPAD_DOWN +#define SDL_GAMEPAD_BUTTON_DPAD_LEFT SDL_CONTROLLER_BUTTON_DPAD_LEFT +#define SDL_GAMEPAD_BUTTON_DPAD_RIGHT SDL_CONTROLLER_BUTTON_DPAD_RIGHT +#define SDL_GAMEPAD_BUTTON_MISC1 SDL_CONTROLLER_BUTTON_MISC1 +#define SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1 SDL_CONTROLLER_BUTTON_PADDLE1 +#define SDL_GAMEPAD_BUTTON_LEFT_PADDLE1 SDL_CONTROLLER_BUTTON_PADDLE2 +#define SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2 SDL_CONTROLLER_BUTTON_PADDLE3 +#define SDL_GAMEPAD_BUTTON_LEFT_PADDLE2 SDL_CONTROLLER_BUTTON_PADDLE4 +#define SDL_GAMEPAD_BUTTON_TOUCHPAD SDL_CONTROLLER_BUTTON_TOUCHPAD +#define SDL_GAMEPAD_BUTTON_MAX SDL_CONTROLLER_BUTTON_MAX +#define SDL_IsGamepad(device_id) SDL_IsGameController(device_id) +#define SDL_OpenGamepad(device_id) SDL_GameControllerOpen(device_id) +#define SDL_CloseGamepad(g) SDL_GameControllerClose(g) +#define SDL_GetGamepadAxisFromString(s) SDL_GameControllerGetAxisFromString(s) +#define SDL_GetGamepadButtonFromString(s) SDL_GameControllerGetButtonFromString(s) +#define SDL_AddGamepadMapping(m) SDL_GameControllerAddMapping(m) +#define SDL_AddGamepadMappingsFromFile(f) SDL_GameControllerAddMappingsFromFile(f) + +static inline void SDL_SetGamepadEventsEnabled(SDL_bool enabled) +{ + SDL_GameControllerEventState(enabled ? SDL_ENABLE : SDL_DISABLE); +} +#endif + +/** + * SDL_joystick.h + */ +#if !SDL_VERSION_ATLEAST(3,0,0) +#define SDL_OpenJoystick(device_id) SDL_JoystickOpen(device_id) +#define SDL_CloseJoystick(j) SDL_JoystickClose(j) +#define SDL_GetJoystickGUID(j) SDL_JoystickGetGUID(j) +#define SDL_GetJoystickGUIDString(g,b,l) SDL_JoystickGetGUIDString(g,b,l) +#define SDL_GetJoystickInstanceID(j) SDL_JoystickInstanceID(j) + +static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) +{ + SDL_JoystickEventState(enabled ? SDL_ENABLE : SDL_DISABLE); +} +#endif + +/** + * SDL_keyboard.h and SDL_keycode.h + */ +#if !SDL_VERSION_ATLEAST(3,0,0) +#define SDL_KMOD_CTRL KMOD_CTRL +#define SDL_KMOD_ALT KMOD_ALT +#define SDL_KMOD_NUM KMOD_NUM +#define SDL_KMOD_CAPS KMOD_CAPS +#define SDL_TextInputActive() SDL_IsTextInputActive() +#endif + +/** + * SDL_pixels.h + */ +#if !SDL_VERSION_ATLEAST(3,0,0) +#define SDL_PIXELFORMAT_XRGB8888 SDL_PIXELFORMAT_RGB888 +#define SDL_PIXELFORMAT_XBGR8888 SDL_PIXELFORMAT_BGR888 +#define SDL_CreatePalette(s) SDL_AllocPalette(s) +#define SDL_DestroyPalette(p) SDL_FreePalette(p) +#define SDL_CreatePixelFormat(f) SDL_AllocFormat(f) +#define SDL_DestroyPixelFormat(pf) SDL_FreeFormat(pf) +#define SDL_GetMasksForPixelFormatEnum(f,b,r,g,b,a) SDL_PixelFormatEnumToMasks(f,b,r,g,b,a) +#endif + +/* SDL_render.h */ +#if !SDL_VERSION_ATLEAST(3,0,0) && SDL_VERSION_ATLEAST(2,0,0) +typedef int SDL_RendererLogicalPresentation; +#define SDL_LOGICAL_PRESENTATION_DISABLED 0 +#define SDL_SCALEMODE_BEST SDL_ScaleModeBest +#define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear +#define SDL_SCALEMODE_NEAREST SDL_ScaleModeNearest +#define SDL_SetRenderClipRect(r, rect) SDL_RenderSetClipRect(r, rect) + +static inline int SDL_SetRenderLogicalPresentation(SDL_Renderer *render, + int w, int h, SDL_RendererLogicalPresentiation p, SDL_ScaleMode s) +{ + return SDL_SetRenderLogicalSize(render, w, h); +} +#endif + +/* SDL_surface.h */ +#if !SDL_VERSION_ATLEAST(3,0,0) && SDL_VERSION_ATLEAST(2,0,0) +#define SDL_DestroySurface(s) SDL_FreeSurface(s) + +static inline SDL_Surface *SDL_CreateSurface(int width, int height, Uint32 format) +{ + Uint32 rmask, gmask, bmask, amask; + int bpp; + + SDL_PixelFormatEnumToMasks(format, &bpp, &rmask, &gmask, &bmask, &amask); + return SDL_CreateRGBSurface(0, width, height, bpp, rmask, gmask, bmask, amask); +} +#endif + +/** + * SDL_thread.h symbols were mostly the same for SDL 1 and SDL 2. + */ +#if !SDL_VERSION_ATLEAST(3,0,0) +typedef SDL_cond SDL_Condition; +typedef SDL_mutex SDL_Mutex; +typedef SDL_sem SDL_Semaphore; +#define SDL_CreateCondition() SDL_CreateCond() +#define SDL_DestroyCondition(c) SDL_DestroyCond(c) +#define SDL_WaitCondition(c,m) SDL_CondWait(c,m) +#define SDL_WaitConditionTimeout(c,m,t) SDL_CondWaitTimeout(c,m,t) +#define SDL_SignalCondition(c) SDL_CondSignal(c) +#define SDL_BroadcastCondition(c) SDL_CondBroadcast(c) +#define SDL_WaitSemaphore(s) SDL_SemWait(s) +#define SDL_PostSemaphore(s) SDL_SemPost(s) +#define SDL_GetCurrentThreadID() SDL_ThreadID() +#endif #endif /* CONFIG_SDL */ __M_END_DECLS -#endif /* __COMPAT_SDL_H */ +#endif /* __SDLMZX_H */ diff --git a/src/audio/audio_struct.h b/src/audio/audio_struct.h index d6153db36..63757fd51 100644 --- a/src/audio/audio_struct.h +++ b/src/audio/audio_struct.h @@ -31,17 +31,17 @@ __M_BEGIN_DECLS #include "../platform.h" #if PLATFORM_BYTE_ORDER == PLATFORM_BIG_ENDIAN -#define SAMPLE_S16SYS SAMPLE_S16MSB +#define SAMPLE_S16SYS SAMPLE_S16BE #else -#define SAMPLE_S16SYS SAMPLE_S16LSB +#define SAMPLE_S16SYS SAMPLE_S16LE #endif enum wav_format { SAMPLE_U8, SAMPLE_S8, - SAMPLE_S16LSB, - SAMPLE_S16MSB + SAMPLE_S16LE, + SAMPLE_S16BE }; struct wav_info diff --git a/src/audio/audio_wav.c b/src/audio/audio_wav.c index f1c75e525..75b813a2a 100644 --- a/src/audio/audio_wav.c +++ b/src/audio/audio_wav.c @@ -106,8 +106,8 @@ static uint32_t wav_read_data(struct wav_stream *w_stream, break; } - case SAMPLE_S16LSB: - case SAMPLE_S16MSB: + case SAMPLE_S16LE: + case SAMPLE_S16BE: { uint8_t *dest = (uint8_t *)buffer; @@ -454,18 +454,18 @@ static boolean load_wav_file_sdl(const char *filename, struct wav_info *spec) spec->freq = sdlspec.freq; switch(sdlspec.format) { - case AUDIO_U8: + case SDL_AUDIO_U8: spec->format = SAMPLE_U8; break; - case AUDIO_S8: + case SDL_AUDIO_S8: spec->format = SAMPLE_S8; break; - case AUDIO_S16LSB: - spec->format = SAMPLE_S16LSB; + case SDL_AUDIO_S16LE: + spec->format = SAMPLE_S16LE; break; // May be returned by SDL on big endian machines. - case AUDIO_S16MSB: - spec->format = SAMPLE_S16MSB; + case SDL_AUDIO_S16BE: + spec->format = SAMPLE_S16BE; break; /** * TODO: SDL 2.0 can technically return AUDIO_S32LSB or AUDIO_F32LSB. @@ -591,7 +591,7 @@ static boolean load_wav_file(vfile *vf, const char *filename, struct wav_info *s if(sbytes == 1) spec->format = SAMPLE_U8; else - spec->format = SAMPLE_S16LSB; + spec->format = SAMPLE_S16LE; spec->channels = channels; // Check for "smpl" chunk for looping info diff --git a/src/event_sdl.c b/src/event_sdl.c index 8ec0485d7..5dbe3f35a 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -27,6 +27,7 @@ #include "util.h" #include +#include #include #include @@ -252,45 +253,45 @@ static int get_pandora_joystick_button(SDL_Keycode key) * No equivalent of this API exists for SDL 1.x. */ -static SDL_GameController *gamepads[MAX_JOYSTICKS]; +static SDL_Gamepad *gamepads[MAX_JOYSTICKS]; -static enum joystick_special_axis sdl_axis_map[SDL_CONTROLLER_AXIS_MAX] = +static enum joystick_special_axis sdl_axis_map[SDL_GAMEPAD_AXIS_MAX] = { - [SDL_CONTROLLER_AXIS_LEFTX] = JOY_AXIS_LEFT_X, - [SDL_CONTROLLER_AXIS_LEFTY] = JOY_AXIS_LEFT_Y, - [SDL_CONTROLLER_AXIS_RIGHTX] = JOY_AXIS_RIGHT_X, - [SDL_CONTROLLER_AXIS_RIGHTY] = JOY_AXIS_RIGHT_Y, - [SDL_CONTROLLER_AXIS_TRIGGERLEFT] = JOY_AXIS_LEFT_TRIGGER, - [SDL_CONTROLLER_AXIS_TRIGGERRIGHT] = JOY_AXIS_RIGHT_TRIGGER + [SDL_GAMEPAD_AXIS_LEFTX] = JOY_AXIS_LEFT_X, + [SDL_GAMEPAD_AXIS_LEFTY] = JOY_AXIS_LEFT_Y, + [SDL_GAMEPAD_AXIS_RIGHTX] = JOY_AXIS_RIGHT_X, + [SDL_GAMEPAD_AXIS_RIGHTY] = JOY_AXIS_RIGHT_Y, + [SDL_GAMEPAD_AXIS_LEFT_TRIGGER] = JOY_AXIS_LEFT_TRIGGER, + [SDL_GAMEPAD_AXIS_RIGHT_TRIGGER] = JOY_AXIS_RIGHT_TRIGGER }; -static int16_t sdl_axis_action_map[SDL_CONTROLLER_AXIS_MAX][2] = +static int16_t sdl_axis_action_map[SDL_GAMEPAD_AXIS_MAX][2] = { - [SDL_CONTROLLER_AXIS_LEFTX] = { -JOY_L_LEFT, -JOY_L_RIGHT }, - [SDL_CONTROLLER_AXIS_LEFTY] = { -JOY_L_UP, -JOY_L_DOWN }, - [SDL_CONTROLLER_AXIS_RIGHTX] = { -JOY_R_LEFT, -JOY_R_RIGHT }, - [SDL_CONTROLLER_AXIS_RIGHTY] = { -JOY_R_UP, -JOY_R_DOWN }, - [SDL_CONTROLLER_AXIS_TRIGGERLEFT] = { 0, -JOY_LTRIGGER }, - [SDL_CONTROLLER_AXIS_TRIGGERRIGHT] = { 0, -JOY_RTRIGGER }, + [SDL_GAMEPAD_AXIS_LEFTX] = { -JOY_L_LEFT, -JOY_L_RIGHT }, + [SDL_GAMEPAD_AXIS_LEFTY] = { -JOY_L_UP, -JOY_L_DOWN }, + [SDL_GAMEPAD_AXIS_RIGHTX] = { -JOY_R_LEFT, -JOY_R_RIGHT }, + [SDL_GAMEPAD_AXIS_RIGHTY] = { -JOY_R_UP, -JOY_R_DOWN }, + [SDL_GAMEPAD_AXIS_LEFT_TRIGGER] = { 0, -JOY_LTRIGGER }, + [SDL_GAMEPAD_AXIS_RIGHT_TRIGGER] = { 0, -JOY_RTRIGGER }, }; -static int16_t sdl_action_map[SDL_CONTROLLER_BUTTON_MAX] = +static int16_t sdl_action_map[SDL_GAMEPAD_BUTTON_MAX] = { - [SDL_CONTROLLER_BUTTON_A] = -JOY_A, - [SDL_CONTROLLER_BUTTON_B] = -JOY_B, - [SDL_CONTROLLER_BUTTON_X] = -JOY_X, - [SDL_CONTROLLER_BUTTON_Y] = -JOY_Y, - [SDL_CONTROLLER_BUTTON_BACK] = -JOY_SELECT, -//[SDL_CONTROLLER_BUTTON_GUIDE] = -JOY_GUIDE, - [SDL_CONTROLLER_BUTTON_START] = -JOY_START, - [SDL_CONTROLLER_BUTTON_LEFTSTICK] = -JOY_LSTICK, - [SDL_CONTROLLER_BUTTON_RIGHTSTICK] = -JOY_RSTICK, - [SDL_CONTROLLER_BUTTON_LEFTSHOULDER] = -JOY_LSHOULDER, - [SDL_CONTROLLER_BUTTON_RIGHTSHOULDER] = -JOY_RSHOULDER, - [SDL_CONTROLLER_BUTTON_DPAD_UP] = -JOY_UP, - [SDL_CONTROLLER_BUTTON_DPAD_DOWN] = -JOY_DOWN, - [SDL_CONTROLLER_BUTTON_DPAD_LEFT] = -JOY_LEFT, - [SDL_CONTROLLER_BUTTON_DPAD_RIGHT] = -JOY_RIGHT + [SDL_GAMEPAD_BUTTON_SOUTH] = -JOY_A, + [SDL_GAMEPAD_BUTTON_EAST] = -JOY_B, + [SDL_GAMEPAD_BUTTON_WEST] = -JOY_X, + [SDL_GAMEPAD_BUTTON_NORTH] = -JOY_Y, + [SDL_GAMEPAD_BUTTON_BACK] = -JOY_SELECT, +//[SDL_GAMEPAD_BUTTON_GUIDE] = -JOY_GUIDE, + [SDL_GAMEPAD_BUTTON_START] = -JOY_START, + [SDL_GAMEPAD_BUTTON_LEFT_STICK] = -JOY_LSTICK, + [SDL_GAMEPAD_BUTTON_RIGHT_STICK] = -JOY_RSTICK, + [SDL_GAMEPAD_BUTTON_LEFT_SHOULDER] = -JOY_LSHOULDER, + [SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER] = -JOY_RSHOULDER, + [SDL_GAMEPAD_BUTTON_DPAD_UP] = -JOY_UP, + [SDL_GAMEPAD_BUTTON_DPAD_DOWN] = -JOY_DOWN, + [SDL_GAMEPAD_BUTTON_DPAD_LEFT] = -JOY_LEFT, + [SDL_GAMEPAD_BUTTON_DPAD_RIGHT] = -JOY_RIGHT }; enum @@ -467,8 +468,8 @@ static void parse_gamepad_read_value(char *key, char *value, static void parse_gamepad_read_entry(char *key, char *value, struct gamepad_axis_map *axes, struct gamepad_map *buttons) { - SDL_GameControllerAxis a; - SDL_GameControllerButton b; + SDL_GamepadAxis a; + SDL_GamepadButton b; struct gamepad_map *single = NULL; struct gamepad_map *neg = NULL; struct gamepad_map *pos = NULL; @@ -478,9 +479,9 @@ static void parse_gamepad_read_entry(char *key, char *value, if(*key == '+' || *key == '-') half_axis = *(key++); - a = SDL_GameControllerGetAxisFromString(key); - b = SDL_GameControllerGetButtonFromString(key); - if(a != SDL_CONTROLLER_AXIS_INVALID) + a = SDL_GetGamepadAxisFromString(key); + b = SDL_GetGamepadButtonFromString(key); + if(a != SDL_GAMEPAD_AXIS_INVALID) { if(half_axis == '+') single = &(axes[a].pos); @@ -496,10 +497,10 @@ static void parse_gamepad_read_entry(char *key, char *value, } else - if(b != SDL_CONTROLLER_BUTTON_INVALID) + if(b != SDL_GAMEPAD_BUTTON_INVALID) { // This button isn't really useful to MZX. - if(b == SDL_CONTROLLER_BUTTON_GUIDE) + if(b == SDL_GAMEPAD_BUTTON_GUIDE) return; single = &(buttons[b]); @@ -610,8 +611,8 @@ static void parse_gamepad_apply(int joy, int16_t mapping, static void parse_gamepad_map(int joystick_index, char *map) { - struct gamepad_axis_map axes[SDL_CONTROLLER_AXIS_MAX]; - struct gamepad_map buttons[SDL_CONTROLLER_BUTTON_MAX]; + struct gamepad_axis_map axes[SDL_GAMEPAD_AXIS_MAX]; + struct gamepad_map buttons[SDL_GAMEPAD_BUTTON_MAX]; boolean select_mapped = false; boolean select_used = false; size_t i; @@ -622,7 +623,7 @@ static void parse_gamepad_map(int joystick_index, char *map) parse_gamepad_read_string(map, axes, buttons); // Apply axes. - for(i = 0; i < SDL_CONTROLLER_AXIS_MAX; i++) + for(i = 0; i < SDL_GAMEPAD_AXIS_MAX; i++) { parse_gamepad_apply(joystick_index, sdl_axis_action_map[i][0], &(axes[i].neg), &select_mapped, &select_used); @@ -632,7 +633,7 @@ static void parse_gamepad_map(int joystick_index, char *map) } // Apply buttons. - for(i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++) + for(i = 0; i < SDL_GAMEPAD_BUTTON_MAX; i++) { parse_gamepad_apply(joystick_index, sdl_action_map[i], &(buttons[i]), &select_mapped, &select_used); @@ -656,15 +657,15 @@ static void parse_gamepad_map(int joystick_index, char *map) static void init_gamepad(SDL_Joystick *joystick, int sdl_joystick_id, int joystick_index) { - SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); + SDL_JoystickGUID guid = SDL_GetJoystickGUID(joystick); char guid_string[33]; - SDL_JoystickGetGUIDString(guid, guid_string, 33); + SDL_GetJoystickGUIDString(guid, guid_string, 33); gamepads[joystick_index] = NULL; - if(SDL_IsGameController(sdl_joystick_id)) + if(SDL_IsGamepad(sdl_joystick_id)) { - SDL_GameController *gamepad = SDL_GameControllerOpen(sdl_joystick_id); + SDL_Gamepad *gamepad = SDL_OpenGamepad(sdl_joystick_id); if(gamepad) { @@ -672,7 +673,11 @@ static void init_gamepad(SDL_Joystick *joystick, int sdl_joystick_id, char *mapping = NULL; gamepads[joystick_index] = gamepad; -#if SDL_VERSION_ATLEAST(2,0,9) +#if SDL_VERSION_ATLEAST(3,0,0) + // This is the equivalent to [...]ForDeviceIndex() and is also currently + // the only way to get the default generated mapping. + mapping = (char *)SDL_GetGamepadInstanceMapping(sdl_joystick_id); +#elif SDL_VERSION_ATLEAST(2,0,9) // NOTE: the other functions for this will not return the default mapping // string; this is the only one that can return everything. Right now, // this only matters for the Emscripten port. @@ -778,7 +783,7 @@ static void load_gamecontrollerdb(void) if(path) { - int result = SDL_GameControllerAddMappingsFromFile(path); + int result = SDL_AddGamepadMappingsFromFile(path); if(result >= 0) debug("--JOYSTICK-- Added %d mappings from '%s'.\n", result, path); } @@ -793,8 +798,8 @@ static void load_gamecontrollerdb(void) */ void gamepad_map_sym(const char *sym, const char *value) { - SDL_GameControllerAxis a; - SDL_GameControllerButton b; + SDL_GamepadAxis a; + SDL_GamepadButton b; int16_t binding = 0; if(joystick_parse_map_value(value, &binding)) @@ -804,16 +809,16 @@ void gamepad_map_sym(const char *sym, const char *value) dir = *(sym++); // Digital axis (default to + if no dir specified). - a = SDL_GameControllerGetAxisFromString(sym); - if(a != SDL_CONTROLLER_AXIS_INVALID) + a = SDL_GetGamepadAxisFromString(sym); + if(a != SDL_GAMEPAD_AXIS_INVALID) { int pos = (dir != '-') ? 1 : 0; sdl_axis_action_map[a][pos] = binding; } // Button - b = SDL_GameControllerGetButtonFromString(sym); - if(b != SDL_CONTROLLER_BUTTON_INVALID) + b = SDL_GetGamepadButtonFromString(sym); + if(b != SDL_GAMEPAD_BUTTON_INVALID) sdl_action_map[b] = binding; } @@ -828,7 +833,7 @@ void gamepad_add_mapping(const char *mapping) // Make sure this is loaded first so it doesn't override the user mapping. load_gamecontrollerdb(); - if(SDL_GameControllerAddMapping(mapping) < 0) + if(SDL_AddGamepadMapping(mapping) < 0) warn("Failed to add gamepad mapping: %s\n", SDL_GetError()); } @@ -870,10 +875,10 @@ static void init_joystick(int sdl_joystick_id) if(joystick_index >= 0) { - SDL_Joystick *joystick = SDL_JoystickOpen(sdl_joystick_id); + SDL_Joystick *joystick = SDL_OpenJoystick(sdl_joystick_id); if(joystick) { - joystick_instance_ids[joystick_index] = SDL_JoystickInstanceID(joystick); + joystick_instance_ids[joystick_index] = SDL_GetJoystickInstanceID(joystick); joysticks[joystick_index] = joystick; joystick_set_active(status, joystick_index, true); @@ -899,12 +904,12 @@ static void close_joystick(int joystick_index) // SDL_GameControllerClose also closes the joystick. if(gamepads[joystick_index]) { - SDL_GameControllerClose(gamepads[joystick_index]); + SDL_CloseGamepad(gamepads[joystick_index]); gamepad_clean_map(joystick_index); gamepads[joystick_index] = NULL; } else - SDL_JoystickClose(joysticks[joystick_index]); + SDL_CloseJoystick(joysticks[joystick_index]); joystick_instance_ids[joystick_index] = -1; joysticks[joystick_index] = NULL; @@ -972,7 +977,7 @@ static boolean process_event(SDL_Event *event) /* Enable converting keycodes to fake unicode presses when text input isn't * active. Enabling text input also enables an onscreen keyboard in some * ports, so it isn't always desired. */ - boolean unicode_fallback = !SDL_IsTextInputActive(); + boolean unicode_fallback = !SDL_TextInputActive(); #else /* SDL 1.2 might also need this (Pandora? doesn't generate unicode presses). */ static boolean unicode_fallback = true; @@ -991,21 +996,46 @@ static boolean process_event(SDL_Event *event) */ if(!numlock_status_initialized) { - status->numlock_status = !!(SDL_GetModState() & KMOD_NUM); + status->numlock_status = !!(SDL_GetModState() & SDL_KMOD_NUM); numlock_status_initialized = true; } switch(event->type) { - case SDL_QUIT: + case SDL_EVENT_QUIT: { - trace("--EVENT_SDL-- SDL_QUIT\n"); + trace("--EVENT_SDL-- SDL_EVENT_QUIT\n"); // Set the exit status status->exit_status = true; break; } -#if SDL_VERSION_ATLEAST(2,0,0) +#if SDL_VERSION_ATLEAST(3,0,0) + case SDL_EVENT_WINDOW_RESIZED: + { + trace("--EVENT_SDL-- SDL_EVENT_WINDOW_RESIZED: (%u,%u)\n"); + // FIXME: doing this might send this event now! + //resize_screen(event->window.data1, event->window.data2); + break; + } + + case SDL_EVENT_WINDOW_FOCUS_LOST: + { + trace("--EVENT_SDL-- SDL_EVENT_WINDOW_FOCUS_LOST\n"); + // Pause while minimized + if(input.unfocus_pause) + { + while(1) + { + SDL_WaitEvent(event); + if(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED) + break; + } + trace("--EVENT_SDL-- SDL_WINDOWEVENT_FOCUS_GAINED\n"); + } + break; + } +#elif SDL_VERSION_ATLEAST(2,0,0) case SDL_WINDOWEVENT: { switch(event->window.event) @@ -1034,8 +1064,8 @@ static boolean process_event(SDL_Event *event) event->window.event == SDL_WINDOWEVENT_FOCUS_GAINED) break; } + trace("--EVENT_SDL-- SDL_WINDOWEVENT_FOCUS_GAINED\n"); } - trace("--EVENT_SDL-- SDL_WINDOWEVENT_FOCUS_GAINED\n"); break; } } @@ -1073,7 +1103,7 @@ static boolean process_event(SDL_Event *event) } #endif // !SDL_VERSION_ATLEAST(2,0,0) - case SDL_MOUSEMOTION: + case SDL_EVENT_MOUSE_MOTION: { SDL_Window *window = SDL_GetWindowFromID(sdl_window_id); int mx_real = event->motion.x; @@ -1107,7 +1137,7 @@ static boolean process_event(SDL_Event *event) } trace( - "--EVENT_SDL-- SDL_MOUSEMOTION: (%d,%d) -> (%d,%d)\n", + "--EVENT_SDL-- SDL_EVENT_MOUSE_MOTION: (%d,%d) -> (%d,%d)\n", mx_real, my_real, mx, my ); status->mouse_pixel_x = mx; @@ -1118,23 +1148,23 @@ static boolean process_event(SDL_Event *event) break; } - case SDL_MOUSEBUTTONDOWN: + case SDL_EVENT_MOUSE_BUTTON_DOWN: { enum mouse_button button = convert_SDL_mouse_internal(event->button.button); - trace("--EVENT_SDL-- SDL_MOUSEBUTTONDOWN: %u\n", event->button.button); + trace("--EVENT_SDL-- SDL_EVENT_MOUSE_BUTTON_DOWN: %u\n", event->button.button); status->mouse_button = button; status->mouse_repeat = button; status->mouse_button_state |= MOUSE_BUTTON(button); status->mouse_repeat_state = 1; status->mouse_drag_state = -1; - status->mouse_time = SDL_GetTicks(); + status->mouse_time = get_ticks(); break; } - case SDL_MOUSEBUTTONUP: + case SDL_EVENT_MOUSE_BUTTON_UP: { enum mouse_button button = convert_SDL_mouse_internal(event->button.button); - trace("--EVENT_SDL-- SDL_MOUSEBUTTONUP: %u\n", event->button.button); + trace("--EVENT_SDL-- SDL_EVENT_MOUSE_BUTTON_UP: %u\n", event->button.button); status->mouse_button_state &= ~MOUSE_BUTTON(button); status->mouse_repeat = MOUSE_NO_BUTTON; status->mouse_drag_state = 0; @@ -1144,24 +1174,28 @@ static boolean process_event(SDL_Event *event) #if SDL_VERSION_ATLEAST(2,0,0) // emulate the X11-style "wheel is a button" that SDL 1.2 used - case SDL_MOUSEWHEEL: + case SDL_EVENT_MOUSE_WHEEL: { uint32_t button; + // floats due to SDL3... + float wheel_x = event->wheel.x; + float wheel_y = event->wheel.y; + trace( - "--EVENT_SDL-- SDL_MOUSEWHEEL: x=%d y=%d\n", - event->wheel.x, event->wheel.y + "--EVENT_SDL-- SDL_EVENT_MOUSE_WHEEL: x=%.2f y=%.2f\n", + wheel_x, wheel_y ); - if(abs(event->wheel.x) > abs(event->wheel.y)) + if(fabsf(wheel_x) > fabsf(wheel_y)) { - if(event->wheel.x < 0) + if(wheel_x < 0) button = MOUSE_BUTTON_WHEELLEFT; else button = MOUSE_BUTTON_WHEELRIGHT; } else { - if(event->wheel.y < 0) + if(wheel_y < 0) button = MOUSE_BUTTON_WHEELDOWN; else button = MOUSE_BUTTON_WHEELUP; @@ -1173,12 +1207,12 @@ static boolean process_event(SDL_Event *event) status->mouse_repeat = MOUSE_NO_BUTTON; status->mouse_repeat_state = 0; status->mouse_drag_state = 0; - status->mouse_time = SDL_GetTicks(); + status->mouse_time = get_ticks(); break; } #endif // SDL_VERSION_ATLEAST(2,0,0) - case SDL_KEYDOWN: + case SDL_EVENT_KEY_DOWN: { uint32_t unicode = 0; @@ -1208,7 +1242,7 @@ static boolean process_event(SDL_Event *event) ckey = convert_SDL_internal(event->key.keysym.sym); trace( - "--EVENT_SDL-- SDL_KEYDOWN: scancode:%d sym:%d -> %d\n", + "--EVENT_SDL-- SDL_EVENT_KEY_DOWN: scancode:%d sym:%d -> %d\n", event->key.keysym.scancode, event->key.keysym.sym, ckey @@ -1237,7 +1271,7 @@ static boolean process_event(SDL_Event *event) // using the internal keycode. if(unicode_fallback && KEYCODE_IS_ASCII(ckey)) { - boolean caps_lock = !!(SDL_GetModState() & KMOD_CAPS); + boolean caps_lock = !!(SDL_GetModState() & SDL_KMOD_CAPS); unicode = convert_internal_unicode(ckey, caps_lock); } @@ -1302,7 +1336,7 @@ static boolean process_event(SDL_Event *event) break; } - case SDL_KEYUP: + case SDL_EVENT_KEY_UP: { #ifdef CONFIG_PANDORA { @@ -1318,7 +1352,7 @@ static boolean process_event(SDL_Event *event) ckey = convert_SDL_internal(event->key.keysym.sym); trace( - "--EVENT_SDL-- SDL_KEYUP: scancode:%d sym:%d -> %d\n", + "--EVENT_SDL-- SDL_EVENT_KEY_UP: scancode:%d sym:%d -> %d\n", event->key.keysym.scancode, event->key.keysym.sym, ckey @@ -1356,11 +1390,11 @@ static boolean process_event(SDL_Event *event) * can't be distinguished from regular key presses, so key_press_unicode * needs to be called without repeating enabled. */ - case SDL_TEXTINPUT: + case SDL_EVENT_TEXT_INPUT: { uint8_t *text = (uint8_t *)event->text.text; - trace("--EVENT_SDL-- SDL_TEXTINPUT: %s\n", text); + trace("--EVENT_SDL-- SDL_EVENT_TEXT_INPUT: %s\n", text); if(unicode_fallback) { @@ -1381,28 +1415,28 @@ static boolean process_event(SDL_Event *event) break; } - case SDL_JOYDEVICEADDED: + case SDL_EVENT_JOYSTICK_ADDED: { // Add a new joystick. // "which" for this event (but not for any other joystick event) is not // a joystick instance ID, but instead an index for SDL_JoystickOpen(). trace( - "--EVENT_SDL-- SDL_JOYDEVICEADDED\n" + "--EVENT_SDL-- SDL_EVENT_JOYSTICK_ADDED\n" ); init_joystick(event->jdevice.which); break; } - case SDL_JOYDEVICEREMOVED: + case SDL_EVENT_JOYSTICK_REMOVED: { // Close a disconnected joystick. int which = event->jdevice.which; int joystick_index = get_joystick_index(which); trace( - "--EVENT_SDL-- SDL_JOYDEVICEREMOVED: j%d\n", + "--EVENT_SDL-- SDL_EVENT_JOYSTICK_REMOVED: j%d\n", joystick_index ); @@ -1415,14 +1449,20 @@ static boolean process_event(SDL_Event *event) break; } - case SDL_CONTROLLERAXISMOTION: + case SDL_EVENT_GAMEPAD_AXIS_MOTION: { // Since gamepad axis mappings can be complicated, use // the gamepad events to update the named axis values. struct config_info *conf = get_config(); +#if SDL_VERSION_ATLEAST(3,0,0) + int value = event->gaxis.value; + int which = event->gaxis.which; + int axis = event->gaxis.axis; +#else int value = event->caxis.value; int which = event->caxis.which; int axis = event->caxis.axis; +#endif enum joystick_special_axis special_axis = sdl_axis_map[axis]; int joystick_index = get_joystick_index(which); @@ -1430,7 +1470,7 @@ static boolean process_event(SDL_Event *event) break; trace( - "--EVENT_SDL-- SDL_CONTROLLERAXISMOTION: j%d sa%d = %d\n", + "--EVENT_SDL-- SDL_EVENT_GAMEPAD_AXIS_MOTION: j%d sa%d = %d\n", joystick_index, special_axis, value ); @@ -1439,7 +1479,7 @@ static boolean process_event(SDL_Event *event) } #endif - case SDL_JOYAXISMOTION: + case SDL_EVENT_JOYSTICK_AXIS_MOTION: { int axis_value = event->jaxis.value; int which = event->jaxis.which; @@ -1451,7 +1491,7 @@ static boolean process_event(SDL_Event *event) break; trace( - "--EVENT_SDL-- SDL_JOYAXISMOTION: j%d a%d = %d\n", + "--EVENT_SDL-- SDL_EVENT_JOYSTICK_AXIS_MOTION: j%d a%d = %d\n", joystick_index, axis, axis_value ); @@ -1459,7 +1499,7 @@ static boolean process_event(SDL_Event *event) break; } - case SDL_JOYBUTTONDOWN: + case SDL_EVENT_JOYSTICK_BUTTON_DOWN: { int which = event->jbutton.which; int button = event->jbutton.button; @@ -1470,7 +1510,7 @@ static boolean process_event(SDL_Event *event) break; trace( - "--EVENT_SDL-- SDL_JOYBUTTONDOWN: j%d b%d\n", + "--EVENT_SDL-- SDL_EVENT_JOYSTICK_BUTTON_DOWN: j%d b%d\n", joystick_index, button ); @@ -1484,7 +1524,7 @@ static boolean process_event(SDL_Event *event) break; } - case SDL_JOYBUTTONUP: + case SDL_EVENT_JOYSTICK_BUTTON_UP: { int which = event->jbutton.which; int button = event->jbutton.button; @@ -1501,7 +1541,7 @@ static boolean process_event(SDL_Event *event) #endif trace( - "--EVENT_SDL-- SDL_JOYBUTTONUP: j%d b%d\n", + "--EVENT_SDL-- SDL_EVENT_JOYSTICK_BUTTON_UP: j%d b%d\n", joystick_index, button ); @@ -1509,7 +1549,7 @@ static boolean process_event(SDL_Event *event) break; } - case SDL_JOYHATMOTION: + case SDL_EVENT_JOYSTICK_HAT_MOTION: { int which = event->jhat.which; int dir = event->jhat.value; @@ -1524,7 +1564,8 @@ static boolean process_event(SDL_Event *event) break; trace( - "--EVENT_SDL-- SDL_JOYHATMOTION: j%d up:%u down:%u left:%u right:%u\n", + "--EVENT_SDL-- SDL_EVENT_JOYSTICK_HAT_MOTION: " + "j%d up:%u down:%u left:%u right:%u\n", joystick_index, hat_u, hat_d, hat_l, hat_r ); @@ -1592,27 +1633,27 @@ boolean __peek_exit_input(void) #if SDL_VERSION_ATLEAST(2,0,0) num_events = - SDL_PeepEvents(events, 256, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT); + SDL_PeepEvents(events, 256, SDL_PEEKEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST); #else /* !SDL_VERSION_ATLEAST(2,0,0) */ num_events = SDL_PeepEvents(events, 256, SDL_PEEKEVENT, SDL_ALLEVENTS); #endif /* SDL_VERSION_ATLEAST(2,0,0) */ for(i = 0; i < num_events; i++) { - if(events[i].type == SDL_QUIT) + if(events[i].type == SDL_EVENT_QUIT) return true; - if(events[i].type == SDL_KEYDOWN) + if(events[i].type == SDL_EVENT_KEY_DOWN) { SDL_KeyboardEvent *ev = &(events[i].key); if(ev->keysym.sym == SDLK_ESCAPE) return true; - if(ev->keysym.sym == SDLK_c && (ev->keysym.mod & KMOD_CTRL)) + if(ev->keysym.sym == SDLK_c && (ev->keysym.mod & SDL_KMOD_CTRL)) return true; - if(ev->keysym.sym == SDLK_F4 && (ev->keysym.mod & KMOD_ALT)) + if(ev->keysym.sym == SDLK_F4 && (ev->keysym.mod & SDL_KMOD_ALT)) return true; } } @@ -1647,7 +1688,11 @@ void __warp_mouse(int x, int y) if((x < 0) || (y < 0)) { +#if SDL_VERSION_ATLEAST(3,0,0) + float current_x, current_y; +#else int current_x, current_y; +#endif SDL_GetMouseState(¤t_x, ¤t_y); if(x < 0) @@ -1669,6 +1714,19 @@ void initialize_joysticks(void) // and 3DS appear to have the same issue. int i, count; +#if SDL_VERSION_ATLEAST(3,0,0) + SDL_JoystickID *instance_ids = SDL_GetJoysticks(&count); + if(instance_ids) + { + if(count > MAX_JOYSTICKS) + count = MAX_JOYSTICKS; + + for(i = 0; i < count; i++) + init_joystick(instance_ids[i]); + + SDL_free(instance_ids); + } +#else count = SDL_NumJoysticks(); if(count > MAX_JOYSTICKS) @@ -1677,11 +1735,12 @@ void initialize_joysticks(void) for(i = 0; i < count; i++) init_joystick(i); #endif +#endif #if SDL_VERSION_ATLEAST(2,0,0) - SDL_GameControllerEventState(SDL_ENABLE); + SDL_SetGamepadEventsEnabled(SDL_TRUE); load_gamecontrollerdb(); #endif - SDL_JoystickEventState(SDL_ENABLE); + SDL_SetJoystickEventsEnabled(SDL_TRUE); } diff --git a/src/graphics.c b/src/graphics.c index 25791d960..67556fd82 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -1560,7 +1560,7 @@ static void set_window_icon(void) if(icon) { SDL_Window *window = SDL_GetWindowFromID(sdl_window_id); - SendMessage(SDL_GetWindowProperty_HWND(window), + SendMessage((HWND)SDL_GetWindowProperty_HWND(window), WM_SETICON, ICON_BIG, (LPARAM)icon); } } @@ -1821,9 +1821,16 @@ boolean init_video(struct config_info *conf, const char *caption) } #ifdef CONFIG_SDL + // TODO: platform function? if(!graphics.system_mouse) + { +#if SDL_VERSION_ATLEAST(3,0,0) + SDL_HideCursor(); +#else SDL_ShowCursor(SDL_DISABLE); #endif + } +#endif ec_load_set_secondary(mzx_res_get_by_id(MZX_DEFAULT_CHR), graphics.default_charset); diff --git a/src/platform_sdl.c b/src/platform_sdl.c index e2c9524c3..56a92d32c 100644 --- a/src/platform_sdl.c +++ b/src/platform_sdl.c @@ -59,7 +59,9 @@ void delay(uint32_t ms) uint64_t get_ticks(void) { -#if SDL_VERSION_ATLEAST(2,0,18) + // SDL_GetTicks returns a 64-bit value in SDL3, but + // SDL_GetTicks64 had to be used prior to that. +#if SDL_VERSION_ATLEAST(2,0,18) && !SDL_VERSION_ATLEAST(3,0,0) return SDL_GetTicks64(); #else return SDL_GetTicks(); @@ -111,7 +113,7 @@ boolean platform_init(void) Uint32 flags = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK; #if SDL_VERSION_ATLEAST(2,0,0) - flags |= SDL_INIT_GAMECONTROLLER; + flags |= SDL_INIT_GAMEPAD; #endif #ifdef CONFIG_PSP @@ -123,7 +125,8 @@ boolean platform_init(void) return false; #endif -#ifdef DEBUG +#if defined(DEBUG) && !SDL_VERSION_ATLEAST(3,0,0) + // Removed in SDL 3. flags |= SDL_INIT_NOPARACHUTE; #endif @@ -142,7 +145,7 @@ boolean platform_init(void) // try again without joystick support flags &= ~SDL_INIT_JOYSTICK; #if SDL_VERSION_ATLEAST(2,0,0) - flags &= ~SDL_INIT_GAMECONTROLLER; + flags &= ~SDL_INIT_GAMEPAD; #endif if(SDL_Init(flags) < 0) diff --git a/src/render_gl.h b/src/render_gl.h index ab2f786d5..de26b2166 100644 --- a/src/render_gl.h +++ b/src/render_gl.h @@ -32,15 +32,33 @@ __M_BEGIN_DECLS #include "util.h" #ifdef CONFIG_SDL +#include "SDLmzx.h" #ifdef CONFIG_GLES + #ifdef CONFIG_RENDER_GL_FIXED +#if SDL_VERSION_ATLEAST(3,0,0) +#include +#else #include #endif +#endif + #ifdef CONFIG_RENDER_GL_PROGRAM +#if SDL_VERSION_ATLEAST(3,0,0) +#include +#else #include #endif +#endif + +#else + +#if SDL_VERSION_ATLEAST(3,0,0) +#include #else #include +#endif + #endif #endif diff --git a/src/render_glsl.c b/src/render_glsl.c index c9e0fabe2..cfe06349d 100644 --- a/src/render_glsl.c +++ b/src/render_glsl.c @@ -40,9 +40,13 @@ #ifdef CONFIG_SDL #include "render_sdl.h" #ifdef ENABLE_GL_DEBUG_OUTPUT +#if SDL_VERSION_ATLEAST(3,0,0) +#include +#else #include #endif #endif +#endif #ifdef CONFIG_EGL #include diff --git a/src/render_sdl.c b/src/render_sdl.c index fe587410b..8ff60998e 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -70,58 +70,145 @@ int sdl_flags(int depth, boolean fullscreen, boolean fullscreen_windowed, return flags; } -boolean sdl_get_fullscreen_resolution(int *width, int *height, boolean scaling) +// Get the current desktop resolution, if possible. +static boolean sdl_get_desktop_display_mode(SDL_DisplayMode *display_mode) { -#if SDL_VERSION_ATLEAST(2,0,0) - SDL_DisplayMode display_mode; - boolean have_mode = false; +#if SDL_VERSION_ATLEAST(3,0,0) + + const SDL_DisplayMode **list; + const SDL_DisplayMode *mode; int count; - int ret; - if(scaling) + mode = SDL_GetDesktopDisplayMode(0); + if(mode) { - // Use the current desktop resolution. - ret = SDL_GetDesktopDisplayMode(0, &display_mode); + *display_mode = *mode; + return true; + } + + warn("Failed to query desktop display mode: %s\n", SDL_GetError()); + list = SDL_GetFullscreenDisplayModes(0, &count); + if(list) + { + if(count) + mode = list[0]; - if(ret) + SDL_free(list); + if(mode) { - count = SDL_GetNumDisplayModes(0); - if(count) - ret = SDL_GetDisplayMode(0, 0, &display_mode); + *display_mode = *mode; + return true; } - if(!ret) - have_mode = true; } - else - { - // Get the smallest possible resolution bigger than 640x350. - // Smaller means the software renderer will occupy more screen space. - SDL_DisplayMode mode; - int min_size = INT_MAX; - int i; - count = SDL_GetNumDisplayModes(0); +#elif SDL_VERSION_ATLEAST(2,0,0) - debug("Display modes:\n"); + if(SDL_GetDesktopDisplayMode(0, display_mode) == 0) + return true; - for(i = 0; i < count; i++) + warn("Failed to query desktop display mode: %s\n", SDL_GetError()); + + if(SDL_GetNumDisplayModes(0)) + if(SDL_GetDisplayMode(0, 0, display_mode) == 0) + return true; + +#endif + + return false; +} + +// Get the smallest possible resolution bigger than 640x350. +// Smaller means the software renderer will occupy more screen space. +static boolean sdl_get_smallest_usable_display_mode(SDL_DisplayMode *display_mode) +{ +#if SDL_VERSION_ATLEAST(3,0,0) + + int min_size = INT_MAX; + int count; + int i; + + const SDL_DisplayMode **list = SDL_GetFullscreenDisplayModes(0, &count); + if(!list) + return false; + + debug("Display modes:\n"); + + for(i = 0; i < count; i++) + { + if(!list[i]) + continue; + + debug("%d: %d x %d, %.2fHz, %s\n", i, list[i]->w, list[i]->h, + list[i]->refresh_rate, SDL_GetPixelFormatName(list[i]->format)); + + if((list[i]->w * list[i]->h < min_size) && + (list[i]->w >= SCREEN_PIX_W) && (list[i]->h >= SCREEN_PIX_H)) { - ret = SDL_GetDisplayMode(0, i, &mode); - debug("%d: %d x %d, %dHz, %s\n", i, mode.w, mode.h, mode.refresh_rate, - SDL_GetPixelFormatName(mode.format)); + min_size = list[i]->w * list[i]->h; + *display_mode = *list[i]; + } + } + SDL_free(list); - if(!ret && (mode.w * mode.h < min_size) && - (mode.w >= SCREEN_PIX_W) && (mode.h >= SCREEN_PIX_H)) - { - min_size = mode.w * mode.h; - display_mode = mode; - have_mode = true; - } + if(min_size < INT_MAX) + return true; + +#elif SDL_VERSION_ATLEAST(2,0,0) + + SDL_DisplayMode mode; + int min_size = INT_MAX; + int count; + int i; + + count = SDL_GetNumDisplayModes(0); + + debug("Display modes:\n"); + + for(i = 0; i < count; i++) + { + if(SDL_GetDisplayMode(0, i, &mode) != 0) + continue; + + debug("%d: %d x %d, %dHz, %s\n", i, mode.w, mode.h, mode.refresh_rate, \ + SDL_GetPixelFormatName(mode.format)); + + if((mode.w * mode.h < min_size) && + (mode.w >= SCREEN_PIX_W) && (mode.h >= SCREEN_PIX_H)) + { + min_size = mode.w * mode.h; + *display_mode = mode; } } + if(min_size < INT_MAX) + return true; + +#endif + + return false; +} + +boolean sdl_get_fullscreen_resolution(int *width, int *height, boolean scaling) +{ +#if SDL_VERSION_ATLEAST(2,0,0) + + SDL_DisplayMode display_mode; + boolean have_mode = false; + + if(scaling) + { + // Use the current desktop resolution. + have_mode = sdl_get_desktop_display_mode(&display_mode); + } + else + { + have_mode = sdl_get_smallest_usable_display_mode(&display_mode); + } if(have_mode) { + debug("\n"); + debug("selected: %d x %d, %.02fHz, %s\n", display_mode.w, display_mode.h, + (float)display_mode.refresh_rate, SDL_GetPixelFormatName(display_mode.format)); *width = display_mode.w; *height = display_mode.h; return true; @@ -194,8 +281,8 @@ static uint32_t sdl_pixel_format_priority(uint32_t pixel_format, break; } - case SDL_PIXELFORMAT_RGB888: - case SDL_PIXELFORMAT_BGR888: + case SDL_PIXELFORMAT_XRGB8888: + case SDL_PIXELFORMAT_XBGR8888: case SDL_PIXELFORMAT_RGBX8888: case SDL_PIXELFORMAT_BGRX8888: case SDL_PIXELFORMAT_ARGB8888: @@ -203,6 +290,11 @@ static uint32_t sdl_pixel_format_priority(uint32_t pixel_format, case SDL_PIXELFORMAT_ABGR8888: case SDL_PIXELFORMAT_BGRA8888: case SDL_PIXELFORMAT_ARGB2101010: +#if SDL_VERSION_ATLEAST(3,0,0) + case SDL_PIXELFORMAT_XRGB2101010: + case SDL_PIXELFORMAT_XBGR2101010: + case SDL_PIXELFORMAT_ABGR2101010: +#endif { // Any 32-bit RGB format is okay. if(bits_per_pixel == BPP_AUTO || bits_per_pixel == 32) @@ -302,21 +394,21 @@ void sdl_destruct_window(struct graphics_data *graphics) // match the pixel format MZX wants. if(render_data->shadow) { - SDL_FreeSurface(render_data->shadow); + SDL_DestroySurface(render_data->shadow); render_data->shadow = NULL; } // Used for 8bpp support for the software renderer. if(render_data->palette) { - SDL_FreePalette(render_data->palette); + SDL_DestroyPalette(render_data->palette); render_data->palette = NULL; } // Used for generating mapped colors for the SDL_Renderer renderers. if(render_data->pixel_format) { - SDL_FreeFormat(render_data->pixel_format); + SDL_DestroyPixelFormat(render_data->pixel_format); render_data->pixel_format = NULL; } @@ -395,8 +487,11 @@ boolean sdl_set_video_mode(struct graphics_data *graphics, int width, #endif render_data->window = SDL_CreateWindow("MegaZeux", - SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, - sdl_flags(depth, fullscreen, fullscreen_windowed, resize)); + // FIXME: +#if !SDL_VERSION_ATLEAST(3,0,0) + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, +#endif + width, height, sdl_flags(depth, fullscreen, fullscreen_windowed, resize)); if(!render_data->window) { @@ -447,14 +542,7 @@ boolean sdl_set_video_mode(struct graphics_data *graphics, int width, if(!matched) { - Uint32 Rmask, Gmask, Bmask, Amask; - int bpp; - - SDL_PixelFormatEnumToMasks(fmt, &bpp, &Rmask, &Gmask, &Bmask, &Amask); - - render_data->shadow = SDL_CreateRGBSurface(0, width, height, bpp, - Rmask, Gmask, Bmask, Amask); - + render_data->shadow = SDL_CreateSurface(width, height, fmt); debug("Blitting enabled. Rendering performance will be reduced.\n"); } else @@ -468,7 +556,7 @@ boolean sdl_set_video_mode(struct graphics_data *graphics, int width, render_data->shadow ? render_data->shadow->format : render_data->screen->format; - render_data->palette = SDL_AllocPalette(SMZX_PAL_SIZE); + render_data->palette = SDL_CreatePalette(SMZX_PAL_SIZE); if(!render_data->palette) { warn("Failed to allocate palette: %s\n", SDL_GetError()); @@ -521,7 +609,11 @@ boolean sdl_set_video_mode(struct graphics_data *graphics, int width, #if defined(CONFIG_RENDER_SOFTSCALE) || defined(CONFIG_RENDER_SDLACCEL) +#if SDL_VERSION_ATLEAST(3,0,0) +#define BEST_RENDERER NULL +#else #define BEST_RENDERER -1 +#endif static boolean is_integer_scale(struct graphics_data *graphics, int width, int height) @@ -541,12 +633,12 @@ static uint32_t get_format_amask(uint32_t format) Uint32 rmask, gmask, bmask, amask; int bpp; - SDL_PixelFormatEnumToMasks(format, &bpp, &rmask, &gmask, &bmask, &amask); + SDL_GetMasksForPixelFormatEnum(format, &bpp, &rmask, &gmask, &bmask, &amask); return amask; } static void find_texture_format(struct graphics_data *graphics, - uint32_t sdl_rendererflags) + boolean requires_blend_ops) { struct sdl_render_data *render_data = (struct sdl_render_data *)graphics->render_data; uint32_t texture_format = SDL_PIXELFORMAT_UNKNOWN; @@ -572,8 +664,7 @@ static void find_texture_format(struct graphics_data *graphics, #endif // Anything using hardware blending must support alpha. - // Blending doesn't require targeting a texture, but this works for now. - if(sdl_rendererflags & SDL_RENDERER_TARGETTEXTURE) + if(requires_blend_ops) need_alpha = true; // Try to use a native texture format to improve performance. @@ -677,10 +768,11 @@ static void find_texture_format(struct graphics_data *graphics, */ boolean sdlrender_set_video_mode(struct graphics_data *graphics, int width, int height, int depth, boolean fullscreen, boolean resize, - uint32_t sdl_rendererflags) + boolean requires_blend_ops) { struct sdl_render_data *render_data = graphics->render_data; boolean fullscreen_windowed = graphics->fullscreen_windowed; + uint32_t sdl_rendererflags = 0; sdl_destruct_window(graphics); @@ -696,6 +788,13 @@ boolean sdlrender_set_video_mode(struct graphics_data *graphics, SDL_SetHint(SDL_HINT_EMSCRIPTEN_ASYNCIFY, "0"); #endif +#if !SDL_VERSION_ATLEAST(3,0,0) + // Flag removed in SDL3; all drivers support it and its presence needs to be + // tested by trying to create a target texture instead. + if(requires_blend_ops) + sdl_renderer_flags |= SDL_RENDERER_TARGETTEXTURE; +#endif + if(graphics->sdl_render_driver[0]) { info("Requesting SDL render driver: '%s'\n", graphics->sdl_render_driver); @@ -703,7 +802,11 @@ boolean sdlrender_set_video_mode(struct graphics_data *graphics, } render_data->window = SDL_CreateWindow("MegaZeux", - SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, +// FIXME: +#if !SDL_VERSION_ATLEAST(3,0,0) + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, +#endif + width, height, sdl_flags(depth, fullscreen, fullscreen_windowed, resize)); if(!render_data->window) @@ -736,7 +839,7 @@ boolean sdlrender_set_video_mode(struct graphics_data *graphics, if(!render_data->rgb_to_yuv) { // This is required for SDL_MapRGBA to work, but YUV formats can ignore it. - render_data->pixel_format = SDL_AllocFormat(render_data->texture_format); + render_data->pixel_format = SDL_CreatePixelFormat(render_data->texture_format); if(!render_data->pixel_format) { warn("Failed to allocate pixel format: %s\n", SDL_GetError()); @@ -833,7 +936,11 @@ boolean gl_set_video_mode(struct graphics_data *graphics, int width, int height, #endif render_data->window = SDL_CreateWindow("MegaZeux", - SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, + // FIXME: +#if !SDL_VERSION_ATLEAST(3,0,0) + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, +#endif + width, height, GL_STRIP_FLAGS(sdl_flags(depth, fullscreen, fullscreen_windowed, resize))); if(!render_data->window) @@ -907,4 +1014,3 @@ boolean gl_swap_buffers(struct graphics_data *graphics) } #endif // CONFIG_RENDER_GL_FIXED || CONFIG_RENDER_GL_PROGRAM - diff --git a/src/render_sdl.h b/src/render_sdl.h index 7981c603c..539c8284a 100644 --- a/src/render_sdl.h +++ b/src/render_sdl.h @@ -77,7 +77,7 @@ boolean sdl_check_video_mode(struct graphics_data *graphics, int width, #if SDL_VERSION_ATLEAST(2,0,0) boolean sdlrender_set_video_mode(struct graphics_data *graphics, int width, int height, int depth, boolean fullscreen, boolean resize, - uint32_t sdl_rendererflags); + boolean requires_blend_ops); void sdlrender_update_colors(struct graphics_data *graphics, struct rgb_color *palette, unsigned int count); @@ -89,8 +89,7 @@ void sdlrender_update_colors(struct graphics_data *graphics, #if SDL_VERSION_ATLEAST(2,0,0) #define GL_ALLOW_FLAGS \ - (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP | \ - SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE) + (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE) #else #define GL_ALLOW_FLAGS (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE) #endif diff --git a/src/render_sdlaccel.c b/src/render_sdlaccel.c index aa6df776c..47e730ae2 100644 --- a/src/render_sdlaccel.c +++ b/src/render_sdlaccel.c @@ -302,7 +302,7 @@ static boolean sdlaccel_set_video_mode(struct graphics_data *graphics, // This requires that the underlying driver supports framebuffer objects. if(!sdlrender_set_video_mode(graphics, width, height, - depth, fullscreen, resize, SDL_RENDERER_TARGETTEXTURE)) + depth, fullscreen, resize, true)) return false; texture[TEX_SCREEN] = @@ -551,8 +551,13 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, SDL_Texture *chars_tex = render_data->sdl.texture[TEX_CHARS]; SDL_Texture *bg_tex = render_data->sdl.texture[TEX_BACKGROUND]; +#if SDL_VERSION_ATLEAST(3,0,0) + SDL_FRect dest_bg = { offx, offy, w * CHAR_W, h * CHAR_H }; + SDL_FRect src_bg = { 0, 0, w, h }; +#else SDL_Rect dest_bg = { offx, offy, w * CHAR_W, h * CHAR_H }; SDL_Rect src_bg = { 0, 0, w, h }; +#endif void *_bg; int bg_pitch; uint32_t *bg; @@ -637,7 +642,7 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, } } SDL_UnlockTexture(bg_tex); - SDL_RenderCopy(renderer, bg_tex, &src_bg, &dest_bg); + SDL_RenderTexture(renderer, bg_tex, &src_bg, &dest_bg); #ifdef RENDER_GEOMETRY @@ -646,8 +651,13 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, #else /* !RENDER_GEOMETRY */ /* Render foreground and partially transparent chars. */ { +#if SDL_VERSION_ATLEAST(3,0,0) + SDL_FRect dest_rect = { 0, 0, CHAR_W, CHAR_H }; + SDL_FRect src_rect = { 0, 0, CHAR_W, CHAR_H }; +#else SDL_Rect dest_rect = { 0, 0, CHAR_W, CHAR_H }; SDL_Rect src_rect = { 0, 0, CHAR_W, CHAR_H }; +#endif next = layer->data; for(y = 0; y < h; y++) { @@ -676,7 +686,7 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, else continue; - SDL_RenderCopy(renderer, chars_tex, &src_rect, &dest_rect); + SDL_RenderTexture(renderer, chars_tex, &src_rect, &dest_rect); } } } @@ -695,7 +705,11 @@ static void sdlaccel_render_cursor(struct graphics_data *graphics, unsigned x, uint8_t *palette = render_data->palette; // Input coordinates are on the character grid. +#if SDL_VERSION_ATLEAST(3,0,0) + SDL_FRect dest = { x * CHAR_W, y * CHAR_H + offset, CHAR_W, lines }; +#else SDL_Rect dest = { x * CHAR_W, y * CHAR_H + offset, CHAR_W, lines }; +#endif SDL_SetRenderDrawBlendMode(render_data->sdl.renderer, SDL_BLENDMODE_NONE); SDL_SetRenderDrawColor(render_data->sdl.renderer, @@ -711,7 +725,11 @@ static void sdlaccel_render_mouse(struct graphics_data *graphics, unsigned x, struct sdlaccel_render_data *render_data = graphics->render_data; // Input coordinates are pixel values. +#if SDL_VERSION_ATLEAST(3,0,0) + SDL_FRect dest = { x, y, w, h }; +#else SDL_Rect dest = { x, y, w, h }; +#endif /* There is no preset inversion blend mode, so make a custom blend mode. * Lower SDL versions should simply fall back to drawing a white rectangle. @@ -738,8 +756,13 @@ static void sdlaccel_sync_screen(struct graphics_data *graphics) struct sdlaccel_render_data *render_data = graphics->render_data; SDL_Renderer *renderer = render_data->sdl.renderer; SDL_Texture *screen_tex = render_data->sdl.texture[TEX_SCREEN]; +#if SDL_VERSION_ATLEAST(3,0,0) + SDL_FRect src; + SDL_FRect dest; +#else SDL_Rect src; SDL_Rect dest; +#endif int width = render_data->w; int height = render_data->h; int v_width, v_height; @@ -756,7 +779,7 @@ static void sdlaccel_sync_screen(struct graphics_data *graphics) dest.h = v_height; SDL_SetRenderTarget(renderer, NULL); - SDL_RenderCopy(renderer, screen_tex, &src, &dest); + SDL_RenderTexture(renderer, screen_tex, &src, &dest); SDL_RenderPresent(renderer); diff --git a/src/render_softscale.c b/src/render_softscale.c index 07b7d2ac3..e51a84aad 100644 --- a/src/render_softscale.c +++ b/src/render_softscale.c @@ -265,14 +265,24 @@ static void softscale_sync_screen(struct graphics_data *graphics) struct softscale_render_data *render_data = graphics->render_data; SDL_Renderer *renderer = render_data->sdl.renderer; SDL_Texture *texture = render_data->sdl.texture[0]; - SDL_Rect *src_rect = &(render_data->texture_rect); + SDL_Rect *texture_rect = &(render_data->texture_rect); +#if SDL_VERSION_ATLEAST(3,0,0) + SDL_FRect src_rect; + SDL_FRect dest_rect; +#else + SDL_Rect src_rect; SDL_Rect dest_rect; +#endif int width = render_data->w; int height = render_data->h; int v_width, v_height; - fix_viewport_ratio(width, height, &v_width, &v_height, graphics->ratio); + src_rect.x = texture_rect->x; + src_rect.y = texture_rect->y; + src_rect.w = texture_rect->w; + src_rect.x = texture_rect->h; + fix_viewport_ratio(width, height, &v_width, &v_height, graphics->ratio); dest_rect.x = (width - v_width) / 2; dest_rect.y = (height - v_height) / 2; dest_rect.w = v_width; @@ -283,7 +293,7 @@ static void softscale_sync_screen(struct graphics_data *graphics) SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, texture, src_rect, &dest_rect); + SDL_RenderTexture(renderer, texture, &src_rect, &dest_rect); SDL_RenderPresent(renderer); } diff --git a/src/thread_sdl.h b/src/thread_sdl.h index c03182bfc..0dddbf666 100644 --- a/src/thread_sdl.h +++ b/src/thread_sdl.h @@ -29,13 +29,19 @@ __M_BEGIN_DECLS #define THREAD_RES int #define THREAD_RETURN do { return 0; } while(0) -typedef SDL_cond *platform_cond; -typedef SDL_mutex *platform_mutex; -typedef SDL_sem *platform_sem; +typedef SDL_Condition *platform_cond; +typedef SDL_Mutex *platform_mutex; +typedef SDL_Semaphore *platform_sem; typedef SDL_Thread *platform_thread; -typedef SDL_threadID platform_thread_id; typedef SDL_ThreadFunction platform_thread_fn; +// Can't fix this with typedefs--SDL_ThreadID meant something else in SDL 2. +#if SDL_VERSION_ATLEAST(3,0,0) +typedef SDL_ThreadID platform_thread_id; +#else +typedef SDL_threadID platform_thread_id; +#endif + static inline boolean platform_mutex_init(platform_mutex *mutex) { platform_mutex m = SDL_CreateMutex(); @@ -55,21 +61,19 @@ static inline boolean platform_mutex_destroy(platform_mutex *mutex) static inline boolean platform_mutex_lock(platform_mutex *mutex) { - if(SDL_LockMutex(*mutex)) - return false; + SDL_LockMutex(*mutex); // Returns void as of SDL 3. return true; } static inline boolean platform_mutex_unlock(platform_mutex *mutex) { - if(SDL_UnlockMutex(*mutex)) - return false; + SDL_UnlockMutex(*mutex); // Returns void as of SDL 3. return true; } static inline boolean platform_cond_init(platform_cond *cond) { - platform_cond c = SDL_CreateCond(); + platform_cond c = SDL_CreateCondition(); if(c) { *cond = c; @@ -80,14 +84,14 @@ static inline boolean platform_cond_init(platform_cond *cond) static inline boolean platform_cond_destroy(platform_cond *cond) { - SDL_DestroyCond(*cond); + SDL_DestroyCondition(*cond); return true; } static inline boolean platform_cond_wait(platform_cond *cond, platform_mutex *mutex) { - if(SDL_CondWait(*cond, *mutex)) + if(SDL_WaitCondition(*cond, *mutex)) return false; return true; } @@ -95,28 +99,28 @@ static inline boolean platform_cond_wait(platform_cond *cond, static inline boolean platform_cond_timedwait(platform_cond *cond, platform_mutex *mutex, unsigned int timeout_ms) { - if(SDL_CondWaitTimeout(*cond, *mutex, (Uint32)timeout_ms)) + if(SDL_WaitConditionTimeout(*cond, *mutex, (Uint32)timeout_ms)) return false; return true; } static inline boolean platform_cond_signal(platform_cond *cond) { - if(SDL_CondSignal(*cond)) + if(SDL_SignalCondition(*cond)) return false; return true; } static inline boolean platform_cond_broadcast(platform_cond *cond) { - if(SDL_CondBroadcast(*cond)) + if(SDL_BroadcastCondition(*cond)) return false; return true; } static inline boolean platform_sem_init(platform_sem *sem, unsigned init_value) { - SDL_sem *ret = SDL_CreateSemaphore(init_value); + platform_sem ret = SDL_CreateSemaphore(init_value); if(ret) { *sem = ret; @@ -133,14 +137,14 @@ static inline boolean platform_sem_destroy(platform_sem *sem) static inline boolean platform_sem_wait(platform_sem *sem) { - if(SDL_SemWait(*sem)) + if(SDL_WaitSemaphore(*sem)) return false; return true; } static inline boolean platform_sem_post(platform_sem *sem) { - if(SDL_SemPost(*sem)) + if(SDL_PostSemaphore(*sem)) return false; return true; } @@ -170,7 +174,7 @@ static inline boolean platform_thread_join(platform_thread *thread) static inline platform_thread_id platform_get_thread_id(void) { - return SDL_ThreadID(); + return SDL_GetCurrentThreadID(); } static inline boolean platform_is_same_thread(platform_thread_id a, From 5fae49e3e84753bb49a34ed9693d5d36bfe08029 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Mon, 15 Apr 2024 14:17:05 -0600 Subject: [PATCH 02/44] Fix SDL2 builds and add SDL_FRect workaround. --- src/SDLmzx.h | 64 ++++++++++++++++++++++++++++++++++++------ src/render_sdl.c | 2 +- src/render_sdlaccel.c | 50 +++++++++++++-------------------- src/render_softscale.c | 30 +++++++++----------- 4 files changed, 88 insertions(+), 58 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 1104b1c09..e48c91623 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -39,6 +39,8 @@ __M_BEGIN_DECLS #endif #endif +#include + /* SDL1 backwards compatibility for SDL2 ************************************/ #if !SDL_VERSION_ATLEAST(2,0,0) @@ -242,8 +244,8 @@ typedef SDL_GameControllerButton SDL_GamepadButton; #define SDL_GAMEPAD_BUTTON_BACK SDL_CONTROLLER_BUTTON_BACK #define SDL_GAMEPAD_BUTTON_GUIDE SDL_CONTROLLER_BUTTON_GUIDE #define SDL_GAMEPAD_BUTTON_START SDL_CONTROLLER_BUTTON_START -#define SDL_GAMEPAD_BUTTON_LEFT_STICK SDL_CONTROLLER_BUTTON_LEFT_STICK -#define SDL_GAMEPAD_BUTTON_RIGHT_STICK SDL_CONTROLLER_BUTTON_RIGHT_STICK +#define SDL_GAMEPAD_BUTTON_LEFT_STICK SDL_CONTROLLER_BUTTON_LEFTSTICK +#define SDL_GAMEPAD_BUTTON_RIGHT_STICK SDL_CONTROLLER_BUTTON_RIGHTSTICK #define SDL_GAMEPAD_BUTTON_LEFT_SHOULDER SDL_CONTROLLER_BUTTON_LEFTSHOULDER #define SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER SDL_CONTROLLER_BUTTON_RIGHTSHOULDER #define SDL_GAMEPAD_BUTTON_DPAD_UP SDL_CONTROLLER_BUTTON_DPAD_UP @@ -308,25 +310,69 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) #define SDL_DestroyPalette(p) SDL_FreePalette(p) #define SDL_CreatePixelFormat(f) SDL_AllocFormat(f) #define SDL_DestroyPixelFormat(pf) SDL_FreeFormat(pf) -#define SDL_GetMasksForPixelFormatEnum(f,b,r,g,b,a) SDL_PixelFormatEnumToMasks(f,b,r,g,b,a) +#define SDL_GetMasksForPixelFormatEnum(f,b,R,G,B,A) SDL_PixelFormatEnumToMasks(f,b,R,G,B,A) +#endif + +/** + * SDL_rect.h + */ +#if !SDL_VERSION_ATLEAST(2,0,10) +typedef struct { float x; float y; float w; float h; } SDL_FRect; #endif -/* SDL_render.h */ +#if SDL_VERSION_ATLEAST(3,0,0) +typedef SDL_FRect SDL_RenderRect; +static inline SDL_RenderRect sdl_render_rect(int x, int y, + int w, int h, int full_w, int full_h) +{ + SDL_FRect tmp = + { + (float)x / full_w, + (float)y / full_h, + (float)w / full_w, + (float)h / full_h + }; + return tmp; +} +#else +typedef SDL_Rect SDL_RenderRect; +static inline SDL_RenderRect sdl_render_rect(int x, int y, + int w, int h, int full_w, int full_h) +{ + SDL_Rect tmp = { x, y, w, h }; + return tmp; +} +#endif + +/** + * SDL_render.h + */ #if !SDL_VERSION_ATLEAST(3,0,0) && SDL_VERSION_ATLEAST(2,0,0) typedef int SDL_RendererLogicalPresentation; #define SDL_LOGICAL_PRESENTATION_DISABLED 0 -#define SDL_SCALEMODE_BEST SDL_ScaleModeBest -#define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear -#define SDL_SCALEMODE_NEAREST SDL_ScaleModeNearest -#define SDL_SetRenderClipRect(r, rect) SDL_RenderSetClipRect(r, rect) +#define SDL_SCALEMODE_BEST SDL_ScaleModeBest +#define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear +#define SDL_SCALEMODE_NEAREST SDL_ScaleModeNearest +#define SDL_SetRenderClipRect(r, rect) SDL_RenderSetClipRect(r, rect) +#define SDL_SetRenderLogicalSize(r, w, h) SDL_RenderSetLogicalSize(r, w, h) static inline int SDL_SetRenderLogicalPresentation(SDL_Renderer *render, - int w, int h, SDL_RendererLogicalPresentiation p, SDL_ScaleMode s) + int w, int h, SDL_RendererLogicalPresentation p, SDL_ScaleMode s) { return SDL_SetRenderLogicalSize(render, w, h); } #endif +static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *texture, + const SDL_RenderRect *src_rect, const SDL_RenderRect *dest_rect) +{ +#if SDL_VERSION_ATLEAST(3,0,0) + return SDL_RenderTexture(renderer, texture, src_rect, dest_rect); +#else + return SDL_RenderCopy(renderer, texture, src_rect, dest_rect); +#endif +} + /* SDL_surface.h */ #if !SDL_VERSION_ATLEAST(3,0,0) && SDL_VERSION_ATLEAST(2,0,0) #define SDL_DestroySurface(s) SDL_FreeSurface(s) diff --git a/src/render_sdl.c b/src/render_sdl.c index 8ff60998e..b4f730c6c 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -792,7 +792,7 @@ boolean sdlrender_set_video_mode(struct graphics_data *graphics, // Flag removed in SDL3; all drivers support it and its presence needs to be // tested by trying to create a target texture instead. if(requires_blend_ops) - sdl_renderer_flags |= SDL_RENDERER_TARGETTEXTURE; + sdl_rendererflags |= SDL_RENDERER_TARGETTEXTURE; #endif if(graphics->sdl_render_driver[0]) diff --git a/src/render_sdlaccel.c b/src/render_sdlaccel.c index 47e730ae2..e362aa8f5 100644 --- a/src/render_sdlaccel.c +++ b/src/render_sdlaccel.c @@ -31,7 +31,6 @@ #include "SDLmzx.h" #include "graphics.h" -#include "platform.h" #include "render.h" #include "renderers.h" #include "render_sdl.h" @@ -551,13 +550,10 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, SDL_Texture *chars_tex = render_data->sdl.texture[TEX_CHARS]; SDL_Texture *bg_tex = render_data->sdl.texture[TEX_BACKGROUND]; -#if SDL_VERSION_ATLEAST(3,0,0) - SDL_FRect dest_bg = { offx, offy, w * CHAR_W, h * CHAR_H }; - SDL_FRect src_bg = { 0, 0, w, h }; -#else - SDL_Rect dest_bg = { offx, offy, w * CHAR_W, h * CHAR_H }; - SDL_Rect src_bg = { 0, 0, w, h }; -#endif + SDL_RenderRect dest_bg = + sdl_render_rect(offx, offy, w * CHAR_W, h * CHAR_H, TEX_SCREEN_W, TEX_SCREEN_H); + SDL_RenderRect src_bg = sdl_render_rect(0, 0, w, h, TEX_BG_W, TEX_BG_H); + void *_bg; int bg_pitch; uint32_t *bg; @@ -642,7 +638,7 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, } } SDL_UnlockTexture(bg_tex); - SDL_RenderTexture(renderer, bg_tex, &src_bg, &dest_bg); + SDL_RenderTexture_mzx(renderer, bg_tex, &src_bg, &dest_bg); #ifdef RENDER_GEOMETRY @@ -651,13 +647,8 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, #else /* !RENDER_GEOMETRY */ /* Render foreground and partially transparent chars. */ { -#if SDL_VERSION_ATLEAST(3,0,0) - SDL_FRect dest_rect = { 0, 0, CHAR_W, CHAR_H }; - SDL_FRect src_rect = { 0, 0, CHAR_W, CHAR_H }; -#else SDL_Rect dest_rect = { 0, 0, CHAR_W, CHAR_H }; SDL_Rect src_rect = { 0, 0, CHAR_W, CHAR_H }; -#endif next = layer->data; for(y = 0; y < h; y++) { @@ -686,7 +677,7 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, else continue; - SDL_RenderTexture(renderer, chars_tex, &src_rect, &dest_rect); + SDL_RenderCopy(renderer, chars_tex, &src_rect, &dest_rect); } } } @@ -756,30 +747,27 @@ static void sdlaccel_sync_screen(struct graphics_data *graphics) struct sdlaccel_render_data *render_data = graphics->render_data; SDL_Renderer *renderer = render_data->sdl.renderer; SDL_Texture *screen_tex = render_data->sdl.texture[TEX_SCREEN]; -#if SDL_VERSION_ATLEAST(3,0,0) - SDL_FRect src; - SDL_FRect dest; -#else - SDL_Rect src; - SDL_Rect dest; -#endif + SDL_RenderRect src; + SDL_RenderRect dest; int width = render_data->w; int height = render_data->h; int v_width, v_height; - src.x = 0; - src.y = 0; - src.w = SCREEN_PIX_W; - src.h = SCREEN_PIX_H; + src = sdl_render_rect(0, 0, SCREEN_PIX_W, SCREEN_PIX_H, + TEX_SCREEN_W, TEX_SCREEN_H); fix_viewport_ratio(width, height, &v_width, &v_height, graphics->ratio); - dest.x = (width - v_width) / 2; - dest.y = (height - v_height) / 2; - dest.w = v_width; - dest.h = v_height; + dest = sdl_render_rect( + (width - v_width) / 2, + (height - v_height) / 2, + v_width, + v_height, + width, + height + ); SDL_SetRenderTarget(renderer, NULL); - SDL_RenderTexture(renderer, screen_tex, &src, &dest); + SDL_RenderTexture_mzx(renderer, screen_tex, &src, &dest); SDL_RenderPresent(renderer); diff --git a/src/render_softscale.c b/src/render_softscale.c index e51a84aad..eeb129235 100644 --- a/src/render_softscale.c +++ b/src/render_softscale.c @@ -32,7 +32,6 @@ #include "render_sdl.h" #include "renderers.h" #include "util.h" -#include "yuv.h" struct softscale_render_data { @@ -266,34 +265,31 @@ static void softscale_sync_screen(struct graphics_data *graphics) SDL_Renderer *renderer = render_data->sdl.renderer; SDL_Texture *texture = render_data->sdl.texture[0]; SDL_Rect *texture_rect = &(render_data->texture_rect); -#if SDL_VERSION_ATLEAST(3,0,0) - SDL_FRect src_rect; - SDL_FRect dest_rect; -#else - SDL_Rect src_rect; - SDL_Rect dest_rect; -#endif + SDL_RenderRect src_rect; + SDL_RenderRect dest_rect; int width = render_data->w; int height = render_data->h; int v_width, v_height; - src_rect.x = texture_rect->x; - src_rect.y = texture_rect->y; - src_rect.w = texture_rect->w; - src_rect.x = texture_rect->h; + src_rect = sdl_render_rect(texture_rect->x, texture_rect->y, + texture_rect->w, texture_rect->h, render_data->texture_width, SCREEN_PIX_H); fix_viewport_ratio(width, height, &v_width, &v_height, graphics->ratio); - dest_rect.x = (width - v_width) / 2; - dest_rect.y = (height - v_height) / 2; - dest_rect.w = v_width; - dest_rect.h = v_height; + dest_rect = sdl_render_rect( + (width - v_width) / 2, + (height - v_height) / 2, + v_width, + v_height, + width, + height + ); softscale_unlock_texture(render_data); SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); SDL_RenderClear(renderer); - SDL_RenderTexture(renderer, texture, &src_rect, &dest_rect); + SDL_RenderTexture_mzx(renderer, texture, &src_rect, &dest_rect); SDL_RenderPresent(renderer); } From a720b8b281b0c93363eed442187d3ba7180bcb25 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Mon, 15 Apr 2024 14:35:03 -0600 Subject: [PATCH 03/44] Fix SDL 1.2 builds(?). --- src/SDLmzx.h | 17 ++++++----------- src/render_sdl.c | 11 ++++++----- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index e48c91623..c41ef9b0b 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -49,15 +49,6 @@ __M_BEGIN_DECLS // counterpart. More complex changes are handled with #ifdefs "in situ". // Data types - -struct SDL_DisplayMode -{ - Uint32 format; - int w; - int h; - int refresh_rate; -}; - typedef SDLKey SDL_Keycode; typedef int (*SDL_ThreadFunction)(void *); typedef Uint32 SDL_threadID; @@ -334,7 +325,7 @@ static inline SDL_RenderRect sdl_render_rect(int x, int y, }; return tmp; } -#else +#elif SDL_VERSION_ATLEAST(2,0,0) typedef SDL_Rect SDL_RenderRect; static inline SDL_RenderRect sdl_render_rect(int x, int y, int w, int h, int full_w, int full_h) @@ -363,6 +354,7 @@ static inline int SDL_SetRenderLogicalPresentation(SDL_Renderer *render, } #endif +#if SDL_VERSION_ATLEAST(2,0,0) static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_RenderRect *src_rect, const SDL_RenderRect *dest_rect) { @@ -372,8 +364,11 @@ static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *tex return SDL_RenderCopy(renderer, texture, src_rect, dest_rect); #endif } +#endif -/* SDL_surface.h */ +/** + * SDL_surface.h + */ #if !SDL_VERSION_ATLEAST(3,0,0) && SDL_VERSION_ATLEAST(2,0,0) #define SDL_DestroySurface(s) SDL_FreeSurface(s) diff --git a/src/render_sdl.c b/src/render_sdl.c index b4f730c6c..7025ed107 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -70,6 +70,7 @@ int sdl_flags(int depth, boolean fullscreen, boolean fullscreen_windowed, return flags; } +#if SDL_VERSION_ATLEAST(2,0,0) // Get the current desktop resolution, if possible. static boolean sdl_get_desktop_display_mode(SDL_DisplayMode *display_mode) { @@ -101,7 +102,7 @@ static boolean sdl_get_desktop_display_mode(SDL_DisplayMode *display_mode) } } -#elif SDL_VERSION_ATLEAST(2,0,0) +#else if(SDL_GetDesktopDisplayMode(0, display_mode) == 0) return true; @@ -153,7 +154,7 @@ static boolean sdl_get_smallest_usable_display_mode(SDL_DisplayMode *display_mod if(min_size < INT_MAX) return true; -#elif SDL_VERSION_ATLEAST(2,0,0) +#else SDL_DisplayMode mode; int min_size = INT_MAX; @@ -186,6 +187,7 @@ static boolean sdl_get_smallest_usable_display_mode(SDL_DisplayMode *display_mod return false; } +#endif /* SDL_VERSION_ATLEAST(2,0,0) */ boolean sdl_get_fullscreen_resolution(int *width, int *height, boolean scaling) { @@ -220,6 +222,7 @@ boolean sdl_get_fullscreen_resolution(int *width, int *height, boolean scaling) return false; } +#if SDL_VERSION_ATLEAST(2,0,0) /** * Determine if MZX supports a particular SDL pixel format. Returns a priority * value if the format is supported or 0 if the format is not supported. Values @@ -232,7 +235,6 @@ boolean sdl_get_fullscreen_resolution(int *width, int *height, boolean scaling) static uint32_t sdl_pixel_format_priority(uint32_t pixel_format, uint32_t bits_per_pixel, uint32_t yuv_priority) { -#if SDL_VERSION_ATLEAST(2,0,0) switch(pixel_format) { case SDL_PIXELFORMAT_INDEX8: @@ -318,10 +320,9 @@ static uint32_t sdl_pixel_format_priority(uint32_t pixel_format, break; } } -#endif /* SDL_VERSION_ATLEAST(2,0,0) */ - return 0; } +#endif /* SDL_VERSION_ATLEAST(2,0,0) */ #if !SDL_VERSION_ATLEAST(2,0,0) /** From 3b1ab5adf272f8313a71e140598e1f5d6391bc10 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Mon, 15 Apr 2024 16:16:38 -0600 Subject: [PATCH 04/44] Update for lastest SDL3 changes. --- src/SDLmzx.h | 40 ++++++++++++++++++++++++++++++++++------ src/about.c | 6 +++--- src/graphics.c | 18 ++++++++++++++++-- src/render_sdl.c | 39 +++++++++++++++++++++++---------------- src/render_sdl.h | 1 + src/render_sdlaccel.c | 34 +++++++++++++++++++++++++++++----- src/render_soft.c | 4 ++++ src/render_softscale.c | 21 +++++++++++++-------- 8 files changed, 123 insertions(+), 40 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index c41ef9b0b..0b0f3de22 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -302,6 +302,14 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) #define SDL_CreatePixelFormat(f) SDL_AllocFormat(f) #define SDL_DestroyPixelFormat(pf) SDL_FreeFormat(pf) #define SDL_GetMasksForPixelFormatEnum(f,b,R,G,B,A) SDL_PixelFormatEnumToMasks(f,b,R,G,B,A) + +#if !SDL_VERSION_ATLEAST(2,0,5) +#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#define SDL_PIXELFORMAT_RGBA32 SDL_PIXELFORMAT_RGBA8888 +#else +#define SDL_PIXELFORMAT_RGBA32 SDL_PIXELFORMAT_ABGR8888 +#endif +#endif #endif /** @@ -312,8 +320,8 @@ typedef struct { float x; float y; float w; float h; } SDL_FRect; #endif #if SDL_VERSION_ATLEAST(3,0,0) -typedef SDL_FRect SDL_RenderRect; -static inline SDL_RenderRect sdl_render_rect(int x, int y, +typedef SDL_FRect SDL_Rect_mzx; +static inline SDL_Rect_mzx sdl_render_rect(int x, int y, int w, int h, int full_w, int full_h) { SDL_FRect tmp = @@ -326,8 +334,8 @@ static inline SDL_RenderRect sdl_render_rect(int x, int y, return tmp; } #elif SDL_VERSION_ATLEAST(2,0,0) -typedef SDL_Rect SDL_RenderRect; -static inline SDL_RenderRect sdl_render_rect(int x, int y, +typedef SDL_Rect SDL_Rect_mzx; +static inline SDL_Rect_mzx sdl_render_rect(int x, int y, int w, int h, int full_w, int full_h) { SDL_Rect tmp = { x, y, w, h }; @@ -347,6 +355,10 @@ typedef int SDL_RendererLogicalPresentation; #define SDL_SetRenderClipRect(r, rect) SDL_RenderSetClipRect(r, rect) #define SDL_SetRenderLogicalSize(r, w, h) SDL_RenderSetLogicalSize(r, w, h) +#if !SDL_VERSION_ATLEAST(2,0,12) +typedef int SDL_ScaleMode; +#endif + static inline int SDL_SetRenderLogicalPresentation(SDL_Renderer *render, int w, int h, SDL_RendererLogicalPresentation p, SDL_ScaleMode s) { @@ -356,7 +368,7 @@ static inline int SDL_SetRenderLogicalPresentation(SDL_Renderer *render, #if SDL_VERSION_ATLEAST(2,0,0) static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *texture, - const SDL_RenderRect *src_rect, const SDL_RenderRect *dest_rect) + const SDL_Rect_mzx *src_rect, const SDL_Rect_mzx *dest_rect) { #if SDL_VERSION_ATLEAST(3,0,0) return SDL_RenderTexture(renderer, texture, src_rect, dest_rect); @@ -369,9 +381,10 @@ static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *tex /** * SDL_surface.h */ -#if !SDL_VERSION_ATLEAST(3,0,0) && SDL_VERSION_ATLEAST(2,0,0) +#if !SDL_VERSION_ATLEAST(3,0,0) #define SDL_DestroySurface(s) SDL_FreeSurface(s) +#if SDL_VERSION_ATLEAST(2,0,0) static inline SDL_Surface *SDL_CreateSurface(int width, int height, Uint32 format) { Uint32 rmask, gmask, bmask, amask; @@ -381,6 +394,7 @@ static inline SDL_Surface *SDL_CreateSurface(int width, int height, Uint32 forma return SDL_CreateRGBSurface(0, width, height, bpp, rmask, gmask, bmask, amask); } #endif +#endif /** * SDL_thread.h symbols were mostly the same for SDL 1 and SDL 2. @@ -400,6 +414,20 @@ typedef SDL_sem SDL_Semaphore; #define SDL_GetCurrentThreadID() SDL_ThreadID() #endif +/** + * SDL_version.h + */ +#if !SDL_VERSION_ATLEAST(3,0,0) +typedef SDL_version SDL_Version; +#endif + +/** + * SDL_video.h + */ +#if !SDL_VERSION_ATLEAST(2,0,16) +#define SDL_SetWindowMouseGrab(w,b) SDL_SetWindowGrab(w,b) +#endif + #endif /* CONFIG_SDL */ __M_END_DECLS diff --git a/src/about.c b/src/about.c index f5284c314..0f904b1e7 100644 --- a/src/about.c +++ b/src/about.c @@ -112,15 +112,15 @@ static char **about_text(int *num_lines) #ifdef CONFIG_SDL { - SDL_version compiled; - SDL_version ver; + SDL_Version compiled; + SDL_Version ver; SDL_VERSION(&compiled); #if SDL_VERSION_ATLEAST(2,0,0) SDL_GetVersion(&ver); #else ver = *SDL_Linked_Version(); #endif - if(memcmp(&compiled, &ver, sizeof(SDL_version))) + if(memcmp(&compiled, &ver, sizeof(SDL_Version))) { lines[i++] = about_line("SDL: %u.%u.%u (compiled: %u.%u.%u)", ver.major, ver.minor, ver.patch, compiled.major, compiled.minor, compiled.patch); diff --git a/src/graphics.c b/src/graphics.c index 67556fd82..722230abd 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -1489,6 +1489,19 @@ static boolean icon_w_h_constraint(png_uint_32 w, png_uint_32 h) return (w == h) && ((w % 16) == 0) && ((h % 16) == 0); } +#if SDL_VERSION_ATLEAST(2,0,0) +static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h, + png_uint_32 *stride, void **pixels) +{ + SDL_Surface *s = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_RGBA32); + if(!s) + return NULL; + + *stride = s->pitch; + *pixels = s->pixels; + return s; +} +#else static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h, png_uint_32 *stride, void **pixels) { @@ -1515,6 +1528,7 @@ static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h, *pixels = s->pixels; return s; } +#endif static SDL_Surface *png_read_icon(const char *name) { @@ -1528,7 +1542,7 @@ static void set_window_grab(boolean grabbed) { #ifdef CONFIG_SDL SDL_Window *window = SDL_GetWindowFromID(sdl_window_id); - SDL_SetWindowGrab(window, grabbed ? SDL_TRUE : SDL_FALSE); + SDL_SetWindowMouseGrab(window, grabbed ? SDL_TRUE : SDL_FALSE); #endif } @@ -1572,7 +1586,7 @@ static void set_window_icon(void) { SDL_Window *window = SDL_GetWindowFromID(sdl_window_id); SDL_SetWindowIcon(window, icon); - SDL_FreeSurface(icon); + SDL_DestroySurface(icon); } } #endif // CONFIG_PNG diff --git a/src/render_sdl.c b/src/render_sdl.c index 7025ed107..2902b435d 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -654,10 +654,19 @@ static void find_texture_format(struct graphics_data *graphics, if(!SDL_GetRendererInfo(render_data->renderer, &rinfo)) { unsigned int depth = graphics->bits_per_pixel; - unsigned int i; + int i; info("SDL render driver: '%s'\n", rinfo.name); +#if SDL_VERSION_ATLEAST(3,0,0) + if(!strcmp(rinfo.name, SDL_SOFTWARE_RENDERER)) +#else + if(rinfo.flags & SDL_RENDERER_SOFTWARE) +#endif + { + warn("Accelerated renderer not available. Rendering will be SLOW!\n"); + } + #ifdef __MACOSX__ // Not clear if Metal supports the custom Apple YUV texture format. if(!strcasecmp(rinfo.name, "opengl")) @@ -669,7 +678,7 @@ static void find_texture_format(struct graphics_data *graphics, need_alpha = true; // Try to use a native texture format to improve performance. - for(i = 0; i < rinfo.num_texture_formats; i++) + for(i = 0; i < (int)rinfo.num_texture_formats; i++) { uint32_t format = rinfo.texture_formats[i]; unsigned int format_priority; @@ -778,10 +787,17 @@ boolean sdlrender_set_video_mode(struct graphics_data *graphics, sdl_destruct_window(graphics); // Use linear filtering unless the display is being integer scaled. +#if SDL_VERSION_ATLEAST(2,0,12) + if(is_integer_scale(graphics, width, height)) + render_data->screen_scale_mode = SDL_SCALEMODE_NEAREST; + else + render_data->screen_scale_mode = SDL_SCALEMODE_LINEAR; +#else if(is_integer_scale(graphics, width, height)) SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); else SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); +#endif #if defined(__EMSCRIPTEN__) && SDL_VERSION_ATLEAST(2,0,10) // Not clear if this hint is required to make this renderer not crash, but @@ -816,23 +832,14 @@ boolean sdlrender_set_video_mode(struct graphics_data *graphics, goto err_free; } + // Note: previously attempted SDL_RENDERER_ACCELERATED first, then + // SDL_RENDERER_SOFTWARE, but these flags were removed in SDL3. render_data->renderer = - SDL_CreateRenderer(render_data->window, BEST_RENDERER, - SDL_RENDERER_ACCELERATED | sdl_rendererflags); - + SDL_CreateRenderer(render_data->window, BEST_RENDERER, sdl_rendererflags); if(!render_data->renderer) { - render_data->renderer = - SDL_CreateRenderer(render_data->window, BEST_RENDERER, - SDL_RENDERER_SOFTWARE | sdl_rendererflags); - - if(!render_data->renderer) - { - warn("Failed to create renderer: %s\n", SDL_GetError()); - goto err_free; - } - - warn("Accelerated renderer not available. Rendering will be SLOW!\n"); + warn("Failed to create renderer: %s\n", SDL_GetError()); + goto err_free; } find_texture_format(graphics, sdl_rendererflags); diff --git a/src/render_sdl.h b/src/render_sdl.h index 539c8284a..64b17bd3b 100644 --- a/src/render_sdl.h +++ b/src/render_sdl.h @@ -37,6 +37,7 @@ struct sdl_render_data SDL_Window *window; SDL_GLContext context; SDL_PixelFormat *pixel_format; + SDL_ScaleMode screen_scale_mode; #else SDL_Overlay *overlay; #endif diff --git a/src/render_sdlaccel.c b/src/render_sdlaccel.c index e362aa8f5..26de9c69c 100644 --- a/src/render_sdlaccel.c +++ b/src/render_sdlaccel.c @@ -35,6 +35,8 @@ #include "renderers.h" #include "render_sdl.h" +#include + // 6 versions of each char, 16*256 chars -> 24576 total "chars" // -> 49152 8x8s -> sqrt ~= 221 > 32 * 6 @@ -314,8 +316,10 @@ static boolean sdlaccel_set_video_mode(struct graphics_data *graphics, goto err_free; } +#if !SDL_VERSION_ATLEAST(2,0,12) // Always use nearest neighbor for the charset and background textures. SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); +#endif tex_chars_w = round_to_power_of_two(TEX_CHARS_PIX_W); tex_chars_h = round_to_power_of_two(TEX_CHARS_PIX_H); @@ -353,6 +357,15 @@ static boolean sdlaccel_set_video_mode(struct graphics_data *graphics, if(SDL_SetTextureBlendMode(texture[TEX_BACKGROUND], SDL_BLENDMODE_BLEND)) warn("Failed to set bg texture blend mode: %s\n", SDL_GetError()); +#if SDL_VERSION_ATLEAST(2,0,12) + if(SDL_SetTextureScaleMode(texture[TEX_SCREEN], render_data->sdl.screen_scale_mode)) + warn("Failed to set screen texture scale mode: %s\n", SDL_GetError()); + if(SDL_SetTextureScaleMode(texture[TEX_CHARS], SDL_SCALEMODE_NEAREST)) + warn("Failed to set char texture scale mode: %s\n", SDL_GetError()); + if(SDL_SetTextureScaleMode(texture[TEX_BACKGROUND], SDL_SCALEMODE_NEAREST)) + warn("Failed to set bg texture scale mode: %s\n", SDL_GetError()); +#endif + SDL_SetRenderTarget(render_data->sdl.renderer, texture[TEX_SCREEN]); render_data->w = width; render_data->h = height; @@ -489,8 +502,19 @@ static void sdlaccel_do_remap_chars(struct graphics_data *graphics, #ifdef RENDER_GEOMETRY static void vertex_char(struct SDL_Vertex *vertex, float topleft_x, - float topleft_y, float tex_x, float tex_y, SDL_Color sdl_color) + float topleft_y, float tex_x, float tex_y, SDL_Color _sdl_color) { +#if SDL_VERSION_ATLEAST(3,0,0) + SDL_FColor sdl_color = + { + _sdl_color.r / 255.0, + _sdl_color.g / 255.0, + _sdl_color.b / 255.0, + _sdl_color.a / 255.0, + }; +#else + SDL_Color sdl_color = _sdl_color; +#endif vertex[0].position.x = topleft_x; vertex[0].position.y = topleft_y; vertex[0].tex_coord.x = tex_x; @@ -550,9 +574,9 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, SDL_Texture *chars_tex = render_data->sdl.texture[TEX_CHARS]; SDL_Texture *bg_tex = render_data->sdl.texture[TEX_BACKGROUND]; - SDL_RenderRect dest_bg = + SDL_Rect_mzx dest_bg = sdl_render_rect(offx, offy, w * CHAR_W, h * CHAR_H, TEX_SCREEN_W, TEX_SCREEN_H); - SDL_RenderRect src_bg = sdl_render_rect(0, 0, w, h, TEX_BG_W, TEX_BG_H); + SDL_Rect_mzx src_bg = sdl_render_rect(0, 0, w, h, TEX_BG_W, TEX_BG_H); void *_bg; int bg_pitch; @@ -747,8 +771,8 @@ static void sdlaccel_sync_screen(struct graphics_data *graphics) struct sdlaccel_render_data *render_data = graphics->render_data; SDL_Renderer *renderer = render_data->sdl.renderer; SDL_Texture *screen_tex = render_data->sdl.texture[TEX_SCREEN]; - SDL_RenderRect src; - SDL_RenderRect dest; + SDL_Rect_mzx src; + SDL_Rect_mzx dest; int width = render_data->w; int height = render_data->h; int v_width, v_height; diff --git a/src/render_soft.c b/src/render_soft.c index 1225f5b30..8d5009b0d 100644 --- a/src/render_soft.c +++ b/src/render_soft.c @@ -59,7 +59,11 @@ static void soft_lock_buffer(struct soft_render_data *render_data, *pixels = (uint32_t *)screen->pixels; *pitch = screen->pitch; +#if SDL_VERSION_ATLEAST(3,0,0) + *bpp = screen->format->bytes_per_pixel * 8; +#else *bpp = screen->format->BytesPerPixel * 8; +#endif *pixels += *pitch * ((screen->h - 350) / 8); *pixels += (screen->w - 640) * *bpp / 64; diff --git a/src/render_softscale.c b/src/render_softscale.c index eeb129235..15087d579 100644 --- a/src/render_softscale.c +++ b/src/render_softscale.c @@ -92,9 +92,10 @@ static boolean softscale_set_video_mode(struct graphics_data *graphics, { struct softscale_render_data *render_data = (struct softscale_render_data *)graphics->render_data; + SDL_Texture *tex; if(!sdlrender_set_video_mode(graphics, width, height, - depth, fullscreen, resize, 0)) + depth, fullscreen, resize, false)) return false; // YUV texture modes are effectively 16-bit to SDL, but MegaZeux treats them @@ -107,16 +108,20 @@ static boolean softscale_set_video_mode(struct graphics_data *graphics, render_data->texture_pixels = NULL; // Initialize the screen texture. - render_data->sdl.texture[0] = - SDL_CreateTexture(render_data->sdl.renderer, render_data->sdl.texture_format, - SDL_TEXTUREACCESS_STREAMING, render_data->texture_width, SCREEN_PIX_H); - - if(!render_data->sdl.texture[0]) + tex = SDL_CreateTexture(render_data->sdl.renderer, render_data->sdl.texture_format, + SDL_TEXTUREACCESS_STREAMING, render_data->texture_width, SCREEN_PIX_H); + if(!tex) { warn("Failed to create texture: %s\n", SDL_GetError()); goto err_free; } +#if SDL_VERSION_ATLEAST(2,0,12) + if(SDL_SetTextureScaleMode(tex, render_data->sdl.screen_scale_mode)) + warn("Failed to set screen texture scale mode: %s\n", SDL_GetError()); +#endif + + render_data->sdl.texture[0] = tex; render_data->w = width; render_data->h = height; return true; @@ -265,8 +270,8 @@ static void softscale_sync_screen(struct graphics_data *graphics) SDL_Renderer *renderer = render_data->sdl.renderer; SDL_Texture *texture = render_data->sdl.texture[0]; SDL_Rect *texture_rect = &(render_data->texture_rect); - SDL_RenderRect src_rect; - SDL_RenderRect dest_rect; + SDL_Rect_mzx src_rect; + SDL_Rect_mzx dest_rect; int width = render_data->w; int height = render_data->h; int v_width, v_height; From c438a6e7add27ff86c9b45de142bdbe701aee4fb Mon Sep 17 00:00:00 2001 From: AliceLR Date: Mon, 15 Apr 2024 18:27:19 -0600 Subject: [PATCH 05/44] Initial (temporary) audio implementation. --- src/audio/audio_sdl.c | 55 ++++++++++++++++++++++++++++++++++++++++++- src/main.c | 4 ++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/audio/audio_sdl.c b/src/audio/audio_sdl.c index 710276a0a..f2d30e54e 100644 --- a/src/audio/audio_sdl.c +++ b/src/audio/audio_sdl.c @@ -34,6 +34,51 @@ static SDL_AudioSpec audio_settings; static SDL_AudioDeviceID audio_device; #endif +#if SDL_VERSION_ATLEAST(3,0,0) + +static SDL_AudioStream *audio_stream; +static void *audio_buffer; + +static void sdl_audio_callback(void *userdata, SDL_AudioStream *stream, + int required_bytes, int requested_bytes) +{ + // TODO: convert to frames/channels/format. + requested_bytes = (requested_bytes >> 2) << 2; + audio_callback((int16_t *)userdata, requested_bytes); +} + +// FIXME merge this into the older version. +void init_audio_platform(struct config_info *conf) +{ + void *tmp; + // TODO: query native spec from audio stream: SDL_GetAudioDeviceFormat + // TODO: configurable audio channels, default frequency setting + SDL_AudioSpec desired_spec = + { + SDL_AUDIO_S16, + 2, + audio.output_frequency, + }; + + // FIXME + tmp = crealloc(audio_buffer, conf->audio_buffer_samples * 4); + if(!tmp) + return; + + audio_device = 0; // FIXME + audio_settings = desired_spec; + audio_buffer = tmp; + audio_stream = + SDL_OpenAudioDeviceStream(0, &desired_spec, sdl_audio_callback, tmp); + + // FIXME this should be done by init_audio. + audio.mix_buffer = (int32_t *)cmalloc(conf->audio_buffer_samples * 8); + audio.buffer_samples = conf->audio_buffer_samples; + SDL_ResumeAudioDevice(audio_device); +} + +#else /* !SDL_VERSION_ATLEAST(3,0,0) */ + static void sdl_audio_callback(void *userdata, Uint8 *stream, int len) { audio_callback((int16_t *)stream, len); @@ -84,9 +129,17 @@ void init_audio_platform(struct config_info *conf) #endif } +#endif /* !SDL_VERSION_ATLEAST(3,0,0) */ + void quit_audio_platform(void) { -#if SDL_VERSION_ATLEAST(2,0,0) +#if SDL_VERSION_ATLEAST(3,0,0) + SDL_PauseAudioDevice(audio_device); + SDL_DestroyAudioStream(audio_stream); + free(audio_buffer); + audio_stream = NULL; + audio_buffer = NULL; +#elif SDL_VERSION_ATLEAST(2,0,0) SDL_PauseAudioDevice(audio_device, 1); #else SDL_PauseAudio(1); diff --git a/src/main.c b/src/main.c index af60a8ef9..5a787c757 100644 --- a/src/main.c +++ b/src/main.c @@ -55,8 +55,12 @@ #include "network/network.h" #ifdef CONFIG_SDL +#if CONFIG_SDL == 3 +#include +#else #include /* SDL_main */ #endif +#endif #ifndef VERSION #error Must define VERSION for MegaZeux version string From a29ffff9e954d3dd7e27b89450ab29666ff87685 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Tue, 16 Apr 2024 02:35:08 -0600 Subject: [PATCH 06/44] Fix Windows builds and regression tests for SDL3. --- src/SDLmzx.h | 2 +- src/main.c | 4 ++++ testworlds/run.sh | 4 ++++ unit/configure.cpp | 2 +- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 0b0f3de22..d97d9538e 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -163,7 +163,7 @@ static inline HWND SDL_GetWindowProperty_HWND(SDL_Window *window) static inline void *SDL_GetWindowProperty_HWND(SDL_Window *window) { return SDL_GetProperty(SDL_GetWindowProperties(window), - SDL_PROPERTY_WINDOW_WIN32_HWND_POINTER, NULL); + SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL); } #endif /* _WIN32 */ diff --git a/src/main.c b/src/main.c index 5a787c757..6116c5801 100644 --- a/src/main.c +++ b/src/main.c @@ -56,6 +56,10 @@ #ifdef CONFIG_SDL #if CONFIG_SDL == 3 +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#endif #include #else #include /* SDL_main */ diff --git a/testworlds/run.sh b/testworlds/run.sh index 48106892b..2f5f65dcb 100755 --- a/testworlds/run.sh +++ b/testworlds/run.sh @@ -82,8 +82,12 @@ fi # Coupled with the software renderer, this will disable video in MZX, speeding things up # and allowing for automated testing. Disabling the SDL audio driver will prevent annoying # noises from occuring during tests, but shouldn't affect audio-related tests. +# SDL 1.2 and SDL2 export SDL_VIDEODRIVER=dummy export SDL_AUDIODRIVER=dummy +# SDL3 +export SDL_VIDEO_DRIVER=dummy +export SDL_AUDIO_DRIVER=dummy # Standalone mode will allow tests.mzx to terminate MZX and no_titlescreen mode # simplifies things. Disable auto update checking to save time. Some platforms diff --git a/unit/configure.cpp b/unit/configure.cpp index adbc8ee58..a8d489a73 100644 --- a/unit/configure.cpp +++ b/unit/configure.cpp @@ -31,7 +31,7 @@ #include "../src/io/vio.h" #ifdef CONFIG_SDL -#include +#include "../src/SDLmzx.h" #endif #ifdef CONFIG_EDITOR From dffd522c5dccd80648ec3aa257e0dcd441dc8308 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Tue, 16 Apr 2024 02:49:19 -0600 Subject: [PATCH 07/44] Micro$haft Winblows --- unit/configure.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/unit/configure.cpp b/unit/configure.cpp index a8d489a73..de4f46446 100644 --- a/unit/configure.cpp +++ b/unit/configure.cpp @@ -32,6 +32,7 @@ #ifdef CONFIG_SDL #include "../src/SDLmzx.h" +#undef IGNORE /* wtf Windows? */ #endif #ifdef CONFIG_EDITOR From 4a0b5f0f0f66382a47a67ec2187abc7e379af919 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Tue, 16 Apr 2024 14:02:29 -0600 Subject: [PATCH 08/44] Better SDL3 pkg-config handling; default to pkgconf. --- Makefile | 20 ++++++++++++-------- arch/compat.inc | 9 +++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 0a751e82a..baee17199 100644 --- a/Makefile +++ b/Makefile @@ -87,18 +87,22 @@ ifneq (${BUILD_SDL},) # SDL 3 # ifeq (${BUILD_SDL},3) -# Check SDL_PREFIX and PREFIX for lib/pkgconfig/sdl3.pc. -ifneq ($(and ${SDL_PREFIX},$(wildcard ${SDL_PREFIX}/lib/pkgconfig/sdl3.pc)),) -SDL_PKGCONFIG ?= --with-path=${SDL_PREFIX}/lib/pkgconfig +# Check SDL_PKG_CONFIG_PATH and LIBDIR for pkgconfig/sdl3.pc. +# Note --with-path is a pkgconf extension. +ifneq ($(and ${SDL_PKG_CONFIG_PATH},$(wildcard ${SDL_PKG_CONFIG_PATH}/sdl3.pc)),) +# nop else -ifneq ($(wildcard ${PREFIX}/lib/pkgconfig/sdl3.pc),) -SDL_PKGCONFIG ?= --with-path=${PREFIX}/lib/pkgconfig +ifneq ($(wildcard ${LIBDIR}/pkgconfig/sdl3.pc),) +SDL_PKG_CONFIG_PATH ?= ${LIBDIR}/pkgconfig endif endif +ifneq (${SDL_PKG_CONFIG_DIR},) +SDL_PKG_CONFIG_FLAGS = --with-path=${SDL_PKG_CONFIG_PATH} +endif -SDL_PREFIX := $(shell pkg-config ${SDL_PKGCONFIG} sdl3 --variable=prefix) -SDL_CFLAGS ?= $(shell pkg-config ${SDL_PKGCONFIG} sdl3 --cflags) -SDL_LDFLAGS ?= $(shell pkg-config ${SDL_PKGCONFIG} sdl3 --libs) +SDL_PREFIX := $(shell ${PKGCONF} ${SDL_PKG_CONFIG_FLAGS} sdl3 --variable=prefix) +SDL_CFLAGS ?= $(shell ${PKGCONF} ${SDL_PKG_CONFIG_FLAGS} sdl3 --cflags) +SDL_LDFLAGS ?= $(shell ${PKGCONF} ${SDL_PKG_CONFIG_FLAGS} sdl3 --libs) endif # SDL3 # diff --git a/arch/compat.inc b/arch/compat.inc index dd164068b..e2e31bae8 100644 --- a/arch/compat.inc +++ b/arch/compat.inc @@ -150,3 +150,12 @@ ifeq (${CLANG_VER_GE_3_3},0) HAS_F_STACK_PROTECTOR_STRONG ?= 1 endif endif # clang + +# +# Get pkgconf or pkg-config (prefer pkgconf). +# +PKGCONF ?= ${shell \ + result=false; \ + command -v pkg-config >/dev/null && result=pkg-config; \ + command -v pkgconf >/dev/null && result=pkgconf; \ + echo $$result; } From adecbc292ac4577508370d4d2bc60c2be137d6d8 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Tue, 21 May 2024 19:48:23 -0600 Subject: [PATCH 09/44] Move SDL3 audio driver to its own compilation unit (still broken). --- src/Makefile.in | 4 ++ src/audio/audio_sdl.c | 57 +-------------------- src/audio/driver_sdl3.c | 109 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 56 deletions(-) create mode 100644 src/audio/driver_sdl3.c diff --git a/src/Makefile.in b/src/Makefile.in index e9f42001c..ec69129c2 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -361,8 +361,12 @@ core_cobjs += ${core_obj}/event_sdl.o ${core_obj}/platform_sdl.o core_cobjs += ${core_obj}/render_sdl.o ifeq (${BUILD_AUDIO},1) +ifeq (${BUILD_SDL},3) +core_cobjs += ${audio_obj}/driver_sdl3.o +else core_cobjs += ${audio_obj}/audio_sdl.o endif +endif core_flags += $(SDL_CFLAGS) core_ldflags += $(SDL_LDFLAGS) diff --git a/src/audio/audio_sdl.c b/src/audio/audio_sdl.c index 8e384a140..35db578cf 100644 --- a/src/audio/audio_sdl.c +++ b/src/audio/audio_sdl.c @@ -34,51 +34,6 @@ static SDL_AudioSpec audio_settings; static SDL_AudioDeviceID audio_device; #endif -#if SDL_VERSION_ATLEAST(3,0,0) - -static SDL_AudioStream *audio_stream; -static void *audio_buffer; - -static void sdl_audio_callback(void *userdata, SDL_AudioStream *stream, - int required_bytes, int requested_bytes) -{ - // TODO: convert to frames/channels/format. - requested_bytes = (requested_bytes >> 2) << 2; - audio_callback((int16_t *)userdata, requested_bytes); -} - -// FIXME merge this into the older version. -void init_audio_platform(struct config_info *conf) -{ - void *tmp; - // TODO: query native spec from audio stream: SDL_GetAudioDeviceFormat - // TODO: configurable audio channels, default frequency setting - SDL_AudioSpec desired_spec = - { - SDL_AUDIO_S16, - 2, - audio.output_frequency, - }; - - // FIXME - tmp = crealloc(audio_buffer, conf->audio_buffer_samples * 4); - if(!tmp) - return; - - audio_device = 0; // FIXME - audio_settings = desired_spec; - audio_buffer = tmp; - audio_stream = - SDL_OpenAudioDeviceStream(0, &desired_spec, sdl_audio_callback, tmp); - - // FIXME this should be done by init_audio. - audio.mix_buffer = (int32_t *)cmalloc(conf->audio_buffer_samples * 8); - audio.buffer_samples = conf->audio_buffer_samples; - SDL_ResumeAudioDevice(audio_device); -} - -#else /* !SDL_VERSION_ATLEAST(3,0,0) */ - static void sdl_audio_callback(void *userdata, Uint8 *stream, int len) { // TODO: non-stereo and 8-bit? @@ -142,19 +97,9 @@ void init_audio_platform(struct config_info *conf) #endif } -#endif /* !SDL_VERSION_ATLEAST(3,0,0) */ - void quit_audio_platform(void) { -#if SDL_VERSION_ATLEAST(3,0,0) - if(audio_stream) - { - SDL_DestroyAudioStream(audio_stream); - audio_stream = NULL; - } - free(audio_buffer); - audio_buffer = NULL; -#elif SDL_VERSION_ATLEAST(2,0,0) +#if SDL_VERSION_ATLEAST(2,0,0) if(audio_device) { SDL_PauseAudioDevice(audio_device, 1); diff --git a/src/audio/driver_sdl3.c b/src/audio/driver_sdl3.c new file mode 100644 index 000000000..e15d3179f --- /dev/null +++ b/src/audio/driver_sdl3.c @@ -0,0 +1,109 @@ +/* MegaZeux + * + * Copyright (C) 2024 Alice Rowan + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "../SDLmzx.h" +#include "../util.h" + +#include "audio.h" +#include "audio_struct.h" + +static SDL_AudioSpec audio_settings; +static SDL_AudioDeviceID audio_device; +static SDL_AudioStream *audio_stream; +static void *audio_buffer; +static unsigned audio_format; + +static void sdl_audio_callback(void *userdata, SDL_AudioStream *stream, + int required_bytes, int requested_bytes) +{ + size_t framesize = SDL_AUDIO_FRAMESIZE(audio_settings); + size_t frames = MAX(0, required_bytes) / framesize; + while(frames > 0) + { + size_t out = audio_mixer_render_frames(userdata, frames, + audio_settings.channels, audio_format); + + assert(out < frames); + SDL_PutAudioStreamData(audio_stream, userdata, out * framesize); + frames -= out; + } +} + +void init_audio_platform(struct config_info *conf) +{ + void *tmp; + // TODO: configurable audio channels, format + + audio_device = SDL_AUDIO_DEVICE_DEFAULT_OUTPUT; + audio_format = SAMPLE_S16; + + memset(&audio_settings, 0, sizeof(audio_settings)); + if(SDL_GetAudioDeviceFormat(audio_device, &audio_settings, NULL) < 0) + { + // Can't query, try to continue anyway... + audio_settings.freq = 48000; + } + audio_settings.format = SDL_AUDIO_S16; + audio_settings.channels = 2; + + if(conf->audio_sample_rate != 0) + { + // Reject very low sample rates to avoid PulseAudio hangs. + audio_settings.freq = MAX(conf->audio_sample_rate, 2048); + } + + if(!audio_mixer_init(audio_settings.freq, conf->audio_buffer_samples, + audio_settings.channels)) + return; + + audio_settings.freq = audio.output_frequency; + + tmp = crealloc(audio_buffer, + audio.buffer_frames * audio.buffer_channels * sizeof(int16_t)); + if(!tmp) + return; + + audio_buffer = tmp; + audio_stream = SDL_OpenAudioDeviceStream(audio_device, &audio_settings, + sdl_audio_callback, tmp); + if(!audio_stream) + goto err; + + SDL_ResumeAudioDevice(audio_device); + return; + +err: + free(tmp); + audio_buffer = NULL; + return; +} + +void quit_audio_platform(void) +{ + if(audio_stream) + { + SDL_DestroyAudioStream(audio_stream); + audio_stream = NULL; + } + free(audio_buffer); + audio_buffer = NULL; +} From b4f40f7aa2218dae86bafdd2e1a695df398d7660 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Tue, 21 May 2024 19:50:21 -0600 Subject: [PATCH 10/44] Fix SDL_PKG_CONFIG_PATH. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 93c7b949d..bed27e57a 100644 --- a/Makefile +++ b/Makefile @@ -96,7 +96,7 @@ ifneq ($(wildcard ${LIBDIR}/pkgconfig/sdl3.pc),) SDL_PKG_CONFIG_PATH ?= ${LIBDIR}/pkgconfig endif endif -ifneq (${SDL_PKG_CONFIG_DIR},) +ifneq (${SDL_PKG_CONFIG_PATH},) SDL_PKG_CONFIG_FLAGS = --with-path=${SDL_PKG_CONFIG_PATH} endif From 7ae338f1e6fcf559c5430864c95a59165e6c8e57 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 22 May 2024 03:46:48 -0600 Subject: [PATCH 11/44] Fix SDL3 audio (needs resampler bugfix). --- src/audio/driver_sdl3.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/audio/driver_sdl3.c b/src/audio/driver_sdl3.c index e15d3179f..69f5044e4 100644 --- a/src/audio/driver_sdl3.c +++ b/src/audio/driver_sdl3.c @@ -27,7 +27,6 @@ #include "audio_struct.h" static SDL_AudioSpec audio_settings; -static SDL_AudioDeviceID audio_device; static SDL_AudioStream *audio_stream; static void *audio_buffer; static unsigned audio_format; @@ -37,12 +36,13 @@ static void sdl_audio_callback(void *userdata, SDL_AudioStream *stream, { size_t framesize = SDL_AUDIO_FRAMESIZE(audio_settings); size_t frames = MAX(0, required_bytes) / framesize; + while(frames > 0) { size_t out = audio_mixer_render_frames(userdata, frames, audio_settings.channels, audio_format); - assert(out < frames); + assert(out <= frames); SDL_PutAudioStreamData(audio_stream, userdata, out * framesize); frames -= out; } @@ -50,10 +50,10 @@ static void sdl_audio_callback(void *userdata, SDL_AudioStream *stream, void init_audio_platform(struct config_info *conf) { + SDL_AudioDeviceID audio_device = SDL_AUDIO_DEVICE_DEFAULT_OUTPUT; void *tmp; // TODO: configurable audio channels, format - audio_device = SDL_AUDIO_DEVICE_DEFAULT_OUTPUT; audio_format = SAMPLE_S16; memset(&audio_settings, 0, sizeof(audio_settings)); @@ -88,7 +88,7 @@ void init_audio_platform(struct config_info *conf) if(!audio_stream) goto err; - SDL_ResumeAudioDevice(audio_device); + SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(audio_stream)); return; err: From 90330a7cd76be939f5d00df5f6235c5a7ca2fd52 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 22 May 2024 16:07:57 -0600 Subject: [PATCH 12/44] Update for latest 'main' version/renderer/keysym changes. --- src/SDLmzx.h | 26 +++++++++++++++++++++++++- src/about.c | 23 ++++++++++++----------- src/event_sdl.c | 4 ++-- src/render_sdl.c | 4 ++++ 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index d97d9538e..c7946d8f5 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -32,6 +32,7 @@ __M_BEGIN_DECLS #if CONFIG_SDL == 3 #include +#include #else #include #if defined(_WIN32) || defined(CONFIG_X11) @@ -284,6 +285,8 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) * SDL_keyboard.h and SDL_keycode.h */ #if !SDL_VERSION_ATLEAST(3,0,0) +#define SDLK_APOSTROPHE SDLK_QUOTE +#define SDLK_GRAVE SDLK_BACKQUOTE #define SDL_KMOD_CTRL KMOD_CTRL #define SDL_KMOD_ALT KMOD_ALT #define SDL_KMOD_NUM KMOD_NUM @@ -418,7 +421,28 @@ typedef SDL_sem SDL_Semaphore; * SDL_version.h */ #if !SDL_VERSION_ATLEAST(3,0,0) -typedef SDL_version SDL_Version; +#define SDL_MICRO_VERSION SDL_PATCHLEVEL + +#undef SDL_VERSIONNUM +#define SDL_VERSIONNUM(x,y,z) ((x) * 1000000 + (y) * 1000 + (z)) +#define SDL_VERSIONNUM_MAJOR(v) ((v) / 1000000) +#define SDL_VERSIONNUM_MINOR(v) (((v) / 1000) % 1000) +#define SDL_VERSIONNUM_MICRO(v) ((v) % 1000) + +#undef SDL_VERSION +#define SDL_VERSION SDL_VERSIONNUM(SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL) + +static inline int replace_SDL_GetVersion(void) +{ + SDL_version ver; +#if SDL_VERSION_ATLEAST(2,0,0) + SDL_GetVersion(&ver); +#else + ver = *SDL_Linked_Version(); +#endif + return SDL_VERSIONNUM(ver.major, ver.minor, ver.patch); +} +#define SDL_GetVersion replace_SDL_GetVersion #endif /** diff --git a/src/about.c b/src/about.c index 0f904b1e7..9861a950c 100644 --- a/src/about.c +++ b/src/about.c @@ -112,21 +112,22 @@ static char **about_text(int *num_lines) #ifdef CONFIG_SDL { - SDL_Version compiled; - SDL_Version ver; - SDL_VERSION(&compiled); -#if SDL_VERSION_ATLEAST(2,0,0) - SDL_GetVersion(&ver); -#else - ver = *SDL_Linked_Version(); -#endif - if(memcmp(&compiled, &ver, sizeof(SDL_Version))) + int ver_compiled = SDL_VERSION; + int ver_linked = SDL_GetVersion(); + + if(ver_compiled != ver_linked) { lines[i++] = about_line("SDL: %u.%u.%u (compiled: %u.%u.%u)", - ver.major, ver.minor, ver.patch, compiled.major, compiled.minor, compiled.patch); + SDL_VERSIONNUM_MAJOR(ver_linked), + SDL_VERSIONNUM_MINOR(ver_linked), + SDL_VERSIONNUM_MICRO(ver_linked), + SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_MICRO_VERSION); } else - lines[i++] = about_line("SDL: %u.%u.%u", ver.major, ver.minor, ver.patch); + { + lines[i++] = about_line("SDL: %u.%u.%u", + SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_MICRO_VERSION); + } } #endif diff --git a/src/event_sdl.c b/src/event_sdl.c index 5dbe3f35a..317c3bd4e 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -50,7 +50,7 @@ static enum keycode convert_SDL_internal(SDL_Keycode key) case SDLK_RETURN: return IKEY_RETURN; case SDLK_ESCAPE: return IKEY_ESCAPE; case SDLK_SPACE: return IKEY_SPACE; - case SDLK_QUOTE: return IKEY_QUOTE; + case SDLK_APOSTROPHE: return IKEY_QUOTE; case SDLK_PLUS: return IKEY_EQUALS; case SDLK_COMMA: return IKEY_COMMA; case SDLK_MINUS: return IKEY_MINUS; @@ -71,7 +71,7 @@ static enum keycode convert_SDL_internal(SDL_Keycode key) case SDLK_LEFTBRACKET: return IKEY_LEFTBRACKET; case SDLK_BACKSLASH: return IKEY_BACKSLASH; case SDLK_RIGHTBRACKET: return IKEY_RIGHTBRACKET; - case SDLK_BACKQUOTE: return IKEY_BACKQUOTE; + case SDLK_GRAVE: return IKEY_BACKQUOTE; case SDLK_a: return IKEY_a; case SDLK_b: return IKEY_b; case SDLK_c: return IKEY_c; diff --git a/src/render_sdl.c b/src/render_sdl.c index 2902b435d..e403eccc9 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -834,8 +834,12 @@ boolean sdlrender_set_video_mode(struct graphics_data *graphics, // Note: previously attempted SDL_RENDERER_ACCELERATED first, then // SDL_RENDERER_SOFTWARE, but these flags were removed in SDL3. +#if SDL_VERSION_ATLEAST(3,0,0) + render_data->renderer = SDL_CreateRenderer(render_data->window, BEST_RENDERER); +#else render_data->renderer = SDL_CreateRenderer(render_data->window, BEST_RENDERER, sdl_rendererflags); +#endif if(!render_data->renderer) { warn("Failed to create renderer: %s\n", SDL_GetError()); From d8ca233dd284703114808cb6c17f9f2f36c87556 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 22 May 2024 16:19:50 -0600 Subject: [PATCH 13/44] Add support for SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES (not that it matters, SDL ignores it...). --- src/audio/driver_sdl3.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/audio/driver_sdl3.c b/src/audio/driver_sdl3.c index 69f5044e4..d7e7ade85 100644 --- a/src/audio/driver_sdl3.c +++ b/src/audio/driver_sdl3.c @@ -51,16 +51,19 @@ static void sdl_audio_callback(void *userdata, SDL_AudioStream *stream, void init_audio_platform(struct config_info *conf) { SDL_AudioDeviceID audio_device = SDL_AUDIO_DEVICE_DEFAULT_OUTPUT; + int frames = 0; + char hint[16]; void *tmp; // TODO: configurable audio channels, format audio_format = SAMPLE_S16; memset(&audio_settings, 0, sizeof(audio_settings)); - if(SDL_GetAudioDeviceFormat(audio_device, &audio_settings, NULL) < 0) + if(SDL_GetAudioDeviceFormat(audio_device, &audio_settings, &frames) < 0) { // Can't query, try to continue anyway... audio_settings.freq = 48000; + frames = 1024; } audio_settings.format = SDL_AUDIO_S16; audio_settings.channels = 2; @@ -71,8 +74,10 @@ void init_audio_platform(struct config_info *conf) audio_settings.freq = MAX(conf->audio_sample_rate, 2048); } - if(!audio_mixer_init(audio_settings.freq, conf->audio_buffer_samples, - audio_settings.channels)) + if(conf->audio_buffer_samples != 0) + frames = conf->audio_buffer_samples; + + if(!audio_mixer_init(audio_settings.freq, frames, audio_settings.channels)) return; audio_settings.freq = audio.output_frequency; @@ -82,6 +87,12 @@ void init_audio_platform(struct config_info *conf) if(!tmp) return; + // The buffer frames need to be configured with this hack. + // This value may be ignored or modified by some platforms. + // FIXME: SDL drivers for ALSA, Pulseaudio, PipeWire, and JACK all ignore this... + snprintf(hint, sizeof(hint), "%u", audio.buffer_frames); + SDL_SetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, hint); + audio_buffer = tmp; audio_stream = SDL_OpenAudioDeviceStream(audio_device, &audio_settings, sdl_audio_callback, tmp); From 075220b8668e5d298b8b6f2fffc747f4cd317022 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Thu, 29 Aug 2024 19:08:17 -0600 Subject: [PATCH 14/44] Partial fixes to update to SDL3 main (8/29). --- src/SDLmzx.h | 48 ++++++++++++++++++++++++--- src/event_sdl.c | 85 ++++++++++++++++++++++++++---------------------- src/render_sdl.c | 14 ++++---- src/render_sdl.h | 2 +- 4 files changed, 100 insertions(+), 49 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index c7946d8f5..ebd42cebc 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -183,7 +183,7 @@ static inline void *SDL_GetWindowProperty_HWND(SDL_Window *window) #define SDL_AUDIO_S16LE AUDIO_S16LSB #define SDL_AUDIO_S16BE AUDIO_S16MSB #else -/* SDL3 defines SDL_FreeWAV to an error type, but MZX needs to keep it to +/* SDL3 defines SDL_FreeWAV to an old name type, but MZX needs to keep it to * transparently support older versions. */ #undef SDL_FreeWAV #define SDL_FreeWAV(w) SDL_free(w) @@ -269,11 +269,13 @@ static inline void SDL_SetGamepadEventsEnabled(SDL_bool enabled) * SDL_joystick.h */ #if !SDL_VERSION_ATLEAST(3,0,0) +#define SDL_GUID SDL_JoystickGUID +#define SDL_GUIDToString SDL_GetJoystickGUIDString #define SDL_OpenJoystick(device_id) SDL_JoystickOpen(device_id) #define SDL_CloseJoystick(j) SDL_JoystickClose(j) #define SDL_GetJoystickGUID(j) SDL_JoystickGetGUID(j) #define SDL_GetJoystickGUIDString(g,b,l) SDL_JoystickGetGUIDString(g,b,l) -#define SDL_GetJoystickInstanceID(j) SDL_JoystickInstanceID(j) +#define SDL_GetJoystickID(j) SDL_JoystickInstanceID(j) static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) { @@ -285,6 +287,32 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) * SDL_keyboard.h and SDL_keycode.h */ #if !SDL_VERSION_ATLEAST(3,0,0) +#define SDLK_A SDLK_a +#define SDLK_B SDLK_b +#define SDLK_C SDLK_c +#define SDLK_D SDLK_d +#define SDLK_E SDLK_e +#define SDLK_F SDLK_f +#define SDLK_G SDLK_g +#define SDLK_H SDLK_h +#define SDLK_I SDLK_i +#define SDLK_J SDLK_j +#define SDLK_K SDLK_k +#define SDLK_L SDLK_l +#define SDLK_M SDLK_m +#define SDLK_N SDLK_n +#define SDLK_O SDLK_o +#define SDLK_P SDLK_p +#define SDLK_Q SDLK_q +#define SDLK_R SDLK_r +#define SDLK_S SDLK_s +#define SDLK_T SDLK_t +#define SDLK_U SDLK_u +#define SDLK_V SDLK_v +#define SDLK_W SDLK_w +#define SDLK_X SDLK_x +#define SDLK_Y SDLK_y +#define SDLK_Z SDLK_z #define SDLK_APOSTROPHE SDLK_QUOTE #define SDLK_GRAVE SDLK_BACKQUOTE #define SDL_KMOD_CTRL KMOD_CTRL @@ -298,13 +326,18 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) * SDL_pixels.h */ #if !SDL_VERSION_ATLEAST(3,0,0) +#define SDL_PIXELFORMAT_XRGB4444 SDL_PIXELFORMAT_RGB444 +#define SDL_PIXELFORMAT_XBGR4444 SDL_PIXELFORMAT_BGR444 +#define SDL_PIXELFORMAT_XRGB1555 SDL_PIXELFORMAT_RGB555 +#define SDL_PIXELFORMAT_XBGR1555 SDL_PIXELFORMAT_BGR555 #define SDL_PIXELFORMAT_XRGB8888 SDL_PIXELFORMAT_RGB888 #define SDL_PIXELFORMAT_XBGR8888 SDL_PIXELFORMAT_BGR888 +#define SDL_PixelFormatDetails SDL_PixelFormat #define SDL_CreatePalette(s) SDL_AllocPalette(s) #define SDL_DestroyPalette(p) SDL_FreePalette(p) -#define SDL_CreatePixelFormat(f) SDL_AllocFormat(f) +#define SDL_GetPixelFormatDetails(f) SDL_AllocFormat(f) #define SDL_DestroyPixelFormat(pf) SDL_FreeFormat(pf) -#define SDL_GetMasksForPixelFormatEnum(f,b,R,G,B,A) SDL_PixelFormatEnumToMasks(f,b,R,G,B,A) +#define SDL_GetMasksForPixelFormat(f,b,R,G,B,A) SDL_PixelFormatEnumToMasks(f,b,R,G,B,A) #if !SDL_VERSION_ATLEAST(2,0,5) #if SDL_BYTEORDER == SDL_BIG_ENDIAN @@ -313,6 +346,12 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) #define SDL_PIXELFORMAT_RGBA32 SDL_PIXELFORMAT_ABGR8888 #endif #endif + +static inline Uint32 SDL_MapSurfaceRGBA(SDL_Surface *surface, + Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + return SDL_MapRGBA(surface->format, r, g, b, a); +} #endif /** @@ -449,6 +488,7 @@ static inline int replace_SDL_GetVersion(void) * SDL_video.h */ #if !SDL_VERSION_ATLEAST(2,0,16) +#define SDL_GL_DestroyContext(gl) SDL_GL_DeleteContext(gl) #define SDL_SetWindowMouseGrab(w,b) SDL_SetWindowGrab(w,b) #endif diff --git a/src/event_sdl.c b/src/event_sdl.c index 317c3bd4e..d8802185c 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -72,32 +72,32 @@ static enum keycode convert_SDL_internal(SDL_Keycode key) case SDLK_BACKSLASH: return IKEY_BACKSLASH; case SDLK_RIGHTBRACKET: return IKEY_RIGHTBRACKET; case SDLK_GRAVE: return IKEY_BACKQUOTE; - case SDLK_a: return IKEY_a; - case SDLK_b: return IKEY_b; - case SDLK_c: return IKEY_c; - case SDLK_d: return IKEY_d; - case SDLK_e: return IKEY_e; - case SDLK_f: return IKEY_f; - case SDLK_g: return IKEY_g; - case SDLK_h: return IKEY_h; - case SDLK_i: return IKEY_i; - case SDLK_j: return IKEY_j; - case SDLK_k: return IKEY_k; - case SDLK_l: return IKEY_l; - case SDLK_m: return IKEY_m; - case SDLK_n: return IKEY_n; - case SDLK_o: return IKEY_o; - case SDLK_p: return IKEY_p; - case SDLK_q: return IKEY_q; - case SDLK_r: return IKEY_r; - case SDLK_s: return IKEY_s; - case SDLK_t: return IKEY_t; - case SDLK_u: return IKEY_u; - case SDLK_v: return IKEY_v; - case SDLK_w: return IKEY_w; - case SDLK_x: return IKEY_x; - case SDLK_y: return IKEY_y; - case SDLK_z: return IKEY_z; + case SDLK_A: return IKEY_a; + case SDLK_B: return IKEY_b; + case SDLK_C: return IKEY_c; + case SDLK_D: return IKEY_d; + case SDLK_E: return IKEY_e; + case SDLK_F: return IKEY_f; + case SDLK_G: return IKEY_g; + case SDLK_H: return IKEY_h; + case SDLK_I: return IKEY_i; + case SDLK_J: return IKEY_j; + case SDLK_K: return IKEY_k; + case SDLK_L: return IKEY_l; + case SDLK_M: return IKEY_m; + case SDLK_N: return IKEY_n; + case SDLK_O: return IKEY_o; + case SDLK_P: return IKEY_p; + case SDLK_Q: return IKEY_q; + case SDLK_R: return IKEY_r; + case SDLK_S: return IKEY_s; + case SDLK_T: return IKEY_t; + case SDLK_U: return IKEY_u; + case SDLK_V: return IKEY_v; + case SDLK_W: return IKEY_w; + case SDLK_X: return IKEY_x; + case SDLK_Y: return IKEY_y; + case SDLK_Z: return IKEY_z; case SDLK_DELETE: return IKEY_DELETE; case SDLK_KP_0: return IKEY_KP0; case SDLK_KP_1: return IKEY_KP1; @@ -657,10 +657,10 @@ static void parse_gamepad_map(int joystick_index, char *map) static void init_gamepad(SDL_Joystick *joystick, int sdl_joystick_id, int joystick_index) { - SDL_JoystickGUID guid = SDL_GetJoystickGUID(joystick); + SDL_GUID guid = SDL_GetJoystickGUID(joystick); char guid_string[33]; - SDL_GetJoystickGUIDString(guid, guid_string, 33); + SDL_GUIDToString(guid, guid_string, 33); gamepads[joystick_index] = NULL; if(SDL_IsGamepad(sdl_joystick_id)) @@ -676,7 +676,7 @@ static void init_gamepad(SDL_Joystick *joystick, int sdl_joystick_id, #if SDL_VERSION_ATLEAST(3,0,0) // This is the equivalent to [...]ForDeviceIndex() and is also currently // the only way to get the default generated mapping. - mapping = (char *)SDL_GetGamepadInstanceMapping(sdl_joystick_id); + mapping = (char *)SDL_GetGamepadMappingForID(sdl_joystick_id); #elif SDL_VERSION_ATLEAST(2,0,9) // NOTE: the other functions for this will not return the default mapping // string; this is the only one that can return everything. Right now, @@ -878,7 +878,7 @@ static void init_joystick(int sdl_joystick_id) SDL_Joystick *joystick = SDL_OpenJoystick(sdl_joystick_id); if(joystick) { - joystick_instance_ids[joystick_index] = SDL_GetJoystickInstanceID(joystick); + joystick_instance_ids[joystick_index] = SDL_GetJoystickID(joystick); joysticks[joystick_index] = joystick; joystick_set_active(status, joystick_index, true); @@ -977,7 +977,7 @@ static boolean process_event(SDL_Event *event) /* Enable converting keycodes to fake unicode presses when text input isn't * active. Enabling text input also enables an onscreen keyboard in some * ports, so it isn't always desired. */ - boolean unicode_fallback = !SDL_TextInputActive(); + boolean unicode_fallback = !SDL_TextInputActive(NULL); // FIXME #else /* SDL 1.2 might also need this (Pandora? doesn't generate unicode presses). */ static boolean unicode_fallback = true; @@ -1212,6 +1212,15 @@ static boolean process_event(SDL_Event *event) } #endif // SDL_VERSION_ATLEAST(2,0,0) +// TODO: this is kind of tacky +#if SDL_VERSION_ATLEAST(3,0,0) +#define FIELD_KEY key +#define FIELD_SCANCODE scancode +#else +#define FIELD_KEY keysym.sym +#define FIELD_SCANCODE keysym.scancode +#endif + case SDL_EVENT_KEY_DOWN: { uint32_t unicode = 0; @@ -1231,7 +1240,7 @@ static boolean process_event(SDL_Event *event) #ifdef CONFIG_PANDORA { // Pandora hack. Certain keys are actually joystick buttons. - int button = get_pandora_joystick_button(event->key.keysym.sym); + int button = get_pandora_joystick_button(event->key.FIELD_KEY); if(button >= 0) { joystick_button_press(status, 0, button); @@ -1240,11 +1249,11 @@ static boolean process_event(SDL_Event *event) } #endif - ckey = convert_SDL_internal(event->key.keysym.sym); + ckey = convert_SDL_internal(event->key.FIELD_KEY); trace( "--EVENT_SDL-- SDL_EVENT_KEY_DOWN: scancode:%d sym:%d -> %d\n", - event->key.keysym.scancode, - event->key.keysym.sym, + event->key.FIELD_SCANCODE, + event->key.FIELD_KEY, ckey ); if(!ckey) @@ -1350,11 +1359,11 @@ static boolean process_event(SDL_Event *event) } #endif - ckey = convert_SDL_internal(event->key.keysym.sym); + ckey = convert_SDL_internal(event->key.FIELD_KEY); trace( "--EVENT_SDL-- SDL_EVENT_KEY_UP: scancode:%d sym:%d -> %d\n", - event->key.keysym.scancode, - event->key.keysym.sym, + event->key.FIELD_SCANCODE, + event->key.FIELD_KEY, ckey ); if(!ckey) diff --git a/src/render_sdl.c b/src/render_sdl.c index e403eccc9..8d6d359e2 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -246,9 +246,9 @@ static uint32_t sdl_pixel_format_priority(uint32_t pixel_format, } #if SDL_VERSION_ATLEAST(2,0,12) - case SDL_PIXELFORMAT_BGR444: + case SDL_PIXELFORMAT_XBGR4444: #endif - case SDL_PIXELFORMAT_RGB444: + case SDL_PIXELFORMAT_XRGB4444: case SDL_PIXELFORMAT_ARGB4444: case SDL_PIXELFORMAT_RGBA4444: case SDL_PIXELFORMAT_ABGR4444: @@ -261,8 +261,8 @@ static uint32_t sdl_pixel_format_priority(uint32_t pixel_format, break; } - case SDL_PIXELFORMAT_RGB555: - case SDL_PIXELFORMAT_BGR555: + case SDL_PIXELFORMAT_XRGB1555: + case SDL_PIXELFORMAT_XBGR1555: case SDL_PIXELFORMAT_ARGB1555: case SDL_PIXELFORMAT_RGBA5551: case SDL_PIXELFORMAT_ABGR1555: @@ -409,7 +409,9 @@ void sdl_destruct_window(struct graphics_data *graphics) // Used for generating mapped colors for the SDL_Renderer renderers. if(render_data->pixel_format) { - SDL_DestroyPixelFormat(render_data->pixel_format); +#if !SDL_VERSION_ATLEAST(3,0,0) + SDL_FreeFormat(render_data->pixel_format); +#endif render_data->pixel_format = NULL; } @@ -434,7 +436,7 @@ void sdl_destruct_window(struct graphics_data *graphics) // Used by the OpenGL renderers. if(render_data->context) { - SDL_GL_DeleteContext(render_data->context); + SDL_GL_DestroyContext(render_data->context); render_data->context = NULL; } diff --git a/src/render_sdl.h b/src/render_sdl.h index 64b17bd3b..62f930b87 100644 --- a/src/render_sdl.h +++ b/src/render_sdl.h @@ -36,7 +36,7 @@ struct sdl_render_data SDL_Palette *palette; SDL_Window *window; SDL_GLContext context; - SDL_PixelFormat *pixel_format; + SDL_PixelFormatDetails *pixel_format; SDL_ScaleMode screen_scale_mode; #else SDL_Overlay *overlay; From 62c973605dc3d91a6179d7bc62dc2e639335633b Mon Sep 17 00:00:00 2001 From: AliceLR Date: Thu, 29 Aug 2024 21:18:48 -0600 Subject: [PATCH 15/44] Fix SDL1.2 and SDL2 builds. --- src/SDLmzx.h | 13 +++++++++---- src/editor/clipboard_x11.c | 2 +- src/event_sdl.c | 4 ++++ src/render_sdl.c | 12 ++++++++---- src/render_sdl.h | 2 +- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index ebd42cebc..63fdedde4 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -319,7 +319,7 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) #define SDL_KMOD_ALT KMOD_ALT #define SDL_KMOD_NUM KMOD_NUM #define SDL_KMOD_CAPS KMOD_CAPS -#define SDL_TextInputActive() SDL_IsTextInputActive() +#define SDL_TextInputActive(w) SDL_IsTextInputActive() #endif /** @@ -335,8 +335,6 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) #define SDL_PixelFormatDetails SDL_PixelFormat #define SDL_CreatePalette(s) SDL_AllocPalette(s) #define SDL_DestroyPalette(p) SDL_FreePalette(p) -#define SDL_GetPixelFormatDetails(f) SDL_AllocFormat(f) -#define SDL_DestroyPixelFormat(pf) SDL_FreeFormat(pf) #define SDL_GetMasksForPixelFormat(f,b,R,G,B,A) SDL_PixelFormatEnumToMasks(f,b,R,G,B,A) #if !SDL_VERSION_ATLEAST(2,0,5) @@ -468,6 +466,11 @@ typedef SDL_sem SDL_Semaphore; #define SDL_VERSIONNUM_MINOR(v) (((v) / 1000) % 1000) #define SDL_VERSIONNUM_MICRO(v) ((v) % 1000) +static inline void SDL_VERSION_ORIG(SDL_version *v) +{ + SDL_VERSION(v); +} + #undef SDL_VERSION #define SDL_VERSION SDL_VERSIONNUM(SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL) @@ -488,9 +491,11 @@ static inline int replace_SDL_GetVersion(void) * SDL_video.h */ #if !SDL_VERSION_ATLEAST(2,0,16) -#define SDL_GL_DestroyContext(gl) SDL_GL_DeleteContext(gl) #define SDL_SetWindowMouseGrab(w,b) SDL_SetWindowGrab(w,b) #endif +#if !SDL_VERSION_ATLEAST(3,0,0) +#define SDL_GL_DestroyContext(gl) SDL_GL_DeleteContext(gl) +#endif #endif /* CONFIG_SDL */ diff --git a/src/editor/clipboard_x11.c b/src/editor/clipboard_x11.c index a66d84264..a06d9522c 100644 --- a/src/editor/clipboard_x11.c +++ b/src/editor/clipboard_x11.c @@ -43,7 +43,7 @@ static inline boolean get_X11_display_and_window(SDL_Window *window, Display **display, Window *xwindow) { SDL_SysWMinfo info; - SDL_VERSION(&info.version); + SDL_VERSION_ORIG(&info.version); if(!window) window = SDL_GetWindowFromID(sdl_window_id); diff --git a/src/event_sdl.c b/src/event_sdl.c index d8802185c..373d40f1c 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -1215,10 +1215,14 @@ static boolean process_event(SDL_Event *event) // TODO: this is kind of tacky #if SDL_VERSION_ATLEAST(3,0,0) #define FIELD_KEY key +#ifdef DEBUG_TRACE #define FIELD_SCANCODE scancode +#endif #else #define FIELD_KEY keysym.sym +#ifdef DEBUG_TRACE #define FIELD_SCANCODE keysym.scancode +#endif #endif case SDL_EVENT_KEY_DOWN: diff --git a/src/render_sdl.c b/src/render_sdl.c index cc517e3a7..c76c395c0 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -406,14 +406,14 @@ void sdl_destruct_window(struct graphics_data *graphics) render_data->palette = NULL; } +#if !SDL_VERSION_ATLEAST(3,0,0) // Used for generating mapped colors for the SDL_Renderer renderers. if(render_data->pixel_format) { -#if !SDL_VERSION_ATLEAST(3,0,0) SDL_FreeFormat(render_data->pixel_format); -#endif render_data->pixel_format = NULL; } +#endif // Used by the SDL renderer-based renderers for HW acceleration. for(i = 0; i < ARRAY_SIZE(render_data->texture); i++) @@ -711,7 +711,7 @@ static uint32_t get_format_amask(uint32_t format) Uint32 rmask, gmask, bmask, amask; int bpp; - SDL_GetMasksForPixelFormatEnum(format, &bpp, &rmask, &gmask, &bmask, &amask); + SDL_GetMasksForPixelFormat(format, &bpp, &rmask, &gmask, &bmask, &amask); return amask; } @@ -927,14 +927,18 @@ boolean sdlrender_set_video_mode(struct graphics_data *graphics, if(!render_data->rgb_to_yuv) { +#if SDL_VERSION_ATLEAST(3,0,0) +#error wtf +#else // This is required for SDL_MapRGBA to work, but YUV formats can ignore it. - render_data->pixel_format = SDL_CreatePixelFormat(render_data->texture_format); + render_data->pixel_format = SDL_AllocFormat(render_data->texture_format); if(!render_data->pixel_format) { warn("Failed to allocate pixel format: %s\n", SDL_GetError()); goto err_free; } render_data->flat_format = render_data->pixel_format; +#endif } SDL_SetRenderDrawColor(render_data->renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); diff --git a/src/render_sdl.h b/src/render_sdl.h index 3cf6479cc..d029fcf8f 100644 --- a/src/render_sdl.h +++ b/src/render_sdl.h @@ -44,7 +44,7 @@ struct sdl_render_data SDL_Surface *screen; SDL_Surface *shadow; SDL_Color *palette_colors; - const SDL_PixelFormat *flat_format; // format used by sdl_update_colors. + const SDL_PixelFormatDetails *flat_format; // format used by sdl_update_colors. // SDL Renderer and overlay renderer texture format configuration. uint32_t (*rgb_to_yuv)(uint8_t r, uint8_t g, uint8_t b); From d4c0b6fc496475fbeef6911db10dfbb78213dbf7 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Thu, 29 Aug 2024 21:23:23 -0600 Subject: [PATCH 16/44] Add missing include to event_sdl.c. --- src/event_sdl.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/event_sdl.c b/src/event_sdl.c index 373d40f1c..4da15e10f 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -23,6 +23,7 @@ #include "configure.h" #include "event.h" #include "graphics.h" +#include "platform.h" #include "render_sdl.h" #include "util.h" From 7f1ee51695a10dee6a2be31ff36dc6b2cd1549e8 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Thu, 29 Aug 2024 23:44:40 -0600 Subject: [PATCH 17/44] More fixes for current SDL3 main. --- src/SDLmzx.h | 6 ++++ src/event_sdl.c | 10 ++++--- src/platform_sdl.c | 15 ++++++++-- src/render_gp2x.c | 8 ++++-- src/render_sdl.c | 70 +++++++++++++++++++++++++++++++++++----------- src/render_soft.c | 13 +++++---- 6 files changed, 90 insertions(+), 32 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 63fdedde4..6e1869e4c 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -424,6 +424,12 @@ static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *tex #if !SDL_VERSION_ATLEAST(3,0,0) #define SDL_DestroySurface(s) SDL_FreeSurface(s) +static inline SDL_bool SDL_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect) +{ + *rect = surface->clip_rect; + return SDL_TRUE; +} + #if SDL_VERSION_ATLEAST(2,0,0) static inline SDL_Surface *SDL_CreateSurface(int width, int height, Uint32 format) { diff --git a/src/event_sdl.c b/src/event_sdl.c index 4da15e10f..06571f0cc 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -1216,11 +1216,13 @@ static boolean process_event(SDL_Event *event) // TODO: this is kind of tacky #if SDL_VERSION_ATLEAST(3,0,0) #define FIELD_KEY key +#define FIELD_MOD mod #ifdef DEBUG_TRACE #define FIELD_SCANCODE scancode #endif #else #define FIELD_KEY keysym.sym +#define FIELD_MOD keysym.mod #ifdef DEBUG_TRACE #define FIELD_SCANCODE keysym.scancode #endif @@ -1355,7 +1357,7 @@ static boolean process_event(SDL_Event *event) #ifdef CONFIG_PANDORA { // Pandora hack. Certain keys are actually joystick buttons. - int button = get_pandora_joystick_button(event->key.keysym.sym); + int button = get_pandora_joystick_button(event->key.FIELD_KEY); if(button >= 0) { joystick_button_release(status, 0, button); @@ -1661,13 +1663,13 @@ boolean __peek_exit_input(void) { SDL_KeyboardEvent *ev = &(events[i].key); - if(ev->keysym.sym == SDLK_ESCAPE) + if(ev->FIELD_KEY == SDLK_ESCAPE) return true; - if(ev->keysym.sym == SDLK_c && (ev->keysym.mod & SDL_KMOD_CTRL)) + if(ev->FIELD_KEY == SDLK_C && (ev->FIELD_MOD & SDL_KMOD_CTRL)) return true; - if(ev->keysym.sym == SDLK_F4 && (ev->keysym.mod & SDL_KMOD_ALT)) + if(ev->FIELD_KEY == SDLK_F4 && (ev->FIELD_MOD & SDL_KMOD_ALT)) return true; } } diff --git a/src/platform_sdl.c b/src/platform_sdl.c index 56a92d32c..d3280b5c0 100644 --- a/src/platform_sdl.c +++ b/src/platform_sdl.c @@ -108,6 +108,15 @@ static void set_dpi_aware(void) } #endif +static inline boolean sdl_init(Uint32 flags) +{ +#if SDL_VERSION_ATLEAST(3,0,0) + return SDL_Init(flags); +#else + return SDL_Init(flags) >= 0; +#endif +} + boolean platform_init(void) { Uint32 flags = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK; @@ -138,7 +147,7 @@ boolean platform_init(void) set_dpi_aware(); #endif - if(SDL_Init(flags) < 0) + if(!sdl_init(flags)) { debug("Failed to initialize SDL; attempting with joystick support disabled: %s\n", SDL_GetError()); @@ -148,7 +157,7 @@ boolean platform_init(void) flags &= ~SDL_INIT_GAMEPAD; #endif - if(SDL_Init(flags) < 0) + if(!sdl_init(flags)) { warn("Failed to initialize SDL: %s\n", SDL_GetError()); return false; @@ -169,7 +178,7 @@ boolean platform_init(void) * TODO: this probably redundant with behavior already in SDL. */ if(!SDL_HasScreenKeyboardSupport()) - SDL_StartTextInput(); + SDL_StartTextInput(NULL); // FIXME #else SDL_EnableUNICODE(1); #endif diff --git a/src/render_gp2x.c b/src/render_gp2x.c index a3791055b..32bd31bd9 100644 --- a/src/render_gp2x.c +++ b/src/render_gp2x.c @@ -187,7 +187,7 @@ static boolean gp2x_set_video_mode(struct graphics_data *graphics, int width, int height, int depth, boolean fullscreen, boolean resize) { struct gp2x_render_data *render_data = graphics->render_data; - const SDL_PixelFormat *format; + const SDL_PixelFormatDetails *format; uint32_t halfmask; if(!sdl_set_video_mode(graphics, width, height, depth, fullscreen, resize)) @@ -283,8 +283,10 @@ static void gp2x_sync_screen(struct graphics_data *graphics) if(render_data->sdl.shadow) { - SDL_Rect src_rect = render_data->sdl.shadow->clip_rect; - SDL_Rect dest_rect = render_data->sdl.screen->clip_rect; + SDL_Rect src_rect; + SDL_Rect dest_rect; + SDL_GetSurfaceClipRect(render_data->sdl.shadow, &src_rect); + SDL_GetSurfaceClipRect(render_data->sdl.screen, &dest_rect); SDL_BlitSurface(render_data->sdl.shadow, &src_rect, render_data->sdl.screen, &dest_rect); } diff --git a/src/render_sdl.c b/src/render_sdl.c index c76c395c0..45d008583 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -25,6 +25,7 @@ #include "yuv.h" #include +#include CORE_LIBSPEC Uint32 sdl_window_id; @@ -88,7 +89,7 @@ static boolean sdl_get_desktop_display_mode(SDL_DisplayMode *display_mode) } warn("Failed to query desktop display mode: %s\n", SDL_GetError()); - list = SDL_GetFullscreenDisplayModes(0, &count); + list = (const SDL_DisplayMode **)SDL_GetFullscreenDisplayModes(0, &count); if(list) { if(count) @@ -128,7 +129,8 @@ static boolean sdl_get_smallest_usable_display_mode(SDL_DisplayMode *display_mod int count; int i; - const SDL_DisplayMode **list = SDL_GetFullscreenDisplayModes(0, &count); + const SDL_DisplayMode **list = + (const SDL_DisplayMode **)SDL_GetFullscreenDisplayModes(0, &count); if(!list) return false; @@ -510,9 +512,15 @@ void sdl_update_colors(struct graphics_data *graphics, return; for(i = 0; i < count; i++) { +#if SDL_VERSION_ATLEAST(3,0,0) + graphics->flat_intensity_palette[i] = + SDL_MapRGBA(render_data->flat_format, NULL, + palette[i].r, palette[i].g, palette[i].b, SDL_ALPHA_OPAQUE); +#else graphics->flat_intensity_palette[i] = SDL_MapRGBA(render_data->flat_format, palette[i].r, palette[i].g, palette[i].b, SDL_ALPHA_OPAQUE); +#endif } } else @@ -536,7 +544,7 @@ boolean sdl_set_video_mode(struct graphics_data *graphics, int width, struct sdl_render_data *render_data = graphics->render_data; #if SDL_VERSION_ATLEAST(2,0,0) - SDL_PixelFormat *format; + SDL_PixelFormatDetails *format; boolean fullscreen_windowed = graphics->fullscreen_windowed; boolean matched = false; Uint32 fmt; @@ -726,27 +734,57 @@ static void find_texture_format(struct graphics_data *graphics, boolean need_alpha = false; uint32_t yuv_priority = YUV_PRIORITY; uint32_t priority = 0; + boolean is_software_renderer = false; + const char *renderer_name; + const uint32_t *formats = NULL; + int num_formats; + +#if SDL_VERSION_ATLEAST(3,0,0) + + SDL_PropertiesID props = SDL_GetRendererProperties(render_data->renderer); + const uint32_t *pos; + + renderer_name = SDL_GetRendererName(render_data->renderer); + if(!strcmp(renderer_name, SDL_SOFTWARE_RENDERER)) + is_software_renderer = true; + + // thanks for the michael mouse API + formats = (const uint32_t *)SDL_GetPointerProperty(props, + SDL_PROP_RENDERER_TEXTURE_FORMATS_POINTER, NULL); + num_formats = 0; + for(pos = formats; pos && *pos != SDL_PIXELFORMAT_UNKNOWN; pos++) + num_formats++; + +#else + SDL_RendererInfo rinfo; if(!SDL_GetRendererInfo(render_data->renderer, &rinfo)) + { + renderer_name = rinfo.name; + num_formats = rinfo.num_texture_formats; + formats = rinfo.texture_formats; + + if(rinfo.flags & SDL_RENDERER_SOFTWARE) + is_software_renderer = true; + } + else + warn("Failed to get renderer info!\n"); + +#endif /* !SDL_VERSION_ATLEAST(3,0,0) */ + + if(formats) { unsigned int depth = graphics->bits_per_pixel; int i; - info("SDL render driver: '%s'\n", rinfo.name); - -#if SDL_VERSION_ATLEAST(3,0,0) - if(!strcmp(rinfo.name, SDL_SOFTWARE_RENDERER)) -#else - if(rinfo.flags & SDL_RENDERER_SOFTWARE) -#endif - { + info("SDL render driver: '%s'\n", renderer_name); + if(is_software_renderer) warn("Accelerated renderer not available. Rendering will be SLOW!\n"); - } #ifdef __MACOSX__ // Not clear if Metal supports the custom Apple YUV texture format. - if(!strcasecmp(rinfo.name, "opengl")) + if(!strcasecmp(renderer_name, "opengl")) yuv_priority = YUV_PRIORITY_APPLE; #endif @@ -755,9 +793,9 @@ static void find_texture_format(struct graphics_data *graphics, need_alpha = true; // Try to use a native texture format to improve performance. - for(i = 0; i < (int)rinfo.num_texture_formats; i++) + for(i = 0; i < num_formats; i++) { - uint32_t format = rinfo.texture_formats[i]; + uint32_t format = formats[i]; unsigned int format_priority; debug("%d: %s\n", i, SDL_GetPixelFormatName(format)); @@ -775,8 +813,6 @@ static void find_texture_format(struct graphics_data *graphics, } } } - else - warn("Failed to get renderer info!\n"); if(texture_format == SDL_PIXELFORMAT_UNKNOWN) { diff --git a/src/render_soft.c b/src/render_soft.c index 320b6814b..5d52643a1 100644 --- a/src/render_soft.c +++ b/src/render_soft.c @@ -54,21 +54,22 @@ static SDL_Surface *soft_get_screen_surface(struct soft_render_data *_render_dat static void soft_lock_buffer(struct soft_render_data *render_data, uint32_t **pixels, unsigned *pitch, unsigned *bpp, uint32_t *amask) { + const SDL_PixelFormatDetails *format = render_data->sdl.flat_format; SDL_Surface *screen = soft_get_screen_surface(render_data); *pixels = (uint32_t *)screen->pixels; *pitch = screen->pitch; #if SDL_VERSION_ATLEAST(3,0,0) - *bpp = screen->format->bytes_per_pixel * 8; + *bpp = format->bytes_per_pixel * 8; #else - *bpp = screen->format->BytesPerPixel * 8; + *bpp = format->BytesPerPixel * 8; #endif *pixels += *pitch * ((screen->h - 350) / 8); *pixels += (screen->w - 640) * *bpp / 64; if(amask) - *amask = screen->format->Amask; + *amask = format->Amask; SDL_LockSurface(screen); } @@ -279,8 +280,10 @@ static void soft_sync_screen(struct graphics_data *graphics) if(render_data->shadow) { - SDL_Rect src_rect = render_data->shadow->clip_rect; - SDL_Rect dest_rect = render_data->screen->clip_rect; + SDL_Rect src_rect; + SDL_Rect dest_rect; + SDL_GetSurfaceClipRect(render_data->shadow, &src_rect); + SDL_GetSurfaceClipRect(render_data->screen, &dest_rect); SDL_BlitSurface(render_data->shadow, &src_rect, render_data->screen, &dest_rect); } From 9cdfcc0c245f3ac21962cb7bbadf343c3d575131 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Fri, 30 Aug 2024 13:59:00 -0600 Subject: [PATCH 18/44] Update MZX to work with SDL3 8ea38ebe. --- src/SDLmzx.h | 1 - src/audio/driver_sdl3.c | 4 +-- src/editor/clipboard_sdl2.c | 4 +++ src/event_sdl.c | 6 +++-- src/platform_sdl.c | 6 +++-- src/render_sdl.c | 54 +++++++++++++++++++++++++++---------- 6 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 6e1869e4c..03651ee49 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -319,7 +319,6 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) #define SDL_KMOD_ALT KMOD_ALT #define SDL_KMOD_NUM KMOD_NUM #define SDL_KMOD_CAPS KMOD_CAPS -#define SDL_TextInputActive(w) SDL_IsTextInputActive() #endif /** diff --git a/src/audio/driver_sdl3.c b/src/audio/driver_sdl3.c index d7e7ade85..cd8b46eb7 100644 --- a/src/audio/driver_sdl3.c +++ b/src/audio/driver_sdl3.c @@ -50,7 +50,7 @@ static void sdl_audio_callback(void *userdata, SDL_AudioStream *stream, void init_audio_platform(struct config_info *conf) { - SDL_AudioDeviceID audio_device = SDL_AUDIO_DEVICE_DEFAULT_OUTPUT; + SDL_AudioDeviceID audio_device = SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK; int frames = 0; char hint[16]; void *tmp; @@ -59,7 +59,7 @@ void init_audio_platform(struct config_info *conf) audio_format = SAMPLE_S16; memset(&audio_settings, 0, sizeof(audio_settings)); - if(SDL_GetAudioDeviceFormat(audio_device, &audio_settings, &frames) < 0) + if(!SDL_GetAudioDeviceFormat(audio_device, &audio_settings, &frames)) { // Can't query, try to continue anyway... audio_settings.freq = 48000; diff --git a/src/editor/clipboard_sdl2.c b/src/editor/clipboard_sdl2.c index 41fd2a394..05ed11658 100644 --- a/src/editor/clipboard_sdl2.c +++ b/src/editor/clipboard_sdl2.c @@ -44,7 +44,11 @@ void copy_buffer_to_clipboard(char **buffer, int lines, int total_length) } dest_ptr[-1] = 0; +#if SDL_VERSION_ATLEAST(3,0,0) + if(!SDL_SetClipboardText(dest_data)) +#else if(SDL_SetClipboardText(dest_data) < 0) +#endif warn("SDL_SetClipboardText failed: %s\n", SDL_GetError()); free(dest_data); diff --git a/src/event_sdl.c b/src/event_sdl.c index 06571f0cc..284596772 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -974,11 +974,13 @@ static boolean process_event(SDL_Event *event) struct buffered_status *status = store_status(); enum keycode ckey; -#if SDL_VERSION_ATLEAST(2,0,0) +#if SDL_VERSION_ATLEAST(3,0,0) + boolean unicode_fallback = false; // FIXME: this is per-window now +#elif SDL_VERSION_ATLEAST(2,0,0) /* Enable converting keycodes to fake unicode presses when text input isn't * active. Enabling text input also enables an onscreen keyboard in some * ports, so it isn't always desired. */ - boolean unicode_fallback = !SDL_TextInputActive(NULL); // FIXME + boolean unicode_fallback = !SDL_IsTextInputActive(); #else /* SDL 1.2 might also need this (Pandora? doesn't generate unicode presses). */ static boolean unicode_fallback = true; diff --git a/src/platform_sdl.c b/src/platform_sdl.c index d3280b5c0..17d0e3e91 100644 --- a/src/platform_sdl.c +++ b/src/platform_sdl.c @@ -164,7 +164,9 @@ boolean platform_init(void) } } -#if SDL_VERSION_ATLEAST(2,0,0) +#if SDL_VERSION_ATLEAST(3,0,0) + // FIXME: Can't do this here, because it is per-window. +#elif SDL_VERSION_ATLEAST(2,0,0) /* Most platforms want text input events always on so they can generate * convenient unicode text values, but in Android this causes some problems: * @@ -178,7 +180,7 @@ boolean platform_init(void) * TODO: this probably redundant with behavior already in SDL. */ if(!SDL_HasScreenKeyboardSupport()) - SDL_StartTextInput(NULL); // FIXME + SDL_StartTextInput(); #else SDL_EnableUNICODE(1); #endif diff --git a/src/render_sdl.c b/src/render_sdl.c index 45d008583..ef2d29ec2 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -81,7 +81,11 @@ static boolean sdl_get_desktop_display_mode(SDL_DisplayMode *display_mode) const SDL_DisplayMode *mode; int count; - mode = SDL_GetDesktopDisplayMode(0); + SDL_DisplayID id = SDL_GetPrimaryDisplay(); + if(id == 0) + return false; + + mode = SDL_GetDesktopDisplayMode(id); if(mode) { *display_mode = *mode; @@ -129,8 +133,12 @@ static boolean sdl_get_smallest_usable_display_mode(SDL_DisplayMode *display_mod int count; int i; - const SDL_DisplayMode **list = - (const SDL_DisplayMode **)SDL_GetFullscreenDisplayModes(0, &count); + const SDL_DisplayMode **list; + SDL_DisplayID id = SDL_GetPrimaryDisplay(); + if(id == 0) + return false; + + list = (const SDL_DisplayMode **)SDL_GetFullscreenDisplayModes(id, &count); if(!list) return false; @@ -402,9 +410,12 @@ void sdl_destruct_window(struct graphics_data *graphics) } // Used for 8bpp support for the software renderer. + // This is attached to the surface in SDL3 and should not be destroyed. if(render_data->palette) { +#if !SDL_VERSION_ATLEAST(3,0,0) SDL_DestroyPalette(render_data->palette); +#endif render_data->palette = NULL; } @@ -544,7 +555,7 @@ boolean sdl_set_video_mode(struct graphics_data *graphics, int width, struct sdl_render_data *render_data = graphics->render_data; #if SDL_VERSION_ATLEAST(2,0,0) - SDL_PixelFormatDetails *format; + SDL_Surface *target; boolean fullscreen_windowed = graphics->fullscreen_windowed; boolean matched = false; Uint32 fmt; @@ -620,35 +631,49 @@ boolean sdl_set_video_mode(struct graphics_data *graphics, int width, render_data->shadow = NULL; } - format = render_data->shadow ? render_data->shadow->format : - render_data->screen->format; - render_data->flat_format = format; + target = render_data->shadow ? render_data->shadow : render_data->screen; +#if SDL_VERSION_ATLEAST(3,0,0) + render_data->flat_format = SDL_GetPixelFormatDetails(target->format); +#else + render_data->flat_format = target->format; +#endif if(fmt == SDL_PIXELFORMAT_INDEX8) { - render_data->palette = SDL_CreatePalette(SMZX_PAL_SIZE); +#if SDL_VERSION_ATLEAST(3,0,0) + render_data->palette = SDL_CreateSurfacePalette(target); if(!render_data->palette) { warn("Failed to allocate palette: %s\n", SDL_GetError()); goto err_free; } - render_data->palette_colors = - (SDL_Color *)ccalloc(SMZX_PAL_SIZE, sizeof(SDL_Color)); - if(!render_data->palette_colors) +#else + render_data->palette = SDL_CreatePalette(SMZX_PAL_SIZE); + if(!render_data->palette) { - warn("Failed to allocate palette colors\n"); + warn("Failed to allocate palette: %s\n", SDL_GetError()); goto err_free; } - if(SDL_SetPixelFormatPalette(format, render_data->palette)) + if(SDL_SetPixelFormatPalette(target->format, render_data->palette)) { warn("Failed to set pixel format palette: %s\n", SDL_GetError()); goto err_free; } +#endif + + render_data->palette_colors = + (SDL_Color *)ccalloc(SMZX_PAL_SIZE, sizeof(SDL_Color)); + if(!render_data->palette_colors) + { + warn("Failed to allocate palette colors\n"); + goto err_free; + } } else { render_data->palette = NULL; + render_data->palette_colors = NULL; } sdl_window_id = SDL_GetWindowID(render_data->window); @@ -964,7 +989,8 @@ boolean sdlrender_set_video_mode(struct graphics_data *graphics, if(!render_data->rgb_to_yuv) { #if SDL_VERSION_ATLEAST(3,0,0) -#error wtf + render_data->flat_format = + SDL_GetPixelFormatDetails(render_data->texture_format); #else // This is required for SDL_MapRGBA to work, but YUV formats can ignore it. render_data->pixel_format = SDL_AllocFormat(render_data->texture_format); From c0d4e56143104ff5f36358f9e426b79192a392a7 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Fri, 1 Nov 2024 18:41:06 -0600 Subject: [PATCH 19/44] Fixes for SDL 3.1.6; fix softscale and sdlaccel; fix resizing; fix OpenGL. --- src/SDLmzx.h | 17 +++++++---------- src/event_sdl.c | 25 ++++++++++++++----------- src/render_sdl.c | 21 +++++++++++++++------ src/render_sdl.h | 8 +++++++- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 5669423f0..61a1fb263 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -220,7 +220,7 @@ typedef SDL_GameControllerButton SDL_GamepadButton; #define SDL_GAMEPAD_AXIS_RIGHTY SDL_CONTROLLER_AXIS_RIGHTY #define SDL_GAMEPAD_AXIS_LEFT_TRIGGER SDL_CONTROLLER_AXIS_TRIGGERLEFT #define SDL_GAMEPAD_AXIS_RIGHT_TRIGGER SDL_CONTROLLER_AXIS_TRIGGERRIGHT -#define SDL_GAMEPAD_AXIS_MAX SDL_CONTROLLER_AXIS_MAX +#define SDL_GAMEPAD_AXIS_COUNT SDL_CONTROLLER_AXIS_MAX #define SDL_GAMEPAD_BUTTON_INVALID SDL_CONTROLLER_BUTTON_INVALID #define SDL_GAMEPAD_BUTTON_SOUTH SDL_CONTROLLER_BUTTON_A #define SDL_GAMEPAD_BUTTON_EAST SDL_CONTROLLER_BUTTON_B @@ -243,7 +243,7 @@ typedef SDL_GameControllerButton SDL_GamepadButton; #define SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2 SDL_CONTROLLER_BUTTON_PADDLE3 #define SDL_GAMEPAD_BUTTON_LEFT_PADDLE2 SDL_CONTROLLER_BUTTON_PADDLE4 #define SDL_GAMEPAD_BUTTON_TOUCHPAD SDL_CONTROLLER_BUTTON_TOUCHPAD -#define SDL_GAMEPAD_BUTTON_MAX SDL_CONTROLLER_BUTTON_MAX +#define SDL_GAMEPAD_BUTTON_COUNT SDL_CONTROLLER_BUTTON_MAX #define SDL_IsGamepad(device_id) SDL_IsGameController(device_id) #define SDL_OpenGamepad(device_id) SDL_GameControllerOpen(device_id) #define SDL_CloseGamepad(g) SDL_GameControllerClose(g) @@ -356,13 +356,7 @@ typedef SDL_FRect SDL_Rect_mzx; static inline SDL_Rect_mzx sdl_render_rect(int x, int y, int w, int h, int full_w, int full_h) { - SDL_FRect tmp = - { - (float)x / full_w, - (float)y / full_h, - (float)w / full_w, - (float)h / full_h - }; + SDL_FRect tmp = { (float)x, (float)y, (float)w, (float)h }; return tmp; } #elif SDL_VERSION_ATLEAST(2,0,0) @@ -414,7 +408,10 @@ static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *tex * SDL_surface.h */ #if !SDL_VERSION_ATLEAST(3,0,0) -#define SDL_DestroySurface(s) SDL_FreeSurface(s) +#define SDL_SCALEMODE_NEAREST SDL_ScaleModeNearest +#define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear +#define SDL_DestroySurface(s) SDL_FreeSurface(s) +#define SDL_FillSurfaceRect(s,r,c) SDL_FillRect(s,r,c) static inline SDL_bool SDL_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect) { diff --git a/src/event_sdl.c b/src/event_sdl.c index c5979c69a..32a3b85f0 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -256,7 +256,7 @@ static int get_pandora_joystick_button(SDL_Keycode key) static SDL_Gamepad *gamepads[MAX_JOYSTICKS]; -static enum joystick_special_axis sdl_axis_map[SDL_GAMEPAD_AXIS_MAX] = +static enum joystick_special_axis sdl_axis_map[SDL_GAMEPAD_AXIS_COUNT] = { [SDL_GAMEPAD_AXIS_LEFTX] = JOY_AXIS_LEFT_X, [SDL_GAMEPAD_AXIS_LEFTY] = JOY_AXIS_LEFT_Y, @@ -266,7 +266,7 @@ static enum joystick_special_axis sdl_axis_map[SDL_GAMEPAD_AXIS_MAX] = [SDL_GAMEPAD_AXIS_RIGHT_TRIGGER] = JOY_AXIS_RIGHT_TRIGGER }; -static int16_t sdl_axis_action_map[SDL_GAMEPAD_AXIS_MAX][2] = +static int16_t sdl_axis_action_map[SDL_GAMEPAD_AXIS_COUNT][2] = { [SDL_GAMEPAD_AXIS_LEFTX] = { -JOY_L_LEFT, -JOY_L_RIGHT }, [SDL_GAMEPAD_AXIS_LEFTY] = { -JOY_L_UP, -JOY_L_DOWN }, @@ -276,7 +276,7 @@ static int16_t sdl_axis_action_map[SDL_GAMEPAD_AXIS_MAX][2] = [SDL_GAMEPAD_AXIS_RIGHT_TRIGGER] = { 0, -JOY_RTRIGGER }, }; -static int16_t sdl_action_map[SDL_GAMEPAD_BUTTON_MAX] = +static int16_t sdl_action_map[SDL_GAMEPAD_BUTTON_COUNT] = { [SDL_GAMEPAD_BUTTON_SOUTH] = -JOY_A, [SDL_GAMEPAD_BUTTON_EAST] = -JOY_B, @@ -612,8 +612,8 @@ static void parse_gamepad_apply(int joy, int16_t mapping, static void parse_gamepad_map(int joystick_index, char *map) { - struct gamepad_axis_map axes[SDL_GAMEPAD_AXIS_MAX]; - struct gamepad_map buttons[SDL_GAMEPAD_BUTTON_MAX]; + struct gamepad_axis_map axes[SDL_GAMEPAD_AXIS_COUNT]; + struct gamepad_map buttons[SDL_GAMEPAD_BUTTON_COUNT]; boolean select_mapped = false; boolean select_used = false; size_t i; @@ -624,7 +624,7 @@ static void parse_gamepad_map(int joystick_index, char *map) parse_gamepad_read_string(map, axes, buttons); // Apply axes. - for(i = 0; i < SDL_GAMEPAD_AXIS_MAX; i++) + for(i = 0; i < SDL_GAMEPAD_AXIS_COUNT; i++) { parse_gamepad_apply(joystick_index, sdl_axis_action_map[i][0], &(axes[i].neg), &select_mapped, &select_used); @@ -634,7 +634,7 @@ static void parse_gamepad_map(int joystick_index, char *map) } // Apply buttons. - for(i = 0; i < SDL_GAMEPAD_BUTTON_MAX; i++) + for(i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; i++) { parse_gamepad_apply(joystick_index, sdl_action_map[i], &(buttons[i]), &select_mapped, &select_used); @@ -1016,8 +1016,11 @@ static boolean process_event(SDL_Event *event) #if SDL_VERSION_ATLEAST(3,0,0) case SDL_EVENT_WINDOW_RESIZED: { - trace("--EVENT_SDL-- SDL_EVENT_WINDOW_RESIZED: (%u,%u)\n"); - video_sync_window_size(event->window.windowID, + unsigned window_id = video_window_by_platform_id(event->window.windowID); + + trace("--EVENT_SDL-- SDL_EVENT_WINDOW_RESIZED: %u (%d,%d)\n", + event->window.windowID, event->window.data1, event->window.data2); + video_sync_window_size(window_id, event->window.data1, event->window.data2); break; } @@ -1759,9 +1762,9 @@ void initialize_joysticks(void) #endif #if SDL_VERSION_ATLEAST(2,0,0) - SDL_SetGamepadEventsEnabled(SDL_TRUE); + SDL_SetGamepadEventsEnabled(true); load_gamecontrollerdb(); #endif - SDL_SetJoystickEventsEnabled(SDL_TRUE); + SDL_SetJoystickEventsEnabled(true); } diff --git a/src/render_sdl.c b/src/render_sdl.c index 136e39f14..232e3db8f 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -71,7 +71,8 @@ int sdl_flags(const struct video_window *window) if(window->is_fullscreen) { -#if SDL_VERSION_ATLEAST(2,0,0) +#if SDL_VERSION_ATLEAST(2,0,0) && !SDL_VERSION_ATLEAST(3,0,0) + /* SDL3 removed the ability to specify this at window creation time. */ if(window->is_fullscreen_windowed) flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; else @@ -768,7 +769,7 @@ boolean sdl_create_window_soft(struct graphics_data *graphics, #endif // !SDL_VERSION_ATLEAST(2,0,0) // Wipe the letterbox area, if any. - SDL_FillRect(render_data->screen, NULL, 0); + SDL_FillSurfaceRect(render_data->screen, NULL, 0); sdl_set_system_cursor(graphics); sdl_set_window_grab(render_data, window->grab_mouse); @@ -1157,11 +1158,15 @@ void sdl_set_texture_scale_mode(struct graphics_data *graphics, { SDL_ScaleMode mode; if(!allow_non_integer || window->is_integer_scaled) - mode = SDL_ScaleModeNearest; + mode = SDL_SCALEMODE_NEAREST; else - mode = SDL_ScaleModeLinear; + mode = SDL_SCALEMODE_LINEAR; - if(SDL_SetTextureScaleMode(render_data->texture[texture_id], mode)) +#if SDL_VERSION_ATLEAST(3,0,0) + if(!SDL_SetTextureScaleMode(render_data->texture[texture_id], mode)) +#else + if(SDL_SetTextureScaleMode(render_data->texture[texture_id], mode) < 0) +#endif warn("Failed to set texture %d scale mode: %s\n", texture_id, SDL_GetError()); } else @@ -1241,7 +1246,11 @@ boolean gl_create_window(struct graphics_data *graphics, goto err_free; } - if(SDL_GL_MakeCurrent(render_data->window, render_data->context)) +#if SDL_VERSION_ATLEAST(3,0,0) + if(!SDL_GL_MakeCurrent(render_data->window, render_data->context)) +#else + if(SDL_GL_MakeCurrent(render_data->window, render_data->context) != 0) +#endif { warn("Failed to make context current: %s\n", SDL_GetError()); goto err_free; diff --git a/src/render_sdl.h b/src/render_sdl.h index 469bdd09a..61c614c1b 100644 --- a/src/render_sdl.h +++ b/src/render_sdl.h @@ -117,7 +117,13 @@ static inline void gl_cleanup(struct graphics_data *graphics) static inline boolean GL_LoadLibrary(enum gl_lib_type type) { - if(!SDL_GL_LoadLibrary(NULL)) return true; +#if SDL_VERSION_ATLEAST(3,0,0) + if(SDL_GL_LoadLibrary(NULL)) +#else + if(SDL_GL_LoadLibrary(NULL) == 0) +#endif + return true; + #if !SDL_VERSION_ATLEAST(2,0,0) // If the context already exists, don't reload the library // This is for SDL 1.2 which doesn't let us unload OpenGL From bc26e3e62ae39fd0c05b999fc8fafefb46974d96 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Sat, 2 Nov 2024 02:00:06 -0600 Subject: [PATCH 20/44] Add audio_output_channels support to the SDL3 audio driver. --- src/audio/driver_sdl3.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/audio/driver_sdl3.c b/src/audio/driver_sdl3.c index cd8b46eb7..2c17b6c32 100644 --- a/src/audio/driver_sdl3.c +++ b/src/audio/driver_sdl3.c @@ -54,7 +54,7 @@ void init_audio_platform(struct config_info *conf) int frames = 0; char hint[16]; void *tmp; - // TODO: configurable audio channels, format + // TODO: 8-bit? audio_format = SAMPLE_S16; @@ -77,6 +77,9 @@ void init_audio_platform(struct config_info *conf) if(conf->audio_buffer_samples != 0) frames = conf->audio_buffer_samples; + if(conf->audio_output_channels != 0) + audio_settings.channels = MIN(conf->audio_output_channels, 2); + if(!audio_mixer_init(audio_settings.freq, frames, audio_settings.channels)) return; From 2476de018011538f88749b8e745f464563e3b67d Mon Sep 17 00:00:00 2001 From: AliceLR Date: Sun, 3 Nov 2024 13:48:08 -0700 Subject: [PATCH 21/44] Windows (partial) fixes for SDL 3.1.6. --- src/SDLmzx.h | 4 ++-- src/thread_sdl.h | 30 ++++++++++++++++-------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 61a1fb263..ba671a39b 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -156,7 +156,7 @@ static inline HWND SDL_GetWindowProperty_HWND(SDL_Window *window) #ifdef _WIN32 static inline void *SDL_GetWindowProperty_HWND(SDL_Window *window) { - return SDL_GetProperty(SDL_GetWindowProperties(window), + return SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL); } #endif /* _WIN32 */ @@ -445,7 +445,7 @@ typedef SDL_sem SDL_Semaphore; #define SDL_SignalCondition(c) SDL_CondSignal(c) #define SDL_BroadcastCondition(c) SDL_CondBroadcast(c) #define SDL_WaitSemaphore(s) SDL_SemWait(s) -#define SDL_PostSemaphore(s) SDL_SemPost(s) +#define SDL_SignalSemaphore(s) SDL_SemPost(s) #define SDL_GetCurrentThreadID() SDL_ThreadID() #endif diff --git a/src/thread_sdl.h b/src/thread_sdl.h index 0dddbf666..c01c021a6 100644 --- a/src/thread_sdl.h +++ b/src/thread_sdl.h @@ -61,13 +61,15 @@ static inline boolean platform_mutex_destroy(platform_mutex *mutex) static inline boolean platform_mutex_lock(platform_mutex *mutex) { - SDL_LockMutex(*mutex); // Returns void as of SDL 3. + // Returns void as of SDL 3. + SDL_LockMutex(*mutex); return true; } static inline boolean platform_mutex_unlock(platform_mutex *mutex) { - SDL_UnlockMutex(*mutex); // Returns void as of SDL 3. + // Returns void as of SDL 3. + SDL_UnlockMutex(*mutex); return true; } @@ -91,30 +93,30 @@ static inline boolean platform_cond_destroy(platform_cond *cond) static inline boolean platform_cond_wait(platform_cond *cond, platform_mutex *mutex) { - if(SDL_WaitCondition(*cond, *mutex)) - return false; + // Returns void as of SDL 3. + SDL_WaitCondition(*cond, *mutex); return true; } static inline boolean platform_cond_timedwait(platform_cond *cond, platform_mutex *mutex, unsigned int timeout_ms) { - if(SDL_WaitConditionTimeout(*cond, *mutex, (Uint32)timeout_ms)) - return false; + // Returns void as of SDL 3. + SDL_WaitConditionTimeout(*cond, *mutex, (Uint32)timeout_ms); return true; } static inline boolean platform_cond_signal(platform_cond *cond) { - if(SDL_SignalCondition(*cond)) - return false; + // Returns void as of SDL 3. + SDL_SignalCondition(*cond); return true; } static inline boolean platform_cond_broadcast(platform_cond *cond) { - if(SDL_BroadcastCondition(*cond)) - return false; + // Returns void as of SDL 3. + SDL_BroadcastCondition(*cond); return true; } @@ -137,15 +139,15 @@ static inline boolean platform_sem_destroy(platform_sem *sem) static inline boolean platform_sem_wait(platform_sem *sem) { - if(SDL_WaitSemaphore(*sem)) - return false; + // Returns void as of SDL 3. + SDL_WaitSemaphore(*sem); return true; } static inline boolean platform_sem_post(platform_sem *sem) { - if(SDL_PostSemaphore(*sem)) - return false; + // Returns void as of SDL 3. + SDL_SignalSemaphore(*sem); return true; } From b94746f16bedfc4b6f405662a5c826facb8c7723 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Sun, 3 Nov 2024 15:32:32 -0700 Subject: [PATCH 22/44] Do not define SDL_FUNCTION_POINTER_IS_VOID_POINTER --- src/SDLmzx.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index ba671a39b..f8ac8b2b2 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -26,10 +26,6 @@ __M_BEGIN_DECLS #if defined(CONFIG_SDL) - -/* TODO: SDL3 hack so the dso code doesn't need to be overhauled (again). */ -#define SDL_FUNCTION_POINTER_IS_VOID_POINTER - #if CONFIG_SDL == 3 #include #include From 35dca23870084dc9d3c2c88af048cbbc66f7d7a5 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Sun, 3 Nov 2024 22:19:27 -0700 Subject: [PATCH 23/44] Enable text events for SDL3. --- src/event_sdl.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/event_sdl.c b/src/event_sdl.c index da6075274..a41e287c4 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -1727,7 +1727,6 @@ void __warp_mouse(int x, int y) */ void sdl_init_window_text_events(unsigned sdl_window_id) { -#if SDL_VERSION_ATLEAST(2,0,0) /* Most platforms want text input events always on so they can generate * convenient unicode text values, but in Android this causes some problems: * @@ -1739,6 +1738,17 @@ void sdl_init_window_text_events(unsigned sdl_window_id) * * TODO: Instead, enable text input on demand at text prompts. */ +#if SDL_VERSION_ATLEAST(3,0,0) +#ifdef ANDROID +/* This is largely wishful thinking and needs thorough Android testing. */ +#error See above comments. +#endif + SDL_Window *window = SDL_GetWindowFromID(sdl_window_id); + SDL_StopTextInput(window); + SDL_SetHint(SDL_HINT_ENABLE_SCREEN_KEYBOARD, "0"); + SDL_StartTextInput(window); + unicode_fallback = false; +#elif SDL_VERSION_ATLEAST(2,0,0) if(!SDL_HasScreenKeyboardSupport()) { SDL_StartTextInput(); From 26217b92e26453f73dc09dae2c5ca2af7df93f98 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Mon, 4 Nov 2024 22:00:14 -0700 Subject: [PATCH 24/44] Switch fully to SDL3-style SDL_CreateWindow. --- src/SDLmzx.h | 5 +++++ src/render_sdl.c | 12 ------------ 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index f8ac8b2b2..5b63d6277 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -486,6 +486,11 @@ static inline int replace_SDL_GetVersion(void) #endif #if !SDL_VERSION_ATLEAST(3,0,0) #define SDL_GL_DestroyContext(gl) SDL_GL_DeleteContext(gl) + +/* SDL3 removed the coordinate arguments to SDL_CreateWindow. + * Windows are created centered by default instead. */ +#define SDL_CreateWindow(title, w, h, flags) SDL_CreateWindow((title), \ + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (w), (h), (flags)) #endif #endif /* CONFIG_SDL */ diff --git a/src/render_sdl.c b/src/render_sdl.c index b15c83343..13f442f67 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -635,10 +635,6 @@ boolean sdl_create_window_soft(struct graphics_data *graphics, #endif render_data->window = SDL_CreateWindow("MegaZeux", - // FIXME: -#if !SDL_VERSION_ATLEAST(3,0,0) - SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, -#endif window->width_px, window->height_px, sdl_flags(window)); if(!render_data->window) @@ -1096,10 +1092,6 @@ boolean sdl_create_window_renderer(struct graphics_data *graphics, } render_data->window = SDL_CreateWindow("MegaZeux", -// FIXME: -#if !SDL_VERSION_ATLEAST(3,0,0) - SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, -#endif window->width_px, window->height_px, sdl_flags(window)); if(!render_data->window) @@ -1240,10 +1232,6 @@ boolean gl_create_window(struct graphics_data *graphics, #endif render_data->window = SDL_CreateWindow("MegaZeux", - // FIXME: -#if !SDL_VERSION_ATLEAST(3,0,0) - SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, -#endif window->width_px, window->height_px, GL_STRIP_FLAGS(sdl_flags(window))); if(!render_data->window) From a390c9ba9ecbd31f497cf6027c8610cd221b45dd Mon Sep 17 00:00:00 2001 From: AliceLR Date: Tue, 5 Nov 2024 03:11:07 -0700 Subject: [PATCH 25/44] Clean up SDL_RenderTexture, SDL_FRect/SDL_Rect #ifdefs, misc. --- src/SDLmzx.h | 34 ++++++++++++++++------------------ src/event_sdl.c | 4 ++-- src/graphics.c | 14 ++------------ src/render_sdl.c | 8 +------- src/render_sdlaccel.c | 25 ++++++++----------------- src/render_softscale.c | 4 ++-- unit/configure.cpp | 2 +- 7 files changed, 32 insertions(+), 59 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 5b63d6277..5b12d96e9 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -349,16 +349,14 @@ typedef struct { float x; float y; float w; float h; } SDL_FRect; #if SDL_VERSION_ATLEAST(3,0,0) typedef SDL_FRect SDL_Rect_mzx; -static inline SDL_Rect_mzx sdl_render_rect(int x, int y, - int w, int h, int full_w, int full_h) +static inline SDL_Rect_mzx sdl_render_rect(int x, int y, int w, int h) { SDL_FRect tmp = { (float)x, (float)y, (float)w, (float)h }; return tmp; } #elif SDL_VERSION_ATLEAST(2,0,0) typedef SDL_Rect SDL_Rect_mzx; -static inline SDL_Rect_mzx sdl_render_rect(int x, int y, - int w, int h, int full_w, int full_h) +static inline SDL_Rect_mzx sdl_render_rect(int x, int y, int w, int h) { SDL_Rect tmp = { x, y, w, h }; return tmp; @@ -374,29 +372,24 @@ typedef int SDL_RendererLogicalPresentation; #define SDL_SCALEMODE_BEST SDL_ScaleModeBest #define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear #define SDL_SCALEMODE_NEAREST SDL_ScaleModeNearest -#define SDL_SetRenderClipRect(r, rect) SDL_RenderSetClipRect(r, rect) -#define SDL_SetRenderLogicalSize(r, w, h) SDL_RenderSetLogicalSize(r, w, h) +#define SDL_SetRenderClipRect(r, rect) (SDL_RenderSetClipRect((r), (rect)) == 0) #if !SDL_VERSION_ATLEAST(2,0,12) typedef int SDL_ScaleMode; #endif -static inline int SDL_SetRenderLogicalPresentation(SDL_Renderer *render, +static inline SDL_bool SDL_SetRenderLogicalPresentation(SDL_Renderer *render, int w, int h, SDL_RendererLogicalPresentation p, SDL_ScaleMode s) { - return SDL_SetRenderLogicalSize(render, w, h); + // Return value 0/-1 -> true/false + return (SDL_bool)(SDL_RenderSetLogicalSize(render, w, h) == 0); } -#endif -#if SDL_VERSION_ATLEAST(2,0,0) -static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *texture, - const SDL_Rect_mzx *src_rect, const SDL_Rect_mzx *dest_rect) +static inline SDL_bool SDL_RenderTexture(SDL_Renderer *renderer, + SDL_Texture *texture, const SDL_Rect_mzx *src_rect, const SDL_Rect_mzx *dest_rect) { -#if SDL_VERSION_ATLEAST(3,0,0) - return SDL_RenderTexture(renderer, texture, src_rect, dest_rect); -#else - return SDL_RenderCopy(renderer, texture, src_rect, dest_rect); -#endif + // Return value 0/-1 -> true/false + return (SDL_bool)(SDL_RenderCopy(renderer, texture, src_rect, dest_rect) == 0); } #endif @@ -407,7 +400,12 @@ static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *tex #define SDL_SCALEMODE_NEAREST SDL_ScaleModeNearest #define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear #define SDL_DestroySurface(s) SDL_FreeSurface(s) -#define SDL_FillSurfaceRect(s,r,c) SDL_FillRect(s,r,c) + +static inline SDL_bool SDL_FillSurfaceRect(SDL_Surface *surface, SDL_Rect *rect, + Uint32 color) +{ + return (SDL_bool)(SDL_FillRect(surface, rect, color) == 0); +} static inline SDL_bool SDL_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect) { diff --git a/src/event_sdl.c b/src/event_sdl.c index a41e287c4..101e84048 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -1800,9 +1800,9 @@ void platform_init_event(void) #endif #if SDL_VERSION_ATLEAST(2,0,0) - SDL_SetGamepadEventsEnabled(true); + SDL_SetGamepadEventsEnabled(1); load_gamecontrollerdb(); #endif - SDL_SetJoystickEventsEnabled(true); + SDL_SetJoystickEventsEnabled(1); } diff --git a/src/graphics.c b/src/graphics.c index df4ef458e..bb4cea2cb 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -1488,22 +1488,12 @@ static boolean icon_w_h_constraint(png_uint_32 w, png_uint_32 h) return (w == h) && ((w % 16) == 0) && ((h % 16) == 0); } -#if SDL_VERSION_ATLEAST(2,0,0) static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h, png_uint_32 *stride, void **pixels) { +#if SDL_VERSION_ATLEAST(2,0,0) SDL_Surface *s = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_RGBA32); - if(!s) - return NULL; - - *stride = s->pitch; - *pixels = s->pixels; - return s; -} #else -static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h, - png_uint_32 *stride, void **pixels) -{ Uint32 rmask, gmask, bmask, amask; SDL_Surface *s; @@ -1520,6 +1510,7 @@ static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h, #endif s = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, rmask, gmask, bmask, amask); +#endif if(!s) return NULL; @@ -1527,7 +1518,6 @@ static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h, *pixels = s->pixels; return s; } -#endif static SDL_Surface *png_read_icon(const char *name) { diff --git a/src/render_sdl.c b/src/render_sdl.c index 13f442f67..326fe4783 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -913,7 +913,6 @@ static void find_texture_format(struct graphics_data *graphics, if(!strcmp(renderer_name, SDL_SOFTWARE_RENDERER)) is_software_renderer = true; - // thanks for the michael mouse API formats = (const uint32_t *)SDL_GetPointerProperty(props, SDL_PROP_RENDERER_TEXTURE_FORMATS_POINTER, NULL); num_formats = 0; @@ -1168,12 +1167,7 @@ void sdl_set_texture_scale_mode(struct graphics_data *graphics, else mode = SDL_SCALEMODE_LINEAR; -#if SDL_VERSION_ATLEAST(3,0,0) - if(!SDL_SetTextureScaleMode(render_data->texture[texture_id], mode)) -#else - if(SDL_SetTextureScaleMode(render_data->texture[texture_id], mode) < 0) -#endif - warn("Failed to set texture %d scale mode: %s\n", texture_id, SDL_GetError()); + SDL_SetTextureScaleMode(render_data->texture[texture_id], mode); } else warn("Texture %d is null!\n", texture_id); diff --git a/src/render_sdlaccel.c b/src/render_sdlaccel.c index 519e95763..c402a4310 100644 --- a/src/render_sdlaccel.c +++ b/src/render_sdlaccel.c @@ -568,9 +568,8 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, SDL_Texture *chars_tex = render_data->sdl.texture[TEX_CHARS]; SDL_Texture *bg_tex = render_data->sdl.texture[TEX_BACKGROUND]; - SDL_Rect_mzx dest_bg = - sdl_render_rect(offx, offy, w * CHAR_W, h * CHAR_H, TEX_SCREEN_W, TEX_SCREEN_H); - SDL_Rect_mzx src_bg = sdl_render_rect(0, 0, w, h, TEX_BG_W, TEX_BG_H); + SDL_Rect_mzx dest_bg = sdl_render_rect(offx, offy, w * CHAR_W, h * CHAR_H); + SDL_Rect_mzx src_bg = sdl_render_rect(0, 0, w, h); void *_bg; int bg_pitch; @@ -656,7 +655,7 @@ static void sdlaccel_render_layer(struct graphics_data *graphics, } } SDL_UnlockTexture(bg_tex); - SDL_RenderTexture_mzx(renderer, bg_tex, &src_bg, &dest_bg); + SDL_RenderTexture(renderer, bg_tex, &src_bg, &dest_bg); #ifdef RENDER_GEOMETRY @@ -714,11 +713,8 @@ static void sdlaccel_render_cursor(struct graphics_data *graphics, unsigned x, uint8_t *palette = render_data->palette; // Input coordinates are on the character grid. -#if SDL_VERSION_ATLEAST(3,0,0) - SDL_FRect dest = { x * CHAR_W, y * CHAR_H + offset, CHAR_W, lines }; -#else - SDL_Rect dest = { x * CHAR_W, y * CHAR_H + offset, CHAR_W, lines }; -#endif + SDL_Rect_mzx dest = + sdl_render_rect(x * CHAR_W, y * CHAR_H + offset, CHAR_W, lines); SDL_SetRenderDrawBlendMode(render_data->sdl.renderer, SDL_BLENDMODE_NONE); SDL_SetRenderDrawColor(render_data->sdl.renderer, @@ -734,11 +730,7 @@ static void sdlaccel_render_mouse(struct graphics_data *graphics, unsigned x, struct sdlaccel_render_data *render_data = graphics->render_data; // Input coordinates are pixel values. -#if SDL_VERSION_ATLEAST(3,0,0) - SDL_FRect dest = { x, y, w, h }; -#else - SDL_Rect dest = { x, y, w, h }; -#endif + SDL_Rect_mzx dest = sdl_render_rect(x, y, w, h); /* There is no preset inversion blend mode, so make a custom blend mode. * Lower SDL versions should simply fall back to drawing a white rectangle. @@ -769,8 +761,7 @@ static void sdlaccel_sync_screen(struct graphics_data *graphics, SDL_Rect_mzx src; SDL_Rect_mzx dest; - src = sdl_render_rect(0, 0, SCREEN_PIX_W, SCREEN_PIX_H, - TEX_SCREEN_W, TEX_SCREEN_H); + src = sdl_render_rect(0, 0, SCREEN_PIX_W, SCREEN_PIX_H); dest.x = window->viewport_x; dest.y = window->viewport_y; @@ -778,7 +769,7 @@ static void sdlaccel_sync_screen(struct graphics_data *graphics, dest.h = window->viewport_height; SDL_SetRenderTarget(renderer, NULL); - SDL_RenderTexture_mzx(renderer, screen_tex, &src, &dest); + SDL_RenderTexture(renderer, screen_tex, &src, &dest); SDL_RenderPresent(renderer); diff --git a/src/render_softscale.c b/src/render_softscale.c index bb0ee9466..59bbada12 100644 --- a/src/render_softscale.c +++ b/src/render_softscale.c @@ -270,7 +270,7 @@ static void softscale_sync_screen(struct graphics_data *graphics, SDL_Rect_mzx dest_rect; src_rect = sdl_render_rect(texture_rect->x, texture_rect->y, - texture_rect->w, texture_rect->h, render_data->texture_width, SCREEN_PIX_H); + texture_rect->w, texture_rect->h); dest_rect.x = window->viewport_x; dest_rect.y = window->viewport_y; @@ -282,7 +282,7 @@ static void softscale_sync_screen(struct graphics_data *graphics, SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); SDL_RenderClear(renderer); - SDL_RenderTexture_mzx(renderer, texture, &src_rect, &dest_rect); + SDL_RenderTexture(renderer, texture, &src_rect, &dest_rect); SDL_RenderPresent(renderer); } diff --git a/unit/configure.cpp b/unit/configure.cpp index 74c5594c0..ba259eda7 100644 --- a/unit/configure.cpp +++ b/unit/configure.cpp @@ -32,7 +32,7 @@ #ifdef CONFIG_SDL #include "../src/SDLmzx.h" -#undef IGNORE /* wtf Windows? */ +#undef IGNORE /* Windows defines this for some reason... */ #endif #ifdef CONFIG_EDITOR From 7d65c9729621948ba6c23f18b5369c10fb7faa8e Mon Sep 17 00:00:00 2001 From: AliceLR Date: Tue, 5 Nov 2024 03:18:22 -0700 Subject: [PATCH 26/44] Remove outdated FIXME from SDL3 audio driver. --- src/audio/driver_sdl3.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/audio/driver_sdl3.c b/src/audio/driver_sdl3.c index 2c17b6c32..d460c6c0a 100644 --- a/src/audio/driver_sdl3.c +++ b/src/audio/driver_sdl3.c @@ -92,7 +92,6 @@ void init_audio_platform(struct config_info *conf) // The buffer frames need to be configured with this hack. // This value may be ignored or modified by some platforms. - // FIXME: SDL drivers for ALSA, Pulseaudio, PipeWire, and JACK all ignore this... snprintf(hint, sizeof(hint), "%u", audio.buffer_frames); SDL_SetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, hint); From 80dca4875f5c74dc581903c1afbd232b217e1a95 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Tue, 5 Nov 2024 17:28:40 -0700 Subject: [PATCH 27/44] Clean up render_gl.h OpenGL include macros. --- docs/STYLE.md | 19 ++++++++++++++++++ src/render_gl.h | 52 +++++++++++++++++++++++-------------------------- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/docs/STYLE.md b/docs/STYLE.md index 0f16f2853..5823fcc7c 100644 --- a/docs/STYLE.md +++ b/docs/STYLE.md @@ -258,6 +258,25 @@ original condition in a block comment after `#endif`. // more code ``` +In complex cases where there is no other option, left-hash indentation is OK: + +``` +#ifdef CONFIG_GLES +# ifdef CONFIG_RENDER_GL_FIXED +# if SDL_VERSION_ATLEAST(3,0,0) +# include +# else +# ... +# endif +# endif +# ifdef CONFIG_RENDER_GL_PROGRAM +# ... +# endif +#else +# ... +#endif +``` + ### Tabulated definitions In some places where many values or repetitive structs or enums are defined, diff --git a/src/render_gl.h b/src/render_gl.h index 14941bbd0..fd532df9b 100644 --- a/src/render_gl.h +++ b/src/render_gl.h @@ -3,6 +3,7 @@ * Copyright (C) 2004-2006 Gilead Kutnick * Copyright (C) 2007,2009 Alistair John Strachan * Copyright (C) 2007 Alan Williams + * Copyright (C) 2024 Alice Rowan * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -32,34 +33,29 @@ __M_BEGIN_DECLS #include "util.h" #ifdef CONFIG_SDL -#include "SDLmzx.h" -#ifdef CONFIG_GLES - -#ifdef CONFIG_RENDER_GL_FIXED -#if SDL_VERSION_ATLEAST(3,0,0) -#include -#else -#include -#endif -#endif - -#ifdef CONFIG_RENDER_GL_PROGRAM -#if SDL_VERSION_ATLEAST(3,0,0) -#include -#else -#include -#endif -#endif - -#else - -#if SDL_VERSION_ATLEAST(3,0,0) -#include -#else -#include -#endif - -#endif +# include "SDLmzx.h" +# ifdef CONFIG_GLES +# ifdef CONFIG_RENDER_GL_FIXED +# if SDL_VERSION_ATLEAST(3,0,0) +# include +# else +# include +# endif +# endif +# ifdef CONFIG_RENDER_GL_PROGRAM +# if SDL_VERSION_ATLEAST(3,0,0) +# include +# else +# include +# endif +# endif +# else +# if SDL_VERSION_ATLEAST(3,0,0) +# include +# else +# include +# endif +# endif #endif #ifndef GLAPIENTRY From ae9c11ed35dfb2624df4b2672c0ea04dfc5f2f3b Mon Sep 17 00:00:00 2001 From: AliceLR Date: Tue, 5 Nov 2024 18:20:24 -0700 Subject: [PATCH 28/44] Add new functions to dejank SDL linked and compiled version fetching. --- src/SDLmzx.h | 25 +++++++++++++------------ src/about.c | 4 ++-- src/editor/clipboard_x11.c | 6 +++--- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 5b12d96e9..c3299c266 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -448,33 +448,34 @@ typedef SDL_sem SDL_Semaphore; */ #if !SDL_VERSION_ATLEAST(3,0,0) #define SDL_MICRO_VERSION SDL_PATCHLEVEL - -#undef SDL_VERSIONNUM -#define SDL_VERSIONNUM(x,y,z) ((x) * 1000000 + (y) * 1000 + (z)) +// This macro *should* be identical to SDL3's SDL_VERSIONNUM. +#define SDL_VERSIONNUM_MZX(x,y,z) ((x) * 1000000 + (y) * 1000 + (z)) #define SDL_VERSIONNUM_MAJOR(v) ((v) / 1000000) #define SDL_VERSIONNUM_MINOR(v) (((v) / 1000) % 1000) #define SDL_VERSIONNUM_MICRO(v) ((v) % 1000) +#else +#define SDL_VERSIONNUM_MZX(x,y,z) SDL_VERSIONNUM((x),(y),(z)) +#endif -static inline void SDL_VERSION_ORIG(SDL_version *v) +static inline int sdl_compiled_version(void) { - SDL_VERSION(v); + return SDL_VERSIONNUM_MZX(SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_MICRO_VERSION); } -#undef SDL_VERSION -#define SDL_VERSION SDL_VERSIONNUM(SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL) - -static inline int replace_SDL_GetVersion(void) +static inline int sdl_linked_version(void) { +#if SDL_VERSION_ATLEAST(3,0,0) + return SDL_GetVersion(); +#else SDL_version ver; #if SDL_VERSION_ATLEAST(2,0,0) SDL_GetVersion(&ver); #else ver = *SDL_Linked_Version(); #endif - return SDL_VERSIONNUM(ver.major, ver.minor, ver.patch); -} -#define SDL_GetVersion replace_SDL_GetVersion + return SDL_VERSIONNUM_MZX(ver.major, ver.minor, ver.patch); #endif +} /** * SDL_video.h diff --git a/src/about.c b/src/about.c index 1b1fed560..3552db72d 100644 --- a/src/about.c +++ b/src/about.c @@ -112,8 +112,8 @@ static char **about_text(int *num_lines) #ifdef CONFIG_SDL { - int ver_compiled = SDL_VERSION; - int ver_linked = SDL_GetVersion(); + int ver_compiled = sdl_compiled_version(); + int ver_linked = sdl_linked_version(); if(ver_compiled != ver_linked) { diff --git a/src/editor/clipboard_x11.c b/src/editor/clipboard_x11.c index 36ca7962d..ed86e6d1f 100644 --- a/src/editor/clipboard_x11.c +++ b/src/editor/clipboard_x11.c @@ -37,13 +37,13 @@ static int copy_buffer_total_length; static int copy_buffer_to_X11_selection(Display *display, XEvent *xevent); #ifdef CONFIG_SDL -#if SDL_VERSION_ATLEAST(1,2,0) +#if SDL_VERSION_ATLEAST(1,2,0) && !SDL_VERSION_ATLEAST(3,0,0) static inline boolean get_X11_display_and_window(SDL_Window *window, Display **display, Window *xwindow) { SDL_SysWMinfo info; - SDL_VERSION_ORIG(&info.version); + SDL_VERSION(&info.version); if(!window) window = sdl_get_current_window(); @@ -92,7 +92,7 @@ static inline void set_X11_event_callback(void) SDL_SetEventFilter(event_callback, sdl_get_current_window()); } -#endif /* SDL_VERSION_ATLEAST(1,2,0) */ +#endif /* SDL_VERSION_ATLEAST(1,2,0) && !SDL_VERSION_ATLEAST(3,0,0) */ #endif /* CONFIG_SDL */ From 3fa517f4c05349b87aa573bc027d40bba5936e70 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 02:26:49 -0700 Subject: [PATCH 29/44] Fix preserving SDL_WINDOW_FULLSCREEN_DESKTOP for SDL2 builds. --- src/render_sdl.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/render_sdl.h b/src/render_sdl.h index 14e102f39..a4c28744f 100644 --- a/src/render_sdl.h +++ b/src/render_sdl.h @@ -94,9 +94,10 @@ void sdl_set_texture_scale_mode(struct graphics_data *graphics, #include "render_gl.h" -#if SDL_VERSION_ATLEAST(2,0,0) +#if SDL_VERSION_ATLEAST(2,0,0) && !SDL_VERSION_ATLEAST(3,0,0) +/* SDL_WINDOW_FULLSCREEN_DESKTOP removed in SDL3. */ #define GL_ALLOW_FLAGS \ - (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE) + (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_RESIZABLE) #else #define GL_ALLOW_FLAGS (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE) #endif From 3997f0cfa734f1842dcc83abb49b15eece20d79d Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 16:57:57 -0700 Subject: [PATCH 30/44] Simplify misc. render_sdl.c changes, remove SDL_GL_MakeCurrent call. --- src/SDLmzx.h | 5 ++++- src/render_sdl.c | 28 ++++------------------------ src/render_sdl.h | 4 ++-- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index c3299c266..bfb6ee217 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -313,13 +313,16 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) /** * SDL_pixels.h */ -#if !SDL_VERSION_ATLEAST(3,0,0) +#if !SDL_VERSION_ATLEAST(2,0,14) +/* Added in 2.0.14 with the old names remaining; old names removed in SDL3. */ #define SDL_PIXELFORMAT_XRGB4444 SDL_PIXELFORMAT_RGB444 #define SDL_PIXELFORMAT_XBGR4444 SDL_PIXELFORMAT_BGR444 #define SDL_PIXELFORMAT_XRGB1555 SDL_PIXELFORMAT_RGB555 #define SDL_PIXELFORMAT_XBGR1555 SDL_PIXELFORMAT_BGR555 #define SDL_PIXELFORMAT_XRGB8888 SDL_PIXELFORMAT_RGB888 #define SDL_PIXELFORMAT_XBGR8888 SDL_PIXELFORMAT_BGR888 +#endif +#if !SDL_VERSION_ATLEAST(3,0,0) #define SDL_PixelFormatDetails SDL_PixelFormat #define SDL_CreatePalette(s) SDL_AllocPalette(s) #define SDL_DestroyPalette(p) SDL_FreePalette(p) diff --git a/src/render_sdl.c b/src/render_sdl.c index 326fe4783..901f1350f 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -1057,7 +1057,6 @@ boolean sdl_create_window_renderer(struct graphics_data *graphics, struct video_window *window, boolean requires_blend_ops) { struct sdl_render_data *render_data = graphics->render_data; - uint32_t sdl_rendererflags = 0; sdl_destruct_window(graphics); @@ -1077,13 +1076,6 @@ boolean sdl_create_window_renderer(struct graphics_data *graphics, SDL_SetHint(SDL_HINT_EMSCRIPTEN_ASYNCIFY, "0"); #endif -#if !SDL_VERSION_ATLEAST(3,0,0) - // Flag removed in SDL3; all drivers support it and its presence needs to be - // tested by trying to create a target texture instead. - if(requires_blend_ops) - sdl_rendererflags |= SDL_RENDERER_TARGETTEXTURE; -#endif - if(graphics->sdl_render_driver[0]) { info("Requesting SDL render driver: '%s'\n", graphics->sdl_render_driver); @@ -1099,13 +1091,11 @@ boolean sdl_create_window_renderer(struct graphics_data *graphics, goto err_free; } - // Note: previously attempted SDL_RENDERER_ACCELERATED first, then - // SDL_RENDERER_SOFTWARE, but these flags were removed in SDL3. #if SDL_VERSION_ATLEAST(3,0,0) render_data->renderer = SDL_CreateRenderer(render_data->window, BEST_RENDERER); #else - render_data->renderer = - SDL_CreateRenderer(render_data->window, BEST_RENDERER, sdl_rendererflags); + render_data->renderer = SDL_CreateRenderer(render_data->window, BEST_RENDERER, + requires_blend_ops ? SDL_RENDERER_TARGETTEXTURE : 0); #endif if(!render_data->renderer) { @@ -1113,7 +1103,7 @@ boolean sdl_create_window_renderer(struct graphics_data *graphics, goto err_free; } - find_texture_format(graphics, sdl_rendererflags); + find_texture_format(graphics, requires_blend_ops); window->bits_per_pixel = graphics->bits_per_pixel; if(!render_data->rgb_to_yuv) @@ -1234,24 +1224,14 @@ boolean gl_create_window(struct graphics_data *graphics, goto err_free; } + /* This automatically makes the context current. */ render_data->context = SDL_GL_CreateContext(render_data->window); - if(!render_data->context) { warn("Failed to create context: %s\n", SDL_GetError()); goto err_free; } -#if SDL_VERSION_ATLEAST(3,0,0) - if(!SDL_GL_MakeCurrent(render_data->window, render_data->context)) -#else - if(SDL_GL_MakeCurrent(render_data->window, render_data->context) != 0) -#endif - { - warn("Failed to make context current: %s\n", SDL_GetError()); - goto err_free; - } - window->platform_id = SDL_GetWindowID(render_data->window); sdl_set_screensaver_enabled(graphics->disable_screensaver == SCREENSAVER_ENABLE); diff --git a/src/render_sdl.h b/src/render_sdl.h index a4c28744f..2ae0d339e 100644 --- a/src/render_sdl.h +++ b/src/render_sdl.h @@ -119,10 +119,9 @@ static inline void gl_cleanup(struct graphics_data *graphics) static inline boolean GL_LoadLibrary(enum gl_lib_type type) { #if SDL_VERSION_ATLEAST(3,0,0) - if(SDL_GL_LoadLibrary(NULL)) + return SDL_GL_LoadLibrary(NULL); #else if(SDL_GL_LoadLibrary(NULL) == 0) -#endif return true; #if !SDL_VERSION_ATLEAST(2,0,0) @@ -131,6 +130,7 @@ static inline boolean GL_LoadLibrary(enum gl_lib_type type) if(strcmp(SDL_GetError(), "OpenGL context already created") == 0) return true; #endif return false; +#endif } static inline dso_fn_ptr GL_GetProcAddress(const char *proc) From 3a1b784e49c315e0437918812f140ad059795c3e Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 17:08:24 -0700 Subject: [PATCH 31/44] Reduce render_sdl.c diff further (hopefully). --- src/render_sdl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/render_sdl.c b/src/render_sdl.c index 901f1350f..917a66dc7 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -717,11 +717,11 @@ boolean sdl_create_window_soft(struct graphics_data *graphics, render_data->palette = SDL_CreateSurfacePalette(target); if(!render_data->palette) { - warn("Failed to allocate palette: %s\n", SDL_GetError()); + warn("Failed to create surface palette: %s\n", SDL_GetError()); goto err_free; } #else - render_data->palette = SDL_CreatePalette(SMZX_PAL_SIZE); + render_data->palette = SDL_AllocPalette(SMZX_PAL_SIZE); if(!render_data->palette) { warn("Failed to allocate palette: %s\n", SDL_GetError()); From c3a5180db6ceb88c5b53a01ebb37880a3c839ebb Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 17:46:45 -0700 Subject: [PATCH 32/44] Cleanup event_sdl.c hacks, fix unicode fallback default enablement. --- src/event_sdl.c | 98 +++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/src/event_sdl.c b/src/event_sdl.c index 101e84048..86399bb2f 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -2,7 +2,7 @@ * * Copyright (C) 2004 Gilead Kutnick * Copyright (C) 2007 Kevin Vance - * Copyright (C) 2019 Alice Rowan + * Copyright (C) 2019, 2024 Alice Rowan * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -926,6 +926,20 @@ static void close_joystick(int joystick_index) } #endif +static inline void read_key_event(int *key, int *mod, int *scancode, + const SDL_KeyboardEvent *ev) +{ +#if SDL_VERSION_ATLEAST(3,0,0) + *key = ev->key; + *mod = ev->mod; + *scancode = ev->scancode; +#else + *key = ev->keysym.sym; + *mod = ev->keysym.mod; + *scancode = ev->keysym.scancode; +#endif +} + static inline uint32_t utf8_next_char(uint8_t **_src) { uint8_t *src = *_src; @@ -980,6 +994,7 @@ static inline uint32_t utf8_next_char(uint8_t **_src) static boolean process_event(SDL_Event *event) { struct buffered_status *status = store_status(); + int key, mod, scancode; enum keycode ckey; /* SDL's numlock keyboard modifier handling seems to be broken on X11, @@ -1179,12 +1194,12 @@ static boolean process_event(SDL_Event *event) #if SDL_VERSION_ATLEAST(2,0,0) // emulate the X11-style "wheel is a button" that SDL 1.2 used + // SDL3 uses floats, SDL2 uses ints. case SDL_EVENT_MOUSE_WHEEL: { uint32_t button; - // floats due to SDL3... - float wheel_x = event->wheel.x; - float wheel_y = event->wheel.y; + float wheel_x = (float)event->wheel.x; + float wheel_y = (float)event->wheel.y; trace( "--EVENT_SDL-- SDL_EVENT_MOUSE_WHEEL: x=%.2f y=%.2f\n", @@ -1193,14 +1208,14 @@ static boolean process_event(SDL_Event *event) if(fabsf(wheel_x) > fabsf(wheel_y)) { - if(wheel_x < 0) + if(wheel_x < 0.0f) button = MOUSE_BUTTON_WHEELLEFT; else button = MOUSE_BUTTON_WHEELRIGHT; } else { - if(wheel_y < 0) + if(wheel_y < 0.0f) button = MOUSE_BUTTON_WHEELDOWN; else button = MOUSE_BUTTON_WHEELUP; @@ -1217,24 +1232,10 @@ static boolean process_event(SDL_Event *event) } #endif // SDL_VERSION_ATLEAST(2,0,0) -// TODO: this is kind of tacky -#if SDL_VERSION_ATLEAST(3,0,0) -#define FIELD_KEY key -#define FIELD_MOD mod -#ifdef DEBUG_TRACE -#define FIELD_SCANCODE scancode -#endif -#else -#define FIELD_KEY keysym.sym -#define FIELD_MOD keysym.mod -#ifdef DEBUG_TRACE -#define FIELD_SCANCODE keysym.scancode -#endif -#endif - case SDL_EVENT_KEY_DOWN: { uint32_t unicode = 0; + read_key_event(&key, &mod, &scancode, &event->key); #if SDL_VERSION_ATLEAST(2,0,0) // SDL 2.0 uses proper key repeat, but derives its timing from the OS. @@ -1251,7 +1252,7 @@ static boolean process_event(SDL_Event *event) #ifdef CONFIG_PANDORA { // Pandora hack. Certain keys are actually joystick buttons. - int button = get_pandora_joystick_button(event->key.FIELD_KEY); + int button = get_pandora_joystick_button(key); if(button >= 0) { joystick_button_press(status, 0, button); @@ -1260,13 +1261,9 @@ static boolean process_event(SDL_Event *event) } #endif - ckey = convert_SDL_internal(event->key.FIELD_KEY); - trace( - "--EVENT_SDL-- SDL_EVENT_KEY_DOWN: scancode:%d sym:%d -> %d\n", - event->key.FIELD_SCANCODE, - event->key.FIELD_KEY, - ckey - ); + ckey = convert_SDL_internal(key); + trace("--EVENT_SDL-- SDL_EVENT_KEY_DOWN: scancode:%d sym:%d -> %d\n", + scancode, key, ckey); if(!ckey) { #if !SDL_VERSION_ATLEAST(2,0,0) @@ -1294,6 +1291,8 @@ static boolean process_event(SDL_Event *event) boolean caps_lock = !!(SDL_GetModState() & SDL_KMOD_CAPS); unicode = convert_internal_unicode(ckey, caps_lock); } + if(unicode) + trace("--EVENT_SDL-- unicode:%d\n", (int)unicode); if((ckey == IKEY_RETURN) && get_alt_status(keycode_internal) && @@ -1358,10 +1357,12 @@ static boolean process_event(SDL_Event *event) case SDL_EVENT_KEY_UP: { + read_key_event(&key, &mod, &scancode, &event->key); + #ifdef CONFIG_PANDORA { // Pandora hack. Certain keys are actually joystick buttons. - int button = get_pandora_joystick_button(event->key.FIELD_KEY); + int button = get_pandora_joystick_button(key); if(button >= 0) { joystick_button_release(status, 0, button); @@ -1370,13 +1371,9 @@ static boolean process_event(SDL_Event *event) } #endif - ckey = convert_SDL_internal(event->key.FIELD_KEY); - trace( - "--EVENT_SDL-- SDL_EVENT_KEY_UP: scancode:%d sym:%d -> %d\n", - event->key.FIELD_SCANCODE, - event->key.FIELD_KEY, - ckey - ); + ckey = convert_SDL_internal(key); + trace("--EVENT_SDL-- SDL_EVENT_KEY_UP: scancode:%d sym:%d -> %d\n", + scancode, key, ckey); if(!ckey) { #if !SDL_VERSION_ATLEAST(2,0,0) @@ -1416,9 +1413,12 @@ static boolean process_event(SDL_Event *event) trace("--EVENT_SDL-- SDL_EVENT_TEXT_INPUT: %s\n", text); - // This should never happen; ignore. if(unicode_fallback) - break; + { + // Clear any unicode keys on the buffer generated from the fallback... + status->unicode_length = 0; + unicode_fallback = false; + } // Decode the input UTF-8 string into UTF-32 for the event buffer. while(*text) @@ -1643,6 +1643,7 @@ boolean __peek_exit_input(void) { SDL_Event events[256]; int num_events; + int key, mod, scancode; int i; SDL_PumpEvents(); @@ -1661,15 +1662,15 @@ boolean __peek_exit_input(void) if(events[i].type == SDL_EVENT_KEY_DOWN) { - SDL_KeyboardEvent *ev = &(events[i].key); + read_key_event(&key, &mod, &scancode, &(events[i].key)); - if(ev->FIELD_KEY == SDLK_ESCAPE) + if(key == SDLK_ESCAPE) return true; - if(ev->FIELD_KEY == SDLK_C && (ev->FIELD_MOD & SDL_KMOD_CTRL)) + if(key == SDLK_C && (mod & SDL_KMOD_CTRL)) return true; - if(ev->FIELD_KEY == SDLK_F4 && (ev->FIELD_MOD & SDL_KMOD_ALT)) + if(key == SDLK_F4 && (mod & SDL_KMOD_ALT)) return true; } } @@ -1747,12 +1748,10 @@ void sdl_init_window_text_events(unsigned sdl_window_id) SDL_StopTextInput(window); SDL_SetHint(SDL_HINT_ENABLE_SCREEN_KEYBOARD, "0"); SDL_StartTextInput(window); - unicode_fallback = false; #elif SDL_VERSION_ATLEAST(2,0,0) if(!SDL_HasScreenKeyboardSupport()) { SDL_StartTextInput(); - unicode_fallback = false; } else { @@ -1761,9 +1760,6 @@ void sdl_init_window_text_events(unsigned sdl_window_id) } #else SDL_EnableUNICODE(1); - /* SDL 1.2 might also need this (Pandora? doesn't generate unicode presses). - * If it isn't required, real unicode events will turn this off. */ - unicode_fallback = true; #endif } @@ -1805,4 +1801,10 @@ void platform_init_event(void) #endif SDL_SetJoystickEventsEnabled(1); + + /* It's not clear which ports do and don't implement SDL text events, so + * enable the unicode fallback at startup until proven otherwise. + * SDL 1.2 might also need this (Pandora? doesn't generate unicode presses). + * If it isn't required, real unicode events will turn this off. */ + unicode_fallback = true; } From 3cc519b98d8f91bf12924ad911bcf6011494471f Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 17:56:15 -0700 Subject: [PATCH 33/44] Fix SDL_SetJoystick/GamepadEventsEnabled fallback param type. --- src/SDLmzx.h | 4 ++-- src/event_sdl.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index bfb6ee217..0878cceee 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -248,7 +248,7 @@ typedef SDL_GameControllerButton SDL_GamepadButton; #define SDL_AddGamepadMapping(m) SDL_GameControllerAddMapping(m) #define SDL_AddGamepadMappingsFromFile(f) SDL_GameControllerAddMappingsFromFile(f) -static inline void SDL_SetGamepadEventsEnabled(SDL_bool enabled) +static inline void SDL_SetGamepadEventsEnabled(boolean enabled) { SDL_GameControllerEventState(enabled ? SDL_ENABLE : SDL_DISABLE); } @@ -266,7 +266,7 @@ static inline void SDL_SetGamepadEventsEnabled(SDL_bool enabled) #define SDL_GetJoystickGUIDString(g,b,l) SDL_JoystickGetGUIDString(g,b,l) #define SDL_GetJoystickID(j) SDL_JoystickInstanceID(j) -static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled) +static inline void SDL_SetJoystickEventsEnabled(boolean enabled) { SDL_JoystickEventState(enabled ? SDL_ENABLE : SDL_DISABLE); } diff --git a/src/event_sdl.c b/src/event_sdl.c index 86399bb2f..765addb7f 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -1796,11 +1796,11 @@ void platform_init_event(void) #endif #if SDL_VERSION_ATLEAST(2,0,0) - SDL_SetGamepadEventsEnabled(1); + SDL_SetGamepadEventsEnabled(true); load_gamecontrollerdb(); #endif - SDL_SetJoystickEventsEnabled(1); + SDL_SetJoystickEventsEnabled(true); /* It's not clear which ports do and don't implement SDL text events, so * enable the unicode fallback at startup until proven otherwise. From 5e4e88b29efff995dd9ea8abecde3787d4291639 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 18:03:37 -0700 Subject: [PATCH 34/44] Flatten compatibility version checks for SDL_pixels.h. --- src/SDLmzx.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 0878cceee..2db934c78 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -313,6 +313,14 @@ static inline void SDL_SetJoystickEventsEnabled(boolean enabled) /** * SDL_pixels.h */ +#if !SDL_VERSION_ATLEAST(2,0,5) +/* Convenience macros added in 2.0.5. */ +#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#define SDL_PIXELFORMAT_RGBA32 SDL_PIXELFORMAT_RGBA8888 +#else +#define SDL_PIXELFORMAT_RGBA32 SDL_PIXELFORMAT_ABGR8888 +#endif +#endif #if !SDL_VERSION_ATLEAST(2,0,14) /* Added in 2.0.14 with the old names remaining; old names removed in SDL3. */ #define SDL_PIXELFORMAT_XRGB4444 SDL_PIXELFORMAT_RGB444 @@ -328,14 +336,6 @@ static inline void SDL_SetJoystickEventsEnabled(boolean enabled) #define SDL_DestroyPalette(p) SDL_FreePalette(p) #define SDL_GetMasksForPixelFormat(f,b,R,G,B,A) SDL_PixelFormatEnumToMasks(f,b,R,G,B,A) -#if !SDL_VERSION_ATLEAST(2,0,5) -#if SDL_BYTEORDER == SDL_BIG_ENDIAN -#define SDL_PIXELFORMAT_RGBA32 SDL_PIXELFORMAT_RGBA8888 -#else -#define SDL_PIXELFORMAT_RGBA32 SDL_PIXELFORMAT_ABGR8888 -#endif -#endif - static inline Uint32 SDL_MapSurfaceRGBA(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { From c0227465ec8b515afe642ab83381c1f045c83aa8 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 18:10:18 -0700 Subject: [PATCH 35/44] Handle another 0/-1 to true/false as a function instead of macro. --- src/SDLmzx.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 2db934c78..57e23dfd9 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -375,23 +375,26 @@ typedef int SDL_RendererLogicalPresentation; #define SDL_SCALEMODE_BEST SDL_ScaleModeBest #define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear #define SDL_SCALEMODE_NEAREST SDL_ScaleModeNearest -#define SDL_SetRenderClipRect(r, rect) (SDL_RenderSetClipRect((r), (rect)) == 0) #if !SDL_VERSION_ATLEAST(2,0,12) typedef int SDL_ScaleMode; #endif +static inline SDL_bool SDL_SetRenderClipRect(SDL_Renderer *render, + SDL_Rect *rect) +{ + return (SDL_bool)(SDL_RenderSetClipRect(render, rect) == 0); +} + static inline SDL_bool SDL_SetRenderLogicalPresentation(SDL_Renderer *render, int w, int h, SDL_RendererLogicalPresentation p, SDL_ScaleMode s) { - // Return value 0/-1 -> true/false return (SDL_bool)(SDL_RenderSetLogicalSize(render, w, h) == 0); } static inline SDL_bool SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect_mzx *src_rect, const SDL_Rect_mzx *dest_rect) { - // Return value 0/-1 -> true/false return (SDL_bool)(SDL_RenderCopy(renderer, texture, src_rect, dest_rect) == 0); } #endif From 6a0204647eb58f46eb12fcb735d4d80706f482fb Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 18:47:25 -0700 Subject: [PATCH 36/44] Fix lack of matching SDL_free for SDL_GetClipboardText. --- src/editor/clipboard.h | 1 + src/editor/clipboard_carbon.c | 7 ++++++- src/editor/clipboard_cocoa.m | 7 ++++++- src/editor/clipboard_null.c | 6 ++++++ src/editor/clipboard_sdl2.c | 5 +++++ src/editor/clipboard_win32.c | 7 ++++++- src/editor/clipboard_x11.c | 5 +++++ src/editor/robo_ed.c | 3 ++- 8 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/editor/clipboard.h b/src/editor/clipboard.h index db9a1743e..a1edba8df 100644 --- a/src/editor/clipboard.h +++ b/src/editor/clipboard.h @@ -27,6 +27,7 @@ __M_BEGIN_DECLS void copy_buffer_to_clipboard(char **buffer, int lines, int total_length); char *get_clipboard_buffer(void); +void free_clipboard_buffer(char *buffer); __M_END_DECLS diff --git a/src/editor/clipboard_carbon.c b/src/editor/clipboard_carbon.c index 1e9e66fbf..ea8807a51 100644 --- a/src/editor/clipboard_carbon.c +++ b/src/editor/clipboard_carbon.c @@ -125,7 +125,7 @@ char *get_clipboard_buffer(void) src_data = (char *)CFDataGetBytePtr(flavorData); length = (size_t)CFDataGetLength(flavorData); - dest_data = cmalloc(length + 1); + dest_data = (char *)cmalloc(length + 1); memcpy(dest_data, src_data, length); dest_data[length] = 0; @@ -137,3 +137,8 @@ char *get_clipboard_buffer(void) CFRelease(clipboard); return dest_data; } + +void free_clipboard_buffer(char *buffer) +{ + free(buffer); +} diff --git a/src/editor/clipboard_cocoa.m b/src/editor/clipboard_cocoa.m index 565f9367a..ccb8636de 100644 --- a/src/editor/clipboard_cocoa.m +++ b/src/editor/clipboard_cocoa.m @@ -115,7 +115,7 @@ void copy_buffer_to_clipboard(char **buffer, int lines, int total_length) goto err; size_t buf_len = [chrdata length]; - char *buf = cmalloc(buf_len + 1); + char *buf = (char *)cmalloc(buf_len + 1); [chrdata getBytes:buf length:buf_len]; buf[buf_len] = '\0'; @@ -126,3 +126,8 @@ void copy_buffer_to_clipboard(char **buffer, int lines, int total_length) [pool release]; return NULL; } + +void free_clipboard_buffer(char *buffer) +{ + free(buffer); +} diff --git a/src/editor/clipboard_null.c b/src/editor/clipboard_null.c index ffb3bb56e..c76e6e9cf 100644 --- a/src/editor/clipboard_null.c +++ b/src/editor/clipboard_null.c @@ -18,6 +18,7 @@ */ #include "clipboard.h" +#include void copy_buffer_to_clipboard(char **buffer, int lines, int total_length) {} @@ -25,3 +26,8 @@ char *get_clipboard_buffer(void) { return NULL; } + +void free_clipboard_buffer(char *buffer) +{ + free(buffer); +} diff --git a/src/editor/clipboard_sdl2.c b/src/editor/clipboard_sdl2.c index 05ed11658..a537362f0 100644 --- a/src/editor/clipboard_sdl2.c +++ b/src/editor/clipboard_sdl2.c @@ -58,3 +58,8 @@ char *get_clipboard_buffer(void) { return SDL_GetClipboardText(); } + +void free_clipboard_buffer(char *buffer) +{ + SDL_free(buffer); +} diff --git a/src/editor/clipboard_win32.c b/src/editor/clipboard_win32.c index 881ccfc71..d34d048e8 100644 --- a/src/editor/clipboard_win32.c +++ b/src/editor/clipboard_win32.c @@ -86,7 +86,7 @@ char *get_clipboard_buffer(void) // CF_TEXT is guaranteed to be null-terminated. length = strlen(src_data); - dest_data = cmalloc(length + 1); + dest_data = (char *)cmalloc(length + 1); strcpy(dest_data, src_data); GlobalUnlock(global_memory); @@ -97,3 +97,8 @@ char *get_clipboard_buffer(void) return NULL; } + +void free_clipboard_buffer(char *buffer) +{ + free(buffer); +} diff --git a/src/editor/clipboard_x11.c b/src/editor/clipboard_x11.c index ed86e6d1f..df6550fb3 100644 --- a/src/editor/clipboard_x11.c +++ b/src/editor/clipboard_x11.c @@ -202,3 +202,8 @@ char *get_clipboard_buffer(void) XUnlockDisplay(display); return dest_data; } + +void free_clipboard_buffer(char *buffer) +{ + free(buffer); +} diff --git a/src/editor/robo_ed.c b/src/editor/robo_ed.c index d4a5e393c..45ebe5a99 100644 --- a/src/editor/robo_ed.c +++ b/src/editor/robo_ed.c @@ -1537,7 +1537,8 @@ static void paste_buffer(struct robot_editor_context *rstate) #endif } - free(ext_buffer); + /* This may need SDL_free if it was allocated by SDL. */ + free_clipboard_buffer(ext_buffer); } else From 420a59a69cabef9e44961856d81ef3338cf5ed49 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 19:06:38 -0700 Subject: [PATCH 37/44] Remove warn() branches on more SDL render functions with changed return types. --- src/render_sdlaccel.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/render_sdlaccel.c b/src/render_sdlaccel.c index c402a4310..7c18d1ae5 100644 --- a/src/render_sdlaccel.c +++ b/src/render_sdlaccel.c @@ -344,10 +344,8 @@ static boolean sdlaccel_create_window(struct graphics_data *graphics, goto err_free; } - if(SDL_SetTextureBlendMode(texture[TEX_CHARS], SDL_BLENDMODE_BLEND)) - warn("Failed to set char texture blend mode: %s\n", SDL_GetError()); - if(SDL_SetTextureBlendMode(texture[TEX_BACKGROUND], SDL_BLENDMODE_BLEND)) - warn("Failed to set bg texture blend mode: %s\n", SDL_GetError()); + SDL_SetTextureBlendMode(texture[TEX_CHARS], SDL_BLENDMODE_BLEND); + SDL_SetTextureBlendMode(texture[TEX_BACKGROUND], SDL_BLENDMODE_BLEND); sdl_set_texture_scale_mode(graphics, window, TEX_SCREEN, true); sdl_set_texture_scale_mode(graphics, window, TEX_CHARS, false); From cdcd3566a06c3802f4f36c9accab7b9d08df5143 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 19:28:45 -0700 Subject: [PATCH 38/44] Changelog entry for initial SDL3 support. --- docs/changelog.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index 6c1b46841..0101a9576 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -7,6 +7,8 @@ FIXES GIT - MZX 2.93c +Initial support for SDL3 has been added, but as it has not been +released yet, no MegaZeux release builds use it currently. As part of the preparation for SDL3, this release includes many long-belated changes to the way MegaZeux resizes windows and calculates the scaled display area within the window (including @@ -56,6 +58,9 @@ USERS DEVELOPERS ++ Added initial support for SDL3. No platforms select SDL3 by + default, but it is now possible to build and test MegaZeux for + platforms that already support the current prerelease (3.1.6). + Split the renderer function "set_video_mode" into two renderer functions "create_window" and "resize_window" to more closely reflect the reality of SDL2/3. The former is used during From c3f693becf17e6b3b9b2d3d56374daa69aa0e03e Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 19:46:00 -0700 Subject: [PATCH 39/44] Check PREFIX/lib/pkgconfig for SDL3 if LIBDIR=. --- Makefile | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Makefile b/Makefile index 38efb745d..1da4e2138 100644 --- a/Makefile +++ b/Makefile @@ -92,9 +92,17 @@ ifeq (${BUILD_SDL},3) ifneq ($(and ${SDL_PKG_CONFIG_PATH},$(wildcard ${SDL_PKG_CONFIG_PATH}/sdl3.pc)),) # nop else +ifneq (${LIBDIR},.) +# Check LIBDIR for SDL3 first, if it is set. ifneq ($(wildcard ${LIBDIR}/pkgconfig/sdl3.pc),) SDL_PKG_CONFIG_PATH ?= ${LIBDIR}/pkgconfig endif +else +# Check dependencies prefix instead for LIBDIR=. platforms. +ifneq ($(wildcard ${PREFIX}/lib/pkgconfig/sdl3.pc),) +SDL_PKG_CONFIG_PATH ?= ${PREFIX}/lib/pkgconfig +endif +endif # LIBDIR=. endif ifneq (${SDL_PKG_CONFIG_PATH},) SDL_PKG_CONFIG_FLAGS = --with-path=${SDL_PKG_CONFIG_PATH} From cb493bd64cf1005fe50fc6834494c436a4ff8aee Mon Sep 17 00:00:00 2001 From: AliceLR Date: Wed, 6 Nov 2024 22:31:11 -0700 Subject: [PATCH 40/44] Prefer boolean over SDL_bool in SDL1/2 -> SDL3 shims. --- src/SDLmzx.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/SDLmzx.h b/src/SDLmzx.h index 57e23dfd9..f64b7d503 100644 --- a/src/SDLmzx.h +++ b/src/SDLmzx.h @@ -380,22 +380,22 @@ typedef int SDL_RendererLogicalPresentation; typedef int SDL_ScaleMode; #endif -static inline SDL_bool SDL_SetRenderClipRect(SDL_Renderer *render, +static inline boolean SDL_SetRenderClipRect(SDL_Renderer *render, SDL_Rect *rect) { - return (SDL_bool)(SDL_RenderSetClipRect(render, rect) == 0); + return SDL_RenderSetClipRect(render, rect) == 0; } -static inline SDL_bool SDL_SetRenderLogicalPresentation(SDL_Renderer *render, +static inline boolean SDL_SetRenderLogicalPresentation(SDL_Renderer *render, int w, int h, SDL_RendererLogicalPresentation p, SDL_ScaleMode s) { - return (SDL_bool)(SDL_RenderSetLogicalSize(render, w, h) == 0); + return SDL_RenderSetLogicalSize(render, w, h) == 0; } -static inline SDL_bool SDL_RenderTexture(SDL_Renderer *renderer, +static inline boolean SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect_mzx *src_rect, const SDL_Rect_mzx *dest_rect) { - return (SDL_bool)(SDL_RenderCopy(renderer, texture, src_rect, dest_rect) == 0); + return SDL_RenderCopy(renderer, texture, src_rect, dest_rect) == 0; } #endif @@ -407,16 +407,16 @@ static inline SDL_bool SDL_RenderTexture(SDL_Renderer *renderer, #define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear #define SDL_DestroySurface(s) SDL_FreeSurface(s) -static inline SDL_bool SDL_FillSurfaceRect(SDL_Surface *surface, SDL_Rect *rect, +static inline boolean SDL_FillSurfaceRect(SDL_Surface *surface, SDL_Rect *rect, Uint32 color) { - return (SDL_bool)(SDL_FillRect(surface, rect, color) == 0); + return SDL_FillRect(surface, rect, color) == 0; } -static inline SDL_bool SDL_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect) +static inline boolean SDL_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect) { *rect = surface->clip_rect; - return SDL_TRUE; + return true; } #if SDL_VERSION_ATLEAST(2,0,0) From 054a1fafa85b12340df778ecf4fcdcc1388f2840 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Thu, 7 Nov 2024 01:55:55 -0700 Subject: [PATCH 41/44] Remove leftover unused field from sdl_render_data. --- src/render_sdl.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/render_sdl.h b/src/render_sdl.h index 2ae0d339e..3caa75558 100644 --- a/src/render_sdl.h +++ b/src/render_sdl.h @@ -37,7 +37,6 @@ struct sdl_render_data SDL_Window *window; SDL_GLContext context; SDL_PixelFormatDetails *pixel_format; - SDL_ScaleMode screen_scale_mode; #else SDL_Overlay *overlay; #endif From b3293e29ce2b2651c0c794fd4f97d05f8386e046 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Thu, 7 Nov 2024 02:09:06 -0700 Subject: [PATCH 42/44] SDL2/SDL3 Renderer texture format lists are different types. --- src/render_sdl.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/render_sdl.c b/src/render_sdl.c index 917a66dc7..37cb2e978 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -901,19 +901,19 @@ static void find_texture_format(struct graphics_data *graphics, uint32_t priority = 0; boolean is_software_renderer = false; const char *renderer_name; - const uint32_t *formats = NULL; int num_formats; #if SDL_VERSION_ATLEAST(3,0,0) SDL_PropertiesID props = SDL_GetRendererProperties(render_data->renderer); - const uint32_t *pos; + const SDL_PixelFormat *formats = NULL; + const SDL_PixelFormat *pos; renderer_name = SDL_GetRendererName(render_data->renderer); if(!strcmp(renderer_name, SDL_SOFTWARE_RENDERER)) is_software_renderer = true; - formats = (const uint32_t *)SDL_GetPointerProperty(props, + formats = (const SDL_PixelFormat *)SDL_GetPointerProperty(props, SDL_PROP_RENDERER_TEXTURE_FORMATS_POINTER, NULL); num_formats = 0; for(pos = formats; pos && *pos != SDL_PIXELFORMAT_UNKNOWN; pos++) @@ -922,6 +922,7 @@ static void find_texture_format(struct graphics_data *graphics, #else SDL_RendererInfo rinfo; + const uint32_t *formats = NULL; if(!SDL_GetRendererInfo(render_data->renderer, &rinfo)) { From 13231adf35a3479785f22416479be5cbce3ac2c4 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Thu, 7 Nov 2024 02:40:31 -0700 Subject: [PATCH 43/44] Revert event_sdl.c key event trace style changes. --- src/event_sdl.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/event_sdl.c b/src/event_sdl.c index 765addb7f..ec8a9e9db 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -1262,8 +1262,10 @@ static boolean process_event(SDL_Event *event) #endif ckey = convert_SDL_internal(key); - trace("--EVENT_SDL-- SDL_EVENT_KEY_DOWN: scancode:%d sym:%d -> %d\n", - scancode, key, ckey); + trace( + "--EVENT_SDL-- SDL_EVENT_KEY_DOWN: scancode:%d sym:%d -> %d\n", + scancode, key, ckey + ); if(!ckey) { #if !SDL_VERSION_ATLEAST(2,0,0) @@ -1372,8 +1374,10 @@ static boolean process_event(SDL_Event *event) #endif ckey = convert_SDL_internal(key); - trace("--EVENT_SDL-- SDL_EVENT_KEY_UP: scancode:%d sym:%d -> %d\n", - scancode, key, ckey); + trace( + "--EVENT_SDL-- SDL_EVENT_KEY_UP: scancode:%d sym:%d -> %d\n", + scancode, key, ckey + ); if(!ckey) { #if !SDL_VERSION_ATLEAST(2,0,0) From 2811749ef0b0dbce8ff998919a2a262f57ea6926 Mon Sep 17 00:00:00 2001 From: AliceLR Date: Thu, 7 Nov 2024 02:56:00 -0700 Subject: [PATCH 44/44] Don't search LIBDIR/pkgconfig for sdl3.pc, it doesn't work. --- Makefile | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 1da4e2138..9a48dce58 100644 --- a/Makefile +++ b/Makefile @@ -87,18 +87,15 @@ ifneq (${BUILD_SDL},) # SDL 3 # ifeq (${BUILD_SDL},3) -# Check SDL_PKG_CONFIG_PATH and LIBDIR for pkgconfig/sdl3.pc. +# Check SDL_PKG_CONFIG_PATH and PREFIX/lib/pkgconfig for sdl3.pc. # Note --with-path is a pkgconf extension. ifneq ($(and ${SDL_PKG_CONFIG_PATH},$(wildcard ${SDL_PKG_CONFIG_PATH}/sdl3.pc)),) # nop else -ifneq (${LIBDIR},.) -# Check LIBDIR for SDL3 first, if it is set. -ifneq ($(wildcard ${LIBDIR}/pkgconfig/sdl3.pc),) -SDL_PKG_CONFIG_PATH ?= ${LIBDIR}/pkgconfig -endif -else # Check dependencies prefix instead for LIBDIR=. platforms. +# This is useless for unix/darwin, which should have sdl3.pc in a +# place pkgconf can find through normal means. +ifeq (${LIBDIR},.) ifneq ($(wildcard ${PREFIX}/lib/pkgconfig/sdl3.pc),) SDL_PKG_CONFIG_PATH ?= ${PREFIX}/lib/pkgconfig endif