From 9a074f8e82e8cfb851c6ba6e60f0b4c04e7e8acc Mon Sep 17 00:00:00 2001 From: DepressedTWM Date: Fri, 15 May 2026 10:37:43 +0400 Subject: [PATCH] vsync for SDL screen --- src/audio.cpp | 23 ++--------- src/audio.h | 4 +- src/audiostream.cpp | 99 +++++++++++++++------------------------------ src/audiostream.h | 15 +++---- src/main.cpp | 9 +++-- 5 files changed, 49 insertions(+), 101 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index 7e41a35..6b72330 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -233,11 +233,7 @@ Audio::Audio(RGSSThreadData &rtData) {} -void Audio::bgmPlay(const char *filename, - int volume, - int pitch, - float pos) -{ +void Audio::bgmPlay(const char *filename, int volume, int pitch, float pos){ p->current_bgm_volume = volume; p->bgm.play(filename, (volume*p->bgm_volume)/100, pitch, pos); } @@ -251,11 +247,7 @@ void Audio::bgmFade(int time){ } -void Audio::bgsPlay(const char *filename, - int volume, - int pitch, - float pos) -{ +void Audio::bgsPlay(const char *filename, int volume, int pitch, float pos){ p->current_bgs_volume = volume; p->bgs.play(filename, (volume*p->sfx_volume)/100, pitch, pos); } @@ -269,10 +261,7 @@ void Audio::bgsFade(int time){ } -void Audio::mePlay(const char *filename, - int volume, - int pitch) -{ +void Audio::mePlay(const char *filename, int volume, int pitch){ p->current_me_volume = volume; p->me.play(filename, (volume*p->bgm_volume)/100, pitch); } @@ -286,10 +275,7 @@ void Audio::meFade(int time){ } -void Audio::sePlay(const char *filename, - int volume, - int pitch) -{ +void Audio::sePlay(const char *filename, int volume, int pitch){ p->se.play(filename, (volume*p->sfx_volume)/100, pitch); } @@ -345,7 +331,6 @@ void Audio::setSFX_Volume(int value){ p->bgs.lockStream(); p->bgs.setVolume(AudioStream::Base, ((float)(p->sfx_volume * p->current_bgs_volume))/10000.0f ); p->bgs.unlockStream(); - } Audio::~Audio() { delete p; } diff --git a/src/audio.h b/src/audio.h index 80e10ec..5412d21 100644 --- a/src/audio.h +++ b/src/audio.h @@ -37,8 +37,7 @@ struct AudioPrivate; struct RGSSThreadData; -class Audio -{ +class Audio{ public: void bgmPlay(const char *filename, int volume = 100, @@ -84,3 +83,4 @@ private: }; #endif // AUDIO_H + diff --git a/src/audiostream.cpp b/src/audiostream.cpp index 4ad9a33..3d0d617 100644 --- a/src/audiostream.cpp +++ b/src/audiostream.cpp @@ -49,16 +49,13 @@ AudioStream::AudioStream(ALStream::LoopMode loopMode, streamMut = SDL_CreateMutex(); } -AudioStream::~AudioStream() -{ - if (fade.thread) - { +AudioStream::~AudioStream(){ + if (fade.thread){ fade.reqTerm.set(); SDL_WaitThread(fade.thread, 0); } - if (fadeIn.thread) - { + if (fadeIn.thread){ fadeIn.rqTerm.set(); SDL_WaitThread(fadeIn.thread, 0); } @@ -73,11 +70,7 @@ AudioStream::~AudioStream() SDL_DestroyMutex(streamMut); } -void AudioStream::play(const std::string &filename, - int volume, - int pitch, - float offset) -{ +void AudioStream::play(const std::string &filename, int volume, int pitch, float offset){ finiFadeOutInt(); lockStream(); @@ -92,8 +85,7 @@ void AudioStream::play(const std::string &filename, if (filename == current.filename && _volume == current.volume && _pitch == current.pitch - && (sState == ALStream::Playing || sState == ALStream::Paused)) - { + && (sState == ALStream::Playing || sState == ALStream::Paused)){ unlockStream(); return; } @@ -101,8 +93,7 @@ void AudioStream::play(const std::string &filename, /* If the filenames are equal, * we update the volume and pitch and continue streaming */ if (filename == current.filename - && (sState == ALStream::Playing || sState == ALStream::Paused)) - { + && (sState == ALStream::Playing || sState == ALStream::Paused)){ setVolume(Base, _volume); stream.setPitch(_pitch); current.volume = _volume; @@ -114,8 +105,7 @@ void AudioStream::play(const std::string &filename, /* Requested audio file is different from current one */ bool diffFile = (filename != current.filename); - switch (sState) - { + switch (sState){ case ALStream::Paused : case ALStream::Playing : stream.stop(); @@ -125,16 +115,13 @@ void AudioStream::play(const std::string &filename, stream.close(); /* falls through */ case ALStream::Closed : - if (diffFile) - { - try - { + if (diffFile){ + try{ /* This will throw on errors while * opening the data source */ stream.open(filename); } - catch (const Exception &e) - { + catch (const Exception &e){ unlockStream(); throw e; } @@ -146,8 +133,7 @@ void AudioStream::play(const std::string &filename, setVolume(Base, _volume); stream.setPitch(_pitch); - if (offset > 0) - { + if (offset > 0){ setVolume(FadeIn, 0); startFadeIn(); } @@ -164,8 +150,7 @@ void AudioStream::play(const std::string &filename, unlockStream(); } -void AudioStream::stop() -{ +void AudioStream::stop(){ finiFadeOutInt(); lockStream(); @@ -177,37 +162,32 @@ void AudioStream::stop() unlockStream(); } -void AudioStream::fadeOut(int duration) -{ +void AudioStream::fadeOut(int duration){ lockStream(); ALStream::State sState = stream.queryState(); noResumeStop = true; - if (fade.active) - { + if (fade.active){ unlockStream(); return; } - if (sState == ALStream::Paused) - { + if (sState == ALStream::Paused){ stream.stop(); unlockStream(); return; } - if (sState != ALStream::Playing) - { + if (sState != ALStream::Playing){ unlockStream(); return; } - if (fade.thread) - { + if (fade.thread){ fade.reqFini.set(); SDL_WaitThread(fade.thread, 0); fade.thread = 0; @@ -228,34 +208,28 @@ void AudioStream::fadeOut(int duration) /* Any access to this classes 'stream' member, * whether state query or modification, must be * protected by a 'lock'/'unlock' pair */ -void AudioStream::lockStream() -{ +void AudioStream::lockStream(){ SDL_LockMutex(streamMut); } -void AudioStream::unlockStream() -{ +void AudioStream::unlockStream(){ SDL_UnlockMutex(streamMut); } -void AudioStream::setVolume(VolumeType type, float value) -{ +void AudioStream::setVolume(VolumeType type, float value){ volumes[type] = value; updateVolume(); } -float AudioStream::getVolume(VolumeType type) -{ +float AudioStream::getVolume(VolumeType type){ return volumes[type]; } -float AudioStream::playingOffset() -{ +float AudioStream::playingOffset(){ return stream.queryOffset(); } -void AudioStream::updateVolume() -{ +void AudioStream::updateVolume(){ float vol = GLOBAL_VOLUME; for (size_t i = 0; i < VolumeTypeCount; ++i) @@ -264,25 +238,21 @@ void AudioStream::updateVolume() stream.setVolume(vol); } -void AudioStream::finiFadeOutInt() -{ - if (fade.thread) - { +void AudioStream::finiFadeOutInt(){ + if (fade.thread){ fade.reqFini.set(); SDL_WaitThread(fade.thread, 0); fade.thread = 0; } - if (fadeIn.thread) - { + if (fadeIn.thread){ fadeIn.rqFini.set(); SDL_WaitThread(fadeIn.thread, 0); fadeIn.thread = 0; } } -void AudioStream::startFadeIn() -{ +void AudioStream::startFadeIn(){ /* Previous fadein should always be terminated in play() */ assert(!fadeIn.thread); @@ -294,10 +264,8 @@ void AudioStream::startFadeIn() (this, fadeIn.threadName); } -void AudioStream::fadeOutThread() -{ - while (true) - { +void AudioStream::fadeOutThread(){ + while (true){ /* Just immediately terminate on request */ if (fade.reqTerm) break; @@ -332,10 +300,8 @@ void AudioStream::fadeOutThread() fade.active.clear(); } -void AudioStream::fadeInThread() -{ - while (true) - { +void AudioStream::fadeInThread(){ + while (true){ if (fadeIn.rqTerm) break; @@ -349,8 +315,7 @@ void AudioStream::fadeInThread() if (state != ALStream::Playing || prog >= 1.0f - || fadeIn.rqFini) - { + || fadeIn.rqFini){ setVolume(FadeIn, 1.0f); unlockStream(); diff --git a/src/audiostream.h b/src/audiostream.h index 010a814..62f10d9 100644 --- a/src/audiostream.h +++ b/src/audiostream.h @@ -28,10 +28,8 @@ #include -struct AudioStream -{ - struct - { +struct AudioStream{ + struct{ std::string filename; float volume; float pitch; @@ -43,8 +41,7 @@ struct AudioStream * playback volume. Used with setVolume(). * Base is set by play(). * External is used by MeWatch */ - enum VolumeType - { + enum VolumeType{ Base = 0, FadeOut, FadeIn, @@ -82,8 +79,7 @@ struct AudioStream SDL_Mutex *streamMut; /* Fade out */ - struct - { + struct{ /* Fade out is in progress */ AtomicFlag active; @@ -107,8 +103,7 @@ struct AudioStream } fade; /* Fade in */ - struct - { + struct{ AtomicFlag rqFini; AtomicFlag rqTerm; diff --git a/src/main.cpp b/src/main.cpp index bfac740..dd24759 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -117,6 +117,7 @@ int rgssThreadFun(void *userdata){ bool vsync = conf.vsync || conf.syncToRefreshrate; SDL_GL_SetSwapInterval(vsync ? 1 : 0); + SDL_SetHint(SDL_HINT_RENDER_VSYNC, vsync ? 1 : 0); #ifndef NDEBUG GLDebugLogger dLogger; @@ -160,7 +161,7 @@ int rgssThreadFun(void *userdata){ static void showInitError(const std::string &msg){ Debug() << msg; - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Sun is not shining:(", msg.c_str(), 0); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error :(", msg.c_str(), 0); } static void setupWindowIcon(const Config &conf, SDL_Window *win){ @@ -173,8 +174,7 @@ static void setupWindowIcon(const Config &conf, SDL_Window *win){ SDL_Surface *iconImg = IMG_Load_IO(iconSrc, true); - if (iconImg) - { + if (iconImg){ SDL_SetWindowIcon(win, iconImg); SDL_DestroySurface(iconImg); } @@ -220,6 +220,9 @@ int main(int argc, char *argv[]){ SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); SDL_SetHint(SDL_HINT_APP_ID, "OneshotSunshine"); SDL_SetHint(SDL_HINT_APP_NAME, "Oneshot: Sunshine"); + SDL_SetHint(SDL_HINT_AUDIO_DEVICE_STREAM_NAME, "Oneshot: sunshine"); + SDL_SetHint(SDL_HINT_AUDIO_DEVICE_STREAM_ROLE, "Game"); + SDL_SetHint(SDL_HINT_VIDEO_X11_ENABLE_XSYNC_EXT, "1"); //X11 work on *BSD,Solaris too! #if defined(__linux__) || defined(BSD) || defined(__sun) SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");