Mercurial > hg > dmlib
changeset 846:05a3ee1ca964
Cleanups.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 21 Oct 2014 10:42:08 +0300 |
parents | 1d3d220fb5cc |
children | cf142447dd63 |
files | src/dmengine.c src/dmengine.h src/dmsimple.c |
diffstat | 3 files changed, 74 insertions(+), 55 deletions(-) [+] |
line wrap: on
line diff
--- a/src/dmengine.c Tue Oct 21 10:31:03 2014 +0300 +++ b/src/dmengine.c Tue Oct 21 10:42:08 2014 +0300 @@ -6,6 +6,7 @@ */ #include "dmengine.h" #include "dmimage.h" +#include <SDL_timer.h> #ifdef DM_USE_TREMOR @@ -396,6 +397,26 @@ } +int engineGetVideoAspect(int width, int height) +{ + if (width > 0 && height > 0) + return (width * 1000) / height; + else + return 1; +} + + +void enginePauseAudio(int status) +{ + if (status) + engine.audioStatus = SDL_AUDIO_PAUSED; + else + engine.audioStatus = SDL_AUDIO_PLAYING; + + SDL_PauseAudio(status); +} + + void engineAudioCallback(void *userdata, Uint8 *stream, int len) { DMEngineData *engine = (DMEngineData *) userdata; @@ -441,21 +462,59 @@ } -void enginePauseAudio(int status) +static int engineAudioThreadFunc(void *userdata) { - if (status) - engine.audioStatus = SDL_AUDIO_PAUSED; - else - engine.audioStatus = SDL_AUDIO_PLAYING; + DMEngineData *engine = (DMEngineData *) userdata; + if (engine == NULL) + return 0; - SDL_PauseAudio(status); + do + { + dmMutexLock(engine->audioStreamMutex); + if (engine->audioStatus == SDL_AUDIO_PLAYING) + { + engineAudioCallback(userdata, engine->audioSimBuf, engine->audioSimBufSize); + } + dmMutexUnlock(engine->audioStreamMutex); + + SDL_Delay(engine->audioSimDelay); + } while (!engine->audioSimDone); + + return 0; } -int engineGetVideoAspect(int width, int height) +int engineInitAudioParts(DMEngineData *engine) { - if (width > 0 && height > 0) - return (width * 1000) / height; - else - return 1; + if (engine == NULL) + return DMERR_NULLPTR; + + engine->audioStreamMutex = dmCreateMutex(); + engine->audioStatus = SDL_AUDIO_STOPPED; + engine->optAfmt.callback = engineAudioCallback; + engine->optAfmt.userdata = (void *) engine; + + engine->audioSampleSize = engine->optAfmt.channels; + switch (engine->optAfmt.format) + { + case AUDIO_S16SYS: + case AUDIO_U16SYS: engine->audioSampleSize *= 2; break; + } + + if (SDL_OpenAudio(&engine->optAfmt, NULL) < 0) + { + // We'll let this pass, as we want to support no-sound. + dmError("Couldn't open SDL audio, falling back to no sound: %s\n", SDL_GetError()); + + // Set up simulated audio thread + engine->audioSimDelay = 1000 / 45; + engine->audioSimBufSize = (engine->optAfmt.freq / 45) * engine->audioSampleSize; + engine->audioSimBuf = dmMalloc(engine->audioSimBufSize); + engine->audioSimDone = FALSE; + engine->audioSimThread = SDL_CreateThread(engineAudioThreadFunc, NULL); + if (engine->audioSimThread == NULL) + return DMERR_INIT_FAIL; + } + + return DMERR_OK; }
--- a/src/dmengine.h Tue Oct 21 10:31:03 2014 +0300 +++ b/src/dmengine.h Tue Oct 21 10:42:08 2014 +0300 @@ -367,6 +367,7 @@ int engineGetTick(DMEngineData *eng); float engineGetTimeDT(DMEngineData *eng); +int engineInitAudioParts(DMEngineData *engine); void engineAudioCallback(void *userdata, Uint8 *stream, int len); void enginePauseAudio(int status); int engineGetVideoAspect(int width, int height);
--- a/src/dmsimple.c Tue Oct 21 10:31:03 2014 +0300 +++ b/src/dmsimple.c Tue Oct 21 10:42:08 2014 +0300 @@ -61,28 +61,6 @@ } -static int engineAudioThreadFunc(void *userdata) -{ - DMEngineData *engine = (DMEngineData *) userdata; - if (engine == NULL) - return 0; - - do - { - dmMutexLock(engine->audioStreamMutex); - if (engine->audioStatus == SDL_AUDIO_PLAYING) - { - engineAudioCallback(userdata, engine->audioSimBuf, engine->audioSimBufSize); - } - dmMutexUnlock(engine->audioStreamMutex); - - SDL_Delay(engine->audioSimDelay); - } while (!engine->audioSimDone); - - return 0; -} - - static int engineShowProgress(int loaded, int total) { int dx = 60, @@ -687,29 +665,10 @@ engine.optAfmt.format, engine.optAfmt.channels, engine.optAfmt.freq, engine.optAfmt.samples); - engine.audioStreamMutex = dmCreateMutex(); - engine.audioStatus = SDL_AUDIO_STOPPED; - engine.optAfmt.callback = engineAudioCallback; - engine.optAfmt.userdata = (void *) &engine; - - engine.audioSampleSize = engine.optAfmt.channels; - switch (engine.optAfmt.format) + if ((err = engineInitAudioParts(&engine)) != DMERR_OK) { - case AUDIO_S16SYS: - case AUDIO_U16SYS: engine.audioSampleSize *= 2; break; - } - - if (SDL_OpenAudio(&engine.optAfmt, NULL) < 0) - { - // We'll let this pass, as we want to support no-sound. - dmError("Couldn't open SDL audio, falling back to no sound: %s\n", SDL_GetError()); - - // Set up simulated audio thread - engine.audioSimDelay = 1000 / 45; - engine.audioSimBufSize = (engine.optAfmt.freq / 45) * engine.audioSampleSize; - engine.audioSimBuf = dmMalloc(engine.audioSimBufSize); - engine.audioSimDone = FALSE; - engine.audioSimThread = SDL_CreateThread(engineAudioThreadFunc, NULL); + dmError("engineInitAudioParts() failed: #%d: %s\n", err, dmErrorStr(err)); + goto error_exit; } // Initialize SDL video