Skip to content

Commit

Permalink
feat: sdl3
Browse files Browse the repository at this point in the history
oops
  • Loading branch information
francisdb committed Oct 21, 2024
1 parent aa5144c commit bacf634
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 123 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ build/
/.vs/
/obj/
/release/

/platforms/**/external/
18 changes: 12 additions & 6 deletions cmake/xpinmame/CMakeLists_linux-x64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.16)
file(READ src/version.h version)
string(REGEX MATCH "#define VERSION_MAJOR +([0-9]+).*#define VERSION_MINOR +([0-9]+).*#define VERSION_REV +([0-9]+)" _ ${version})

set(PROJECT_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}")
set(PROJECT_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}")
message("PROJECT_VERSION=${PROJECT_VERSION}")

project(xpinmame VERSION ${PROJECT_VERSION})
Expand Down Expand Up @@ -651,9 +651,9 @@ find_package(
ALSA
)

find_package(
SDL2 REQUIRED
)
#find_package(
# SDL2 REQUIRED
#)

target_include_directories(xpinmame PUBLIC
src
Expand All @@ -665,7 +665,12 @@ target_include_directories(xpinmame PUBLIC
# ${X11_X11_INCLUDE_PATH}
# ${X11_Xv_INCLUDE_PATH}
${ALSA_INCLUDE_DIRS}
${SDL2_INCLUDE_DIRS}
# ${SDL2_INCLUDE_DIRS}
${CMAKE_SOURCE_DIR}/platforms/linux/x64/external/include
)

target_link_directories(xpinmame PUBLIC
${CMAKE_SOURCE_DIR}/platforms/linux/x64/external/lib
)

target_link_libraries(xpinmame PUBLIC
Expand All @@ -674,7 +679,8 @@ target_link_libraries(xpinmame PUBLIC
# ${X11_Xv_LIB}
# ${X11_Xext_LIB}
${ALSA_LIBRARIES}
${SDL2_LIBRARIES}
# ${SDL2_LIBRARIES}
SDL3
)

set_target_properties(xpinmame PROPERTIES
Expand Down
60 changes: 60 additions & 0 deletions platforms/linux/x64/external.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

set -e

SDL_VERSION=3.1.3

NUM_PROCS=$(nproc)

echo "Building external libraries..."
echo " SDL_VERSION: ${SDL_VERSION}"
echo ""

if [ -z "${BUILD_TYPE}" ]; then
BUILD_TYPE="Release"
fi

echo "Build type: ${BUILD_TYPE}"
echo "Procs: ${NUM_PROCS}"
echo ""

CACHE_DIR="external/cache/${BUILD_TYPE}"

rm -rf external/include external/lib
mkdir -p external/include external/lib ${CACHE_DIR}

rm -rf tmp
mkdir tmp
cd tmp

#
# build SDL3, SDL_image, SDL_ttf and copy to external
#

CACHE_NAME="SDL-${SDL_VERSION}_002"

if [ ! -f "../${CACHE_DIR}/${CACHE_NAME}.cache" ]; then
curl -sL https://github.com/libsdl-org/SDL/releases/download/preview-${SDL_VERSION}/SDL3-${SDL_VERSION}.tar.xz -o SDL3-${SDL_VERSION}.tar.xz
tar -xf SDL3-3.1.3.tar.xz
cd SDL3-3.1.3
cmake \
-DSDL_SHARED=ON \
-DSDL_STATIC=OFF \
-DSDL_TEST_LIBRARY=OFF \
-DSDL_OPENGLES=OFF \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-B build
cmake --build build -- -j${NUM_PROCS}
mkdir -p ../../${CACHE_DIR}/${CACHE_NAME}/include/SDL3
cp -r include/SDL3/* ../../${CACHE_DIR}/${CACHE_NAME}/include/SDL3
mkdir -p ../../${CACHE_DIR}/${CACHE_NAME}/lib
cp -a build/*.{so,so.*} ../../${CACHE_DIR}/${CACHE_NAME}/lib
cd ..

touch "../${CACHE_DIR}/${CACHE_NAME}.cache"
fi

mkdir -p ../external/include/SDL3
cp -r ../${CACHE_DIR}/${CACHE_NAME}/include/SDL3/* ../external/include/SDL3
cp -a ../${CACHE_DIR}/${CACHE_NAME}/lib/*.{so,so.*} ../external/lib

57 changes: 24 additions & 33 deletions src/unix/sysdep/dsp-drivers/sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Version 0.1, January 2002
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL2/SDL.h"
#include <SDL3/SDL.h>
#include "sysdep/sysdep_dsp.h"
#include "sysdep/sysdep_dsp_priv.h"
#include "sysdep/plugin_manager.h"
Expand Down Expand Up @@ -70,7 +70,7 @@ const struct plugin_struct sysdep_dsp_sdl = {
};

struct sdl_info {
SDL_AudioDeviceID id;
SDL_AudioStream *stream;
};

/* public methods (static but exported through the sysdep_dsp or plugin
Expand Down Expand Up @@ -107,7 +107,7 @@ static void *sdl_dsp_create(const void *flags)
dsp->hw_info.samplerate = params->samplerate;

audiospec->format = (dsp->hw_info.type & SYSDEP_DSP_16BIT)?
AUDIO_S16SYS : AUDIO_S8;
SDL_AUDIO_S16 : SDL_AUDIO_S8;
audiospec->channels = (dsp->hw_info.type & SYSDEP_DSP_STEREO)? 2:1;
audiospec->freq = dsp->hw_info.samplerate;

Expand All @@ -121,38 +121,30 @@ static void *sdl_dsp_create(const void *flags)
fprintf(stderr, "sdl info: driver = %s\n", SDL_GetCurrentAudioDriver());

// get audio subsystem and open a queue with above spec
SDL_AudioSpec *obtained;
if (!(obtained = calloc(1, sizeof(SDL_AudioSpec))))
{
fprintf(stderr, "sdl error: malloc failed for SDL_AudioSpec\n");
sdl_dsp_destroy(dsp);
return NULL;
}

const SDL_AudioDeviceID id = SDL_OpenAudioDevice(NULL, 0, audiospec, obtained, 0);
if (id == 0) {
SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, audiospec, NULL, NULL);
if (stream == NULL) {
fprintf(stderr, "sdl error: SDL_OpenAudioDevice() failed: %s\n", SDL_GetError());
return NULL;
}

// free the spec
free(audiospec);
const SDL_AudioDeviceID device_id = SDL_GetAudioStreamDevice(stream);
SDL_ResumeAudioDevice(device_id);

// get the actual obtained spec
SDL_GetAudioStreamFormat(stream, audiospec, NULL);

// print the id
fprintf(stderr, "sdl info: device id = %d\n", id);
fprintf(stderr, "sdl info: device id = %d\n", device_id);
// print the obtained contents
fprintf(stderr, "sdl info: obtained->format = %d\n", obtained->format);
fprintf(stderr, "sdl info: obtained->channels = %d\n", obtained->channels);
fprintf(stderr, "sdl info: obtained->freq = %d\n", obtained->freq);
fprintf(stderr, "sdl info: obtained->samples = %d\n", obtained->samples);
fprintf(stderr, "sdl info: obtained->callback = %p\n", obtained->callback);
fprintf(stderr, "sdl info: obtained->userdata = %p\n", obtained->userdata);
fprintf(stderr, "sdl info: obtained->format = %d\n", audiospec->format);
fprintf(stderr, "sdl info: obtained->channels = %d\n", audiospec->channels);
fprintf(stderr, "sdl info: obtained->freq = %d\n", audiospec->freq);

// resume playing on device
SDL_PauseAudioDevice(id, 0);
// free the spec
free(audiospec);

struct sdl_info *info = (struct sdl_info *)calloc(1, sizeof(struct sdl_info));
info->id = id;
info->stream = stream;

dsp->_priv = info;

Expand All @@ -165,9 +157,9 @@ static void *sdl_dsp_create(const void *flags)
}

static void sdl_dsp_destroy(struct sysdep_dsp_struct *dsp)
{
SDL_CloseAudio();
{
const struct sdl_info *info = (struct sdl_info *)dsp->_priv;
SDL_DestroyAudioStream(info->stream);
free(dsp);
}

Expand All @@ -178,13 +170,12 @@ static int sdl_dsp_write(struct sysdep_dsp_struct *dsp, unsigned char *data,
// count is the number of samples to write
const int len = (dsp->hw_info.type & SYSDEP_DSP_STEREO)? count * 4 : count * 2;

// get device id from dsp
// get stream from dsp
const struct sdl_info *info = (struct sdl_info *)dsp->_priv;
const SDL_AudioDeviceID dev = info->id;

const int result = SDL_QueueAudio(dev, data, len);
if (result != 0) {
fprintf(stderr, "error: SDL_QueueAudio() failed: %s\n", SDL_GetError());
const bool success = SDL_PutAudioStreamData(info->stream, data, len);
if (!success) {
fprintf(stderr, "error: SDL_PutAudioStreamData() failed: %s\n", SDL_GetError());
return 0;
}
return count;
Expand Down
Loading

0 comments on commit bacf634

Please sign in to comment.