Skip to content

Commit

Permalink
Work towards SDL3 for NME_LOCAL_TOOLKIT
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsando committed Sep 13, 2024
1 parent 8f8559f commit 9290346
Show file tree
Hide file tree
Showing 14 changed files with 772 additions and 382 deletions.
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
[submodule "project/toolkit/sdl/SDL"]
path = project/toolkit/sdl/SDL
url = https://github.com/libsdl-org/SDL.git
branch = SDL2
branch = main
[submodule "project/toolkit/sdl-mixer/SDL_mixer"]
path = project/toolkit/sdl-mixer/SDL_mixer
url = https://github.com/libsdl-org/SDL_mixer.git
branch = SDL2
branch = main
[submodule "project/toolkit/modplug/libmodplug"]
path = project/toolkit/modplug/libmodplug
url = https://github.com/Konstanty/libmodplug.git
22 changes: 14 additions & 8 deletions project/ToolkitBuild.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
<set name="NME_METAL" value="1" if="mac" unless="nme_no_metal" />
<set name="NME_NO_OGL" value="1" if="nme_no_ogl" />
<set name="NME_NO_OGL" value="1" if="nme_metal" unless="mac" />
<set name="NME_ANGLE" value="1" if="windows||winrt" unless="NME_NO_ANGLE" />
<set name="NME_ANGLE" value="1" if="windows||winrt" unless="NME_NO_ANGLE || NME_LOCAL_TOOLKIT" />
<set name="NME_DYNAMIC_ANGLE" value="1" if="NME_LOCAL_TOOLKIT" unless="NME_NO_ANGLE" />
<set name="NATIVE_TOOLKIT_SDL_ANGLE" value="1" if="NME_ANGLE" />
<set name="NATIVE_TOOLKIT_STATIC_ANGLE" value="1" if="NME_ANGLE" />

Expand Down Expand Up @@ -77,14 +78,15 @@
<set name="NME_SDL2" value="1" if="macos" />
<set name="NME_SDL2" value="1" if="winrpi" />
<set name="NME_SDL2" value="1" if="gcw0" />
<set name="NME_SDL3" value="1" if="NME_LOCAL_TOOLKIT" />
<set name="SYSTEM_FREETYPE" value="1" if="gcw0" />

<set name="NME_SDL_ANY" value="1" if="NME_NATIVE_SDL_SYSTEM" />
<set name="NME_SDL_ANY" value="1" if="NME_SDL2" />
<set name="NME_MIXER" value="1" if="NME_SDL_ANY" unless="NME_NO_AUDIO" />

<!-- Use opengl backend ? -->
<set name="NME_NO_OGL" if="winrt" unless="NME_ANGLE" />
<set name="NME_NO_OGL" if="winrt" unless="NME_ANGLE || NME_DYNAMIC_ANGLE" />
<set name="NME_OGL" value="1" unless="NME_NO_OGL" />
<set name="NATIVE_TOOLKIT_METAL" value="1" if="NME_METAL" />

Expand Down Expand Up @@ -176,8 +178,6 @@
</section>




<include name="${NATIVE_TOOLKIT_PATH}/angle/files.xml" if="NME_ANGLE" unless="ANGLE_DIR" />
<include name="${ANGLE_DIR}/files.xml" if="NME_ANGLE ANGLE_DIR" />

Expand Down Expand Up @@ -267,6 +267,7 @@
<compilerflag value="-DNME_POLY2TRI" if="NME_POLY2TRI" />
<compilerflag value="-DNME_WORKER_THREADS" if="NME_WORKER_THREADS" />
<compilerflag value="-DNME_ANGLE" if="NME_ANGLE" />
<compilerflag value="-DNME_DYNAMIC_ANGLE" if="NME_DYNAMIC_ANGLE" />
<compilerflag value="-DNME_OGL" if="NME_OGL" />
<compilerflag value="-DNME_METAL" if="NME_METAL" />
<compilerflag value="-DNME_AUDIO" unless="NME_NO_AUDIO" />
Expand Down Expand Up @@ -355,6 +356,7 @@
</section>

