Skip to content

Commit

Permalink
FIXME Initial SDL3 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceLR committed Apr 15, 2024
1 parent 9b17356 commit 6545b61
Show file tree
Hide file tree
Showing 15 changed files with 685 additions and 224 deletions.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
10 changes: 6 additions & 4 deletions config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1519,7 +1521,7 @@ else
fi

#
# Softscale renderer (SDL 2)
# Softscale renderer (SDL 2+)
#
if [ "$SOFTSCALE" = "true" ]; then
echo "Softscale renderer enabled."
Expand Down Expand Up @@ -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
Expand Down
224 changes: 216 additions & 8 deletions src/SDLmzx.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* MegaZeux
*
* Copyright (C) 2007 Alistair John Strachan <[email protected]>
* Copyright (C) 2024 Alice Rowan <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
Expand All @@ -17,22 +18,28 @@
* 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"

__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 <SDL3/SDL.h>
#else
#include <SDL.h>
#if defined(_WIN32) || defined(CONFIG_X11)
#include <SDL_syswm.h>
#endif
#endif

/* SDL1 backwards compatibility for SDL2 ************************************/

#if !SDL_VERSION_ATLEAST(2,0,0)

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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 */
8 changes: 4 additions & 4 deletions src/audio/audio_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 6545b61

Please sign in to comment.