Skip to content

Commit daecdcb

Browse files
committed
ruby: Update to SDL3
1 parent 5d0de5f commit daecdcb

File tree

7 files changed

+59
-56
lines changed

7 files changed

+59
-56
lines changed

cmake/finders/FindSDL.cmake

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ include(FindPackageHandleStandardArgs)
4242

4343
find_package(PkgConfig QUIET)
4444
if(PKG_CONFIG_FOUND)
45-
pkg_search_module(PC_SDL QUIET sdl2)
45+
pkg_search_module(PC_SDL QUIET sdl3)
4646
endif()
4747

4848
# SDL_set_soname: Set SONAME on imported library target
@@ -64,10 +64,18 @@ macro(SDL_set_soname)
6464
unset(_result)
6565
endmacro()
6666

67+
find_library(
68+
SDL_LIBRARY
69+
NAMES SDL3 SDL3-3.0.0 SDL3-3.0
70+
HINTS ${PC_SDL_LIBRARY_DIRS}
71+
PATHS ${CMAKE_SOURCE_DIR}/.deps /usr/lib /usr/local/lib
72+
DOC "SDL location"
73+
)
74+
6775
find_path(
6876
SDL_INCLUDE_DIR
69-
NAMES SDL.h SDL2/SDL.h
70-
HINTS ${PC_SDL_INCLUDE_DIRS}
77+
NAMES SDL.h SDL3/SDL.h
78+
HINTS ${PC_SDL_INCLUDE_DIRS} ${SDL_LIBRARY}/..
7179
PATHS ${CMAKE_SOURCE_DIR}/.deps /usr/include /usr/local/include
7280
DOC "SDL include directory"
7381
# "$<$<PLATFORM_ID:Darwin>:NO_DEFAULT_PATH>"
@@ -82,15 +90,6 @@ else()
8290
set(SDL_VERSION 0.0.0)
8391
endif()
8492

85-
find_library(
86-
SDL_LIBRARY
87-
NAMES SDL2 SDL2-2.0.0 SDL2-2.0
88-
HINTS ${PC_SDL_LIBRARY_DIRS}
89-
PATHS ${CMAKE_SOURCE_DIR}/.deps /usr/lib /usr/local/lib
90-
DOC "SDL location"
91-
# "$<$<PLATFORM_ID:Darwin>:NO_DEFAULT_PATH>"
92-
)
93-
9493
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin|Windows")
9594
set(SDL_ERROR_REASON "Ensure that ares-deps are provided as part of CMAKE_PREFIX_PATH.")
9695
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD")

cmake/macos/defaults.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
include_guard(GLOBAL)
44

55
# Required to avoid us finding a system SDL2.framework before our provided SDL2.dylib
6-
set(CMAKE_FIND_FRAMEWORK LAST)
6+
# set(CMAKE_FIND_FRAMEWORK LAST)
77

88
# Set empty codesigning team if not specified as cache variable
99
if(NOT ARES_CODESIGN_TEAM)

cmake/macos/helpers.cmake

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ function(_bundle_dependencies target)
112112

113113
if(is_system_framework OR is_xcode_framework)
114114
continue()
115-
elseif(is_framework)
116-
file(REAL_PATH "../../.." library_location BASE_DIRECTORY "${imported_location}")
117115
elseif(_required_macos VERSION_GREATER CMAKE_OSX_DEPLOYMENT_TARGET)
118116
continue()
119117
elseif(NOT library_type STREQUAL "STATIC_LIBRARY")

ruby/audio/sdl.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <SDL2/SDL.h>
1+
#include <SDL3/SDL.h>
22

33
struct AudioSDL : AudioDriver {
44
AudioSDL& self = *this;
@@ -34,32 +34,32 @@ struct AudioSDL : AudioDriver {
3434

3535
auto clear() -> void override {
3636
if(!ready()) return;
37-
SDL_ClearQueuedAudio(_device);
37+
SDL_ClearAudioStream(_stream);
3838
}
3939

4040
auto output(const f64 samples[]) -> void override {
4141
if(!ready()) return;
4242

4343
if(self.blocking) {
44-
auto bytesRemaining = SDL_GetQueuedAudioSize(_device);
44+
auto bytesRemaining = SDL_GetAudioStreamAvailable(_stream);
4545
while(bytesRemaining > _bufferSize) {
4646
//wait for audio to drain
4747
auto bytesToWait = bytesRemaining - _bufferSize;
4848
auto bytesPerSample = bitsPerSample / 8.0;
4949
auto samplesRemaining = bytesToWait / bytesPerSample;
5050
auto secondsRemaining = samplesRemaining / frequency;
5151
usleep(secondsRemaining * 1000000);
52-
bytesRemaining = SDL_GetQueuedAudioSize(_device);
52+
bytesRemaining = SDL_GetAudioStreamAvailable(_stream);
5353
}
5454
}
5555

5656
std::unique_ptr<f32[]> output = std::make_unique<f32[]>(channels);
5757
for(auto n : range(channels)) output[n] = samples[n];
58-
SDL_QueueAudio(_device, &output[0], channels * sizeof(f32));
58+
SDL_PutAudioStreamData(_stream, &output[0], channels * sizeof(f32));
5959
}
6060

6161
auto level() -> f64 override {
62-
return SDL_GetQueuedAudioSize(_device) / ((f64)_bufferSize);
62+
return SDL_GetAudioStreamAvailable(_stream) / ((f64)_bufferSize);
6363
}
6464

6565
private:
@@ -72,20 +72,24 @@ struct AudioSDL : AudioDriver {
7272

7373
SDL_InitSubSystem(SDL_INIT_AUDIO);
7474

75-
SDL_AudioSpec want{}, have{};
76-
want.freq = frequency;
77-
want.format = AUDIO_F32SYS;
78-
want.channels = 2;
79-
75+
SDL_AudioSpec spec;
76+
spec.format = SDL_AUDIO_F32;
77+
spec.channels = 2;
78+
spec.freq = frequency;
8079
auto desired_samples = (latency * frequency) / 1000.0f;
81-
want.samples = pow(2, ceil(log2(desired_samples))); // SDL2 requires power-of-two buffer sizes
82-
83-
_device = SDL_OpenAudioDevice(NULL,0,&want,&have,0);
84-
frequency = have.freq;
85-
channels = have.channels;
86-
bitsPerSample = SDL_AUDIO_BITSIZE(have.format);
87-
_bufferSize = have.size;
88-
SDL_PauseAudioDevice(_device, 0);
80+
string desired_samples_string = (string)desired_samples;
81+
SDL_SetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, desired_samples_string);
82+
83+
SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL);
84+
_device = SDL_GetAudioStreamDevice(stream);
85+
SDL_ResumeAudioDevice(_device);
86+
_stream = stream;
87+
frequency = spec.freq;
88+
channels = spec.channels;
89+
int bufferFrameSize;
90+
SDL_GetAudioDeviceFormat(_device, &spec, &bufferFrameSize);
91+
bitsPerSample = SDL_AUDIO_BITSIZE(spec.format);
92+
_bufferSize = bufferFrameSize * channels * 4;
8993

9094
_ready = true;
9195
clear();
@@ -105,5 +109,6 @@ struct AudioSDL : AudioDriver {
105109
bool _ready = false;
106110

107111
SDL_AudioDeviceID _device = 0;
112+
SDL_AudioStream *_stream;
108113
u32 _bufferSize = 0;
109114
};

ruby/cmake/os-macos.cmake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ target_link_libraries(
4343
if(SDL_FOUND)
4444
target_link_libraries(
4545
ruby
46-
PRIVATE "$<LINK_LIBRARY:WEAK_LIBRARY,SDL::SDL>"
47-
# "$<$<BOOL:${SDL_FOUND}>:SDL::SDL>"
46+
PRIVATE "$<LINK_LIBRARY:WEAK_FRAMEWORK,SDL::SDL>"
4847
)
4948
endif()
5049

ruby/input/joypad/sdl.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,27 @@ struct InputJoypadSDL {
2828
}
2929

3030
auto poll(vector<shared_pointer<HID::Device>>& devices) -> void {
31-
SDL_JoystickUpdate();
31+
SDL_UpdateJoysticks();
3232
SDL_Event event;
3333
while(SDL_PollEvent(&event)) {
34-
if(event.type == SDL_JOYDEVICEADDED || event.type == SDL_JOYDEVICEREMOVED) {
34+
if(event.type == SDL_EVENT_JOYSTICK_ADDED || event.type == SDL_EVENT_JOYSTICK_REMOVED) {
3535
enumerate();
3636
}
3737
}
3838

3939
for(auto& jp : joypads) {
4040
for(u32 n : range(jp.hid->axes().size())) {
41-
assign(jp, HID::Joypad::GroupID::Axis, n, (s16)SDL_JoystickGetAxis(jp.handle, n));
41+
assign(jp, HID::Joypad::GroupID::Axis, n, (s16)SDL_GetJoystickAxis(jp.handle, n));
4242
}
4343

4444
for(s32 n = 0; n < (s32)jp.hid->hats().size() - 1; n += 2) {
45-
u8 state = SDL_JoystickGetHat(jp.handle, n >> 1);
45+
u8 state = SDL_GetJoystickHat(jp.handle, n >> 1);
4646
assign(jp, HID::Joypad::GroupID::Hat, n + 0, state & SDL_HAT_LEFT ? -32767 : state & SDL_HAT_RIGHT ? +32767 : 0);
4747
assign(jp, HID::Joypad::GroupID::Hat, n + 1, state & SDL_HAT_UP ? -32767 : state & SDL_HAT_DOWN ? +32767 : 0);
4848
}
4949

5050
for(u32 n : range(jp.hid->buttons().size())) {
51-
assign(jp, HID::Joypad::GroupID::Button, n, (bool)SDL_JoystickGetButton(jp.handle, n));
51+
assign(jp, HID::Joypad::GroupID::Button, n, (bool)SDL_GetJoystickButton(jp.handle, n));
5252
}
5353

5454
devices.append(jp.hid);
@@ -59,7 +59,7 @@ struct InputJoypadSDL {
5959
for(auto& jp : joypads) {
6060
if(jp.hid->id() != id) continue;
6161

62-
SDL_JoystickRumble(jp.handle, strong, weak, 0);
62+
SDL_RumbleJoystick(jp.handle, strong, weak, 0);
6363
return true;
6464
}
6565

@@ -70,14 +70,14 @@ struct InputJoypadSDL {
7070
terminate();
7171
SDL_Init(SDL_INIT_EVENTS);
7272
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
73-
SDL_JoystickEventState(SDL_ENABLE);
73+
//SDL_JoystickEventState(1);
7474
enumerate();
7575
return true;
7676
}
7777

7878
auto terminate() -> void {
7979
for(auto& jp : joypads) {
80-
SDL_JoystickClose(jp.handle);
80+
SDL_CloseJoystick(jp.handle);
8181
}
8282
joypads.reset();
8383
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
@@ -86,31 +86,33 @@ struct InputJoypadSDL {
8686
private:
8787
auto enumerate() -> void {
8888
for(auto& joypad : joypads) {
89-
SDL_JoystickClose(joypad.handle);
89+
SDL_CloseJoystick(joypad.handle);
9090
}
9191
joypads.reset();
92-
93-
for(u32 id : range(SDL_NumJoysticks())) {
92+
int num_joysticks;
93+
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
94+
for(int i = 0; i < num_joysticks; i++) {
95+
SDL_JoystickID id = joysticks[i];
9496
Joypad jp;
9597
jp.id = id;
96-
jp.handle = SDL_JoystickOpen(jp.id);
98+
jp.handle = SDL_OpenJoystick(jp.id);
9799
if(!jp.handle) {
98100
const char *err = SDL_GetError();
99101
print("Error opening SDL joystick id ", id, ": ", err);
100102
continue;
101103
}
102104

103-
s32 axes = SDL_JoystickNumAxes(jp.handle);
104-
s32 hats = SDL_JoystickNumHats(jp.handle) * 2;
105-
s32 buttons = SDL_JoystickNumButtons(jp.handle);
105+
s32 axes = SDL_GetNumJoystickAxes(jp.handle);
106+
s32 hats = SDL_GetNumJoystickHats(jp.handle) * 2;
107+
s32 buttons = SDL_GetNumJoystickButtons(jp.handle);
106108
if(axes < 0 || hats < 0 || buttons < 0) {
107109
const char *err = SDL_GetError();
108110
print("Error retrieving SDL joystick information for device ", jp.handle, " at index ", id, ": ", err);
109111
continue;
110112
}
111113

112-
u16 vid = SDL_JoystickGetVendor(jp.handle);
113-
u16 pid = SDL_JoystickGetProduct(jp.handle);
114+
u16 vid = SDL_GetJoystickVendor(jp.handle);
115+
u16 pid = SDL_GetJoystickProduct(jp.handle);
114116
if(vid == 0) vid = HID::Joypad::GenericVendorID;
115117
if(pid == 0) pid = HID::Joypad::GenericProductID;
116118

@@ -124,7 +126,7 @@ struct InputJoypadSDL {
124126

125127
joypads.append(jp);
126128
}
127-
128-
SDL_JoystickUpdate();
129+
SDL_free(joysticks);
130+
SDL_UpdateJoysticks();
129131
}
130132
};

ruby/input/sdl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <SDL2/SDL.h>
1+
#include <SDL3/SDL.h>
22

33
#if defined(PLATFORM_WINDOWS)
44
#include "shared/rawinput.cpp"

0 commit comments

Comments
 (0)