<compilerflag value="-I${this_dir}/toolkit" if="NME_LOCAL_TOOLKIT" />
<compilerflag value="-DNME_SDL3" if="NME_SDL3" />
<compilerflag value="-I${NME_ZLIB}"/>
<compilerflag value="-I${NME_PNG}"/>
<compilerflag value="-I${NME_PNG}.." if="NME_LOCAL_TOOLKIT" />
Expand Down Expand Up @@ -394,9 +396,11 @@
<file name="${SRC_DIR}/sdl2/SDL2Stage.cpp"/>
<section unless="NME_NATIVE_SDL_SYSTEM" >
<compilerflag value="-I${SDL_CONFIG_PATH}" if="SDL_CONFIG_PATH" />
<compilerflag value="-I${NME_LIBSDL}include" />
<compilerflag value="-I${NME_LIBSDL}include/SDL3" if="NME_SDL3" />
<compilerflag value="-I${NME_LIBSDL}include" inless="NME_SDL3" />
<compilerflag value="-I${NME_LIBSDL_MIXER}" if="NME_MIXER" />
<compilerflag value="-I${NME_LIBSDL_MIXER}/include" if="NME_MIXER" />
<compilerflag value="-I${NME_LIBSDL_MIXER}/include/SDL3_mixer" if="NME_MIXER NME_SDL3" />
<compilerflag value="-I${NME_LIBSDL_MIXER}/include" if="NME_MIXER" unless="NME_SDL3" />
</section>
<section if="gcw0" >
<compilerflag value="-I${GCW0_SDK}/usr/mipsel-gcw0-linux-uclibc/sysroot/usr/include/SDL2" />
Expand Down Expand Up @@ -523,6 +527,7 @@
<compilerflag value="-DNME_POLY2TRI" if="NME_POLY2TRI" />
<compilerflag value="-DNME_WORKER_THREADS" if="NME_WORKER_THREADS" />
<compilerflag value="-DNME_ANGLE" if="NME_ANGLE" />
<compilerflag value="-DNME_DYNAMIC_ANGLE" if="NME_DYNAMIC_ANGLE" />
<compilerflag value="-I${ANGLE_DIR}/include" if="NME_ANGLE" />

<cache value="1" />
Expand Down Expand Up @@ -683,7 +688,7 @@

<section if="windows" unless="winrt">
<lib name="gdi32.lib" />
<lib name="opengl32.lib" unless="NME_ANGLE" />
<lib name="opengl32.lib" unless="NME_ANGLE || NME_DYNAMIC_ANGLE" />
<lib name="user32.lib" />
<lib name="kernel32.lib" />
<lib name="advapi32.lib" />
Expand Down Expand Up @@ -728,7 +733,8 @@
<depend name="${HXCPP}/include/hx/HxcppMain.h"/>
<file name="${this_dir}/src/winrt/Main.cpp" />
<compilerflag value="-Iinclude" />
<compilerflag value="-I${NME_LIBSDL}include" />
<compilerflag value="-I${NME_LIBSDL}include" unless="NME_SDL3" />
<compilerflag value="-I${NME_LIBSDL}include/SDL3" if="NME_SDL3" />
<compilerflag value="-I${SDL_CONFIG_PATH}" if="SDL_CONFIG_PATH" />
<cache value="1" />
</files>
Expand Down
75 changes: 59 additions & 16 deletions project/src/audio/SDLSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include <Sound.h>
#include <hx/Thread.h>

#ifndef NME_SDL3
#define SDL_IOFromConstMem(data, len) SDL_RWFromConstMem(data, len)
#else
#define Mix_LoadWAV_RW Mix_LoadWAV_IO
#define Mix_LoadMUS_RW Mix_LoadMUS_IO
#endif


namespace nme
Expand Down Expand Up @@ -44,7 +50,7 @@ void onMusicDone()

