remove source code support for SDL2

This commit is contained in:
Sasha Szpakowski
2024-10-18 20:22:26 -03:00
parent caca6196fc
commit 337d8fa048
38 changed files with 161 additions and 2419 deletions
+1 -3
View File
@@ -802,8 +802,6 @@ target_link_libraries(love_joystick_root PUBLIC
add_library(love_joystick_sdl STATIC
src/modules/joystick/sdl/Joystick.cpp
src/modules/joystick/sdl/Joystick.h
src/modules/joystick/sdl/JoystickSDL3.cpp
src/modules/joystick/sdl/JoystickSDL3.h
src/modules/joystick/sdl/JoystickModule.cpp
src/modules/joystick/sdl/JoystickModule.h
)
@@ -1982,7 +1980,7 @@ love_group_projects(NAME "liblove" NESTED TARGETS ${LIBLOVE_DEPENDENCIES})
love_group_projects(NAME "liblove/libraries" NESTED TARGETS ${LIBLOVE_LIBRARIES})
love_group_projects(NAME "liblove" TARGETS liblove ${LOVE_EXTRA_DEPENDECIES})
love_group_projects(NAME "lovedep" TARGETS lovedep::SDL2 lovedep::Freetype lovedep::Harfbuzz lovedep::OpenAL lovedep::Modplug lovedep::Theora lovedep::Vorbis lovedep::Ogg lovedep::Zlib lovedep::Lua)
love_group_projects(NAME "lovedep" TARGETS lovedep::SDL lovedep::Freetype lovedep::Harfbuzz lovedep::OpenAL lovedep::Modplug lovedep::Theora lovedep::Vorbis lovedep::Ogg lovedep::Zlib lovedep::Lua)
love_group_projects(NAME "lovedep" TARGETS lua51 alcommon al-excommon harfbuzz-subset zlib)
-12
View File
@@ -27,11 +27,7 @@
#include <set>
#include <unordered_map>
#if __has_include(<SDL3/SDL.h>)
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#endif
#include <jni.h>
#include <android/asset_manager.h>
@@ -44,14 +40,6 @@
#include "libraries/physfs/physfs.h"
#include "filesystem/physfs/PhysfsIo.h"
// Backward compatibility
#if !SDL_VERSION_ATLEAST(3, 0, 0)
#define SDL_GetAndroidJNIEnv SDL_AndroidGetJNIEnv
#define SDL_GetAndroidActivity SDL_AndroidGetActivity
#define SDL_GetAndroidInternalStoragePath SDL_AndroidGetInternalStoragePath
#define SDL_GetAndroidExternalStoragePath SDL_AndroidGetExternalStoragePath
#endif
namespace love
{
namespace android
-4
View File
@@ -123,10 +123,6 @@
# define LOVE_LEGENDARY_APP_ARGV_HACK
#endif
#if defined(LOVE_ANDROID) || defined(LOVE_IOS)
# define LOVE_LEGENDARY_ACCELEROMETER_AS_JOYSTICK_HACK
#endif
#if defined(LOVE_WINDOWS) || defined(LOVE_LINUX) || defined(LOVE_ANDROID)
# define LOVE_GRAPHICS_VULKAN
#endif
-6
View File
@@ -20,13 +20,7 @@
#include "delay.h"
#if __has_include(<SDL3/SDL_timer.h>)
#include <SDL3/SDL_timer.h>
#include <SDL3/SDL_version.h>
#else
#include <SDL_timer.h>
#include <SDL_version.h>
#endif
namespace love
{
-6
View File
@@ -23,17 +23,11 @@
#include "common/Variant.h"
#include "modules/love/love.h"
#if __has_include(<SDL3/SDL.h>)
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#endif
#ifdef LOVE_BUILD_EXE
#if SDL_VERSION_ATLEAST(3, 0, 0)
#include <SDL3/SDL_main.h>
#endif
// Lua
extern "C" {
+4 -195
View File
@@ -35,63 +35,7 @@
#include <cmath>
#if __has_include(<SDL3/SDL_version.h>)
#include <SDL3/SDL_version.h>
#else
#include <SDL_version.h>
#endif
#if SDL_VERSION_ATLEAST(3, 0, 0)
#include "joystick/sdl/JoystickSDL3.h"
#else
#include "joystick/sdl/Joystick.h"
#endif
#if !SDL_VERSION_ATLEAST(3, 0, 0)
#define SDL_EVENT_DID_ENTER_BACKGROUND SDL_APP_DIDENTERBACKGROUND
#define SDL_EVENT_WILL_ENTER_FOREGROUND SDL_APP_WILLENTERFOREGROUND
#define SDL_EVENT_KEY_DOWN SDL_KEYDOWN
#define SDL_EVENT_KEY_UP SDL_KEYUP
#define SDL_EVENT_TEXT_INPUT SDL_TEXTINPUT
#define SDL_EVENT_TEXT_EDITING SDL_TEXTEDITING
#define SDL_EVENT_MOUSE_MOTION SDL_MOUSEMOTION
#define SDL_EVENT_MOUSE_BUTTON_DOWN SDL_MOUSEBUTTONDOWN
#define SDL_EVENT_MOUSE_BUTTON_UP SDL_MOUSEBUTTONUP
#define SDL_EVENT_MOUSE_WHEEL SDL_MOUSEWHEEL
#define SDL_EVENT_FINGER_DOWN SDL_FINGERDOWN
#define SDL_EVENT_FINGER_UP SDL_FINGERUP
#define SDL_EVENT_FINGER_MOTION SDL_FINGERMOTION
#define SDL_EVENT_DROP_FILE SDL_DROPFILE
#define SDL_EVENT_QUIT SDL_QUIT
#define SDL_EVENT_TERMINATING SDL_APP_TERMINATING
#define SDL_EVENT_LOW_MEMORY SDL_APP_LOWMEMORY
#define SDL_EVENT_LOCALE_CHANGED SDL_LOCALECHANGED
#define SDL_EVENT_SENSOR_UPDATE SDL_SENSORUPDATE
#define SDL_EVENT_JOYSTICK_BUTTON_DOWN SDL_JOYBUTTONDOWN
#define SDL_EVENT_JOYSTICK_BUTTON_UP SDL_JOYBUTTONUP
#define SDL_EVENT_JOYSTICK_AXIS_MOTION SDL_JOYAXISMOTION
#define SDL_EVENT_JOYSTICK_HAT_MOTION SDL_JOYHATMOTION
#define SDL_EVENT_JOYSTICK_ADDED SDL_JOYDEVICEADDED
#define SDL_EVENT_JOYSTICK_REMOVED SDL_JOYDEVICEREMOVED
#define SDL_EVENT_GAMEPAD_BUTTON_DOWN SDL_CONTROLLERBUTTONDOWN
#define SDL_EVENT_GAMEPAD_BUTTON_UP SDL_CONTROLLERBUTTONUP
#define SDL_EVENT_GAMEPAD_AXIS_MOTION SDL_CONTROLLERAXISMOTION
#define SDL_EVENT_GAMEPAD_SENSOR_UPDATE SDL_CONTROLLERSENSORUPDATE
#define SDL_EVENT_WINDOW_FOCUS_GAINED SDL_WINDOWEVENT_FOCUS_GAINED
#define SDL_EVENT_WINDOW_FOCUS_LOST SDL_WINDOWEVENT_FOCUS_LOST
#define SDL_EVENT_WINDOW_MOUSE_ENTER SDL_WINDOWEVENT_ENTER
#define SDL_EVENT_WINDOW_MOUSE_LEAVE SDL_WINDOWEVENT_LEAVE
#define SDL_EVENT_WINDOW_SHOWN SDL_WINDOWEVENT_SHOWN
#define SDL_EVENT_WINDOW_HIDDEN SDL_WINDOWEVENT_HIDDEN
#define SDL_EVENT_WINDOW_RESIZED SDL_WINDOWEVENT_RESIZED
#define SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED SDL_WINDOWEVENT_SIZE_CHANGED
#define SDL_EVENT_WINDOW_MINIMIZED SDL_WINDOWEVENT_MINIMIZED
#define SDL_EVENT_WINDOW_RESTORED SDL_WINDOWEVENT_RESTORED
#endif
namespace love
{
@@ -139,12 +83,7 @@ static void normalizedToDPICoords(double *x, double *y)
// SDL's event watch callbacks trigger when the event is actually posted inside
// SDL, unlike with SDL_PollEvents. This is useful for some events which require
// handling inside the function which triggered them on some backends.
#if SDL_VERSION_ATLEAST(3, 0, 0)
static bool
#else
static int
#endif
SDLCALL watchAppEvents(void * /*udata*/, SDL_Event *event)
static bool SDLCALL watchAppEvents(void * /*udata*/, SDL_Event *event)
{
auto gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
@@ -168,11 +107,7 @@ SDLCALL watchAppEvents(void * /*udata*/, SDL_Event *event)
Event::Event()
: love::event::Event("love.event.sdl")
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_InitSubSystem(SDL_INIT_EVENTS))
#else
if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0)
#endif
throw love::Exception("Could not initialize SDL events subsystem (%s)", SDL_GetError());
SDL_AddEventWatch(watchAppEvents, this);
@@ -180,11 +115,7 @@ Event::Event()
Event::~Event()
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_RemoveEventWatch(watchAppEvents, this);
#else
SDL_DelEventWatch(watchAppEvents, this);
#endif
SDL_QuitSubSystem(SDL_INIT_EVENTS);
}
@@ -211,11 +142,7 @@ Message *Event::wait()
SDL_Event e;
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_WaitEvent(&e))
#else
if (SDL_WaitEvent(&e) != 1)
#endif
return nullptr;
return convert(e);
@@ -277,19 +204,11 @@ Message *Event::convert(const SDL_Event &e)
break;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
love::keyboard::sdl::Keyboard::getConstant(e.key.key, key);
#else
love::keyboard::sdl::Keyboard::getConstant(e.key.keysym.sym, key);
#endif
if (!love::keyboard::Keyboard::getConstant(key, txt))
txt = "unknown";
#if SDL_VERSION_ATLEAST(3, 0, 0)
love::keyboard::sdl::Keyboard::getConstant(e.key.scancode, scancode);
#else
love::keyboard::sdl::Keyboard::getConstant(e.key.keysym.scancode, scancode);
#endif
if (!love::keyboard::Keyboard::getConstant(scancode, txt2))
txt2 = "unknown";
@@ -299,19 +218,11 @@ Message *Event::convert(const SDL_Event &e)
msg = new Message("keypressed", vargs);
break;
case SDL_EVENT_KEY_UP:
#if SDL_VERSION_ATLEAST(3, 0, 0)
love::keyboard::sdl::Keyboard::getConstant(e.key.key, key);
#else
love::keyboard::sdl::Keyboard::getConstant(e.key.keysym.sym, key);
#endif
if (!love::keyboard::Keyboard::getConstant(key, txt))
txt = "unknown";
#if SDL_VERSION_ATLEAST(3, 0, 0)
love::keyboard::sdl::Keyboard::getConstant(e.key.scancode, scancode);
#else
love::keyboard::sdl::Keyboard::getConstant(e.key.keysym.scancode, scancode);
#endif
if (!love::keyboard::Keyboard::getConstant(scancode, txt2))
txt2 = "unknown";
@@ -389,16 +300,6 @@ Message *Event::convert(const SDL_Event &e)
case SDL_EVENT_MOUSE_WHEEL:
vargs.emplace_back((double) e.wheel.x);
vargs.emplace_back((double) e.wheel.y);
#if SDL_VERSION_ATLEAST(2, 0, 18) && !SDL_VERSION_ATLEAST(3, 0, 0)
// These values will be garbage if 2.0.18+ headers are used but a lower
// version of SDL is used at runtime, but other bits of code already
// prevent running in that situation.
vargs.emplace_back((double) e.wheel.preciseX);
vargs.emplace_back((double) e.wheel.preciseY);
#else
vargs.emplace_back((double) e.wheel.x);
vargs.emplace_back((double) e.wheel.y);
#endif
txt = e.wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? "flipped" : "standard";
vargs.emplace_back(txt, strlen(txt));
@@ -413,11 +314,7 @@ Message *Event::convert(const SDL_Event &e)
// (and SDL doesn't differentiate.) Non-screen touch devices like Mac
// trackpads won't give touch coords in the window's coordinate-space.
#ifndef LOVE_MACOS
#if SDL_VERSION_ATLEAST(3, 0, 0)
touchinfo.id = (int64) e.tfinger.fingerID;
#else
touchinfo.id = (int64)e.tfinger.fingerId;
#endif
touchinfo.x = e.tfinger.x;
touchinfo.y = e.tfinger.y;
touchinfo.dx = e.tfinger.dx;
@@ -463,12 +360,11 @@ Message *Event::convert(const SDL_Event &e)
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP:
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
#if SDL_VERSION_ATLEAST(2, 0, 14) && defined(LOVE_ENABLE_SENSOR)
#if defined(LOVE_ENABLE_SENSOR)
case SDL_EVENT_GAMEPAD_SENSOR_UPDATE:
#endif
msg = convertJoystickEvent(e);
break;
#if SDL_VERSION_ATLEAST(3, 0, 0)
case SDL_EVENT_WINDOW_FOCUS_GAINED:
case SDL_EVENT_WINDOW_FOCUS_LOST:
case SDL_EVENT_WINDOW_MOUSE_ENTER:
@@ -479,17 +375,9 @@ Message *Event::convert(const SDL_Event &e)
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
case SDL_EVENT_WINDOW_MINIMIZED:
case SDL_EVENT_WINDOW_RESTORED:
#else
case SDL_WINDOWEVENT:
#endif
msg = convertWindowEvent(e);
break;
#if SDL_VERSION_ATLEAST(3, 0, 0)
case SDL_EVENT_DISPLAY_ORIENTATION:
#else
case SDL_DISPLAYEVENT:
if (e.display.event == SDL_DISPLAYEVENT_ORIENTATION)
#endif
{
auto orientation = window::Window::ORIENTATION_UNKNOWN;
switch ((SDL_DisplayOrientation) e.display.data1)
@@ -515,7 +403,6 @@ Message *Event::convert(const SDL_Event &e)
if (!window::Window::getConstant(orientation, txt))
txt = "unknown";
#if SDL_VERSION_ATLEAST(3, 0, 0)
int count = 0;
int displayindex = 0;
SDL_DisplayID *displays = SDL_GetDisplays(&count);
@@ -529,9 +416,6 @@ Message *Event::convert(const SDL_Event &e)
}
SDL_free(displays);
vargs.emplace_back((double)(displayindex + 1));
#else
vargs.emplace_back((double)(e.display.display + 1));
#endif
vargs.emplace_back(txt, strlen(txt));
msg = new Message("displayrotated", vargs);
@@ -541,11 +425,7 @@ Message *Event::convert(const SDL_Event &e)
filesystem = Module::getInstance<filesystem::Filesystem>(Module::M_FILESYSTEM);
if (filesystem != nullptr)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
const char *filepath = e.drop.data;
#else
const char *filepath = e.drop.file;
#endif
// Allow mounting any dropped path, so zips or dirs can be mounted.
filesystem->allowMountingForPath(filepath);
@@ -562,9 +442,6 @@ Message *Event::convert(const SDL_Event &e)
file->release();
}
}
#if !SDL_VERSION_ATLEAST(3, 0, 0)
SDL_free(e.drop.file);
#endif
break;
case SDL_EVENT_QUIT:
case SDL_EVENT_TERMINATING:
@@ -573,11 +450,9 @@ Message *Event::convert(const SDL_Event &e)
case SDL_EVENT_LOW_MEMORY:
msg = new Message("lowmemory");
break;
#if SDL_VERSION_ATLEAST(2, 0, 14)
case SDL_EVENT_LOCALE_CHANGED:
msg = new Message("localechanged");
break;
#endif
case SDL_EVENT_SENSOR_UPDATE:
sensorInstance = Module::getInstance<sensor::Sensor>(M_SENSOR);
if (sensorInstance)
@@ -587,21 +462,13 @@ Message *Event::convert(const SDL_Event &e)
for (void *s: sensors)
{
SDL_Sensor *sensor = (SDL_Sensor *) s;
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SensorID id = SDL_GetSensorID(sensor);
#else
SDL_SensorID id = SDL_SensorGetInstanceID(sensor);
#endif
if (e.sensor.which == id)
{
// Found sensor
const char *sensorType;
#if SDL_VERSION_ATLEAST(3, 0, 0)
auto sdltype = SDL_GetSensorType(sensor);
#else
auto sdltype = SDL_SensorGetType(sensor);
#endif
if (!sensor::Sensor::getConstant(sensor::sdl::Sensor::convert(sdltype), sensorType))
sensorType = "unknown";
@@ -686,13 +553,8 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP:
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
const auto &b = e.gbutton;
if (!joystick::sdl::Joystick::getConstant((SDL_GamepadButton) b.button, padbutton))
#else
const auto &b = e.cbutton;
if (!joystick::sdl::Joystick::getConstant((SDL_GameControllerButton) b.button, padbutton))
#endif
break;
if (!joystick::Joystick::getConstant(padbutton, txt))
@@ -709,21 +571,12 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const
}
break;
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (joystick::sdl::Joystick::getConstant((SDL_GamepadAxis) e.gaxis.axis, padaxis))
#else
if (joystick::sdl::Joystick::getConstant((SDL_GameControllerAxis) e.caxis.axis, padaxis))
#endif
{
if (!joystick::Joystick::getConstant(padaxis, txt))
break;
#if SDL_VERSION_ATLEAST(3, 0, 0)
const auto &a = e.gaxis;
#else
const auto &a = e.caxis;
#endif
stick = joymodule->getJoystickFromID(a.which);
if (!stick)
break;
@@ -754,14 +607,10 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const
msg = new Message("joystickremoved", vargs);
}
break;
#if SDL_VERSION_ATLEAST(2, 0, 14) && defined(LOVE_ENABLE_SENSOR)
#if defined(LOVE_ENABLE_SENSOR)
case SDL_EVENT_GAMEPAD_SENSOR_UPDATE:
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
const auto &sens = e.gsensor;
#else
const auto &sens = e.csensor;
#endif
stick = joymodule->getJoystickFromID(sens.which);
if (stick)
{
@@ -781,7 +630,7 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const
}
}
break;
#endif // SDL_VERSION_ATLEAST(2, 0, 14) && defined(LOVE_ENABLE_SENSOR)
#endif // defined(LOVE_ENABLE_SENSOR)
default:
break;
}
@@ -799,11 +648,7 @@ Message *Event::convertWindowEvent(const SDL_Event &e)
window::Window *win = nullptr;
graphics::Graphics *gfx = nullptr;
#if SDL_VERSION_ATLEAST(3, 0, 0)
auto event = e.type;
#else
auto event = e.window.event;
#endif
switch (event)
{
@@ -822,7 +667,6 @@ Message *Event::convertWindowEvent(const SDL_Event &e)
vargs.emplace_back(event == SDL_EVENT_WINDOW_SHOWN);
msg = new Message("visible", vargs);
break;
#if SDL_VERSION_ATLEAST(3, 0, 0)
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
{
double width = e.window.data1;
@@ -852,41 +696,6 @@ Message *Event::convertWindowEvent(const SDL_Event &e)
msg = new Message("resize", vargs);
}
break;
#else
case SDL_EVENT_WINDOW_RESIZED:
{
double width = e.window.data1;
double height = e.window.data2;
gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
win = Module::getInstance<window::Window>(Module::M_WINDOW);
// WINDOWEVENT_SIZE_CHANGED will always occur before RESIZED.
// The size values in the Window aren't necessarily the same as the
// graphics size, which is what we want to output.
if (gfx)
{
width = gfx->getWidth();
height = gfx->getHeight();
}
else if (win)
{
width = win->getWidth();
height = win->getHeight();
windowToDPICoords(&width, &height);
}
vargs.emplace_back(width);
vargs.emplace_back(height);
msg = new Message("resize", vargs);
}
break;
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
win = Module::getInstance<window::Window>(Module::M_WINDOW);
if (win)
win->onSizeChanged(e.window.data1, e.window.data2);
break;
#endif
case SDL_EVENT_WINDOW_MINIMIZED:
case SDL_EVENT_WINDOW_RESTORED:
#ifdef LOVE_ANDROID
-4
View File
@@ -26,11 +26,7 @@
#include "audio/Source.h"
// SDL
#if __has_include(<SDL3/SDL_events.h>)
#include <SDL3/SDL_events.h>
#else
#include <SDL_events.h>
#endif
namespace love
{
@@ -62,11 +62,7 @@
#include <string>
#ifdef LOVE_ANDROID
#if __has_include(<SDL3/SDL.h>)
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#endif
#include "common/android.h"
#endif
@@ -634,17 +630,10 @@ std::string Filesystem::getFullCommonPath(CommonPath path)
#elif defined(LOVE_ANDROID)
std::string storagepath;
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (isAndroidSaveExternal())
storagepath = SDL_GetAndroidExternalStoragePath();
else
storagepath = SDL_GetAndroidInternalStoragePath();
#else
if (isAndroidSaveExternal())
storagepath = SDL_AndroidGetExternalStoragePath();
else
storagepath = SDL_AndroidGetInternalStoragePath();
#endif
switch (path)
{
+1 -16
View File
@@ -34,13 +34,7 @@
#endif
// SDL
#if __has_include(<SDL3/SDL_loadso.h>)
#include <SDL3/SDL_loadso.h>
#include <SDL3/SDL_version.h>
#else
#include <SDL_loadso.h>
#include <SDL_version.h>
#endif
// STL
#include <vector>
@@ -939,11 +933,7 @@ int extloader(lua_State *L)
}
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SharedObject *handle = nullptr;
#else
void *handle = nullptr;
#endif
auto *inst = instance();
#ifdef LOVE_ANDROID
@@ -1004,12 +994,7 @@ int extloader(lua_State *L)
// We look for both loveopen_ and luaopen_, so libraries with specific love support
// can tell when they've been loaded by love.
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_FunctionPointer func = nullptr;
#else
void *func = nullptr;
#endif
func = SDL_LoadFunction(handle, ("loveopen_" + tokenized_function).c_str());
SDL_FunctionPointer func = SDL_LoadFunction(handle, ("loveopen_" + tokenized_function).c_str());
if (!func)
func = SDL_LoadFunction(handle, ("luaopen_" + tokenized_function).c_str());
-24
View File
@@ -45,13 +45,7 @@
#include <cstdio>
#ifdef LOVE_IOS
#if __has_include(<SDL3/SDL_video.h>)
#include <SDL3/SDL_video.h>
#include <SDL3/SDL_version.h>
#else
#include <SDL_syswm.h>
#include <SDL_version.h>
#endif
#endif
namespace love
@@ -288,23 +282,12 @@ GLuint Graphics::getSystemBackbufferFBO() const
{
#ifdef LOVE_IOS
// Hack: iOS uses a custom FBO.
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_PropertiesID props = SDL_GetWindowProperties(SDL_GL_GetCurrentWindow());
GLuint resolveframebuffer = (GLuint)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_UIKIT_OPENGL_RESOLVE_FRAMEBUFFER_NUMBER, 0);
if (resolveframebuffer != 0)
return resolveframebuffer;
else
return (GLuint)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_UIKIT_OPENGL_FRAMEBUFFER_NUMBER, 0);
#else
SDL_SysWMinfo info = {};
SDL_VERSION(&info.version);
SDL_GetWindowWMInfo(SDL_GL_GetCurrentWindow(), &info);
if (info.info.uikit.resolveFramebuffer != 0)
return info.info.uikit.resolveFramebuffer;
else
return info.info.uikit.framebuffer;
#endif
#else
return 0;
#endif
@@ -1306,16 +1289,9 @@ void Graphics::present(void *screenshotCallbackData)
#ifdef LOVE_IOS
// Hack: SDL's color renderbuffer must be bound when swapBuffers is called.
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_PropertiesID props = SDL_GetWindowProperties(SDL_GL_GetCurrentWindow());
GLuint colorbuffer = (GLuint)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_UIKIT_OPENGL_RENDERBUFFER_NUMBER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, colorbuffer);
#else
SDL_SysWMinfo info = {};
SDL_VERSION(&info.version);
SDL_GetWindowWMInfo(SDL_GL_GetCurrentWindow(), &info);
glBindRenderbuffer(GL_RENDERBUFFER, info.info.uikit.colorbuffer);
#endif
#endif
for (StreamBuffer *buffer : batchedDrawState.vb)
-17
View File
@@ -37,20 +37,10 @@
#include <cstdio>
// For SDL_GL_GetProcAddress.
#if __has_include(<SDL3/SDL_video.h>)
#include <SDL3/SDL_video.h>
#else
#include <SDL_video.h>
#endif
#ifdef LOVE_IOS
#if __has_include(<SDL3/SDL_video.h>)
#include <SDL3/SDL_video.h>
#include <SDL3/SDL_version.h>
#else
#include <SDL_syswm.h>
#include <SDL_version.h>
#endif
#endif
#ifdef LOVE_ANDROID
@@ -972,15 +962,8 @@ GLuint OpenGL::getDefaultFBO() const
{
#ifdef LOVE_IOS
// Hack: iOS uses a custom FBO.
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_PropertiesID props = SDL_GetWindowProperties(SDL_GL_GetCurrentWindow());
return (GLuint)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_UIKIT_OPENGL_FRAMEBUFFER_NUMBER, 0);
#else
SDL_SysWMinfo info = {};
SDL_VERSION(&info.version);
SDL_GetWindowWMInfo(SDL_GL_GetCurrentWindow(), &info);
return info.info.uikit.framebuffer;
#endif
#else
return 0;
#endif
+1 -31
View File
@@ -29,13 +29,7 @@
#include "Shader.h"
#include "Vulkan.h"
#if __has_include(<SDL3/SDL_version.h>)
#include <SDL3/SDL_version.h>
#include <SDL3/SDL_vulkan.h>
#else
#include <SDL_version.h>
#include <SDL_vulkan.h>
#endif
#include <algorithm>
#include <vector>
@@ -102,11 +96,7 @@ static void checkOptionalInstanceExtensions(OptionalInstanceExtensions& ext)
Graphics::Graphics()
: love::graphics::Graphics("love.graphics.vulkan")
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_Vulkan_LoadLibrary(nullptr))
#else
if (SDL_Vulkan_LoadLibrary(nullptr))
#endif
throw love::Exception("could not find vulkan");
volkInitializeCustom((PFN_vkGetInstanceProcAddr)SDL_Vulkan_GetVkGetInstanceProcAddr());
@@ -129,19 +119,12 @@ Graphics::Graphics()
// GetInstanceExtensions works with a null window parameter as long as
// SDL_Vulkan_LoadLibrary has been called (which we do earlier).
unsigned int count;
#if SDL_VERSION_ATLEAST(3, 0, 0)
unsigned int count = 0;
char const* const* extensions_string = SDL_Vulkan_GetInstanceExtensions(&count);
if (extensions_string == nullptr)
throw love::Exception("couldn't retrieve sdl vulkan extensions");
std::vector<const char*> extensions(extensions_string, extensions_string + count);
#else
if (SDL_Vulkan_GetInstanceExtensions(nullptr, &count, nullptr) != SDL_TRUE)
throw love::Exception("couldn't retrieve sdl vulkan extensions");
std::vector<const char*> extensions = {};
#endif
checkOptionalInstanceExtensions(optionalInstanceExtensions);
@@ -150,14 +133,6 @@ Graphics::Graphics()
if (optionalInstanceExtensions.debugInfo)
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
#if !SDL_VERSION_ATLEAST(3, 0, 0)
size_t additional_extension_count = extensions.size();
extensions.resize(additional_extension_count + count);
if (SDL_Vulkan_GetInstanceExtensions(nullptr, &count, extensions.data() + additional_extension_count) != SDL_TRUE)
throw love::Exception("couldn't retrieve sdl vulkan extensions");
#endif
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
createInfo.ppEnabledExtensionNames = extensions.data();
@@ -1878,13 +1853,8 @@ void Graphics::createSurface()
{
auto window = Module::getInstance<love::window::Window>(M_WINDOW);
const void *handle = window->getHandle();
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_Vulkan_CreateSurface((SDL_Window*)handle, instance, nullptr, &surface))
throw love::Exception("failed to create window surface");
#else
if (SDL_Vulkan_CreateSurface((SDL_Window*)handle, instance, &surface) != SDL_TRUE)
throw love::Exception("failed to create window surface");
#endif
}
SwapChainSupportDetails Graphics::querySwapChainSupport(VkPhysicalDevice device)
+136 -354
View File
@@ -25,22 +25,12 @@
#include "sensor/sdl/Sensor.h"
// SDL
#if __has_include(<SDL3/SDL_version.h>)
#include <SDL3/SDL_version.h>
#else
#include <SDL_version.h>
#endif
#include <SDL3/SDL_guid.h>
// C++
#include <algorithm>
#include <limits>
#if !SDL_VERSION_ATLEAST(3, 0, 0)
#ifndef SDL_TICKS_PASSED
#define SDL_TICKS_PASSED(A, B) ((Sint32)((B) - (A)) <= 0)
#endif
namespace love
{
namespace joystick
@@ -51,25 +41,12 @@ namespace sdl
Joystick::Joystick(int id)
: joyhandle(nullptr)
, controller(nullptr)
, haptic(nullptr)
, joystickType(JOYSTICK_TYPE_UNKNOWN)
, instanceid(-1)
, id(id)
, vibration()
{
}
Joystick::Joystick(int id, int joyindex)
: joyhandle(nullptr)
, controller(nullptr)
, haptic(nullptr)
, instanceid(-1)
, id(id)
, vibration()
{
open(joyindex);
}
Joystick::~Joystick()
{
close();
@@ -77,38 +54,36 @@ Joystick::~Joystick()
bool Joystick::open(int64 deviceid)
{
int deviceindex = (int) deviceid;
close();
joyhandle = SDL_JoystickOpen(deviceindex);
joyhandle = SDL_OpenJoystick((SDL_JoystickID) deviceid);
if (joyhandle)
{
instanceid = SDL_JoystickInstanceID(joyhandle);
instanceid = SDL_GetJoystickID(joyhandle);
// SDL_JoystickGetGUIDString uses 32 bytes plus the null terminator.
char cstr[33];
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUID(joyhandle);
SDL_JoystickGetGUIDString(sdlguid, cstr, (int) sizeof(cstr));
SDL_GUID sdlguid = SDL_GetJoystickGUID(joyhandle);
SDL_GUIDToString(sdlguid, cstr, (int) sizeof(cstr));
pguid = std::string(cstr);
// See if SDL thinks this is a Game Controller.
openGamepad(deviceindex);
openGamepad(deviceid);
// Prefer the Joystick name for consistency.
const char *joyname = SDL_JoystickName(joyhandle);
const char *joyname = SDL_GetJoystickName(joyhandle);
if (!joyname && controller)
joyname = SDL_GameControllerName(controller);
joyname = SDL_GetGamepadName(controller);
if (joyname)
name = joyname;
switch (SDL_JoystickGetType(joyhandle))
switch (SDL_GetJoystickType(joyhandle))
{
case SDL_JOYSTICK_TYPE_GAMECONTROLLER:
case SDL_JOYSTICK_TYPE_GAMEPAD:
joystickType = JOYSTICK_TYPE_GAMEPAD;
break;
case SDL_JOYSTICK_TYPE_WHEEL:
@@ -146,25 +121,20 @@ bool Joystick::open(int64 deviceid)
void Joystick::close()
{
if (haptic)
SDL_HapticClose(haptic);
if (controller)
SDL_GameControllerClose(controller);
SDL_CloseGamepad(controller);
if (joyhandle)
SDL_JoystickClose(joyhandle);
SDL_CloseJoystick(joyhandle);
joyhandle = nullptr;
controller = nullptr;
haptic = nullptr;
instanceid = -1;
vibration = Vibration();
}
bool Joystick::isConnected() const
{
return joyhandle != nullptr && SDL_JoystickGetAttached(joyhandle);
return joyhandle != nullptr && SDL_JoystickConnected(joyhandle);
}
const char *Joystick::getName() const
@@ -179,17 +149,17 @@ Joystick::JoystickType Joystick::getJoystickType() const
int Joystick::getAxisCount() const
{
return isConnected() ? SDL_JoystickNumAxes(joyhandle) : 0;
return isConnected() ? SDL_GetNumJoystickAxes(joyhandle) : 0;
}
int Joystick::getButtonCount() const
{
return isConnected() ? SDL_JoystickNumButtons(joyhandle) : 0;
return isConnected() ? SDL_GetNumJoystickButtons(joyhandle) : 0;
}
int Joystick::getHatCount() const
{
return isConnected() ? SDL_JoystickNumHats(joyhandle) : 0;
return isConnected() ? SDL_GetNumJoystickHats(joyhandle) : 0;
}
float Joystick::getAxis(int axisindex) const
@@ -197,7 +167,7 @@ float Joystick::getAxis(int axisindex) const
if (!isConnected() || axisindex < 0 || axisindex >= getAxisCount())
return 0;
return clampval(((float) SDL_JoystickGetAxis(joyhandle, axisindex))/32768.0f);
return clampval(((float) SDL_GetJoystickAxis(joyhandle, axisindex))/32768.0f);
}
std::vector<float> Joystick::getAxes() const
@@ -211,7 +181,7 @@ std::vector<float> Joystick::getAxes() const
axes.reserve(count);
for (int i = 0; i < count; i++)
axes.push_back(clampval(((float) SDL_JoystickGetAxis(joyhandle, i))/32768.0f));
axes.push_back(clampval(((float) SDL_GetJoystickAxis(joyhandle, i))/32768.0f));
return axes;
}
@@ -223,7 +193,7 @@ Joystick::Hat Joystick::getHat(int hatindex) const
if (!isConnected() || hatindex < 0 || hatindex >= getHatCount())
return h;
getConstant(SDL_JoystickGetHat(joyhandle, hatindex), h);
getConstant(SDL_GetJoystickHat(joyhandle, hatindex), h);
return h;
}
@@ -240,7 +210,7 @@ bool Joystick::isDown(const std::vector<int> &buttonlist) const
if (button < 0 || button >= numbuttons)
continue;
if (SDL_JoystickGetButton(joyhandle, button) == 1)
if (SDL_GetJoystickButton(joyhandle, button))
return true;
}
@@ -252,11 +222,7 @@ void Joystick::setPlayerIndex(int index)
if (!isConnected())
return;
#if SDL_VERSION_ATLEAST(2, 0, 12)
SDL_JoystickSetPlayerIndex(joyhandle, index);
#else
LOVE_UNUSED(index);
#endif
SDL_SetJoystickPlayerIndex(joyhandle, index);
}
int Joystick::getPlayerIndex() const
@@ -264,27 +230,21 @@ int Joystick::getPlayerIndex() const
if (!isConnected())
return -1;
#if SDL_VERSION_ATLEAST(2, 0, 12)
return SDL_JoystickGetPlayerIndex(joyhandle);
#else
return -1;
#endif
return SDL_GetJoystickPlayerIndex(joyhandle);
}
bool Joystick::openGamepad(int64 deviceid)
{
int deviceindex = (int) deviceid;
if (!SDL_IsGameController(deviceindex))
if (!SDL_IsGamepad((SDL_JoystickID)deviceid))
return false;
if (isGamepad())
{
SDL_GameControllerClose(controller);
SDL_CloseGamepad(controller);
controller = nullptr;
}
controller = SDL_GameControllerOpen(deviceindex);
controller = SDL_OpenGamepad((SDL_JoystickID)deviceid);
return isGamepad();
}
@@ -298,31 +258,20 @@ Joystick::GamepadType Joystick::getGamepadType() const
if (controller == nullptr)
return GAMEPAD_TYPE_UNKNOWN;
#if SDL_VERSION_ATLEAST(2, 0, 12)
switch (SDL_GameControllerGetType(controller))
switch (SDL_GetGamepadType(controller))
{
case SDL_CONTROLLER_TYPE_UNKNOWN: return GAMEPAD_TYPE_UNKNOWN;
case SDL_CONTROLLER_TYPE_XBOX360: return GAMEPAD_TYPE_XBOX360;
case SDL_CONTROLLER_TYPE_XBOXONE: return GAMEPAD_TYPE_XBOXONE;
case SDL_CONTROLLER_TYPE_PS3: return GAMEPAD_TYPE_PS3;
case SDL_CONTROLLER_TYPE_PS4: return GAMEPAD_TYPE_PS4;
case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO: return GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
#if SDL_VERSION_ATLEAST(2, 0, 14)
case SDL_CONTROLLER_TYPE_VIRTUAL: return GAMEPAD_TYPE_VIRTUAL;
case SDL_CONTROLLER_TYPE_PS5: return GAMEPAD_TYPE_PS5;
#endif
#if SDL_VERSION_ATLEAST(2, 0, 16)
case SDL_CONTROLLER_TYPE_AMAZON_LUNA: return GAMEPAD_TYPE_AMAZON_LUNA;
case SDL_CONTROLLER_TYPE_GOOGLE_STADIA: return GAMEPAD_TYPE_STADIA;
#endif
#if SDL_VERSION_ATLEAST(2, 24, 0)
case SDL_CONTROLLER_TYPE_NVIDIA_SHIELD: return GAMEPAD_TYPE_NVIDIA_SHIELD;
case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_LEFT: return GAMEPAD_TYPE_JOYCON_LEFT;
case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT: return GAMEPAD_TYPE_JOYCON_RIGHT;
case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_PAIR: return GAMEPAD_TYPE_JOYCON_PAIR;
#endif
case SDL_GAMEPAD_TYPE_UNKNOWN: return GAMEPAD_TYPE_UNKNOWN;
case SDL_GAMEPAD_TYPE_XBOX360: return GAMEPAD_TYPE_XBOX360;
case SDL_GAMEPAD_TYPE_XBOXONE: return GAMEPAD_TYPE_XBOXONE;
case SDL_GAMEPAD_TYPE_PS3: return GAMEPAD_TYPE_PS3;
case SDL_GAMEPAD_TYPE_PS4: return GAMEPAD_TYPE_PS4;
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO: return GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
case SDL_GAMEPAD_TYPE_PS5: return GAMEPAD_TYPE_PS5;
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT: return GAMEPAD_TYPE_JOYCON_LEFT;
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT: return GAMEPAD_TYPE_JOYCON_RIGHT;
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR: return GAMEPAD_TYPE_JOYCON_PAIR;
default: return GAMEPAD_TYPE_UNKNOWN;
}
#endif
return GAMEPAD_TYPE_UNKNOWN;
}
@@ -332,11 +281,11 @@ float Joystick::getGamepadAxis(love::joystick::Joystick::GamepadAxis axis) const
if (!isConnected() || !isGamepad())
return 0.f;
SDL_GameControllerAxis sdlaxis;
SDL_GamepadAxis sdlaxis;
if (!getConstant(axis, sdlaxis))
return 0.f;
Sint16 value = SDL_GameControllerGetAxis(controller, sdlaxis);
Sint16 value = SDL_GetGamepadAxis(controller, sdlaxis);
return clampval((float) value / 32768.0f);
}
@@ -346,14 +295,14 @@ bool Joystick::isGamepadDown(const std::vector<GamepadButton> &blist) const
if (!isConnected() || !isGamepad())
return false;
SDL_GameControllerButton sdlbutton;
SDL_GamepadButton sdlbutton;
for (GamepadButton button : blist)
{
if (!getConstant(button, sdlbutton))
continue;
if (SDL_GameControllerGetButton(controller, sdlbutton) == 1)
if (SDL_GetGamepadButton(controller, sdlbutton))
return true;
}
@@ -368,48 +317,57 @@ Joystick::JoystickInput Joystick::getGamepadMapping(const GamepadInput &input) c
if (!isGamepad())
return jinput;
SDL_GameControllerButtonBind sdlbind = {};
sdlbind.bindType = SDL_CONTROLLER_BINDTYPE_NONE;
SDL_GameControllerButton sdlbutton;
SDL_GameControllerAxis sdlaxis;
SDL_GamepadButton sdlbutton = SDL_GAMEPAD_BUTTON_INVALID;
SDL_GamepadAxis sdlaxis = SDL_GAMEPAD_AXIS_INVALID;
switch (input.type)
{
case INPUT_TYPE_BUTTON:
if (getConstant(input.button, sdlbutton))
sdlbind = SDL_GameControllerGetBindForButton(controller, sdlbutton);
getConstant(input.button, sdlbutton);
break;
case INPUT_TYPE_AXIS:
if (getConstant(input.axis, sdlaxis))
sdlbind = SDL_GameControllerGetBindForAxis(controller, sdlaxis);
getConstant(input.axis, sdlaxis);
break;
default:
break;
}
switch (sdlbind.bindType)
int bindcount = 0;
SDL_GamepadBinding **sdlbindings = SDL_GetGamepadBindings(controller, &bindcount);
for (int i = 0; i < bindcount; i++)
{
case SDL_CONTROLLER_BINDTYPE_BUTTON:
jinput.type = INPUT_TYPE_BUTTON;
jinput.button = sdlbind.value.button;
break;
case SDL_CONTROLLER_BINDTYPE_AXIS:
jinput.type = INPUT_TYPE_AXIS;
jinput.axis = sdlbind.value.axis;
break;
case SDL_CONTROLLER_BINDTYPE_HAT:
if (getConstant(sdlbind.value.hat.hat_mask, jinput.hat.value))
const SDL_GamepadBinding *b = sdlbindings[i];
if ((input.type == INPUT_TYPE_BUTTON && b->output_type == SDL_GAMEPAD_BINDTYPE_BUTTON && b->output.button == sdlbutton)
|| (input.type == INPUT_TYPE_AXIS && b->output_type == SDL_GAMEPAD_BINDTYPE_AXIS && b->output.axis.axis == sdlaxis))
{
jinput.type = INPUT_TYPE_HAT;
jinput.hat.index = sdlbind.value.hat.hat;
switch (b->input_type)
{
case SDL_GAMEPAD_BINDTYPE_BUTTON:
jinput.type = INPUT_TYPE_BUTTON;
jinput.button = b->input.button;
break;
case SDL_GAMEPAD_BINDTYPE_AXIS:
jinput.type = INPUT_TYPE_AXIS;
jinput.axis = b->input.axis.axis;
break;
case SDL_GAMEPAD_BINDTYPE_HAT:
if (getConstant(b->input.hat.hat_mask, jinput.hat.value))
{
jinput.type = INPUT_TYPE_HAT;
jinput.hat.index = b->input.hat.hat;
}
break;
case SDL_GAMEPAD_BINDTYPE_NONE:
default:
break;
}
break;
}
break;
case SDL_CONTROLLER_BINDTYPE_NONE:
default:
break;
}
SDL_free(sdlbindings);
return jinput;
}
@@ -418,12 +376,12 @@ std::string Joystick::getGamepadMappingString() const
char *sdlmapping = nullptr;
if (controller != nullptr)
sdlmapping = SDL_GameControllerMapping(controller);
sdlmapping = SDL_GetGamepadMapping(controller);
if (sdlmapping == nullptr)
{
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(pguid.c_str());
sdlmapping = SDL_GameControllerMappingForGUID(sdlguid);
SDL_GUID sdlguid = SDL_StringToGUID(pguid.c_str());
sdlmapping = SDL_GetGamepadMappingForGUID(sdlguid);
}
if (sdlmapping == nullptr)
@@ -467,9 +425,9 @@ void Joystick::getDeviceInfo(int &vendorID, int &productID, int &productVersion)
{
if (joyhandle != nullptr)
{
vendorID = SDL_JoystickGetVendor(joyhandle);
productID = SDL_JoystickGetProduct(joyhandle);
productVersion = SDL_JoystickGetProductVersion(joyhandle);
vendorID = SDL_GetJoystickVendor(joyhandle);
productID = SDL_GetJoystickProduct(joyhandle);
productVersion = SDL_GetJoystickProductVersion(joyhandle);
}
else
{
@@ -479,76 +437,13 @@ void Joystick::getDeviceInfo(int &vendorID, int &productID, int &productVersion)
}
}
bool Joystick::checkCreateHaptic()
bool Joystick::isVibrationSupported()
{
if (!isConnected())
return false;
if (!SDL_WasInit(SDL_INIT_HAPTIC) && SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0)
return false;
if (haptic && SDL_HapticIndex(haptic) != -1)
return true;
if (haptic)
{
SDL_HapticClose(haptic);
haptic = nullptr;
}
haptic = SDL_HapticOpenFromJoystick(joyhandle);
vibration = Vibration();
return haptic != nullptr;
}
bool Joystick::isVibrationSupported()
{
#if SDL_VERSION_ATLEAST(2, 0, 18)
if (isConnected() && SDL_JoystickHasRumble(joyhandle) == SDL_TRUE)
return true;
#endif
if (!checkCreateHaptic())
return false;
unsigned int features = SDL_HapticQuery(haptic);
if ((features & SDL_HAPTIC_LEFTRIGHT) != 0)
return true;
// Some gamepad drivers only support left/right motors via a custom effect.
if (isGamepad() && (features & SDL_HAPTIC_CUSTOM) != 0)
return true;
// Test for simple sine wave support as a last resort.
if ((features & SDL_HAPTIC_SINE) != 0)
return true;
return false;
}
bool Joystick::runVibrationEffect()
{
if (vibration.id != -1)
{
if (SDL_HapticUpdateEffect(haptic, vibration.id, &vibration.effect) == 0)
{
if (SDL_HapticRunEffect(haptic, vibration.id, 1) == 0)
return true;
}
// If the effect fails to update, we should destroy and re-create it.
SDL_HapticDestroyEffect(haptic, vibration.id);
vibration.id = -1;
}
vibration.id = SDL_HapticNewEffect(haptic, &vibration.effect);
if (vibration.id != -1 && SDL_HapticRunEffect(haptic, vibration.id, 1) == 0)
return true;
return false;
SDL_PropertiesID props = SDL_GetJoystickProperties(joyhandle);
return SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, false);
}
bool Joystick::setVibration(float left, float right, float duration)
@@ -560,148 +455,39 @@ bool Joystick::setVibration(float left, float right, float duration)
return setVibration();
if (!isConnected())
{
vibration.left = vibration.right = 0.0f;
vibration.endtime = SDL_HAPTIC_INFINITY;
return false;
}
Uint32 length = SDL_HAPTIC_INFINITY;
Uint32 length = LOVE_UINT32_MAX;
if (duration >= 0.0f)
{
float maxduration = (float) (std::numeric_limits<Uint32>::max() / 1000.0);
length = Uint32(std::min(duration, maxduration) * 1000);
}
bool success = false;
if (SDL_JoystickRumble(joyhandle, (Uint16)(left * LOVE_UINT16_MAX), (Uint16)(right * LOVE_UINT16_MAX), length) == 0)
success = true;
if (!success && !checkCreateHaptic())
return false;
unsigned int features = SDL_HapticQuery(haptic);
int axes = SDL_HapticNumAxes(haptic);
if (!success && (features & SDL_HAPTIC_LEFTRIGHT) != 0)
{
memset(&vibration.effect, 0, sizeof(SDL_HapticEffect));
vibration.effect.type = SDL_HAPTIC_LEFTRIGHT;
vibration.effect.leftright.length = length;
vibration.effect.leftright.large_magnitude = Uint16(left * LOVE_UINT16_MAX);
vibration.effect.leftright.small_magnitude = Uint16(right * LOVE_UINT16_MAX);
success = runVibrationEffect();
}
// Some gamepad drivers only give support for controlling individual motors
// through a custom FF effect.
if (!success && isGamepad() && (features & SDL_HAPTIC_CUSTOM) && axes == 2)
{
// NOTE: this may cause issues with drivers which support custom effects
// but aren't similar to https://github.com/d235j/360Controller .
// Custom effect data is clamped to 0x7FFF in SDL.
vibration.data[0] = vibration.data[2] = Uint16(left * 0x7FFF);
vibration.data[1] = vibration.data[3] = Uint16(right * 0x7FFF);
memset(&vibration.effect, 0, sizeof(SDL_HapticEffect));
vibration.effect.type = SDL_HAPTIC_CUSTOM;
vibration.effect.custom.length = length;
vibration.effect.custom.channels = 2;
vibration.effect.custom.period = 10;
vibration.effect.custom.samples = 2;
vibration.effect.custom.data = vibration.data;
success = runVibrationEffect();
}
// Fall back to a simple sine wave if all else fails. This only supports a
// single strength value.
if (!success && (features & SDL_HAPTIC_SINE) != 0)
{
memset(&vibration.effect, 0, sizeof(SDL_HapticEffect));
vibration.effect.type = SDL_HAPTIC_SINE;
vibration.effect.periodic.length = length;
vibration.effect.periodic.period = 10;
float strength = std::max(left, right);
vibration.effect.periodic.magnitude = Sint16(strength * 0x7FFF);
success = runVibrationEffect();
}
if (success)
{
vibration.left = left;
vibration.right = right;
if (length == SDL_HAPTIC_INFINITY)
vibration.endtime = SDL_HAPTIC_INFINITY;
else
vibration.endtime = SDL_GetTicks() + length;
}
else
{
vibration.left = vibration.right = 0.0f;
vibration.endtime = SDL_HAPTIC_INFINITY;
}
return success;
return SDL_RumbleJoystick(joyhandle, (Uint16)(left * LOVE_UINT16_MAX), (Uint16)(right * LOVE_UINT16_MAX), length) == 0;
}
bool Joystick::setVibration()
{
bool success = false;
if (!success)
success = isConnected() && SDL_JoystickRumble(joyhandle, 0, 0, 0) == 0;
if (!success && SDL_WasInit(SDL_INIT_HAPTIC) && haptic && SDL_HapticIndex(haptic) != -1)
success = (SDL_HapticStopEffect(haptic, vibration.id) == 0);
if (success)
vibration.left = vibration.right = 0.0f;
return success;
return isConnected() && SDL_RumbleJoystick(joyhandle, 0, 0, 0) == 0;
}
void Joystick::getVibration(float &left, float &right)
{
if (vibration.endtime != SDL_HAPTIC_INFINITY)
{
// With some drivers, the effect physically stops at the right time, but
// SDL_HapticGetEffectStatus still thinks it's playing. So we explicitly
// stop it once it's done, just to be sure.
if (SDL_TICKS_PASSED(SDL_GetTicks(), vibration.endtime))
{
setVibration();
vibration.endtime = SDL_HAPTIC_INFINITY;
}
}
// Check if the haptic effect has stopped playing.
int id = vibration.id;
if (!haptic || id == -1 || SDL_HapticGetEffectStatus(haptic, id) != 1)
vibration.left = vibration.right = 0.0f;
left = vibration.left;
right = vibration.right;
// Deprecated.
left = 0.0f;
right = 0.0f;
}
bool Joystick::hasSensor(Sensor::SensorType type) const
{
#if SDL_VERSION_ATLEAST(2, 0, 14) && defined(LOVE_ENABLE_SENSOR)
#if defined(LOVE_ENABLE_SENSOR)
using SDLSensor = love::sensor::sdl::Sensor;
if (!isGamepad())
return false;
return SDL_GameControllerHasSensor(controller, SDLSensor::convert(type)) == SDL_TRUE;
return SDL_GamepadHasSensor(controller, SDLSensor::convert(type));
#else
return false;
#endif
@@ -709,13 +495,13 @@ bool Joystick::hasSensor(Sensor::SensorType type) const
bool Joystick::isSensorEnabled(Sensor::SensorType type) const
{
#if SDL_VERSION_ATLEAST(2, 0, 14) && defined(LOVE_ENABLE_SENSOR)
#if defined(LOVE_ENABLE_SENSOR)
using SDLSensor = love::sensor::sdl::Sensor;
if (!isGamepad())
return false;
return SDL_GameControllerIsSensorEnabled(controller, SDLSensor::convert(type)) == SDL_TRUE;
return SDL_GamepadSensorEnabled(controller, SDLSensor::convert(type));
#else
return false;
#endif
@@ -723,13 +509,13 @@ bool Joystick::isSensorEnabled(Sensor::SensorType type) const
void Joystick::setSensorEnabled(Sensor::SensorType type, bool enabled)
{
#if SDL_VERSION_ATLEAST(2, 0, 14) && defined(LOVE_ENABLE_SENSOR)
#if defined(LOVE_ENABLE_SENSOR)
using SDLSensor = love::sensor::sdl::Sensor;
if (!isGamepad())
throw love::Exception("Sensor is only supported on gamepad");
if (SDL_GameControllerSetSensorEnabled(controller, SDLSensor::convert(type), enabled ? SDL_TRUE : SDL_FALSE) != 0)
if (!SDL_SetGamepadSensorEnabled(controller, SDLSensor::convert(type), enabled))
{
const char *name = nullptr;
SDLSensor::getConstant(type, name);
@@ -737,13 +523,13 @@ void Joystick::setSensorEnabled(Sensor::SensorType type, bool enabled)
throw love::Exception("Could not open \"%s\" SDL gamepad sensor (%s)", name, SDL_GetError());
}
#else
throw love::Exception("Compiled version of SDL or LOVE does not support gamepad sensor");
throw love::Exception("Compiled version of LOVE does not support gamepad sensor");
#endif
}
std::vector<float> Joystick::getSensorData(Sensor::SensorType type) const
{
#if SDL_VERSION_ATLEAST(2, 0, 14) && defined(LOVE_ENABLE_SENSOR)
#if defined(LOVE_ENABLE_SENSOR)
using SDLSensor = love::sensor::sdl::Sensor;
if (!isGamepad())
@@ -759,7 +545,7 @@ std::vector<float> Joystick::getSensorData(Sensor::SensorType type) const
throw love::Exception("\"%s\" gamepad sensor is not enabled", name);
}
if (SDL_GameControllerGetSensorData(controller, SDLSensor::convert(type), data.data(), (int) data.size()) != 0)
if (!SDL_GetGamepadSensorData(controller, SDLSensor::convert(type), data.data(), (int) data.size()))
{
const char *name = nullptr;
SDLSensor::getConstant(type, name);
@@ -769,7 +555,7 @@ std::vector<float> Joystick::getSensorData(Sensor::SensorType type) const
return data;
#else
throw love::Exception("Compiled version of SDL or LOVE does not support gamepad sensor");
throw love::Exception("Compiled version of LOVE does not support gamepad sensor");
#endif
}
@@ -783,22 +569,22 @@ bool Joystick::getConstant(Joystick::Hat in, Uint8 &out)
return hats.find(in, out);
}
bool Joystick::getConstant(SDL_GameControllerAxis in, Joystick::GamepadAxis &out)
bool Joystick::getConstant(SDL_GamepadAxis in, Joystick::GamepadAxis &out)
{
return gpAxes.find(in, out);
}
bool Joystick::getConstant(Joystick::GamepadAxis in, SDL_GameControllerAxis &out)
bool Joystick::getConstant(Joystick::GamepadAxis in, SDL_GamepadAxis &out)
{
return gpAxes.find(in, out);
}
bool Joystick::getConstant(SDL_GameControllerButton in, Joystick::GamepadButton &out)
bool Joystick::getConstant(SDL_GamepadButton in, Joystick::GamepadButton &out)
{
return gpButtons.find(in, out);
}
bool Joystick::getConstant(Joystick::GamepadButton in, SDL_GameControllerButton &out)
bool Joystick::getConstant(Joystick::GamepadButton in, SDL_GamepadButton &out)
{
return gpButtons.find(in, out);
}
@@ -818,49 +604,45 @@ EnumMap<Joystick::Hat, Uint8, Joystick::HAT_MAX_ENUM>::Entry Joystick::hatEntrie
EnumMap<Joystick::Hat, Uint8, Joystick::HAT_MAX_ENUM> Joystick::hats(Joystick::hatEntries, sizeof(Joystick::hatEntries));
EnumMap<Joystick::GamepadAxis, SDL_GameControllerAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM>::Entry Joystick::gpAxisEntries[] =
EnumMap<Joystick::GamepadAxis, SDL_GamepadAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM>::Entry Joystick::gpAxisEntries[] =
{
{Joystick::GAMEPAD_AXIS_LEFTX, SDL_CONTROLLER_AXIS_LEFTX},
{Joystick::GAMEPAD_AXIS_LEFTY, SDL_CONTROLLER_AXIS_LEFTY},
{Joystick::GAMEPAD_AXIS_RIGHTX, SDL_CONTROLLER_AXIS_RIGHTX},
{Joystick::GAMEPAD_AXIS_RIGHTY, SDL_CONTROLLER_AXIS_RIGHTY},
{Joystick::GAMEPAD_AXIS_TRIGGERLEFT, SDL_CONTROLLER_AXIS_TRIGGERLEFT},
{Joystick::GAMEPAD_AXIS_TRIGGERRIGHT, SDL_CONTROLLER_AXIS_TRIGGERRIGHT},
{Joystick::GAMEPAD_AXIS_LEFTX, SDL_GAMEPAD_AXIS_LEFTX},
{Joystick::GAMEPAD_AXIS_LEFTY, SDL_GAMEPAD_AXIS_LEFTY},
{Joystick::GAMEPAD_AXIS_RIGHTX, SDL_GAMEPAD_AXIS_RIGHTX},
{Joystick::GAMEPAD_AXIS_RIGHTY, SDL_GAMEPAD_AXIS_RIGHTY},
{Joystick::GAMEPAD_AXIS_TRIGGERLEFT, SDL_GAMEPAD_AXIS_LEFT_TRIGGER},
{Joystick::GAMEPAD_AXIS_TRIGGERRIGHT, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER},
};
EnumMap<Joystick::GamepadAxis, SDL_GameControllerAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM> Joystick::gpAxes(Joystick::gpAxisEntries, sizeof(Joystick::gpAxisEntries));
EnumMap<Joystick::GamepadAxis, SDL_GamepadAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM> Joystick::gpAxes(Joystick::gpAxisEntries, sizeof(Joystick::gpAxisEntries));
EnumMap<Joystick::GamepadButton, SDL_GameControllerButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM>::Entry Joystick::gpButtonEntries[] =
EnumMap<Joystick::GamepadButton, SDL_GamepadButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM>::Entry Joystick::gpButtonEntries[] =
{
{Joystick::GAMEPAD_BUTTON_A, SDL_CONTROLLER_BUTTON_A},
{Joystick::GAMEPAD_BUTTON_B, SDL_CONTROLLER_BUTTON_B},
{Joystick::GAMEPAD_BUTTON_X, SDL_CONTROLLER_BUTTON_X},
{Joystick::GAMEPAD_BUTTON_Y, SDL_CONTROLLER_BUTTON_Y},
{Joystick::GAMEPAD_BUTTON_BACK, SDL_CONTROLLER_BUTTON_BACK},
{Joystick::GAMEPAD_BUTTON_GUIDE, SDL_CONTROLLER_BUTTON_GUIDE},
{Joystick::GAMEPAD_BUTTON_START, SDL_CONTROLLER_BUTTON_START},
{Joystick::GAMEPAD_BUTTON_LEFTSTICK, SDL_CONTROLLER_BUTTON_LEFTSTICK},
{Joystick::GAMEPAD_BUTTON_RIGHTSTICK, SDL_CONTROLLER_BUTTON_RIGHTSTICK},
{Joystick::GAMEPAD_BUTTON_LEFTSHOULDER, SDL_CONTROLLER_BUTTON_LEFTSHOULDER},
{Joystick::GAMEPAD_BUTTON_RIGHTSHOULDER, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER},
{Joystick::GAMEPAD_BUTTON_DPAD_UP, SDL_CONTROLLER_BUTTON_DPAD_UP},
{Joystick::GAMEPAD_BUTTON_DPAD_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN},
{Joystick::GAMEPAD_BUTTON_DPAD_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT},
{Joystick::GAMEPAD_BUTTON_DPAD_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT},
#if SDL_VERSION_ATLEAST(2, 0, 14)
{Joystick::GAMEPAD_BUTTON_MISC1, SDL_CONTROLLER_BUTTON_MISC1},
{Joystick::GAMEPAD_BUTTON_PADDLE1, SDL_CONTROLLER_BUTTON_PADDLE1},
{Joystick::GAMEPAD_BUTTON_PADDLE2, SDL_CONTROLLER_BUTTON_PADDLE2},
{Joystick::GAMEPAD_BUTTON_PADDLE3, SDL_CONTROLLER_BUTTON_PADDLE3},
{Joystick::GAMEPAD_BUTTON_PADDLE4, SDL_CONTROLLER_BUTTON_PADDLE4},
{Joystick::GAMEPAD_BUTTON_TOUCHPAD, SDL_CONTROLLER_BUTTON_TOUCHPAD},
#endif
{Joystick::GAMEPAD_BUTTON_A, SDL_GAMEPAD_BUTTON_SOUTH},
{Joystick::GAMEPAD_BUTTON_B, SDL_GAMEPAD_BUTTON_EAST},
{Joystick::GAMEPAD_BUTTON_X, SDL_GAMEPAD_BUTTON_WEST},
{Joystick::GAMEPAD_BUTTON_Y, SDL_GAMEPAD_BUTTON_NORTH},
{Joystick::GAMEPAD_BUTTON_BACK, SDL_GAMEPAD_BUTTON_BACK},
{Joystick::GAMEPAD_BUTTON_GUIDE, SDL_GAMEPAD_BUTTON_GUIDE},
{Joystick::GAMEPAD_BUTTON_START, SDL_GAMEPAD_BUTTON_START},
{Joystick::GAMEPAD_BUTTON_LEFTSTICK, SDL_GAMEPAD_BUTTON_LEFT_STICK},
{Joystick::GAMEPAD_BUTTON_RIGHTSTICK, SDL_GAMEPAD_BUTTON_RIGHT_STICK},
{Joystick::GAMEPAD_BUTTON_LEFTSHOULDER, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER},
{Joystick::GAMEPAD_BUTTON_RIGHTSHOULDER, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER},
{Joystick::GAMEPAD_BUTTON_DPAD_UP, SDL_GAMEPAD_BUTTON_DPAD_UP},
{Joystick::GAMEPAD_BUTTON_DPAD_DOWN, SDL_GAMEPAD_BUTTON_DPAD_DOWN},
{Joystick::GAMEPAD_BUTTON_DPAD_LEFT, SDL_GAMEPAD_BUTTON_DPAD_LEFT},
{Joystick::GAMEPAD_BUTTON_DPAD_RIGHT, SDL_GAMEPAD_BUTTON_DPAD_RIGHT},
{Joystick::GAMEPAD_BUTTON_MISC1, SDL_GAMEPAD_BUTTON_MISC1},
{Joystick::GAMEPAD_BUTTON_PADDLE1, SDL_GAMEPAD_BUTTON_LEFT_PADDLE1},
{Joystick::GAMEPAD_BUTTON_PADDLE2, SDL_GAMEPAD_BUTTON_LEFT_PADDLE2},
{Joystick::GAMEPAD_BUTTON_PADDLE3, SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1},
{Joystick::GAMEPAD_BUTTON_PADDLE4, SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2},
{Joystick::GAMEPAD_BUTTON_TOUCHPAD, SDL_GAMEPAD_BUTTON_TOUCHPAD},
};
EnumMap<Joystick::GamepadButton, SDL_GameControllerButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM> Joystick::gpButtons(Joystick::gpButtonEntries, sizeof(Joystick::gpButtonEntries));
EnumMap<Joystick::GamepadButton, SDL_GamepadButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM> Joystick::gpButtons(Joystick::gpButtonEntries, sizeof(Joystick::gpButtonEntries));
} // sdl
} // joystick
} // love
#endif // !SDL_VERSION_ATLEAST(3, 0, 0)
+10 -39
View File
@@ -24,15 +24,10 @@
// LOVE
#include "joystick/Joystick.h"
#include "common/EnumMap.h"
#include "common/int.h"
// SDL
#if __has_include(<SDL3/SDL.h>)
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#endif
#if !SDL_VERSION_ATLEAST(3, 0, 0)
namespace love
{
@@ -46,7 +41,6 @@ class Joystick : public love::joystick::Joystick
public:
Joystick(int id);
Joystick(int id, int joyindex);
virtual ~Joystick();
@@ -104,22 +98,18 @@ public:
static bool getConstant(Hat in, Uint8 &out);
static bool getConstant(Uint8 in, Hat &out);
static bool getConstant(SDL_GameControllerAxis in, GamepadAxis &out);
static bool getConstant(GamepadAxis in, SDL_GameControllerAxis &out);
static bool getConstant(SDL_GamepadAxis in, GamepadAxis &out);
static bool getConstant(GamepadAxis in, SDL_GamepadAxis &out);
static bool getConstant(SDL_GameControllerButton in, GamepadButton &out);
static bool getConstant(GamepadButton in, SDL_GameControllerButton &out);
static bool getConstant(SDL_GamepadButton in, GamepadButton &out);
static bool getConstant(GamepadButton in, SDL_GamepadButton &out);
private:
Joystick() {}
bool checkCreateHaptic();
bool runVibrationEffect();
SDL_Joystick *joyhandle;
SDL_GameController *controller;
SDL_Haptic *haptic;
SDL_Gamepad *controller;
JoystickType joystickType;
@@ -129,31 +119,14 @@ private:
std::string name;
struct Vibration
{
float left = 0.0f;
float right = 0.0f;
SDL_HapticEffect effect;
Uint16 data[4];
int id = -1;
Uint32 endtime = SDL_HAPTIC_INFINITY;
// Old versions of VS2013 have trouble with initializing these in-line.
Vibration()
: effect()
, data()
{}
} vibration;
static EnumMap<Hat, Uint8, Joystick::HAT_MAX_ENUM>::Entry hatEntries[];
static EnumMap<Hat, Uint8, Joystick::HAT_MAX_ENUM> hats;
static EnumMap<GamepadAxis, SDL_GameControllerAxis, GAMEPAD_AXIS_MAX_ENUM>::Entry gpAxisEntries[];
static EnumMap<GamepadAxis, SDL_GameControllerAxis, GAMEPAD_AXIS_MAX_ENUM> gpAxes;
static EnumMap<GamepadAxis, SDL_GamepadAxis, GAMEPAD_AXIS_MAX_ENUM>::Entry gpAxisEntries[];
static EnumMap<GamepadAxis, SDL_GamepadAxis, GAMEPAD_AXIS_MAX_ENUM> gpAxes;
static EnumMap<GamepadButton, SDL_GameControllerButton, GAMEPAD_BUTTON_MAX_ENUM>::Entry gpButtonEntries[];
static EnumMap<GamepadButton, SDL_GameControllerButton, GAMEPAD_BUTTON_MAX_ENUM> gpButtons;
static EnumMap<GamepadButton, SDL_GamepadButton, GAMEPAD_BUTTON_MAX_ENUM>::Entry gpButtonEntries[];
static EnumMap<GamepadButton, SDL_GamepadButton, GAMEPAD_BUTTON_MAX_ENUM> gpButtons;
};
@@ -161,6 +134,4 @@ private:
} // joystick
} // love
#endif // !SDL_VERSION_ATLEAST(3, 0, 0)
#endif // LOVE_JOYSTICK_SDL_JOYSTICK_H
-103
View File
@@ -21,14 +21,9 @@
#include "common/config.h"
#include "JoystickModule.h"
#include "Joystick.h"
#include "JoystickSDL3.h"
// SDL
#if __has_include(<SDL3/SDL.h>)
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#endif
// C++
#include <sstream>
@@ -47,15 +42,10 @@ namespace sdl
JoystickModule::JoystickModule()
: love::joystick::JoystickModule("love.joystick.sdl")
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD))
#else
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0)
#endif
throw love::Exception("Could not initialize SDL joystick subsystem (%s)", SDL_GetError());
// Initialize any joysticks which are already connected.
#if SDL_VERSION_ATLEAST(3, 0, 0)
int count = 0;
SDL_JoystickID *sticks = SDL_GetJoysticks(&count);
for (int i = 0; i < count; i++)
@@ -66,15 +56,6 @@ JoystickModule::JoystickModule()
// removed via love.event.
SDL_SetJoystickEventsEnabled(true);
SDL_SetGamepadEventsEnabled(true);
#else
for (int i = 0; i < SDL_NumJoysticks(); i++)
addJoystick(i);
// Start joystick event watching. Joysticks are automatically added and
// removed via love.event.
SDL_JoystickEventState(SDL_ENABLE);
SDL_GameControllerEventState(SDL_ENABLE);
#endif
}
JoystickModule::~JoystickModule()
@@ -86,14 +67,7 @@ JoystickModule::~JoystickModule()
stick->release();
}
if (SDL_WasInit(SDL_INIT_HAPTIC) != 0)
SDL_QuitSubSystem(SDL_INIT_HAPTIC);
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD);
#else
SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
#endif
}
love::joystick::Joystick *JoystickModule::getJoystick(int joyindex)
@@ -134,11 +108,7 @@ love::joystick::Joystick *JoystickModule::getJoystickFromID(int instanceid)
love::joystick::Joystick *JoystickModule::addJoystick(int64 deviceid)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (deviceid == 0)
#else
if (deviceid < 0 || (int) deviceid >= SDL_NumJoysticks())
#endif
return nullptr;
std::string guidstr = getDeviceGUID(deviceid);
@@ -158,11 +128,7 @@ love::joystick::Joystick *JoystickModule::addJoystick(int64 deviceid)
if (!joystick)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
joystick = new Joystick((int) joysticks.size());
#else
joystick = new Joystick((int) joysticks.size());
#endif
joysticks.push_back(joystick);
}
@@ -218,17 +184,10 @@ bool JoystickModule::setGamepadMapping(const std::string &guid, Joystick::Gamepa
if (guid.length() != 32)
throw love::Exception("Invalid joystick GUID: %s", guid.c_str());
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GUID sdlguid = SDL_StringToGUID(guid.c_str());
std::string mapstr;
char *sdlmapstr = SDL_GetGamepadMappingForGUID(sdlguid);
#else
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(guid.c_str());
std::string mapstr;
char *sdlmapstr = SDL_GameControllerMappingForGUID(sdlguid);
#endif
if (sdlmapstr)
{
mapstr = sdlmapstr;
@@ -310,11 +269,7 @@ bool JoystickModule::setGamepadMapping(const std::string &guid, Joystick::Gamepa
}
// 1 == added, 0 == updated, -1 == error.
#if SDL_VERSION_ATLEAST(3, 0, 0)
int status = SDL_AddGamepadMapping(mapstr.c_str());
#else
int status = SDL_GameControllerAddMapping(mapstr.c_str());
#endif
if (status != -1)
recentGamepadGUIDs[guid] = true;
@@ -329,13 +284,8 @@ bool JoystickModule::setGamepadMapping(const std::string &guid, Joystick::Gamepa
std::string JoystickModule::stringFromGamepadInput(Joystick::GamepadInput gpinput) const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GamepadAxis sdlaxis;
SDL_GamepadButton sdlbutton;
#else
SDL_GameControllerAxis sdlaxis;
SDL_GameControllerButton sdlbutton;
#endif
const char *gpinputname = nullptr;
@@ -343,19 +293,11 @@ std::string JoystickModule::stringFromGamepadInput(Joystick::GamepadInput gpinpu
{
case Joystick::INPUT_TYPE_AXIS:
if (Joystick::getConstant(gpinput.axis, sdlaxis))
#if SDL_VERSION_ATLEAST(3, 0, 0)
gpinputname = SDL_GetGamepadStringForAxis(sdlaxis);
#else
gpinputname = SDL_GameControllerGetStringForAxis(sdlaxis);
#endif
break;
case Joystick::INPUT_TYPE_BUTTON:
if (Joystick::getConstant(gpinput.button, sdlbutton))
#if SDL_VERSION_ATLEAST(3, 0, 0)
gpinputname = SDL_GetGamepadStringForButton(sdlbutton);
#else
gpinputname = SDL_GameControllerGetStringForButton(sdlbutton);
#endif
break;
default:
break;
@@ -405,7 +347,6 @@ void JoystickModule::checkGamepads(const std::string &guid) const
// Make sure all connected joysticks of a certain guid that are
// gamepad-capable are opened as such.
#if SDL_VERSION_ATLEAST(3, 0, 0)
int count = 0;
SDL_JoystickID *sdlsticks = SDL_GetJoysticks(&count);
for (int d_index = 0; d_index < count; d_index++)
@@ -414,14 +355,6 @@ void JoystickModule::checkGamepads(const std::string &guid) const
continue;
auto sdlid = sdlsticks[d_index];
#else
for (int d_index = 0; d_index < SDL_NumJoysticks(); d_index++)
{
if (!SDL_IsGameController(d_index))
continue;
auto sdlid = d_index;
#endif
if (guid.compare(getDeviceGUID(sdlid)) != 0)
continue;
@@ -433,7 +366,6 @@ void JoystickModule::checkGamepads(const std::string &guid) const
// Big hack time: open the index as a game controller and compare
// the underlying joystick handle to the active stick's.
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Gamepad *controller = SDL_OpenGamepad(sdlid);
if (controller == nullptr)
continue;
@@ -443,17 +375,6 @@ void JoystickModule::checkGamepads(const std::string &guid) const
SDL_Joystick *sdlstick = SDL_GetGamepadJoystick(controller);
bool open_gamepad = (sdlstick == (SDL_Joystick *) stick->getHandle());
SDL_CloseGamepad(controller);
#else
SDL_GameController *controller = SDL_GameControllerOpen(d_index);
if (controller == nullptr)
continue;
// GameController objects are reference-counted in SDL, so we don't want to
// have a joystick open when trying to re-initialize it
SDL_Joystick *sdlstick = SDL_GameControllerGetJoystick(controller);
bool open_gamepad = (sdlstick == (SDL_Joystick *) stick->getHandle());
SDL_GameControllerClose(controller);
#endif
// open as gamepad if necessary
if (open_gamepad)
@@ -467,22 +388,12 @@ std::string JoystickModule::getDeviceGUID(int64 deviceid) const
// SDL_JoystickGetGUIDString uses 32 bytes plus the null terminator.
char guidstr[33] = {'\0'};
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (deviceid <= 0)
return std::string("");
// SDL's GUIDs identify *classes* of devices, instead of unique devices.
SDL_GUID guid = SDL_GetJoystickGUIDForID((SDL_JoystickID)deviceid);
SDL_GUIDToString(guid, guidstr, sizeof(guidstr));
#else
int deviceindex = (int) deviceid;
if (deviceindex < 0 || deviceindex >= SDL_NumJoysticks())
return std::string("");
// SDL2's GUIDs identify *classes* of devices, instead of unique devices.
SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(deviceindex);
SDL_JoystickGetGUIDString(guid, guidstr, sizeof(guidstr));
#endif
return std::string(guidstr);
}
@@ -527,11 +438,7 @@ void JoystickModule::loadGamepadMappings(const std::string &mappings)
mapping.erase(pstartpos, pendpos - pstartpos + 1);
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (SDL_AddGamepadMapping(mapping.c_str()) != -1)
#else
if (SDL_GameControllerAddMapping(mapping.c_str()) != -1)
#endif
{
success = true;
std::string guid = mapping.substr(0, mapping.find_first_of(','));
@@ -551,13 +458,8 @@ void JoystickModule::loadGamepadMappings(const std::string &mappings)
std::string JoystickModule::getGamepadMappingString(const std::string &guid) const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GUID sdlguid = SDL_StringToGUID(guid.c_str());
char *sdlmapping = SDL_GetGamepadMappingForGUID(sdlguid);
#else
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(guid.c_str());
char *sdlmapping = SDL_GameControllerMappingForGUID(sdlguid);
#endif
if (sdlmapping == nullptr)
return "";
@@ -580,13 +482,8 @@ std::string JoystickModule::saveGamepadMappings()
for (const auto &g : recentGamepadGUIDs)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GUID sdlguid = SDL_StringToGUID(g.first.c_str());
char *sdlmapping = SDL_GetGamepadMappingForGUID(sdlguid);
#else
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(g.first.c_str());
char *sdlmapping = SDL_GameControllerMappingForGUID(sdlguid);
#endif
if (sdlmapping == nullptr)
continue;
+2 -2
View File
@@ -69,10 +69,10 @@ private:
std::string getDeviceGUID(int64 deviceid) const;
// Lists of currently connected Joysticks.
std::vector<Joystick *> activeSticks;
std::vector<love::joystick::Joystick *> activeSticks;
// Persistent list of all Joysticks which have been connected at some point.
std::list<Joystick *> joysticks;
std::list<love::joystick::Joystick *> joysticks;
// Persistent map indicating GUIDs for Gamepads which have been connected or
// modified at some point.
-658
View File
@@ -1,658 +0,0 @@
/**
* Copyright (c) 2006-2023 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "common/config.h"
#include "JoystickSDL3.h"
#include "common/int.h"
#include "sensor/sdl/Sensor.h"
// SDL
#if __has_include(<SDL3/SDL_version.h>)
#include <SDL3/SDL_version.h>
#else
#include <SDL_version.h>
#endif
#if SDL_VERSION_ATLEAST(3, 0, 0)
#include <SDL3/SDL_guid.h>
// C++
#include <algorithm>
#include <limits>
namespace love
{
namespace joystick
{
namespace sdl
{
Joystick::Joystick(int id)
: joyhandle(nullptr)
, controller(nullptr)
, joystickType(JOYSTICK_TYPE_UNKNOWN)
, instanceid(-1)
, id(id)
{
}
Joystick::~Joystick()
{
close();
}
bool Joystick::open(int64 deviceid)
{
close();
joyhandle = SDL_OpenJoystick((SDL_JoystickID) deviceid);
if (joyhandle)
{
instanceid = SDL_GetJoystickID(joyhandle);
// SDL_JoystickGetGUIDString uses 32 bytes plus the null terminator.
char cstr[33];
SDL_GUID sdlguid = SDL_GetJoystickGUID(joyhandle);
SDL_GUIDToString(sdlguid, cstr, (int) sizeof(cstr));
pguid = std::string(cstr);
// See if SDL thinks this is a Game Controller.
openGamepad(deviceid);
// Prefer the Joystick name for consistency.
const char *joyname = SDL_GetJoystickName(joyhandle);
if (!joyname && controller)
joyname = SDL_GetGamepadName(controller);
if (joyname)
name = joyname;
switch (SDL_GetJoystickType(joyhandle))
{
case SDL_JOYSTICK_TYPE_GAMEPAD:
joystickType = JOYSTICK_TYPE_GAMEPAD;
break;
case SDL_JOYSTICK_TYPE_WHEEL:
joystickType = JOYSTICK_TYPE_WHEEL;
break;
case SDL_JOYSTICK_TYPE_ARCADE_STICK:
joystickType = JOYSTICK_TYPE_ARCADE_STICK;
break;
case SDL_JOYSTICK_TYPE_FLIGHT_STICK:
joystickType = JOYSTICK_TYPE_FLIGHT_STICK;
break;
case SDL_JOYSTICK_TYPE_DANCE_PAD:
joystickType = JOYSTICK_TYPE_DANCE_PAD;
break;
case SDL_JOYSTICK_TYPE_GUITAR:
joystickType = JOYSTICK_TYPE_GUITAR;
break;
case SDL_JOYSTICK_TYPE_DRUM_KIT:
joystickType = JOYSTICK_TYPE_DRUM_KIT;
break;
case SDL_JOYSTICK_TYPE_ARCADE_PAD:
joystickType = JOYSTICK_TYPE_ARCADE_PAD;
break;
case SDL_JOYSTICK_TYPE_THROTTLE:
joystickType = JOYSTICK_TYPE_THROTTLE;
break;
default:
joystickType = JOYSTICK_TYPE_UNKNOWN;
break;
}
}
return isConnected();
}
void Joystick::close()
{
if (controller)
SDL_CloseGamepad(controller);
if (joyhandle)
SDL_CloseJoystick(joyhandle);
joyhandle = nullptr;
controller = nullptr;
instanceid = -1;
}
bool Joystick::isConnected() const
{
return joyhandle != nullptr && SDL_JoystickConnected(joyhandle);
}
const char *Joystick::getName() const
{
return name.c_str();
}
Joystick::JoystickType Joystick::getJoystickType() const
{
return joystickType;
}
int Joystick::getAxisCount() const
{
return isConnected() ? SDL_GetNumJoystickAxes(joyhandle) : 0;
}
int Joystick::getButtonCount() const
{
return isConnected() ? SDL_GetNumJoystickButtons(joyhandle) : 0;
}
int Joystick::getHatCount() const
{
return isConnected() ? SDL_GetNumJoystickHats(joyhandle) : 0;
}
float Joystick::getAxis(int axisindex) const
{
if (!isConnected() || axisindex < 0 || axisindex >= getAxisCount())
return 0;
return clampval(((float) SDL_GetJoystickAxis(joyhandle, axisindex))/32768.0f);
}
std::vector<float> Joystick::getAxes() const
{
std::vector<float> axes;
int count = getAxisCount();
if (!isConnected() || count <= 0)
return axes;
axes.reserve(count);
for (int i = 0; i < count; i++)
axes.push_back(clampval(((float) SDL_GetJoystickAxis(joyhandle, i))/32768.0f));
return axes;
}
Joystick::Hat Joystick::getHat(int hatindex) const
{
Hat h = HAT_INVALID;
if (!isConnected() || hatindex < 0 || hatindex >= getHatCount())
return h;
getConstant(SDL_GetJoystickHat(joyhandle, hatindex), h);
return h;
}
bool Joystick::isDown(const std::vector<int> &buttonlist) const
{
if (!isConnected())
return false;
int numbuttons = getButtonCount();
for (int button : buttonlist)
{
if (button < 0 || button >= numbuttons)
continue;
if (SDL_GetJoystickButton(joyhandle, button))
return true;
}
return false;
}
void Joystick::setPlayerIndex(int index)
{
if (!isConnected())
return;
SDL_SetJoystickPlayerIndex(joyhandle, index);
}
int Joystick::getPlayerIndex() const
{
if (!isConnected())
return -1;
return SDL_GetJoystickPlayerIndex(joyhandle);
}
bool Joystick::openGamepad(int64 deviceid)
{
if (!SDL_IsGamepad((SDL_JoystickID)deviceid))
return false;
if (isGamepad())
{
SDL_CloseGamepad(controller);
controller = nullptr;
}
controller = SDL_OpenGamepad((SDL_JoystickID)deviceid);
return isGamepad();
}
bool Joystick::isGamepad() const
{
return controller != nullptr;
}
Joystick::GamepadType Joystick::getGamepadType() const
{
if (controller == nullptr)
return GAMEPAD_TYPE_UNKNOWN;
switch (SDL_GetGamepadType(controller))
{
case SDL_GAMEPAD_TYPE_UNKNOWN: return GAMEPAD_TYPE_UNKNOWN;
case SDL_GAMEPAD_TYPE_XBOX360: return GAMEPAD_TYPE_XBOX360;
case SDL_GAMEPAD_TYPE_XBOXONE: return GAMEPAD_TYPE_XBOXONE;
case SDL_GAMEPAD_TYPE_PS3: return GAMEPAD_TYPE_PS3;
case SDL_GAMEPAD_TYPE_PS4: return GAMEPAD_TYPE_PS4;
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO: return GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
case SDL_GAMEPAD_TYPE_PS5: return GAMEPAD_TYPE_PS5;
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT: return GAMEPAD_TYPE_JOYCON_LEFT;
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT: return GAMEPAD_TYPE_JOYCON_RIGHT;
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR: return GAMEPAD_TYPE_JOYCON_PAIR;
default: return GAMEPAD_TYPE_UNKNOWN;
}
return GAMEPAD_TYPE_UNKNOWN;
}
float Joystick::getGamepadAxis(love::joystick::Joystick::GamepadAxis axis) const
{
if (!isConnected() || !isGamepad())
return 0.f;
SDL_GamepadAxis sdlaxis;
if (!getConstant(axis, sdlaxis))
return 0.f;
Sint16 value = SDL_GetGamepadAxis(controller, sdlaxis);
return clampval((float) value / 32768.0f);
}
bool Joystick::isGamepadDown(const std::vector<GamepadButton> &blist) const
{
if (!isConnected() || !isGamepad())
return false;
SDL_GamepadButton sdlbutton;
for (GamepadButton button : blist)
{
if (!getConstant(button, sdlbutton))
continue;
if (SDL_GetGamepadButton(controller, sdlbutton))
return true;
}
return false;
}
Joystick::JoystickInput Joystick::getGamepadMapping(const GamepadInput &input) const
{
Joystick::JoystickInput jinput;
jinput.type = INPUT_TYPE_MAX_ENUM;
if (!isGamepad())
return jinput;
SDL_GamepadButton sdlbutton = SDL_GAMEPAD_BUTTON_INVALID;
SDL_GamepadAxis sdlaxis = SDL_GAMEPAD_AXIS_INVALID;
switch (input.type)
{
case INPUT_TYPE_BUTTON:
getConstant(input.button, sdlbutton);
break;
case INPUT_TYPE_AXIS:
getConstant(input.axis, sdlaxis);
break;
default:
break;
}
int bindcount = 0;
SDL_GamepadBinding **sdlbindings = SDL_GetGamepadBindings(controller, &bindcount);
for (int i = 0; i < bindcount; i++)
{
const SDL_GamepadBinding *b = sdlbindings[i];
if ((input.type == INPUT_TYPE_BUTTON && b->output_type == SDL_GAMEPAD_BINDTYPE_BUTTON && b->output.button == sdlbutton)
|| (input.type == INPUT_TYPE_AXIS && b->output_type == SDL_GAMEPAD_BINDTYPE_AXIS && b->output.axis.axis == sdlaxis))
{
switch (b->input_type)
{
case SDL_GAMEPAD_BINDTYPE_BUTTON:
jinput.type = INPUT_TYPE_BUTTON;
jinput.button = b->input.button;
break;
case SDL_GAMEPAD_BINDTYPE_AXIS:
jinput.type = INPUT_TYPE_AXIS;
jinput.axis = b->input.axis.axis;
break;
case SDL_GAMEPAD_BINDTYPE_HAT:
if (getConstant(b->input.hat.hat_mask, jinput.hat.value))
{
jinput.type = INPUT_TYPE_HAT;
jinput.hat.index = b->input.hat.hat;
}
break;
case SDL_GAMEPAD_BINDTYPE_NONE:
default:
break;
}
break;
}
}
SDL_free(sdlbindings);
return jinput;
}
std::string Joystick::getGamepadMappingString() const
{
char *sdlmapping = nullptr;
if (controller != nullptr)
sdlmapping = SDL_GetGamepadMapping(controller);
if (sdlmapping == nullptr)
{
SDL_GUID sdlguid = SDL_StringToGUID(pguid.c_str());
sdlmapping = SDL_GetGamepadMappingForGUID(sdlguid);
}
if (sdlmapping == nullptr)
return "";
std::string mappingstr(sdlmapping);
SDL_free(sdlmapping);
// Matches SDL_GameControllerAddMappingsFromRW.
if (mappingstr.find_last_of(',') != mappingstr.length() - 1)
mappingstr += ",";
if (mappingstr.find("platform:") == std::string::npos)
mappingstr += "platform:" + std::string(SDL_GetPlatform());
return mappingstr;
}
void *Joystick::getHandle() const
{
return joyhandle;
}
std::string Joystick::getGUID() const
{
// SDL2's GUIDs identify *classes* of devices, instead of unique devices.
return pguid;
}
int Joystick::getInstanceID() const
{
return instanceid;
}
int Joystick::getID() const
{
return id;
}
void Joystick::getDeviceInfo(int &vendorID, int &productID, int &productVersion) const
{
if (joyhandle != nullptr)
{
vendorID = SDL_GetJoystickVendor(joyhandle);
productID = SDL_GetJoystickProduct(joyhandle);
productVersion = SDL_GetJoystickProductVersion(joyhandle);
}
else
{
vendorID = 0;
productID = 0;
productVersion = 0;
}
}
bool Joystick::isVibrationSupported()
{
if (!isConnected())
return false;
SDL_PropertiesID props = SDL_GetJoystickProperties(joyhandle);
return SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, false);
}
bool Joystick::setVibration(float left, float right, float duration)
{
left = std::min(std::max(left, 0.0f), 1.0f);
right = std::min(std::max(right, 0.0f), 1.0f);
if (left == 0.0f && right == 0.0f)
return setVibration();
if (!isConnected())
return false;
Uint32 length = LOVE_UINT32_MAX;
if (duration >= 0.0f)
{
float maxduration = (float) (std::numeric_limits<Uint32>::max() / 1000.0);
length = Uint32(std::min(duration, maxduration) * 1000);
}
return SDL_RumbleJoystick(joyhandle, (Uint16)(left * LOVE_UINT16_MAX), (Uint16)(right * LOVE_UINT16_MAX), length) == 0;
}
bool Joystick::setVibration()
{
return isConnected() && SDL_RumbleJoystick(joyhandle, 0, 0, 0) == 0;
}
void Joystick::getVibration(float &left, float &right)
{
// Deprecated.
left = 0.0f;
right = 0.0f;
}
bool Joystick::hasSensor(Sensor::SensorType type) const
{
#if defined(LOVE_ENABLE_SENSOR)
using SDLSensor = love::sensor::sdl::Sensor;
if (!isGamepad())
return false;
return SDL_GamepadHasSensor(controller, SDLSensor::convert(type));
#else
return false;
#endif
}
bool Joystick::isSensorEnabled(Sensor::SensorType type) const
{
#if defined(LOVE_ENABLE_SENSOR)
using SDLSensor = love::sensor::sdl::Sensor;
if (!isGamepad())
return false;
return SDL_GamepadSensorEnabled(controller, SDLSensor::convert(type));
#else
return false;
#endif
}
void Joystick::setSensorEnabled(Sensor::SensorType type, bool enabled)
{
#if defined(LOVE_ENABLE_SENSOR)
using SDLSensor = love::sensor::sdl::Sensor;
if (!isGamepad())
throw love::Exception("Sensor is only supported on gamepad");
if (!SDL_SetGamepadSensorEnabled(controller, SDLSensor::convert(type), enabled))
{
const char *name = nullptr;
SDLSensor::getConstant(type, name);
throw love::Exception("Could not open \"%s\" SDL gamepad sensor (%s)", name, SDL_GetError());
}
#else
throw love::Exception("Compiled version of LOVE does not support gamepad sensor");
#endif
}
std::vector<float> Joystick::getSensorData(Sensor::SensorType type) const
{
#if defined(LOVE_ENABLE_SENSOR)
using SDLSensor = love::sensor::sdl::Sensor;
if (!isGamepad())
throw love::Exception("Sensor is only supported on gamepad");
std::vector<float> data(3);
if (!isSensorEnabled(type))
{
const char *name = nullptr;
SDLSensor::getConstant(type, name);
throw love::Exception("\"%s\" gamepad sensor is not enabled", name);
}
if (!SDL_GetGamepadSensorData(controller, SDLSensor::convert(type), data.data(), (int) data.size()))
{
const char *name = nullptr;
SDLSensor::getConstant(type, name);
throw love::Exception("Could not get \"%s\" SDL gamepad sensor data (%s)", name, SDL_GetError());
}
return data;
#else
throw love::Exception("Compiled version of LOVE does not support gamepad sensor");
#endif
}
bool Joystick::getConstant(Uint8 in, Joystick::Hat &out)
{
return hats.find(in, out);
}
bool Joystick::getConstant(Joystick::Hat in, Uint8 &out)
{
return hats.find(in, out);
}
bool Joystick::getConstant(SDL_GamepadAxis in, Joystick::GamepadAxis &out)
{
return gpAxes.find(in, out);
}
bool Joystick::getConstant(Joystick::GamepadAxis in, SDL_GamepadAxis &out)
{
return gpAxes.find(in, out);
}
bool Joystick::getConstant(SDL_GamepadButton in, Joystick::GamepadButton &out)
{
return gpButtons.find(in, out);
}
bool Joystick::getConstant(Joystick::GamepadButton in, SDL_GamepadButton &out)
{
return gpButtons.find(in, out);
}
EnumMap<Joystick::Hat, Uint8, Joystick::HAT_MAX_ENUM>::Entry Joystick::hatEntries[] =
{
{Joystick::HAT_CENTERED, SDL_HAT_CENTERED},
{Joystick::HAT_UP, SDL_HAT_UP},
{Joystick::HAT_RIGHT, SDL_HAT_RIGHT},
{Joystick::HAT_DOWN, SDL_HAT_DOWN},
{Joystick::HAT_LEFT, SDL_HAT_LEFT},
{Joystick::HAT_RIGHTUP, SDL_HAT_RIGHTUP},
{Joystick::HAT_RIGHTDOWN, SDL_HAT_RIGHTDOWN},
{Joystick::HAT_LEFTUP, SDL_HAT_LEFTUP},
{Joystick::HAT_LEFTDOWN, SDL_HAT_LEFTDOWN},
};
EnumMap<Joystick::Hat, Uint8, Joystick::HAT_MAX_ENUM> Joystick::hats(Joystick::hatEntries, sizeof(Joystick::hatEntries));
EnumMap<Joystick::GamepadAxis, SDL_GamepadAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM>::Entry Joystick::gpAxisEntries[] =
{
{Joystick::GAMEPAD_AXIS_LEFTX, SDL_GAMEPAD_AXIS_LEFTX},
{Joystick::GAMEPAD_AXIS_LEFTY, SDL_GAMEPAD_AXIS_LEFTY},
{Joystick::GAMEPAD_AXIS_RIGHTX, SDL_GAMEPAD_AXIS_RIGHTX},
{Joystick::GAMEPAD_AXIS_RIGHTY, SDL_GAMEPAD_AXIS_RIGHTY},
{Joystick::GAMEPAD_AXIS_TRIGGERLEFT, SDL_GAMEPAD_AXIS_LEFT_TRIGGER},
{Joystick::GAMEPAD_AXIS_TRIGGERRIGHT, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER},
};
EnumMap<Joystick::GamepadAxis, SDL_GamepadAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM> Joystick::gpAxes(Joystick::gpAxisEntries, sizeof(Joystick::gpAxisEntries));
EnumMap<Joystick::GamepadButton, SDL_GamepadButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM>::Entry Joystick::gpButtonEntries[] =
{
{Joystick::GAMEPAD_BUTTON_A, SDL_GAMEPAD_BUTTON_SOUTH},
{Joystick::GAMEPAD_BUTTON_B, SDL_GAMEPAD_BUTTON_EAST},
{Joystick::GAMEPAD_BUTTON_X, SDL_GAMEPAD_BUTTON_WEST},
{Joystick::GAMEPAD_BUTTON_Y, SDL_GAMEPAD_BUTTON_NORTH},
{Joystick::GAMEPAD_BUTTON_BACK, SDL_GAMEPAD_BUTTON_BACK},
{Joystick::GAMEPAD_BUTTON_GUIDE, SDL_GAMEPAD_BUTTON_GUIDE},
{Joystick::GAMEPAD_BUTTON_START, SDL_GAMEPAD_BUTTON_START},
{Joystick::GAMEPAD_BUTTON_LEFTSTICK, SDL_GAMEPAD_BUTTON_LEFT_STICK},
{Joystick::GAMEPAD_BUTTON_RIGHTSTICK, SDL_GAMEPAD_BUTTON_RIGHT_STICK},
{Joystick::GAMEPAD_BUTTON_LEFTSHOULDER, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER},
{Joystick::GAMEPAD_BUTTON_RIGHTSHOULDER, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER},
{Joystick::GAMEPAD_BUTTON_DPAD_UP, SDL_GAMEPAD_BUTTON_DPAD_UP},
{Joystick::GAMEPAD_BUTTON_DPAD_DOWN, SDL_GAMEPAD_BUTTON_DPAD_DOWN},
{Joystick::GAMEPAD_BUTTON_DPAD_LEFT, SDL_GAMEPAD_BUTTON_DPAD_LEFT},
{Joystick::GAMEPAD_BUTTON_DPAD_RIGHT, SDL_GAMEPAD_BUTTON_DPAD_RIGHT},
{Joystick::GAMEPAD_BUTTON_MISC1, SDL_GAMEPAD_BUTTON_MISC1},
{Joystick::GAMEPAD_BUTTON_PADDLE1, SDL_GAMEPAD_BUTTON_LEFT_PADDLE1},
{Joystick::GAMEPAD_BUTTON_PADDLE2, SDL_GAMEPAD_BUTTON_LEFT_PADDLE2},
{Joystick::GAMEPAD_BUTTON_PADDLE3, SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1},
{Joystick::GAMEPAD_BUTTON_PADDLE4, SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2},
{Joystick::GAMEPAD_BUTTON_TOUCHPAD, SDL_GAMEPAD_BUTTON_TOUCHPAD},
};
EnumMap<Joystick::GamepadButton, SDL_GamepadButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM> Joystick::gpButtons(Joystick::gpButtonEntries, sizeof(Joystick::gpButtonEntries));
} // sdl
} // joystick
} // love
#endif // SDL_VERSION_ATLEAST(3, 0, 0)
-145
View File
@@ -1,145 +0,0 @@
/**
* Copyright (c) 2006-2023 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_JOYSTICK_SDL_JOYSTICK_SDL3_H
#define LOVE_JOYSTICK_SDL_JOYSTICK_SDL3_H
// LOVE
#include "joystick/Joystick.h"
#include "common/EnumMap.h"
#include "common/int.h"
// SDL
#if __has_include(<SDL3/SDL.h>)
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#endif
#if SDL_VERSION_ATLEAST(3, 0, 0)
namespace love
{
namespace joystick
{
namespace sdl
{
class Joystick : public love::joystick::Joystick
{
public:
Joystick(int id);
virtual ~Joystick();
bool open(int64 deviceid) override;
void close() override;
bool isConnected() const override;
const char *getName() const override;
JoystickType getJoystickType() const override;
int getAxisCount() const override;
int getButtonCount() const override;
int getHatCount() const override;
float getAxis(int axisindex) const override;
std::vector<float> getAxes() const override;
Hat getHat(int hatindex) const override;
bool isDown(const std::vector<int> &buttonlist) const override;
void setPlayerIndex(int index) override;
int getPlayerIndex() const override;
bool openGamepad(int64 deviceid) override;
bool isGamepad() const override;
GamepadType getGamepadType() const override;
float getGamepadAxis(GamepadAxis axis) const override;
bool isGamepadDown(const std::vector<GamepadButton> &blist) const override;
JoystickInput getGamepadMapping(const GamepadInput &input) const override;
std::string getGamepadMappingString() const override;
void *getHandle() const override;
std::string getGUID() const override;
int getInstanceID() const override;
int getID() const override;
void getDeviceInfo(int &vendorID, int &productID, int &productVersion) const override;
bool isVibrationSupported() override;
bool setVibration(float left, float right, float duration = -1.0f) override;
bool setVibration() override;
void getVibration(float &left, float &right) override;
bool hasSensor(Sensor::SensorType type) const override;
bool isSensorEnabled(Sensor::SensorType type) const override;
void setSensorEnabled(Sensor::SensorType type, bool enabled) override;
std::vector<float> getSensorData(Sensor::SensorType type) const override;
static bool getConstant(Hat in, Uint8 &out);
static bool getConstant(Uint8 in, Hat &out);
static bool getConstant(SDL_GamepadAxis in, GamepadAxis &out);
static bool getConstant(GamepadAxis in, SDL_GamepadAxis &out);
static bool getConstant(SDL_GamepadButton in, GamepadButton &out);
static bool getConstant(GamepadButton in, SDL_GamepadButton &out);
private:
Joystick() {}
SDL_Joystick *joyhandle;
SDL_Gamepad *controller;
JoystickType joystickType;
SDL_JoystickID instanceid;
std::string pguid;
int id;
std::string name;
static EnumMap<Hat, Uint8, Joystick::HAT_MAX_ENUM>::Entry hatEntries[];
static EnumMap<Hat, Uint8, Joystick::HAT_MAX_ENUM> hats;
static EnumMap<GamepadAxis, SDL_GamepadAxis, GAMEPAD_AXIS_MAX_ENUM>::Entry gpAxisEntries[];
static EnumMap<GamepadAxis, SDL_GamepadAxis, GAMEPAD_AXIS_MAX_ENUM> gpAxes;
static EnumMap<GamepadButton, SDL_GamepadButton, GAMEPAD_BUTTON_MAX_ENUM>::Entry gpButtonEntries[];
static EnumMap<GamepadButton, SDL_GamepadButton, GAMEPAD_BUTTON_MAX_ENUM> gpButtons;
};
} // sdl
} // joystick
} // love
#endif // SDL_VERSION_ATLEAST(3, 0, 0)
#endif // LOVE_JOYSTICK_SDL_JOYSTICK_SDL3_H
-23
View File
@@ -307,10 +307,6 @@ std::map<std::string, uint32> Keyboard::stringToKey =
{"audioplay", Keyboard::KEY_AUDIOPLAY},
{"audiomute", Keyboard::KEY_AUDIOMUTE},
{"mediaselect", Keyboard::KEY_MEDIASELECT},
{"www", Keyboard::KEY_WWW},
{"mail", Keyboard::KEY_MAIL},
{"calculator", Keyboard::KEY_CALCULATOR},
{"computer", Keyboard::KEY_COMPUTER},
{"appsearch", Keyboard::KEY_APP_SEARCH},
{"apphome", Keyboard::KEY_APP_HOME},
{"appback", Keyboard::KEY_APP_BACK},
@@ -319,12 +315,6 @@ std::map<std::string, uint32> Keyboard::stringToKey =
{"apprefresh", Keyboard::KEY_APP_REFRESH},
{"appbookmarks", Keyboard::KEY_APP_BOOKMARKS},
{"brightnessdown", Keyboard::KEY_BRIGHTNESSDOWN},
{"brightnessup", Keyboard::KEY_BRIGHTNESSUP},
{"displayswitch", Keyboard::KEY_DISPLAYSWITCH},
{"kbdillumtoggle", Keyboard::KEY_KBDILLUMTOGGLE},
{"kbdillumdown", Keyboard::KEY_KBDILLUMDOWN},
{"kbdillumup", Keyboard::KEY_KBDILLUMUP},
{"eject", Keyboard::KEY_EJECT},
{"sleep", Keyboard::KEY_SLEEP},
};
@@ -568,10 +558,6 @@ StringMap<Keyboard::Scancode, Keyboard::SCANCODE_MAX_ENUM>::Entry Keyboard::scan
{"audioplay", SCANCODE_AUDIOPLAY},
{"audiomute", SCANCODE_AUDIOMUTE},
{"mediaselect", SCANCODE_MEDIASELECT},
{"www", SCANCODE_WWW},
{"mail", SCANCODE_MAIL},
{"calculator", SCANCODE_CALCULATOR},
{"computer", SCANCODE_COMPUTER},
{"acsearch", SCANCODE_AC_SEARCH},
{"achome", SCANCODE_AC_HOME},
{"acback", SCANCODE_AC_BACK},
@@ -580,17 +566,8 @@ StringMap<Keyboard::Scancode, Keyboard::SCANCODE_MAX_ENUM>::Entry Keyboard::scan
{"acrefresh", SCANCODE_AC_REFRESH},
{"acbookmarks", SCANCODE_AC_BOOKMARKS},
{"brightnessdown", SCANCODE_BRIGHTNESSDOWN},
{"brightnessup", SCANCODE_BRIGHTNESSUP},
{"displayswitch", SCANCODE_DISPLAYSWITCH},
{"kbdillumtoggle", SCANCODE_KBDILLUMTOGGLE},
{"kbdillumdown", SCANCODE_KBDILLUMDOWN},
{"kbdillumup", SCANCODE_KBDILLUMUP},
{"eject", SCANCODE_EJECT},
{"sleep", SCANCODE_SLEEP},
{"app1", SCANCODE_APP1},
{"app2", SCANCODE_APP2},
};
StringMap<Keyboard::Scancode, Keyboard::SCANCODE_MAX_ENUM> Keyboard::scancodes(Keyboard::scancodeEntries, sizeof(Keyboard::scancodeEntries));
-23
View File
@@ -280,10 +280,6 @@ public:
SCANCODE_AUDIOPLAY,
SCANCODE_AUDIOMUTE,
SCANCODE_MEDIASELECT,
SCANCODE_WWW,
SCANCODE_MAIL,
SCANCODE_CALCULATOR,
SCANCODE_COMPUTER,
SCANCODE_AC_SEARCH,
SCANCODE_AC_HOME,
SCANCODE_AC_BACK,
@@ -292,18 +288,9 @@ public:
SCANCODE_AC_REFRESH,
SCANCODE_AC_BOOKMARKS,
SCANCODE_BRIGHTNESSDOWN,
SCANCODE_BRIGHTNESSUP,
SCANCODE_DISPLAYSWITCH,
SCANCODE_KBDILLUMTOGGLE,
SCANCODE_KBDILLUMDOWN,
SCANCODE_KBDILLUMUP,
SCANCODE_EJECT,
SCANCODE_SLEEP,
SCANCODE_APP1,
SCANCODE_APP2,
SCANCODE_MAX_ENUM
};
@@ -515,10 +502,6 @@ public:
KEY_AUDIOPLAY = LOVE_KEY(SCANCODE_AUDIOPLAY),
KEY_AUDIOMUTE = LOVE_KEY(SCANCODE_AUDIOMUTE),
KEY_MEDIASELECT = LOVE_KEY(SCANCODE_MEDIASELECT),
KEY_WWW = LOVE_KEY(SCANCODE_WWW),
KEY_MAIL = LOVE_KEY(SCANCODE_MAIL),
KEY_CALCULATOR = LOVE_KEY(SCANCODE_CALCULATOR),
KEY_COMPUTER = LOVE_KEY(SCANCODE_COMPUTER),
KEY_APP_SEARCH = LOVE_KEY(SCANCODE_AC_SEARCH),
KEY_APP_HOME = LOVE_KEY(SCANCODE_AC_HOME),
KEY_APP_BACK = LOVE_KEY(SCANCODE_AC_BACK),
@@ -527,12 +510,6 @@ public:
KEY_APP_REFRESH = LOVE_KEY(SCANCODE_AC_REFRESH),
KEY_APP_BOOKMARKS = LOVE_KEY(SCANCODE_AC_BOOKMARKS),
KEY_BRIGHTNESSDOWN = LOVE_KEY(SCANCODE_BRIGHTNESSDOWN),
KEY_BRIGHTNESSUP = LOVE_KEY(SCANCODE_BRIGHTNESSUP),
KEY_DISPLAYSWITCH = LOVE_KEY(SCANCODE_DISPLAYSWITCH),
KEY_KBDILLUMTOGGLE = LOVE_KEY(SCANCODE_KBDILLUMTOGGLE),
KEY_KBDILLUMDOWN = LOVE_KEY(SCANCODE_KBDILLUMDOWN),
KEY_KBDILLUMUP = LOVE_KEY(SCANCODE_KBDILLUMUP),
KEY_EJECT = LOVE_KEY(SCANCODE_EJECT),
KEY_SLEEP = LOVE_KEY(SCANCODE_SLEEP),
};
-121
View File
@@ -18,21 +18,10 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#if __has_include(<SDL3/SDL_version.h>)
#include <SDL3/SDL_version.h>
#else
#include <SDL_version.h>
#endif
#include "Keyboard.h"
#include "window/Window.h"
#include "common/config.h"
// SDL before 2.0.18 lack KMOD_SCROLL. Use KMOD_RESERVED instead.
#if !SDL_VERSION_ATLEAST(2, 0, 18)
#define KMOD_SCROLL KMOD_RESERVED
#endif // !SDL_VERSION_ATLEAST(2, 0, 18)
namespace love
{
namespace keyboard
@@ -40,7 +29,6 @@ namespace keyboard
namespace sdl
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
static SDL_Window *getSDLWindow()
{
auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
@@ -48,7 +36,6 @@ static SDL_Window *getSDLWindow()
return (SDL_Window *) window->getHandle();
return nullptr;
}
#endif
Keyboard::Keyboard()
: love::keyboard::Keyboard("love.keyboard.sdl")
@@ -68,22 +55,14 @@ bool Keyboard::hasKeyRepeat() const
bool Keyboard::isDown(const std::vector<Key> &keylist) const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
const bool *state = SDL_GetKeyboardState(nullptr);
#else
const Uint8 *state = SDL_GetKeyboardState(nullptr);
#endif
for (Key key : keylist)
{
SDL_Keycode sdlkey = SDLK_UNKNOWN;
if (getConstant(key, sdlkey))
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Scancode scancode = SDL_GetScancodeFromKey(sdlkey, nullptr);
#else
SDL_Scancode scancode = SDL_GetScancodeFromKey(sdlkey);
#endif
if (state[scancode])
return true;
}
@@ -94,11 +73,7 @@ bool Keyboard::isDown(const std::vector<Key> &keylist) const
bool Keyboard::isScancodeDown(const std::vector<Scancode> &scancodelist) const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
const bool *state = SDL_GetKeyboardState(nullptr);
#else
const Uint8 *state = SDL_GetKeyboardState(nullptr);
#endif
for (Scancode scancode : scancodelist)
{
@@ -117,7 +92,6 @@ bool Keyboard::isModifierActive(ModifierKey key) const
switch (key)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
case MODKEY_NUMLOCK:
return (modstate & SDL_KMOD_NUM) != 0;
case MODKEY_CAPSLOCK:
@@ -126,16 +100,6 @@ bool Keyboard::isModifierActive(ModifierKey key) const
return (modstate & SDL_KMOD_SCROLL) != 0;
case MODKEY_MODE:
return (modstate & SDL_KMOD_MODE) != 0;
#else
case MODKEY_NUMLOCK:
return (modstate & KMOD_NUM) != 0;
case MODKEY_CAPSLOCK:
return (modstate & KMOD_CAPS) != 0;
case MODKEY_SCROLLLOCK:
return (modstate & KMOD_SCROLL) != 0;
case MODKEY_MODE:
return (modstate & KMOD_MODE) != 0;
#endif
default:
break;
}
@@ -148,11 +112,7 @@ Keyboard::Key Keyboard::getKeyFromScancode(Scancode scancode) const
SDL_Scancode sdlscancode = SDL_SCANCODE_UNKNOWN;
scancodes.find(scancode, sdlscancode);
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Keycode sdlkey = SDL_GetKeyFromScancode(sdlscancode, SDL_KMOD_NONE, false);
#else
SDL_Keycode sdlkey = SDL_GetKeyFromScancode(sdlscancode);
#endif
Key key = KEY_UNKNOWN;
getConstant(sdlkey, key);
@@ -166,11 +126,7 @@ Keyboard::Scancode Keyboard::getScancodeFromKey(Key key) const
SDL_Keycode sdlkey = SDLK_UNKNOWN;
if (getConstant(key, sdlkey))
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Scancode sdlscancode = SDL_GetScancodeFromKey(sdlkey, nullptr);
#else
SDL_Scancode sdlscancode = SDL_GetScancodeFromKey(sdlkey);
#endif
scancodes.find(sdlscancode, scancode);
}
@@ -179,7 +135,6 @@ Keyboard::Scancode Keyboard::getScancodeFromKey(Key key) const
void Keyboard::setTextInput(bool enable)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Window *window = getSDLWindow();
if (window == nullptr)
return;
@@ -187,12 +142,6 @@ void Keyboard::setTextInput(bool enable)
SDL_StartTextInput(window);
else
SDL_StopTextInput(window);
#else
if (enable)
SDL_StartTextInput();
else
SDL_StopTextInput();
#endif
}
void Keyboard::setTextInput(bool enable, double x, double y, double w, double h)
@@ -208,36 +157,24 @@ void Keyboard::setTextInput(bool enable, double x, double y, double w, double h)
SDL_Rect rect = {(int) x, (int) y, (int) w, (int) h};
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Window *sdlwindow = getSDLWindow();
if (sdlwindow != nullptr)
SDL_SetTextInputArea(sdlwindow, &rect, 0);
#else
SDL_SetTextInputRect(&rect);
#endif
setTextInput(enable);
}
bool Keyboard::hasTextInput() const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Window *window = getSDLWindow();
if (window == nullptr)
return false;
return SDL_TextInputActive(window);
#else
return SDL_IsTextInputActive() != SDL_FALSE;
#endif
}
bool Keyboard::hasScreenKeyboard() const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
return SDL_HasScreenKeyboardSupport();
#else
return SDL_HasScreenKeyboardSupport() != SDL_FALSE;
#endif
}
bool Keyboard::getConstant(Key in, SDL_Keycode &out)
@@ -421,25 +358,12 @@ std::map<Keyboard::Key, SDL_Keycode> Keyboard::keyToSDLKey =
{ KEY_MODE, SDLK_MODE },
#if SDL_VERSION_ATLEAST(3, 0, 0)
{ KEY_AUDIONEXT, SDLK_MEDIA_NEXT_TRACK },
{ KEY_AUDIOPREV, SDLK_MEDIA_PREVIOUS_TRACK },
{ KEY_AUDIOSTOP, SDLK_MEDIA_STOP },
{ KEY_AUDIOPLAY, SDLK_MEDIA_PLAY },
{ KEY_AUDIOMUTE, SDLK_MUTE },
{ KEY_MEDIASELECT, SDLK_MEDIA_SELECT },
#else
{ KEY_AUDIONEXT, SDLK_AUDIONEXT },
{ KEY_AUDIOPREV, SDLK_AUDIOPREV },
{ KEY_AUDIOSTOP, SDLK_AUDIOSTOP },
{ KEY_AUDIOPLAY, SDLK_AUDIOPLAY },
{ KEY_AUDIOMUTE, SDLK_AUDIOMUTE },
{ KEY_MEDIASELECT, SDLK_MEDIASELECT },
{ KEY_WWW, SDLK_WWW },
{ KEY_MAIL, SDLK_MAIL },
{ KEY_CALCULATOR, SDLK_CALCULATOR },
{ KEY_COMPUTER, SDLK_COMPUTER },
#endif
{ KEY_APP_SEARCH, SDLK_AC_SEARCH },
{ KEY_APP_HOME, SDLK_AC_HOME },
{ KEY_APP_BACK, SDLK_AC_BACK },
@@ -448,27 +372,13 @@ std::map<Keyboard::Key, SDL_Keycode> Keyboard::keyToSDLKey =
{ KEY_APP_REFRESH, SDLK_AC_REFRESH },
{ KEY_APP_BOOKMARKS, SDLK_AC_BOOKMARKS },
#if SDL_VERSION_ATLEAST(3, 0, 0)
{ KEY_EJECT, SDLK_MEDIA_EJECT },
#else
{ KEY_BRIGHTNESSDOWN, SDLK_BRIGHTNESSDOWN },
{ KEY_BRIGHTNESSUP, SDLK_BRIGHTNESSUP },
{ KEY_DISPLAYSWITCH, SDLK_DISPLAYSWITCH },
{ KEY_KBDILLUMTOGGLE, SDLK_KBDILLUMTOGGLE },
{ KEY_KBDILLUMDOWN, SDLK_KBDILLUMDOWN },
{ KEY_KBDILLUMUP, SDLK_KBDILLUMUP },
{ KEY_EJECT, SDLK_EJECT },
#endif
{ KEY_SLEEP, SDLK_SLEEP },
};
std::map<SDL_Keycode, Keyboard::Key> Keyboard::sdlKeyToKey;
#if SDL_VERSION_ATLEAST(3, 0, 0)
EnumMap<Keyboard::Scancode, SDL_Scancode, SDL_SCANCODE_COUNT>::Entry Keyboard::scancodeEntries[] =
#else
EnumMap<Keyboard::Scancode, SDL_Scancode, SDL_NUM_SCANCODES>::Entry Keyboard::scancodeEntries[] =
#endif
{
{SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN},
@@ -699,25 +609,12 @@ EnumMap<Keyboard::Scancode, SDL_Scancode, SDL_NUM_SCANCODES>::Entry Keyboard::sc
{SCANCODE_MODE, SDL_SCANCODE_MODE},
#if SDL_VERSION_ATLEAST(3, 0, 0)
{SCANCODE_AUDIONEXT, SDL_SCANCODE_MEDIA_NEXT_TRACK},
{SCANCODE_AUDIOPREV, SDL_SCANCODE_MEDIA_PREVIOUS_TRACK},
{SCANCODE_AUDIOSTOP, SDL_SCANCODE_MEDIA_STOP},
{SCANCODE_AUDIOPLAY, SDL_SCANCODE_MEDIA_PLAY},
{SCANCODE_AUDIOMUTE, SDL_SCANCODE_MUTE},
{SCANCODE_MEDIASELECT, SDL_SCANCODE_MEDIA_SELECT},
#else
{SCANCODE_AUDIONEXT, SDL_SCANCODE_AUDIONEXT},
{SCANCODE_AUDIOPREV, SDL_SCANCODE_AUDIOPREV},
{SCANCODE_AUDIOSTOP, SDL_SCANCODE_AUDIOSTOP},
{SCANCODE_AUDIOPLAY, SDL_SCANCODE_AUDIOPLAY},
{SCANCODE_AUDIOMUTE, SDL_SCANCODE_AUDIOMUTE},
{SCANCODE_MEDIASELECT, SDL_SCANCODE_MEDIASELECT},
{SCANCODE_WWW, SDL_SCANCODE_WWW},
{SCANCODE_MAIL, SDL_SCANCODE_MAIL},
{SCANCODE_CALCULATOR, SDL_SCANCODE_CALCULATOR},
{SCANCODE_COMPUTER, SDL_SCANCODE_COMPUTER},
#endif
{SCANCODE_AC_SEARCH, SDL_SCANCODE_AC_SEARCH},
{SCANCODE_AC_HOME, SDL_SCANCODE_AC_HOME},
{SCANCODE_AC_BACK, SDL_SCANCODE_AC_BACK},
@@ -726,29 +623,11 @@ EnumMap<Keyboard::Scancode, SDL_Scancode, SDL_NUM_SCANCODES>::Entry Keyboard::sc
{SCANCODE_AC_REFRESH, SDL_SCANCODE_AC_REFRESH},
{SCANCODE_AC_BOOKMARKS, SDL_SCANCODE_AC_BOOKMARKS},
#if SDL_VERSION_ATLEAST(3, 0, 0)
{SCANCODE_EJECT, SDL_SCANCODE_MEDIA_EJECT},
{SCANCODE_SLEEP, SDL_SCANCODE_SLEEP},
#else
{SCANCODE_BRIGHTNESSDOWN, SDL_SCANCODE_BRIGHTNESSDOWN},
{SCANCODE_BRIGHTNESSUP, SDL_SCANCODE_BRIGHTNESSUP},
{SCANCODE_DISPLAYSWITCH, SDL_SCANCODE_DISPLAYSWITCH},
{SCANCODE_KBDILLUMTOGGLE, SDL_SCANCODE_KBDILLUMTOGGLE},
{SCANCODE_KBDILLUMDOWN, SDL_SCANCODE_KBDILLUMDOWN},
{SCANCODE_KBDILLUMUP, SDL_SCANCODE_KBDILLUMUP},
{SCANCODE_EJECT, SDL_SCANCODE_EJECT},
{SCANCODE_SLEEP, SDL_SCANCODE_SLEEP},
{SCANCODE_APP1, SDL_SCANCODE_APP1},
{SCANCODE_APP2, SDL_SCANCODE_APP2},
#endif
};
#if SDL_VERSION_ATLEAST(3, 0, 0)
EnumMap<Keyboard::Scancode, SDL_Scancode, SDL_SCANCODE_COUNT> Keyboard::scancodes(Keyboard::scancodeEntries, sizeof(Keyboard::scancodeEntries));
#else
EnumMap<Keyboard::Scancode, SDL_Scancode, SDL_NUM_SCANCODES> Keyboard::scancodes(Keyboard::scancodeEntries, sizeof(Keyboard::scancodeEntries));
#endif
} // sdl
} // keyboard
-11
View File
@@ -26,13 +26,7 @@
#include "common/EnumMap.h"
// SDL
#if __has_include(<SDL3/SDL_keyboard.h>)
#include <SDL3/SDL_keyboard.h>
#include <SDL3/SDL_version.h>
#else
#include <SDL_keyboard.h>
#include <SDL_version.h>
#endif
#include <map>
@@ -78,13 +72,8 @@ private:
static std::map<Key, SDL_Keycode> keyToSDLKey;
static std::map<SDL_Keycode, Key> sdlKeyToKey;
#if SDL_VERSION_ATLEAST(3, 0, 0)
static EnumMap<Scancode, SDL_Scancode, SDL_SCANCODE_COUNT>::Entry scancodeEntries[];
static EnumMap<Scancode, SDL_Scancode, SDL_SCANCODE_COUNT> scancodes;
#else
static EnumMap<Scancode, SDL_Scancode, SDL_NUM_SCANCODES>::Entry scancodeEntries[];
static EnumMap<Scancode, SDL_Scancode, SDL_NUM_SCANCODES> scancodes;
#endif
}; // Keyboard
+1 -7
View File
@@ -210,7 +210,6 @@ function love.init()
identity = false,
appendidentity = false,
externalstorage = false, -- Only relevant for Android.
accelerometerjoystick = nil, -- Only relevant for Android / iOS, deprecated.
gammacorrect = false,
highdpi = false,
renderers = nil,
@@ -243,11 +242,6 @@ function love.init()
love._openConsole()
end
-- Hack for disabling accelerometer-as-joystick on Android / iOS.
if love._setAccelerometerAsJoystick then
love._setAccelerometerAsJoystick(c.accelerometerjoystick)
end
if love._setGammaCorrect then
love._setGammaCorrect(c.gammacorrect)
end
@@ -380,7 +374,7 @@ function love.init()
y = c.window.y,
}), "Could not set window mode")
if c.window.icon then
assert(love.image, "If an icon is set in love.conf, love.image must be loaded!")
assert(love.image, "If an icon is set in love.conf, love.image must be loaded.")
love.window.setIcon(love.image.newImageData(c.window.icon))
end
end
-34
View File
@@ -37,11 +37,7 @@
#endif // LOVE_WINDOWS
#ifdef LOVE_ANDROID
#if __has_include(<SDL3/SDL.h>)
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#endif
#endif // LOVE_ANDROID
#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
@@ -51,14 +47,6 @@
#include <fstream>
#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK
#ifdef LOVE_LEGENDARY_ACCELEROMETER_AS_JOYSTICK_HACK
#if __has_include(<SDL3/SDL_version.h>)
#undef LOVE_LEGENDARY_ACCELEROMETER_AS_JOYSTICK_HACK
#else
#include <SDL_hints.h>
#endif
#endif // LOVE_LEGENDARY_ACCELEROMETER_AS_JOYSTICK_HACK
// Libraries.
#ifdef LOVE_ENABLE_LUASOCKET
# include "libraries/luasocket/luasocket.h"
@@ -259,10 +247,6 @@ static const luaL_Reg modules[] = {
int w__openConsole(lua_State *L);
#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK
#ifdef LOVE_LEGENDARY_ACCELEROMETER_AS_JOYSTICK_HACK
int w__setAccelerometerAsJoystick(lua_State *L);
#endif
#ifdef LOVE_ANDROID
static int w_print_sdl_log(lua_State *L)
{
@@ -560,11 +544,6 @@ int luaopen_love(lua_State *L)
lua_setfield(L, -2, "_openConsole");
#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK
#ifdef LOVE_LEGENDARY_ACCELEROMETER_AS_JOYSTICK_HACK
lua_pushcfunction(L, w__setAccelerometerAsJoystick);
lua_setfield(L, -2, "_setAccelerometerAsJoystick");
#endif
lua_pushcfunction(L, w__setGammaCorrect);
lua_setfield(L, -2, "_setGammaCorrect");
@@ -772,19 +751,6 @@ int w__openConsole(lua_State *L)
#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK
#ifdef LOVE_LEGENDARY_ACCELEROMETER_AS_JOYSTICK_HACK
int w__setAccelerometerAsJoystick(lua_State *L)
{
bool enable = (bool) lua_toboolean(L, 1);
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, enable ? "1" : "0");
if (enable)
love::luax_markdeprecated(L, 1, "accelerometerjoystick", love::API_FIELD, love::DEPRECATED_REPLACED, "love.sensor module");
return 0;
}
#endif // LOVE_LEGENDARY_ACCELEROMETER_AS_JOYSTICK_HACK
int luaopen_love_nogame(lua_State *L)
{
if (luaL_loadbuffer(L, (const char *)love::nogame_lua, sizeof(love::nogame_lua), "=[love \"nogame.lua\"]") == 0)
+1 -49
View File
@@ -22,12 +22,6 @@
#include "Cursor.h"
#include "common/config.h"
#if __has_include(<SDL3/SDL_version.h>)
#include <SDL3/SDL_version.h>
#else
#include <SDL_version.h>
#endif
namespace love
{
namespace mouse
@@ -44,34 +38,12 @@ Cursor::Cursor(image::ImageData *data, int hotx, int hoty)
int h = data->getHeight();
int pitch = w * 4;
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Surface *surface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA8888, data->getData(), pitch);
#else
Uint32 rmask, gmask, bmask, amask;
#ifdef LOVE_BIG_ENDIAN
rmask = 0xFF000000;
gmask = 0x00FF0000;
bmask = 0x0000FF00;
amask = 0x000000FF;
#else
rmask = 0x000000FF;
gmask = 0x0000FF00;
bmask = 0x00FF0000;
amask = 0xFF000000;
#endif
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(data->getData(), w, h, 32, pitch, rmask, gmask, bmask, amask);
#endif
if (!surface)
throw love::Exception("Cannot create cursor: out of memory!");
throw love::Exception("Cannot create cursor: out of memory.");
cursor = SDL_CreateColorCursor(surface, hotx, hoty);
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_DestroySurface(surface);
#else
SDL_FreeSurface(surface);
#endif
if (!cursor)
throw love::Exception("Cannot create cursor: %s", SDL_GetError());
@@ -95,13 +67,8 @@ Cursor::Cursor(mouse::Cursor::SystemCursor cursortype)
Cursor::~Cursor()
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (cursor)
SDL_DestroyCursor(cursor);
#else
if (cursor)
SDL_FreeCursor(cursor);
#endif
}
void *Cursor::getHandle() const
@@ -121,7 +88,6 @@ Cursor::SystemCursor Cursor::getSystemType() const
EnumMap<Cursor::SystemCursor, SDL_SystemCursor, Cursor::CURSOR_MAX_ENUM>::Entry Cursor::systemCursorEntries[] =
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
{Cursor::CURSOR_ARROW, SDL_SYSTEM_CURSOR_DEFAULT},
{Cursor::CURSOR_IBEAM, SDL_SYSTEM_CURSOR_TEXT},
{Cursor::CURSOR_WAIT, SDL_SYSTEM_CURSOR_WAIT},
@@ -134,20 +100,6 @@ EnumMap<Cursor::SystemCursor, SDL_SystemCursor, Cursor::CURSOR_MAX_ENUM>::Entry
{Cursor::CURSOR_SIZEALL, SDL_SYSTEM_CURSOR_MOVE},
{Cursor::CURSOR_NO, SDL_SYSTEM_CURSOR_NOT_ALLOWED},
{Cursor::CURSOR_HAND, SDL_SYSTEM_CURSOR_POINTER},
#else
{Cursor::CURSOR_ARROW, SDL_SYSTEM_CURSOR_ARROW},
{Cursor::CURSOR_IBEAM, SDL_SYSTEM_CURSOR_IBEAM},
{Cursor::CURSOR_WAIT, SDL_SYSTEM_CURSOR_WAIT},
{Cursor::CURSOR_CROSSHAIR, SDL_SYSTEM_CURSOR_CROSSHAIR},
{Cursor::CURSOR_WAITARROW, SDL_SYSTEM_CURSOR_WAITARROW},
{Cursor::CURSOR_SIZENWSE, SDL_SYSTEM_CURSOR_SIZENWSE},
{Cursor::CURSOR_SIZENESW, SDL_SYSTEM_CURSOR_SIZENESW},
{Cursor::CURSOR_SIZEWE, SDL_SYSTEM_CURSOR_SIZEWE},
{Cursor::CURSOR_SIZENS, SDL_SYSTEM_CURSOR_SIZENS},
{Cursor::CURSOR_SIZEALL, SDL_SYSTEM_CURSOR_SIZEALL},
{Cursor::CURSOR_NO, SDL_SYSTEM_CURSOR_NO},
{Cursor::CURSOR_HAND, SDL_SYSTEM_CURSOR_HAND},
#endif
};
EnumMap<Cursor::SystemCursor, SDL_SystemCursor, Cursor::CURSOR_MAX_ENUM> Cursor::systemCursors(Cursor::systemCursorEntries, sizeof(Cursor::systemCursorEntries));
-4
View File
@@ -26,11 +26,7 @@
#include "common/EnumMap.h"
// SDL
#if __has_include(<SDL3/SDL_mouse.h>)
#include <SDL3/SDL_mouse.h>
#else
#include <SDL_mouse.h>
#endif
namespace love
{
-54
View File
@@ -23,13 +23,7 @@
#include "window/sdl/Window.h"
// SDL
#if __has_include(<SDL3/SDL_version.h>)
#include <SDL3/SDL_mouse.h>
#include <SDL3/SDL_version.h>
#else
#include <SDL_mouse.h>
#include <SDL_version.h>
#endif
namespace love
{
@@ -38,7 +32,6 @@ namespace mouse
namespace sdl
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
static SDL_Window *getSDLWindow()
{
auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
@@ -46,7 +39,6 @@ static SDL_Window *getSDLWindow()
return (SDL_Window *) window->getHandle();
return nullptr;
}
#endif
// SDL reports mouse coordinates in the window coordinate system in OS X, but
// we want them in pixel coordinates (may be different with high-DPI enabled.)
@@ -138,13 +130,8 @@ bool Mouse::isCursorSupported() const
void Mouse::getPosition(double &x, double &y) const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
float mx, my;
SDL_GetMouseState(&mx, &my);
#else
int mx, my;
SDL_GetMouseState(&mx, &my);
#endif
x = (double) mx;
y = (double) my;
@@ -178,17 +165,12 @@ void Mouse::setPosition(double x, double y)
void Mouse::getGlobalPosition(double &x, double &y, int &displayindex) const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
float globalx, globaly;
#else
int globalx, globaly;
#endif
SDL_GetGlobalMouseState(&globalx, &globaly);
auto mx = globalx;
auto my = globaly;
#if SDL_VERSION_ATLEAST(3, 0, 0)
int displaycount = 0;
SDL_DisplayID *displays = SDL_GetDisplays(&displaycount);
@@ -206,22 +188,6 @@ void Mouse::getGlobalPosition(double &x, double &y, int &displayindex) const
if (SDL_PointInRectFloat(&p, &frect))
break;
}
#else
int displaycount = SDL_GetNumVideoDisplays();
for (displayindex = 0; displayindex < displaycount; displayindex++)
{
SDL_Rect rect = {};
SDL_GetDisplayBounds(displayindex, &rect);
mx -= rect.x;
my -= rect.y;
SDL_Point p = { globalx, globaly };
if (SDL_PointInRect(&p, &rect))
break;
}
#endif
if (displayindex >= displaycount)
displayindex = 0;
@@ -232,14 +198,10 @@ void Mouse::getGlobalPosition(double &x, double &y, int &displayindex) const
void Mouse::setVisible(bool visible)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (visible)
SDL_ShowCursor();
else
SDL_HideCursor();
#else
SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
#endif
}
bool Mouse::isDown(const std::vector<int> &buttons) const
@@ -263,11 +225,7 @@ bool Mouse::isDown(const std::vector<int> &buttons) const
break;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (buttonstate & SDL_BUTTON_MASK(button))
#else
if (buttonstate & SDL_BUTTON(button))
#endif
return true;
}
@@ -276,11 +234,7 @@ bool Mouse::isDown(const std::vector<int> &buttons) const
bool Mouse::isVisible() const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
return SDL_CursorVisible();
#else
return SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE;
#endif
}
void Mouse::setGrabbed(bool grab)
@@ -301,26 +255,18 @@ bool Mouse::isGrabbed() const
bool Mouse::setRelativeMode(bool relative)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Window *sdlwindow = getSDLWindow();
if (sdlwindow == nullptr)
return false;
return SDL_SetWindowRelativeMouseMode(sdlwindow, relative);
#else
return SDL_SetRelativeMouseMode(relative ? SDL_TRUE : SDL_FALSE) == 0;
#endif
}
bool Mouse::getRelativeMode() const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Window *sdlwindow = getSDLWindow();
if (sdlwindow == nullptr)
return false;
return SDL_GetWindowRelativeMouseMode(sdlwindow);
#else
return SDL_GetRelativeMouseMode() != SDL_FALSE;
#endif
}
} // sdl
-48
View File
@@ -22,13 +22,8 @@
#include "Sensor.h"
// SDL
#if __has_include(<SDL3/SDL.h>)
#include <SDL3/SDL.h>
#include <SDL3/SDL_sensor.h>
#else
#include <SDL.h>
#include <SDL_sensor.h>
#endif
namespace love
{
@@ -41,11 +36,7 @@ Sensor::Sensor()
: love::sensor::Sensor("love.sensor.sdl")
, sensors()
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_InitSubSystem(SDL_INIT_SENSOR))
#else
if (SDL_InitSubSystem(SDL_INIT_SENSOR) < 0)
#endif
throw love::Exception("Could not initialize SDL sensor subsystem (%s)", SDL_GetError());
}
@@ -56,7 +47,6 @@ Sensor::~Sensor()
bool Sensor::hasSensor(SensorType type)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
int count = 0;
SDL_SensorID *sensorIDs = SDL_GetSensors(&count);
for (int i = 0; i < count; i++)
@@ -68,13 +58,6 @@ bool Sensor::hasSensor(SensorType type)
}
}
SDL_free(sensorIDs);
#else
for (int i = 0; i < SDL_NumSensors(); i++)
{
if (convert(SDL_SensorGetDeviceType(i)) == type)
return true;
}
#endif
return false;
}
@@ -88,16 +71,11 @@ void Sensor::setEnabled(SensorType type, bool enable)
{
if (sensors[type] && !enable)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_CloseSensor(sensors[type]);
#else
SDL_SensorClose(sensors[type]);
#endif
sensors[type] = nullptr;
}
else if (sensors[type] == nullptr && enable)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
int count = 0;
SDL_SensorID *sensorIDs = SDL_GetSensors(&count);
for (int i = 0; i < count; i++)
@@ -119,24 +97,6 @@ void Sensor::setEnabled(SensorType type, bool enable)
}
}
SDL_free(sensorIDs);
#else
for (int i = 0; i < SDL_NumSensors(); i++)
{
if (convert(SDL_SensorGetDeviceType(i)) == type)
{
SDL_Sensor *sensorHandle = SDL_SensorOpen(i);
if (sensorHandle == nullptr)
{
const char *name = nullptr;
getConstant(type, name);
throw love::Exception("Could not open \"%s\" SDL sensor (%s)", name, SDL_GetError());
}
sensors[type] = sensorHandle;
}
}
#endif
}
}
@@ -152,11 +112,7 @@ std::vector<float> Sensor::getData(SensorType type)
std::vector<float> values(3);
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_GetSensorData(sensors[type], values.data(), (int) values.size()))
#else
if (SDL_SensorGetData(sensors[type], values.data(), (int) values.size()) != 0)
#endif
{
const char *name = nullptr;
getConstant(type, name);
@@ -190,11 +146,7 @@ const char *Sensor::getSensorName(SensorType type)
throw love::Exception("\"%s\" sensor is not enabled", name);
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
return SDL_GetSensorName(sensors[type]);
#else
return SDL_SensorGetName(sensors[type]);
#endif
}
Sensor::SensorType Sensor::convert(SDL_SensorType type)
-4
View File
@@ -25,11 +25,7 @@
#include "sensor/Sensor.h"
// SDL
#if __has_include(<SDL3/SDL_sensor.h>)
#include <SDL3/SDL_sensor.h>
#else
#include <SDL_sensor.h>
#endif
// std
#include <map>
-32
View File
@@ -23,20 +23,9 @@
#include "window/Window.h"
// SDL
#if __has_include(<SDL3/SDL_clipboard.h>)
#include <SDL3/SDL_clipboard.h>
#include <SDL3/SDL_cpuinfo.h>
#include <SDL3/SDL_version.h>
#include <SDL3/SDL_locale.h>
#else
#include <SDL_clipboard.h>
#include <SDL_cpuinfo.h>
#include <SDL_version.h>
#if SDL_VERSION_ATLEAST(2, 0, 14)
#include <SDL_locale.h>
#endif
#endif
namespace love
{
@@ -52,11 +41,7 @@ System::System()
int System::getProcessorCount() const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
return SDL_GetNumLogicalCPUCores();
#else
return SDL_GetCPUCount();
#endif
}
bool System::isWindowOpen() const
@@ -106,7 +91,6 @@ std::vector<std::string> System::getPreferredLocales() const
{
std::vector<std::string> result;
#if SDL_VERSION_ATLEAST(3, 0, 0)
int count = 0;
SDL_Locale **locales = SDL_GetPreferredLocales(&count);
for (int i = 0; i < count; i++)
@@ -118,22 +102,6 @@ std::vector<std::string> System::getPreferredLocales() const
result.push_back(locale->language);
}
SDL_free(locales);
#elif SDL_VERSION_ATLEAST(2, 0, 14)
SDL_Locale *locales = SDL_GetPreferredLocales();
if (locales)
{
for (SDL_Locale* locale = locales; locale->language != nullptr; locale++)
{
if (locale->country)
result.push_back(std::string(locale->language) + "_" + std::string(locale->country));
else
result.push_back(locale->language);
}
SDL_free(locales);
}
#endif
return result;
}
-4
View File
@@ -26,11 +26,7 @@
#include "common/EnumMap.h"
// SDL
#if __has_include(<SDL3/SDL_power.h>)
#include <SDL3/SDL_power.h>
#else
#include <SDL_power.h>
#endif
namespace love
{
-4
View File
@@ -26,11 +26,7 @@
#include "threads.h"
// SDL
#if __has_include(<SDL3/SDL_thread.h>)
#include <SDL3/SDL_thread.h>
#else
#include <SDL_thread.h>
#endif
namespace love
{
-23
View File
@@ -50,38 +50,22 @@ void Mutex::unlock()
Conditional::Conditional()
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
cond = SDL_CreateCondition();
#else
cond = SDL_CreateCond();
#endif
}
Conditional::~Conditional()
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_DestroyCondition(cond);
#else
SDL_DestroyCond(cond);
#endif
}
void Conditional::signal()
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SignalCondition(cond);
#else
SDL_CondSignal(cond);
#endif
}
void Conditional::broadcast()
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_BroadcastCondition(cond);
#else
SDL_CondBroadcast(cond);
#endif
}
bool Conditional::wait(thread::Mutex *_mutex, int timeout)
@@ -90,7 +74,6 @@ bool Conditional::wait(thread::Mutex *_mutex, int timeout)
// however, you're asking for it if you're
// mixing thread implementations.
Mutex *mutex = (Mutex *) _mutex;
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (timeout < 0)
{
SDL_WaitCondition(cond, mutex->mutex);
@@ -98,12 +81,6 @@ bool Conditional::wait(thread::Mutex *_mutex, int timeout)
}
else
return SDL_WaitConditionTimeout(cond, mutex->mutex, timeout);
#else
if (timeout < 0)
return !SDL_CondWait(cond, mutex->mutex);
else
return (SDL_CondWaitTimeout(cond, mutex->mutex, timeout) == 0);
#endif
}
} // sdl
-15
View File
@@ -24,15 +24,8 @@
#include "common/config.h"
#include "thread/threads.h"
#if __has_include(<SDL3/SDL_version.h>)
#include <SDL3/SDL_version.h>
#include <SDL3/SDL_thread.h>
#include <SDL3/SDL_mutex.h>
#else
#include <SDL_version.h>
#include <SDL_thread.h>
#include <SDL_mutex.h>
#endif
namespace love
{
@@ -55,11 +48,7 @@ public:
private:
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Mutex *mutex;
#else
SDL_mutex *mutex;
#endif
Mutex(const Mutex&/* mutex*/) {}
friend class Conditional;
@@ -79,11 +68,7 @@ public:
private:
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Condition *cond;
#else
SDL_cond *cond;
#endif
}; // Conditional
-18
View File
@@ -23,12 +23,6 @@
#include "common/Exception.h"
#include "Touch.h"
#if __has_include(<SDL3/SDL_version.h>)
#include <SDL3/SDL_version.h>
#else
#include <SDL_version.h>
#endif
// C++
#include <algorithm>
@@ -69,19 +63,11 @@ void Touch::onEvent(Uint32 eventtype, const TouchInfo &info)
switch (eventtype)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
case SDL_EVENT_FINGER_DOWN:
#else
case SDL_FINGERDOWN:
#endif
touches.erase(std::remove_if(touches.begin(), touches.end(), compare), touches.end());
touches.push_back(info);
break;
#if SDL_VERSION_ATLEAST(3, 0, 0)
case SDL_EVENT_FINGER_MOTION:
#else
case SDL_FINGERMOTION:
#endif
{
for (TouchInfo &touch : touches)
{
@@ -90,11 +76,7 @@ void Touch::onEvent(Uint32 eventtype, const TouchInfo &info)
}
break;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
case SDL_EVENT_FINGER_UP:
#else
case SDL_FINGERUP:
#endif
touches.erase(std::remove_if(touches.begin(), touches.end(), compare), touches.end());
break;
default:
-4
View File
@@ -25,11 +25,7 @@
#include "touch/Touch.h"
// SDL
#if __has_include(<SDL3/SDL_events.h>)
#include <SDL3/SDL_events.h>
#else
#include <SDL_events.h>
#endif
namespace love
{
+4 -307
View File
@@ -43,17 +43,8 @@
// C
#include <cstdio>
// SDL
#if !SDL_VERSION_ATLEAST(3, 0, 0)
#include <SDL_syswm.h>
#endif
#ifdef LOVE_GRAPHICS_VULKAN
#if __has_include(<SDL3/SDL_vulkan.h>)
#include <SDL3/SDL_vulkan.h>
#else
#include <SDL_vulkan.h>
#endif
#endif
#if defined(LOVE_WINDOWS)
@@ -84,6 +75,7 @@ void setHighDPIAllowedImplementation(bool enable)
#if defined(LOVE_WINDOWS)
// Windows uses a different API than SDL_WINDOW_ALLOW_HIGHDPI.
// This must be set before the video subsystem is initialized.
// FIXME: How does this work in SDL3?
SDL_SetHint(SDL_HINT_WINDOWS_DPI_SCALING, enable ? "1" : "0");
#else
LOVE_UNUSED(enable);
@@ -105,11 +97,7 @@ Window::Window()
, displayedWindowError(false)
, contextAttribs()
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_InitSubSystem(SDL_INIT_VIDEO))
#else
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
#endif
throw love::Exception("Could not initialize SDL video subsystem (%s)", SDL_GetError());
// Make sure the screensaver doesn't activate by default.
@@ -118,11 +106,7 @@ Window::Window()
#ifdef LOVE_WINDOWS
// Turned off by default, because it (ironically) causes stuttering issues
// on some setups. More investigation is needed before enabling it.
#if SDL_VERSION_ATLEAST(3, 0, 0)
canUseDwmFlush = SDL_GetHintBoolean("LOVE_GRAPHICS_VSYNC_DWM", false);
#else
canUseDwmFlush = SDL_GetHintBoolean("LOVE_GRAPHICS_VSYNC_DWM", SDL_FALSE) != SDL_FALSE;
#endif
#endif
}
@@ -329,11 +313,7 @@ bool Window::createWindowAndContext(int x, int y, int w, int h, Uint32 windowfla
{
if (glcontext)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GL_DestroyContext(glcontext);
#else
SDL_GL_DeleteContext(glcontext);
#endif
glcontext = nullptr;
}
@@ -348,19 +328,11 @@ bool Window::createWindowAndContext(int x, int y, int w, int h, Uint32 windowfla
if (window)
{
SDL_DestroyWindow(window);
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_FlushEvents(SDL_EVENT_WINDOW_FIRST, SDL_EVENT_WINDOW_LAST);
#else
SDL_FlushEvent(SDL_WINDOWEVENT);
#endif
window = nullptr;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
window = SDL_CreateWindow(title.c_str(), w, h, windowflags);
#else
window = SDL_CreateWindow(title.c_str(), x, y, w, h, windowflags);
#endif
if (!window)
{
@@ -368,9 +340,7 @@ bool Window::createWindowAndContext(int x, int y, int w, int h, Uint32 windowfla
return false;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SetWindowPosition(window, x, y);
#endif
if (attribs != nullptr && renderer == love::graphics::Renderer::RENDERER_OPENGL)
{
@@ -386,11 +356,7 @@ bool Window::createWindowAndContext(int x, int y, int w, int h, Uint32 windowfla
// Make sure the context's version is at least what we requested.
if (glcontext && !checkGLVersion(*attribs, glversion))
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GL_DestroyContext(glcontext);
#else
SDL_GL_DeleteContext(glcontext);
#endif
glcontext = nullptr;
}
@@ -494,7 +460,6 @@ bool Window::createWindowAndContext(int x, int y, int w, int h, Uint32 windowfla
return true;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
struct SDLDisplayIDs
{
SDLDisplayIDs()
@@ -519,7 +484,6 @@ static SDL_DisplayID GetSDLDisplayIDForIndex(int displayindex)
return (SDL_DisplayID) 0;
return displayids.ids[displayindex];
}
#endif
bool Window::setWindow(int width, int height, WindowSettings *settings)
{
@@ -542,28 +506,17 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
f.minwidth = std::max(f.minwidth, 1);
f.minheight = std::max(f.minheight, 1);
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDLDisplayIDs displays;
int displaycount = displays.count;
#else
int displaycount = getDisplayCount();
#endif
f.displayindex = std::min(std::max(f.displayindex, 0), displaycount - 1);
// Use the desktop resolution if a width or height of 0 is specified.
if (width == 0 || height == 0)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
const SDL_DisplayMode *mode = SDL_GetDesktopDisplayMode(displays.ids[f.displayindex]);
width = mode->w;
height = mode->h;
#else
SDL_DisplayMode mode = {};
SDL_GetDesktopDisplayMode(f.displayindex, &mode);
width = mode.w;
height = mode.h;
#endif
}
// On Android, disable fullscreen first on window creation so it's
@@ -599,15 +552,10 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
}
Uint32 sdlflags = 0;
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_DisplayMode fsmode = {};
#else
SDL_DisplayMode fsmode = {0, width, height, 0, nullptr};
#endif
if (f.fullscreen)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
sdlflags |= SDL_WINDOW_FULLSCREEN;
if (f.fstype == FULLSCREEN_EXCLUSIVE)
@@ -627,28 +575,6 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
return false;
}
}
#else
if (f.fstype == FULLSCREEN_EXCLUSIVE)
{
// Fullscreen window creation will bug out if no mode can be used.
if (SDL_GetClosestDisplayMode(f.displayindex, &fsmode, &fsmode) == nullptr)
{
// GetClosestDisplayMode will fail if we request a size larger
// than the largest available display mode, so we'll try to use
// the largest (first) mode in that case.
if (SDL_GetDisplayMode(f.displayindex, 0, &fsmode) < 0)
return false;
}
sdlflags |= SDL_WINDOW_FULLSCREEN;
width = fsmode.w;
height = fsmode.h;
}
else
{
sdlflags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
#endif
}
bool needsetmode = false;
@@ -658,34 +584,22 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
if (isOpen())
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (fsmode.w > 0 && fsmode.h > 0)
SDL_SetWindowFullscreenMode(window, &fsmode);
else
SDL_SetWindowFullscreenMode(window, nullptr);
if (SDL_SetWindowFullscreen(window, (sdlflags & SDL_WINDOW_FULLSCREEN) != 0) && renderer == graphics::RENDERER_OPENGL)
#else
if (f.fullscreen && f.fstype == FULLSCREEN_EXCLUSIVE)
SDL_SetWindowDisplayMode(window, &fsmode);
if (SDL_SetWindowFullscreen(window, sdlflags) == 0 && renderer == graphics::RENDERER_OPENGL)
#endif
SDL_GL_MakeCurrent(window, glcontext);
// TODO: should we make this conditional, to avoid love.resize events when the size doesn't change?
SDL_SetWindowSize(window, width, height);
if (this->settings.resizable != f.resizable)
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SetWindowResizable(window, f.resizable);
#else
SDL_SetWindowResizable(window, f.resizable ? SDL_TRUE : SDL_FALSE);
#endif
if (this->settings.borderless != f.borderless)
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SetWindowBordered(window, !f.borderless);
#else
SDL_SetWindowBordered(window, f.borderless ? SDL_FALSE : SDL_TRUE);
#endif
}
else
{
@@ -706,15 +620,9 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
sdlflags |= SDL_WINDOW_BORDERLESS;
// Note: this flag is ignored on Windows.
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (isHighDPIAllowed())
sdlflags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
#else
if (isHighDPIAllowed())
sdlflags |= SDL_WINDOW_ALLOW_HIGHDPI;
#endif
#if SDL_VERSION_ATLEAST(3, 0, 0)
Uint32 createflags = sdlflags & (~SDL_WINDOW_FULLSCREEN);
if (!createWindowAndContext(x, y, width, height, createflags, renderer))
@@ -728,10 +636,6 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
SDL_SetWindowFullscreenMode(window, nullptr);
SDL_SetWindowFullscreen(window, true);
}
#else
if (!createWindowAndContext(x, y, width, height, sdlflags, renderer))
return false;
#endif
needsetmode = true;
}
@@ -771,6 +675,7 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
context = (void *) SDL_Metal_GetLayer(metalView);
#endif
// TODO: try/catch
graphics->setMode(context, (int) scaledw, (int) scaledh, pixelWidth, pixelHeight, f.stencil, f.depth, f.msaa);
}
else
@@ -788,9 +693,7 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
love::android::setImmersive(fullscreen);
#endif
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SyncWindow(window);
#endif
return true;
}
@@ -800,29 +703,9 @@ bool Window::onSizeChanged(int width, int height)
if (!window)
return false;
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
#else
windowWidth = width;
windowHeight = height;
#endif
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_GetWindowSizeInPixels(window, &pixelWidth, &pixelHeight))
#else
// TODO: Use SDL_GetWindowSizeInPixels here when supported.
if (glcontext != nullptr)
SDL_GL_GetDrawableSize(window, &pixelWidth, &pixelHeight);
#ifdef LOVE_GRAPHICS_METAL
else if (metalView != nullptr)
SDL_Metal_GetDrawableSize(window, &pixelWidth, &pixelHeight);
#endif
#ifdef LOVE_GRAPHICS_VULKAN
else if (windowRenderer == graphics::RENDERER_VULKAN)
SDL_Vulkan_GetDrawableSize(window, &pixelWidth, &pixelHeight);
#endif
else
#endif
{
pixelWidth = width;
pixelHeight = height;
@@ -840,9 +723,7 @@ bool Window::onSizeChanged(int width, int height)
void Window::updateSettings(const WindowSettings &newsettings, bool updateGraphicsViewport)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SyncWindow(window);
#endif
Uint32 wflags = SDL_GetWindowFlags(window);
@@ -852,27 +733,9 @@ void Window::updateSettings(const WindowSettings &newsettings, bool updateGraphi
pixelWidth = windowWidth;
pixelHeight = windowHeight;
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GetWindowSizeInPixels(window, &pixelWidth, &pixelHeight);
#else
// TODO: Use SDL_GetWindowSizeInPixels here when supported.
if ((wflags & SDL_WINDOW_OPENGL) != 0)
SDL_GL_GetDrawableSize(window, &pixelWidth, &pixelHeight);
#ifdef LOVE_GRAPHICS_METAL
else if ((wflags & SDL_WINDOW_METAL) != 0)
SDL_Metal_GetDrawableSize(window, &pixelWidth, &pixelHeight);
#endif
#ifdef LOVE_GRAPHICS_VULKAN
else if ((wflags & SDL_WINDOW_VULKAN) != 0)
SDL_Vulkan_GetDrawableSize(window, &pixelWidth, &pixelHeight);
#endif
#endif
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (((wflags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN) && SDL_GetWindowFullscreenMode(window) == nullptr)
#else
if ((wflags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)
#endif
{
settings.fullscreen = true;
settings.fstype = FULLSCREEN_DESKTOP;
@@ -902,11 +765,7 @@ void Window::updateSettings(const WindowSettings &newsettings, bool updateGraphi
getPosition(settings.x, settings.y, settings.displayindex);
#if SDL_VERSION_ATLEAST(3, 0, 0)
setHighDPIAllowed((wflags & SDL_WINDOW_HIGH_PIXEL_DENSITY) != 0);
#else
setHighDPIAllowed((wflags & SDL_WINDOW_ALLOW_HIGHDPI) != 0);
#endif
settings.usedpiscale = newsettings.usedpiscale;
@@ -921,19 +780,11 @@ void Window::updateSettings(const WindowSettings &newsettings, bool updateGraphi
settings.stencil = newsettings.stencil;
settings.depth = newsettings.depth;
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDLDisplayIDs displayids;
const SDL_DisplayMode *dmode = SDL_GetCurrentDisplayMode(displayids.ids[settings.displayindex]);
// May be 0 if the refresh rate can't be determined.
settings.refreshrate = dmode->refresh_rate;
#else
SDL_DisplayMode dmode = {};
SDL_GetCurrentDisplayMode(settings.displayindex, &dmode);
// May be 0 if the refresh rate can't be determined.
settings.refreshrate = (double) dmode.refresh_rate;
#endif
// Update the viewport size now instead of waiting for event polling.
if (updateGraphicsViewport && graphics.get())
@@ -972,11 +823,7 @@ void Window::close(bool allowExceptions)
if (glcontext)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GL_DestroyContext(glcontext);
#else
SDL_GL_DeleteContext(glcontext);
#endif
glcontext = nullptr;
}
@@ -995,11 +842,7 @@ void Window::close(bool allowExceptions)
// The old window may have generated pending events which are no longer
// relevant. Destroy them all!
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_FlushEvents(SDL_EVENT_WINDOW_FIRST, SDL_EVENT_WINDOW_LAST);
#else
SDL_FlushEvent(SDL_WINDOWEVENT);
#endif
}
open = false;
@@ -1017,7 +860,6 @@ bool Window::setFullscreen(bool fullscreen, FullscreenType fstype)
newsettings.fullscreen = fullscreen;
newsettings.fstype = fstype;
#if SDL_VERSION_ATLEAST(3, 0, 0)
bool sdlflags = fullscreen;
if (fullscreen)
{
@@ -1031,36 +873,12 @@ bool Window::setFullscreen(bool fullscreen, FullscreenType fstype)
SDL_SetWindowFullscreenMode(window, &mode);
}
}
#else
Uint32 sdlflags = 0;
if (fullscreen)
{
if (fstype == FULLSCREEN_DESKTOP)
sdlflags = SDL_WINDOW_FULLSCREEN_DESKTOP;
else
{
sdlflags = SDL_WINDOW_FULLSCREEN;
SDL_DisplayMode mode = {};
mode.w = windowWidth;
mode.h = windowHeight;
SDL_GetClosestDisplayMode(SDL_GetWindowDisplayIndex(window), &mode, &mode);
SDL_SetWindowDisplayMode(window, &mode);
}
}
#endif
#ifdef LOVE_ANDROID
love::android::setImmersive(fullscreen);
#endif
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (SDL_SetWindowFullscreen(window, sdlflags))
#else
if (SDL_SetWindowFullscreen(window, sdlflags) == 0)
#endif
{
if (glcontext)
SDL_GL_MakeCurrent(window, glcontext);
@@ -1079,21 +897,13 @@ bool Window::setFullscreen(bool fullscreen)
int Window::getDisplayCount() const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDLDisplayIDs displayids;
return displayids.count;
#else
return SDL_GetNumVideoDisplays();
#endif
}
const char *Window::getDisplayName(int displayindex) const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
const char *name = SDL_GetDisplayName(GetSDLDisplayIDForIndex(displayindex));
#else
const char *name = SDL_GetDisplayName(displayindex);
#endif
if (name == nullptr)
throw love::Exception("Invalid display index: %d", displayindex + 1);
@@ -1103,11 +913,7 @@ const char *Window::getDisplayName(int displayindex) const
Window::DisplayOrientation Window::getDisplayOrientation(int displayindex) const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
switch (SDL_GetCurrentDisplayOrientation(GetSDLDisplayIDForIndex(displayindex)))
#else
switch (SDL_GetDisplayOrientation(displayindex))
#endif
{
case SDL_ORIENTATION_UNKNOWN: return ORIENTATION_UNKNOWN;
case SDL_ORIENTATION_LANDSCAPE: return ORIENTATION_LANDSCAPE;
@@ -1123,7 +929,6 @@ std::vector<Window::WindowSize> Window::getFullscreenSizes(int displayindex) con
{
std::vector<WindowSize> sizes;
#if SDL_VERSION_ATLEAST(3, 0, 0)
int count = 0;
SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(GetSDLDisplayIDForIndex(displayindex), &count);
@@ -1131,14 +936,6 @@ std::vector<Window::WindowSize> Window::getFullscreenSizes(int displayindex) con
{
// TODO: other mode properties?
WindowSize w = {modes[i]->w, modes[i]->h};
#else
for (int i = 0; i < SDL_GetNumDisplayModes(displayindex); i++)
{
SDL_DisplayMode mode = {};
SDL_GetDisplayMode(displayindex, i, &mode);
WindowSize w = {mode.w, mode.h};
#endif
// SDL2's display mode list has multiple entries for modes of the same
// size with different bits per pixel, so we need to filter those out.
@@ -1146,16 +943,13 @@ std::vector<Window::WindowSize> Window::getFullscreenSizes(int displayindex) con
sizes.push_back(w);
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_free(modes);
#endif
return sizes;
}
void Window::getDesktopDimensions(int displayindex, int &width, int &height) const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
const SDL_DisplayMode *mode = SDL_GetDesktopDisplayMode(GetSDLDisplayIDForIndex(displayindex));
if (mode != nullptr)
{
@@ -1163,15 +957,6 @@ void Window::getDesktopDimensions(int displayindex, int &width, int &height) con
width = mode->w;
height = mode->h;
}
#else
if (displayindex >= 0 && displayindex < getDisplayCount())
{
SDL_DisplayMode mode = {};
SDL_GetDesktopDisplayMode(displayindex, &mode);
width = mode.w;
height = mode.h;
}
#endif
else
{
width = 0;
@@ -1194,10 +979,7 @@ void Window::setPosition(int x, int y, int displayindex)
y += displaybounds.y;
SDL_SetWindowPosition(window, x, y);
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SyncWindow(window);
#endif
settings.useposition = true;
}
@@ -1211,7 +993,6 @@ void Window::getPosition(int &x, int &y, int &displayindex)
return;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_DisplayID displayid = SDL_GetDisplayForWindow(window);
SDLDisplayIDs displayids;
displayindex = 0;
@@ -1223,9 +1004,6 @@ void Window::getPosition(int &x, int &y, int &displayindex)
break;
}
}
#else
displayindex = std::max(SDL_GetWindowDisplayIndex(window), 0);
#endif
SDL_GetWindowPosition(window, &x, &y);
@@ -1235,11 +1013,7 @@ void Window::getPosition(int &x, int &y, int &displayindex)
if (x != 0 || y != 0)
{
SDL_Rect displaybounds = {};
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GetDisplayBounds(displayid, &displaybounds);
#else
SDL_GetDisplayBounds(displayindex, &displaybounds);
#endif
x -= displaybounds.x;
y -= displaybounds.y;
@@ -1304,41 +1078,18 @@ bool Window::setIcon(love::image::ImageData *imgd)
if (!window)
return false;
#if !SDL_VERSION_ATLEAST(3, 0, 0)
Uint32 rmask, gmask, bmask, amask;
#ifdef LOVE_BIG_ENDIAN
rmask = 0xFF000000;
gmask = 0x00FF0000;
bmask = 0x0000FF00;
amask = 0x000000FF;
#else
rmask = 0x000000FF;
gmask = 0x0000FF00;
bmask = 0x00FF0000;
amask = 0xFF000000;
#endif
#endif
int w = imgd->getWidth();
int h = imgd->getHeight();
int bytesperpixel = (int) getPixelFormatBlockSize(imgd->getFormat());
int pitch = w * bytesperpixel;
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Surface *sdlicon = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA8888, imgd->getData(), pitch);
#else
SDL_Surface *sdlicon = SDL_CreateRGBSurfaceFrom(imgd->getData(), w, h, bytesperpixel * 8, pitch, rmask, gmask, bmask, amask);
#endif
if (!sdlicon)
return false;
SDL_SetWindowIcon(window, sdlicon);
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_DestroySurface(sdlicon);
#else
SDL_FreeSurface(sdlicon);
#endif
return true;
}
@@ -1356,7 +1107,6 @@ void Window::setVSync(int vsync)
// Check if adaptive vsync was requested but not supported, and fall
// back to regular vsync if so.
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (vsync == -1)
{
int actualvsync = 0;
@@ -1364,10 +1114,6 @@ void Window::setVSync(int vsync)
if (actualvsync != -1)
SDL_GL_SetSwapInterval(1);
}
#else
if (vsync == -1 && SDL_GL_GetSwapInterval() != -1)
SDL_GL_SetSwapInterval(1);
#endif
}
#ifdef LOVE_GRAPHICS_VULKAN
@@ -1391,13 +1137,9 @@ int Window::getVSync() const
{
if (glcontext != nullptr)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
int interval = 0;
SDL_GL_GetSwapInterval(&interval);
return interval;
#else
return SDL_GL_GetSwapInterval();
#endif
}
#if defined(LOVE_GRAPHICS_METAL)
@@ -1433,11 +1175,7 @@ void Window::setDisplaySleepEnabled(bool enable)
bool Window::isDisplaySleepEnabled() const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
return SDL_ScreenSaverEnabled();
#else
return SDL_IsScreenSaverEnabled() != SDL_FALSE;
#endif
}
void Window::minimize()
@@ -1515,27 +1253,16 @@ void Window::swapBuffers()
dwmRefreshRate = (double)info.rateRefresh.uiNumerator / (double)info.rateRefresh.uiDenominator;
SDL_DisplayMode dmode = {};
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_DisplayID display = SDL_GetDisplayForWindow(window);
const SDL_DisplayMode* modePtr = SDL_GetCurrentDisplayMode(display);
if (modePtr)
dmode = *modePtr;
#else
int displayindex = SDL_GetWindowDisplayIndex(window);
if (displayindex >= 0)
SDL_GetCurrentDisplayMode(displayindex, &dmode);
#endif
if (dmode.refresh_rate > 0 && dwmRefreshRate > 0 && (fabs(dmode.refresh_rate - dwmRefreshRate) < 2))
{
SDL_GL_SetSwapInterval(0);
#if SDL_VERSION_ATLEAST(3, 0, 0)
int interval = 0;
if (SDL_GL_GetSwapInterval(&interval) == 0 && interval == 0)
#else
if (SDL_GL_GetSwapInterval() == 0)
#endif
useDwmFlush = true;
else
SDL_GL_SetSwapInterval(swapInterval);
@@ -1568,32 +1295,20 @@ bool Window::hasMouseFocus() const
bool Window::isVisible() const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
return window && (SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN) == 0;
#else
return window && (SDL_GetWindowFlags(window) & SDL_WINDOW_SHOWN) != 0;
#endif
}
void Window::setMouseGrab(bool grab)
{
mouseGrabbed = grab;
if (window)
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SetWindowMouseGrab(window, grab);
#else
SDL_SetWindowGrab(window, (SDL_bool) grab);
#endif
}
bool Window::isMouseGrabbed() const
{
if (window)
#if SDL_VERSION_ATLEAST(3, 0, 0)
return SDL_GetWindowMouseGrab(window);
#else
return SDL_GetWindowGrab(window) != SDL_FALSE;
#endif
else
return mouseGrabbed;
}
@@ -1739,11 +1454,7 @@ bool Window::showMessageBox(const std::string &title, const std::string &message
SDL_MessageBoxFlags flags = convertMessageBoxType(type);
SDL_Window *sdlwindow = attachtowindow ? window : nullptr;
#if SDL_VERSION_ATLEAST(3, 0, 0)
return SDL_ShowSimpleMessageBox(flags, title.c_str(), message.c_str(), sdlwindow);
#else
return SDL_ShowSimpleMessageBox(flags, title.c_str(), message.c_str(), sdlwindow) >= 0;
#endif
}
int Window::showMessageBox(const MessageBoxData &data)
@@ -1763,11 +1474,7 @@ int Window::showMessageBox(const MessageBoxData &data)
{
SDL_MessageBoxButtonData sdlbutton = {};
#if SDL_VERSION_ATLEAST(3, 0, 0)
sdlbutton.buttonID = i;
#else
sdlbutton.buttonid = i;
#endif
sdlbutton.text = data.buttons[i].c_str();
if (i == data.enterButtonIndex)
@@ -1796,17 +1503,7 @@ void Window::requestAttention(bool continuous)
FLASHWINFO flashinfo = { sizeof(FLASHWINFO) };
#if SDL_VERSION_ATLEAST(3, 0, 0)
flashinfo.hwnd = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
#else
SDL_SysWMinfo wminfo = {};
SDL_VERSION(&wminfo.version);
if (!SDL_GetWindowWMInfo(window, &wminfo))
return;
flashinfo.hwnd = wminfo.info.win.window;
#endif
flashinfo.uCount = 1;
flashinfo.dwFlags = FLASHW_ALL;
-4
View File
@@ -27,11 +27,7 @@
#include "graphics/Graphics.h"
// SDL
#if __has_include(<SDL3/SDL.h>)
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#endif
namespace love
{