vsync for SDL screen

This commit is contained in:
DepressedTWM 2026-05-15 10:37:43 +04:00
parent a7e38b0a77
commit 9a074f8e82
5 changed files with 49 additions and 101 deletions

View File

@ -233,11 +233,7 @@ Audio::Audio(RGSSThreadData &rtData)
{} {}
void Audio::bgmPlay(const char *filename, void Audio::bgmPlay(const char *filename, int volume, int pitch, float pos){
int volume,
int pitch,
float pos)
{
p->current_bgm_volume = volume; p->current_bgm_volume = volume;
p->bgm.play(filename, (volume*p->bgm_volume)/100, pitch, pos); 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, void Audio::bgsPlay(const char *filename, int volume, int pitch, float pos){
int volume,
int pitch,
float pos)
{
p->current_bgs_volume = volume; p->current_bgs_volume = volume;
p->bgs.play(filename, (volume*p->sfx_volume)/100, pitch, pos); 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, void Audio::mePlay(const char *filename, int volume, int pitch){
int volume,
int pitch)
{
p->current_me_volume = volume; p->current_me_volume = volume;
p->me.play(filename, (volume*p->bgm_volume)/100, pitch); 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, void Audio::sePlay(const char *filename, int volume, int pitch){
int volume,
int pitch)
{
p->se.play(filename, (volume*p->sfx_volume)/100, 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.lockStream();
p->bgs.setVolume(AudioStream::Base, ((float)(p->sfx_volume * p->current_bgs_volume))/10000.0f ); p->bgs.setVolume(AudioStream::Base, ((float)(p->sfx_volume * p->current_bgs_volume))/10000.0f );
p->bgs.unlockStream(); p->bgs.unlockStream();
} }
Audio::~Audio() { delete p; } Audio::~Audio() { delete p; }

View File

@ -37,8 +37,7 @@
struct AudioPrivate; struct AudioPrivate;
struct RGSSThreadData; struct RGSSThreadData;
class Audio class Audio{
{
public: public:
void bgmPlay(const char *filename, void bgmPlay(const char *filename,
int volume = 100, int volume = 100,
@ -84,3 +83,4 @@ private:
}; };
#endif // AUDIO_H #endif // AUDIO_H

View File

@ -49,16 +49,13 @@ AudioStream::AudioStream(ALStream::LoopMode loopMode,
streamMut = SDL_CreateMutex(); streamMut = SDL_CreateMutex();
} }
AudioStream::~AudioStream() AudioStream::~AudioStream(){
{ if (fade.thread){
if (fade.thread)
{
fade.reqTerm.set(); fade.reqTerm.set();
SDL_WaitThread(fade.thread, 0); SDL_WaitThread(fade.thread, 0);
} }
if (fadeIn.thread) if (fadeIn.thread){
{
fadeIn.rqTerm.set(); fadeIn.rqTerm.set();
SDL_WaitThread(fadeIn.thread, 0); SDL_WaitThread(fadeIn.thread, 0);
} }
@ -73,11 +70,7 @@ AudioStream::~AudioStream()
SDL_DestroyMutex(streamMut); SDL_DestroyMutex(streamMut);
} }
void AudioStream::play(const std::string &filename, void AudioStream::play(const std::string &filename, int volume, int pitch, float offset){
int volume,
int pitch,
float offset)
{
finiFadeOutInt(); finiFadeOutInt();
lockStream(); lockStream();
@ -92,8 +85,7 @@ void AudioStream::play(const std::string &filename,
if (filename == current.filename if (filename == current.filename
&& _volume == current.volume && _volume == current.volume
&& _pitch == current.pitch && _pitch == current.pitch
&& (sState == ALStream::Playing || sState == ALStream::Paused)) && (sState == ALStream::Playing || sState == ALStream::Paused)){
{
unlockStream(); unlockStream();
return; return;
} }
@ -101,8 +93,7 @@ void AudioStream::play(const std::string &filename,
/* If the filenames are equal, /* If the filenames are equal,
* we update the volume and pitch and continue streaming */ * we update the volume and pitch and continue streaming */
if (filename == current.filename if (filename == current.filename
&& (sState == ALStream::Playing || sState == ALStream::Paused)) && (sState == ALStream::Playing || sState == ALStream::Paused)){
{
setVolume(Base, _volume); setVolume(Base, _volume);
stream.setPitch(_pitch); stream.setPitch(_pitch);
current.volume = _volume; current.volume = _volume;
@ -114,8 +105,7 @@ void AudioStream::play(const std::string &filename,
/* Requested audio file is different from current one */ /* Requested audio file is different from current one */
bool diffFile = (filename != current.filename); bool diffFile = (filename != current.filename);
switch (sState) switch (sState){
{
case ALStream::Paused : case ALStream::Paused :
case ALStream::Playing : case ALStream::Playing :
stream.stop(); stream.stop();
@ -125,16 +115,13 @@ void AudioStream::play(const std::string &filename,
stream.close(); stream.close();
/* falls through */ /* falls through */
case ALStream::Closed : case ALStream::Closed :
if (diffFile) if (diffFile){
{ try{
try
{
/* This will throw on errors while /* This will throw on errors while
* opening the data source */ * opening the data source */
stream.open(filename); stream.open(filename);
} }
catch (const Exception &e) catch (const Exception &e){
{
unlockStream(); unlockStream();
throw e; throw e;
} }
@ -146,8 +133,7 @@ void AudioStream::play(const std::string &filename,
setVolume(Base, _volume); setVolume(Base, _volume);
stream.setPitch(_pitch); stream.setPitch(_pitch);
if (offset > 0) if (offset > 0){
{
setVolume(FadeIn, 0); setVolume(FadeIn, 0);
startFadeIn(); startFadeIn();
} }
@ -164,8 +150,7 @@ void AudioStream::play(const std::string &filename,
unlockStream(); unlockStream();
} }
void AudioStream::stop() void AudioStream::stop(){
{
finiFadeOutInt(); finiFadeOutInt();
lockStream(); lockStream();
@ -177,37 +162,32 @@ void AudioStream::stop()
unlockStream(); unlockStream();
} }
void AudioStream::fadeOut(int duration) void AudioStream::fadeOut(int duration){
{
lockStream(); lockStream();
ALStream::State sState = stream.queryState(); ALStream::State sState = stream.queryState();
noResumeStop = true; noResumeStop = true;
if (fade.active) if (fade.active){
{
unlockStream(); unlockStream();
return; return;
} }
if (sState == ALStream::Paused) if (sState == ALStream::Paused){
{
stream.stop(); stream.stop();
unlockStream(); unlockStream();
return; return;
} }
if (sState != ALStream::Playing) if (sState != ALStream::Playing){
{
unlockStream(); unlockStream();
return; return;
} }
if (fade.thread) if (fade.thread){
{
fade.reqFini.set(); fade.reqFini.set();
SDL_WaitThread(fade.thread, 0); SDL_WaitThread(fade.thread, 0);
fade.thread = 0; fade.thread = 0;
@ -228,34 +208,28 @@ void AudioStream::fadeOut(int duration)
/* Any access to this classes 'stream' member, /* Any access to this classes 'stream' member,
* whether state query or modification, must be * whether state query or modification, must be
* protected by a 'lock'/'unlock' pair */ * protected by a 'lock'/'unlock' pair */
void AudioStream::lockStream() void AudioStream::lockStream(){
{
SDL_LockMutex(streamMut); SDL_LockMutex(streamMut);
} }
void AudioStream::unlockStream() void AudioStream::unlockStream(){
{
SDL_UnlockMutex(streamMut); SDL_UnlockMutex(streamMut);
} }
void AudioStream::setVolume(VolumeType type, float value) void AudioStream::setVolume(VolumeType type, float value){
{
volumes[type] = value; volumes[type] = value;
updateVolume(); updateVolume();
} }
float AudioStream::getVolume(VolumeType type) float AudioStream::getVolume(VolumeType type){
{
return volumes[type]; return volumes[type];
} }
float AudioStream::playingOffset() float AudioStream::playingOffset(){
{
return stream.queryOffset(); return stream.queryOffset();
} }
void AudioStream::updateVolume() void AudioStream::updateVolume(){
{
float vol = GLOBAL_VOLUME; float vol = GLOBAL_VOLUME;
for (size_t i = 0; i < VolumeTypeCount; ++i) for (size_t i = 0; i < VolumeTypeCount; ++i)
@ -264,25 +238,21 @@ void AudioStream::updateVolume()
stream.setVolume(vol); stream.setVolume(vol);
} }
void AudioStream::finiFadeOutInt() void AudioStream::finiFadeOutInt(){
{ if (fade.thread){
if (fade.thread)
{
fade.reqFini.set(); fade.reqFini.set();
SDL_WaitThread(fade.thread, 0); SDL_WaitThread(fade.thread, 0);
fade.thread = 0; fade.thread = 0;
} }
if (fadeIn.thread) if (fadeIn.thread){
{
fadeIn.rqFini.set(); fadeIn.rqFini.set();
SDL_WaitThread(fadeIn.thread, 0); SDL_WaitThread(fadeIn.thread, 0);
fadeIn.thread = 0; fadeIn.thread = 0;
} }
} }
void AudioStream::startFadeIn() void AudioStream::startFadeIn(){
{
/* Previous fadein should always be terminated in play() */ /* Previous fadein should always be terminated in play() */
assert(!fadeIn.thread); assert(!fadeIn.thread);
@ -294,10 +264,8 @@ void AudioStream::startFadeIn()
<AudioStream, &AudioStream::fadeInThread>(this, fadeIn.threadName); <AudioStream, &AudioStream::fadeInThread>(this, fadeIn.threadName);
} }
void AudioStream::fadeOutThread() void AudioStream::fadeOutThread(){
{ while (true){
while (true)
{
/* Just immediately terminate on request */ /* Just immediately terminate on request */
if (fade.reqTerm) if (fade.reqTerm)
break; break;
@ -332,10 +300,8 @@ void AudioStream::fadeOutThread()
fade.active.clear(); fade.active.clear();
} }
void AudioStream::fadeInThread() void AudioStream::fadeInThread(){
{ while (true){
while (true)
{
if (fadeIn.rqTerm) if (fadeIn.rqTerm)
break; break;
@ -349,8 +315,7 @@ void AudioStream::fadeInThread()
if (state != ALStream::Playing if (state != ALStream::Playing
|| prog >= 1.0f || prog >= 1.0f
|| fadeIn.rqFini) || fadeIn.rqFini){
{
setVolume(FadeIn, 1.0f); setVolume(FadeIn, 1.0f);
unlockStream(); unlockStream();

View File

@ -28,10 +28,8 @@
#include <string> #include <string>
struct AudioStream struct AudioStream{
{ struct{
struct
{
std::string filename; std::string filename;
float volume; float volume;
float pitch; float pitch;
@ -43,8 +41,7 @@ struct AudioStream
* playback volume. Used with setVolume(). * playback volume. Used with setVolume().
* Base is set by play(). * Base is set by play().
* External is used by MeWatch */ * External is used by MeWatch */
enum VolumeType enum VolumeType{
{
Base = 0, Base = 0,
FadeOut, FadeOut,
FadeIn, FadeIn,
@ -82,8 +79,7 @@ struct AudioStream
SDL_Mutex *streamMut; SDL_Mutex *streamMut;
/* Fade out */ /* Fade out */
struct struct{
{
/* Fade out is in progress */ /* Fade out is in progress */
AtomicFlag active; AtomicFlag active;
@ -107,8 +103,7 @@ struct AudioStream
} fade; } fade;
/* Fade in */ /* Fade in */
struct struct{
{
AtomicFlag rqFini; AtomicFlag rqFini;
AtomicFlag rqTerm; AtomicFlag rqTerm;

View File

@ -117,6 +117,7 @@ int rgssThreadFun(void *userdata){
bool vsync = conf.vsync || conf.syncToRefreshrate; bool vsync = conf.vsync || conf.syncToRefreshrate;
SDL_GL_SetSwapInterval(vsync ? 1 : 0); SDL_GL_SetSwapInterval(vsync ? 1 : 0);
SDL_SetHint(SDL_HINT_RENDER_VSYNC, vsync ? 1 : 0);
#ifndef NDEBUG #ifndef NDEBUG
GLDebugLogger dLogger; GLDebugLogger dLogger;
@ -160,7 +161,7 @@ int rgssThreadFun(void *userdata){
static void showInitError(const std::string &msg){ static void showInitError(const std::string &msg){
Debug() << 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){ 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); SDL_Surface *iconImg = IMG_Load_IO(iconSrc, true);
if (iconImg) if (iconImg){
{
SDL_SetWindowIcon(win, iconImg); SDL_SetWindowIcon(win, iconImg);
SDL_DestroySurface(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_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
SDL_SetHint(SDL_HINT_APP_ID, "OneshotSunshine"); SDL_SetHint(SDL_HINT_APP_ID, "OneshotSunshine");
SDL_SetHint(SDL_HINT_APP_NAME, "Oneshot: Sunshine"); 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! //X11 work on *BSD,Solaris too!
#if defined(__linux__) || defined(BSD) || defined(__sun) #if defined(__linux__) || defined(BSD) || defined(__sun)
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0"); SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");