#ifdef EMSCRIPTEN
namespace {
void Mix_QuerySpec(int *frequency, Uint16 *format, int *channels)
void Mix_QuerySpec(int *frequency, SDL_AudioFormat *format, int *channels)
{
*frequency = 44100;
*format = 32784;
Expand Down Expand Up @@ -128,9 +134,9 @@ class SDLSoundChannel : public SoundChannel
{
enum { BUF_SIZE = (1<<17) };

int mFrequency;
Uint16 mFormat;
int mChannels;
int mFrequency;
SDL_AudioFormat mFormat;
int mChannels;


int mAsyncFrequency;
Expand Down Expand Up @@ -342,7 +348,7 @@ class SDLSoundChannel : public SoundChannel
mDynamicDataDue = 0;
mDynamicRequestPending = 0;
mFrequency = 0;
mFormat = 0;
mFormat = (SDL_AudioFormat)0;
mChannels = 0;
mBufferAheadSamples = 0;

Expand All @@ -361,10 +367,14 @@ class SDLSoundChannel : public SoundChannel
Mix_QuerySpec(&mFrequency, &mFormat, &mChannels);
if (mFrequency!=44100)
ELOG("Warning - Frequency mismatch %d",mFrequency);
#ifdef NME_SDL3
if (mFormat!=SDL_AUDIO_F32)
#else
if (mFormat!=32784)
ELOG("Warning - Format mismatch %d",mFormat);
#endif
ELOG("Warning - Format mismatch %d",(int)mFormat);
if (mChannels!=2)
ELOG("Warning - channe mismatch %d",mChannels);
ELOG("Warning - channel mismatch %d",mChannels);

if (sMusicFrequency==0)
sMusicFrequency = mFrequency;
Expand Down Expand Up @@ -651,7 +661,7 @@ class SDLSound : public Sound
std::string filename;
bool loaded;
int frequency;
Uint16 format;
SDL_AudioFormat format;
int channels;
double duration;
INmeSoundData *soundData;
Expand All @@ -672,7 +682,7 @@ class SDLSound : public Sound
initSound();
if (Init())
{
mChunk = Mix_LoadWAV_RW(SDL_RWFromConstMem(inData, len), 1);
mChunk = Mix_LoadWAV_RW(SDL_IOFromConstMem(inData, len), 1);

if (!mChunk)
{
Expand Down Expand Up @@ -701,7 +711,7 @@ class SDLSound : public Sound
mChunk = 0;
loaded = false;
frequency = 0;
format = 0;
format = (SDL_AudioFormat)0;
channels = 0;
duration = 0.0;
soundData = 0;
Expand Down Expand Up @@ -730,6 +740,25 @@ class SDLSound : public Sound
int bytes = soundData->getDecodedByteCount();
if (soundData->getRate()!=frequency || !soundData->getIsStereo())
{
#ifdef NME_SDL3

Uint8 *dst_data = NULL;
int dst_len = 0;
const SDL_AudioSpec src_spec = { format,
soundData->getIsStereo() ? 2 : 1,
soundData->getRate() };
const SDL_AudioSpec dst_spec = { format, channels, frequency };
if (!SDL_ConvertAudioSamples(&src_spec, data, soundData->getDecodedByteCount(),
&dst_spec, &dst_data, &dst_len))
{
printf("Could not convert data?\n");
}
else
{
mChunk = Mix_QuickLoad_RAW(dst_data, dst_len);
SDL_free(dst_data);
}
#else
SDL_AudioCVT wavecvt;
if (SDL_BuildAudioCVT(&wavecvt,
format, soundData->getIsStereo() ? 2 : 1, soundData->getRate(),
Expand All @@ -744,6 +773,8 @@ class SDLSound : public Sound
SDL_ConvertAudio(&wavecvt);
mChunk = Mix_QuickLoad_RAW(wavecvt.buf, wavecvt.len_cvt);
}
#endif

soundData->release();
soundData = 0;
}
Expand Down Expand Up @@ -787,9 +818,9 @@ class SDLSound : public Sound
if (n>0)
{
#ifndef NME_SDL12
mChunk = Mix_LoadWAV_RW(SDL_RWFromConstMem(resource.Bytes(),n),false);
mChunk = Mix_LoadWAV_RW(SDL_IOFromConstMem(resource.Bytes(),n),false);
#else
mChunk = Mix_LoadWAV_RW(SDL_RWFromConstMem(resource.Bytes(),2));
mChunk = Mix_LoadWAV_RW(SDL_IOFromConstMem(resource.Bytes(),2));
#endif
}
if (!mChunk)
Expand Down Expand Up @@ -879,7 +910,11 @@ class SDLMusicChannel : public SoundChannel
//int sdlLoops = inLoops<0 ? -1 : inLoops==0 ? 1 : inLoops;
int sdlLoops = inLoops<0 ? -1 : inLoops==0 ? 0 : inLoops-1;
//int sdlLoops = inLoops;
#ifdef NME_SDL3
if (!Mix_PlayMusic( mMusic, sdlLoops ))
#else
if (Mix_PlayMusic( mMusic, sdlLoops )<0)
#endif
{
onMusicDone();
}
Expand Down Expand Up @@ -1021,9 +1056,9 @@ class SDLMusic : public Sound
reso.resize(n);
memcpy(&reso[0], resource.Bytes(), n);
#ifdef NME_SDL2
mMusic = Mix_LoadMUS_RW(SDL_RWFromConstMem(&reso[0], reso.size()),false);
mMusic = Mix_LoadMUS_RW(SDL_IOFromConstMem(&reso[0], reso.size()),false);
#else
mMusic = Mix_LoadMUS_RW(SDL_RWFromConstMem(&reso[0], reso.size()));
mMusic = Mix_LoadMUS_RW(SDL_IOFromConstMem(&reso[0], reso.size()));
#endif

if (mMusic)
Expand Down Expand Up @@ -1066,9 +1101,9 @@ class SDLMusic : public Sound
memcpy(&reso[0], inData, len);

#ifdef NME_SDL2
mMusic = Mix_LoadMUS_RW(SDL_RWFromConstMem(&reso[0], len),false);
mMusic = Mix_LoadMUS_RW(SDL_IOFromConstMem(&reso[0], len),false);
#else
mMusic = Mix_LoadMUS_RW(SDL_RWFromConstMem(&reso[0], len));
mMusic = Mix_LoadMUS_RW(SDL_IOFromConstMem(&reso[0], len));
#endif


Expand Down Expand Up @@ -1118,7 +1153,11 @@ void SuspendSdlSound()
return;

sSoundPaused = true;
#ifdef NME_SDL3
SDL_PauseAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK);
#else
SDL_PauseAudio(true);
#endif
}

void ResumeSdlSound()
Expand All @@ -1127,7 +1166,11 @@ void ResumeSdlSound()
return;

sSoundPaused = false;
#ifdef NME_SDL3
SDL_ResumeAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK);
#else
SDL_PauseAudio(false);
#endif
}

Sound *CreateSdlSound(const std::string &inFilename,bool inForceMusic)
Expand Down
23 changes: 22 additions & 1 deletion project/src/opengl/OGL.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef INCLUDED_OGL_H
#define INCLUDED_OGL_H

#if defined(NME_DYNAMIC_ANGLE)
#define DYNAMIC_OGL
#endif

#if defined(NME_ANGLE)
// Static link, not dll import
#define EGLAPI
Expand Down Expand Up @@ -56,7 +60,9 @@

#define NME_GL_LEVEL 300
#define NEED_EXTENSIONS
#define DYNAMIC_OGL
#ifndef DYNAMIC_OGL
#define DYNAMIC_OGL
#endif

#define GL_GLEXT_PROTOTYPES
#include <SDL_opengl.h>
Expand Down Expand Up @@ -110,6 +116,21 @@ typedef ptrdiff_t GLsizeiptrARB;

#endif

namespace nme {
void *GetGlFunction(const char *functionName);
}


#if defined(NME_GLES)
const bool nmeEglMode = true;
#elif defined(NME_DYNAMIC_ANGLE)
extern bool nmeEglMode;
#else
const bool nmeEglMode = false;
#endif



#if defined(HX_WINDOWS) && !defined(NME_ANGLE)
typedef HDC WinDC;
typedef HGLRC GLCtx;
Expand Down
Loading

0 comments on commit 9290346

Please sign in to comment.