Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to SDL3 #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt update
sudo apt install libgl1-mesa-dev libfreetype6-dev libfontconfig1-dev libxxf86vm-dev libpng-dev libvorbis-dev g++ libwebp-dev git libsdl2-dev ninja-build
sudo apt install libgl1-mesa-dev libfreetype6-dev libfontconfig1-dev libxxf86vm-dev libpng-dev libvorbis-dev g++ libwebp-dev git libsdl3-dev ninja-build
- name: Configure with CMake and GCC
run: cmake -Bbuild -G"Ninja Multi-Config"
- name: Build Debug
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
brew install sdl2 libvorbis webp ninja
brew install sdl3 libvorbis webp ninja
- name: Build with CMake
run: |
cmake -Bbuild-cmake -GNinja
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:
image: registry.fedoraproject.org/fedora-minimal:40
steps:
- name: Install dependencies
run: microdnf install -y cmake ninja-build mingw64-pkg-config mingw64-libvorbis mingw64-SDL2 mingw64-fontconfig mingw64-libwebp mingw64-dlfcn mingw32-nsis mingw64-gcc-c++ tar gzip
run: microdnf install -y cmake ninja-build mingw64-pkg-config mingw64-libvorbis mingw64-SDL3 mingw64-fontconfig mingw64-libwebp mingw64-dlfcn mingw32-nsis mingw64-gcc-c++ tar gzip
- uses: actions/checkout@v4
- name: Build with CMake and GCC
run: |
Expand Down
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ linux:
allow_failure: false
stage: build
script:
- dnf install -y cmake ninja-build gcc-c++ fontconfig-devel freetype-devel libvorbis-devel libwebp-devel SDL2-devel libtheora-devel clang util-linux clang-tools-extra >/dev/null
- dnf install -y cmake ninja-build gcc-c++ fontconfig-devel freetype-devel libvorbis-devel libwebp-devel SDL3-devel libtheora-devel clang util-linux clang-tools-extra >/dev/null
- .github/ci.py
- CXXFLAGS=-fdiagnostics-color cmake -Bbuild -GNinja
- cd build
Expand All @@ -44,7 +44,7 @@ mingw:
allow_failure: false
stage: build
script:
- dnf install -y cmake ninja-build mingw64-pkg-config mingw64-libvorbis mingw64-SDL2 mingw64-fontconfig mingw64-libwebp mingw64-dlfcn mingw64-gcc-c++ > /dev/null
- dnf install -y cmake ninja-build mingw64-pkg-config mingw64-libvorbis mingw64-SDL3 mingw64-fontconfig mingw64-libwebp mingw64-dlfcn mingw64-gcc-c++ > /dev/null
- mingw64-cmake -G"Ninja Multi-Config" -Bbuild-mingw -DJNGL_WINAPI=0
- cd build-mingw
- ninja
Expand Down
28 changes: 14 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ else()
endif()
if(UNIX AND NOT IOS AND NOT EMSCRIPTEN)
find_package(PkgConfig REQUIRED)
pkg_check_modules(SDL2 REQUIRED sdl2)
target_include_directories(jngl PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(jngl PRIVATE ${SDL2_LINK_LIBRARIES})
pkg_check_modules(SDL3 REQUIRED sdl3)
target_include_directories(jngl PRIVATE ${SDL3_INCLUDE_DIRS})
target_link_libraries(jngl PRIVATE ${SDL3_LINK_LIBRARIES})
endif()

if(NOT IOS AND NOT EMSCRIPTEN)
Expand All @@ -238,9 +238,9 @@ else()
if(MSVC)
target_compile_definitions(jngl PUBLIC _USE_MATH_DEFINES)
if(JNGL_WINAPI)
CPMAddPackage(NAME sdl2
URL https://www.libsdl.org/release/SDL2-2.30.11.zip
URL_HASH SHA1=2464d38c9d247ba223f28d4ddc1a7570edf02030
CPMAddPackage(NAME sdl
URL https://www.libsdl.org/release/SDL3-3.1.6.zip
URL_HASH SHA1=b16ad311975378dcc87b11fc02c84eaa1f028b38
OPTIONS
"SDL_ATOMIC OFF"
"SDL_CPUINFO ON"
Expand All @@ -257,17 +257,17 @@ else()
"SDL_TIMERS OFF"
)
else()
CPMAddPackage(NAME sdl2
URL https://www.libsdl.org/release/SDL2-2.30.11.zip
URL_HASH SHA1=2464d38c9d247ba223f28d4ddc1a7570edf02030
CPMAddPackage(NAME sdl
URL https://www.libsdl.org/release/SDL3-3.1.6.zip
URL_HASH SHA1=b16ad311975378dcc87b11fc02c84eaa1f028b38
OPTIONS
"SDL_SENSOR OFF" # doesn't work with UWP
)
endif()
target_link_libraries(jngl PRIVATE SDL2-static)
target_link_libraries(jngl PRIVATE SDL3-static)
if(WINDOWS_STORE)
target_compile_definitions(jngl PUBLIC JNGL_UWP)
target_include_directories(jngl PUBLIC ${sdl2_SOURCE_DIR}/include ${sdl2_BINARY_DIR}/include) # TODO: This should be PRIVATE
target_include_directories(jngl PUBLIC ${sdl3_SOURCE_DIR}/include ${sdl3_BINARY_DIR}/include) # TODO: This should be PRIVATE
target_link_options(jngl PUBLIC $<IF:$<CONFIG:Debug>,/defaultlib:vccorlibd.lib /defaultlib:msvcrtd.lib,/defaultlib:vccorlib.lib /defaultlib:msvcrt.lib>)
endif()
elseif(IOS)
Expand Down Expand Up @@ -309,9 +309,9 @@ else()

find_package(PkgConfig)
if(PkgConfig_FOUND)
pkg_check_modules(SDL2 REQUIRED sdl2)
target_include_directories(jngl PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(jngl PRIVATE ${SDL2_LINK_LIBRARIES})
pkg_check_modules(SDL3 REQUIRED sdl3)
target_include_directories(jngl PRIVATE ${SDL3_INCLUDE_DIRS})
target_link_libraries(jngl PRIVATE ${SDL3_LINK_LIBRARIES})
if(JNGL_VIDEO)
if(APPLE)
# On macOS we'll build theora from source, since Homebrew's version crashes due
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ cmake --build build

```
sudo apt-get install libgl1-mesa-dev libfreetype6-dev libfontconfig1-dev libpng-dev \
libxxf86vm-dev libvorbis-dev cmake g++ libwebp-dev git libsdl2-dev
libxxf86vm-dev libvorbis-dev cmake g++ libwebp-dev git libsdl3-dev
```

### Fedora

```
sudo dnf install fontconfig-devel freetype-devel libvorbis-devel libwebp-devel \
cmake SDL2-devel gcc-c++ libatomic
cmake SDL3-devel gcc-c++ libatomic
```

### Arch Linux

```
pacman -Syu --needed cmake gcc sdl2 pkg-config fontconfig libwebp libvorbis
pacman -Syu --needed cmake gcc sdl3 pkg-config fontconfig libwebp libvorbis
```

## Windows
Expand All @@ -52,7 +52,7 @@ Set up [MSYS2](https://www.msys2.org/) and install the following in a MinGW-w64
pacman -Syu --needed mingw-w64-x86_64-gcc \
mingw-w64-x86_64-freetype mingw-w64-x86_64-libvorbis mingw-w64-x86_64-libwebp \
mingw-w64-x86_64-dlfcn mingw-w64-x86_64-cmake make mingw-w64-x86_64-gdb \
mingw-w64-x86_64-libtheora mingw-w64-x86_64-SDL2
mingw-w64-x86_64-libtheora mingw-w64-x86_64-SDL3
```

### Visual Studio 2017 or newer
Expand All @@ -68,7 +68,7 @@ and then open `build/jngl.sln`.
Use [Homebrew](http://brew.sh/) to install the build dependencies:

```
brew install sdl2 freetype libvorbis webp pkg-config cmake
brew install sdl3 freetype libvorbis webp pkg-config cmake
```

## Android
Expand Down
55 changes: 24 additions & 31 deletions src/audio/sdl/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "../Stream.hpp"
#include "../constants.hpp"

#include <SDL.h>
#include <SDL3/SDL.h>

#include <cassert>
#include <vector>
Expand Down Expand Up @@ -78,61 +78,54 @@ struct engine::Impl {
struct SdlImpl : public Backend {
std::shared_ptr<void> sdl_init;

SDL_AudioDeviceID device;
SDL_AudioStream* device = nullptr;

std::vector<float> buffer;

std::shared_ptr<Stream> output;

explicit SdlImpl(std::shared_ptr<Stream> output) : output(std::move(output)) {
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
if (!SDL_Init(SDL_INIT_AUDIO)) {
throw std::runtime_error(SDL_GetError());
}
SDL_AudioSpec desired, obtained;
desired.freq = frequency;
desired.channels = 2;
desired.format = AUDIO_S16SYS;
#ifdef __EMSCRIPTEN__
desired.samples = 2048;
#else
desired.samples = 256;
#endif
desired.callback = &callback;
desired.userdata = this;
if (device = SDL_OpenAudioDevice(nullptr, 0, &desired, &obtained, 0); device == 0) {
SDL_AudioSpec spec;
SDL_zero(spec);
spec.freq = frequency;
spec.channels = 2;
spec.format = SDL_AUDIO_F32;
if (device = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec,
&callback, this);
!device) {
throw std::runtime_error(SDL_GetError());
}

internal::debug("Initialized audio: {} channels, {} Hz, {} samples",
static_cast<int>(obtained.channels), obtained.freq, obtained.samples);
internal::debug("Initialized audio: {} channels, {} Hz, {} samples", spec.channels,
spec.freq, spec.freq);

buffer.resize(obtained.samples * obtained.channels);
SDL_PauseAudioDevice(device, 0);
SDL_ResumeAudioStreamDevice(device);
}
~SdlImpl() override {
SDL_CloseAudioDevice(device);
SDL_DestroyAudioStream(device);
}

static void callback(void* userdata, std::uint8_t* dst_u8, int len) {
static std::string const profiler_str = "audio";
// prof::profiler prof(profiler_str);

static void callback(void* userdata, SDL_AudioStream* stream, int additionalAmount, int totalAmount) {
auto self = static_cast<SdlImpl*>(userdata);
std::int16_t* dst = reinterpret_cast<std::int16_t*>(dst_u8);

std::size_t const size = len / 2;
self->buffer.resize(additionalAmount);
std::size_t const size = additionalAmount;
std::size_t read = 0;
read = self->output->read(self->buffer.data(), size);
std::fill(self->buffer.data() + read, self->buffer.data() + size, 0.f);

for (auto s : self->buffer) {
*dst++ = static_cast<std::int16_t>(
std::max(std::min((65535.f * s - 1.f) / 2.f, 32767.f), -32768.f));
}
SDL_PutAudioStreamData(stream, self->buffer.data(), additionalAmount * sizeof(float));
}

void setPause(bool pause) override {
SDL_PauseAudioDevice(device, pause ? 1 : 0);
if (pause) {
SDL_PauseAudioStreamDevice(device);
} else {
SDL_ResumeAudioStreamDevice(device);
}
}
};
};
Expand Down
Loading
Loading