Skip to content

Commit

Permalink
Port over ability to disable audio module/openal
Browse files Browse the repository at this point in the history
  • Loading branch information
Speak2Erase committed May 29, 2024
1 parent 36ef004 commit bc1608b
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 50 deletions.
12 changes: 12 additions & 0 deletions binding/binding-mri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MKXPZ_NO_OPENAL
#include "audio/audio.h"
#endif
#include "filesystem/filesystem.h"
#include "display/graphics.h"
#include "display/font.h"
Expand Down Expand Up @@ -89,7 +91,9 @@ void windowVXBindingInit();
void tilemapVXBindingInit();

void inputBindingInit();
#ifndef MKXPZ_NO_OPENAL
void audioBindingInit();
#endif
void graphicsBindingInit();

void fileIntBindingInit();
Expand All @@ -113,7 +117,9 @@ void oneshotWallpaperBindingInit();
void oneshotWallpaperBindingTerminate();
#endif

#ifndef MKXPZ_NO_OPENAL
void modshotAleffectBindingInit();
#endif
void modshotwindowBindingInit();
void modshotSystemBindingInit();

Expand Down Expand Up @@ -187,7 +193,9 @@ static void mriBindingInit() {
}

inputBindingInit();
#ifndef MKXPZ_NO_OPENAL
audioBindingInit();
#endif
graphicsBindingInit();

fileIntBindingInit();
Expand All @@ -208,7 +216,9 @@ static void mriBindingInit() {
oneshotNikoBindingInit();
oneshotWallpaperBindingInit();

#ifndef MKXPZ_NO_OPENAL
modshotAleffectBindingInit();
#endif
modshotwindowBindingInit();
modshotSystemBindingInit();

Expand Down Expand Up @@ -793,7 +803,9 @@ static VALUE rgssMainRescue(VALUE arg, VALUE exc) {

static void processReset() {
shState->graphics().reset();
#ifndef MKXPZ_NO_OPENAL
shState->audio().reset();
#endif

shState->rtData().rqReset.clear();
shState->graphics().repaintWait(shState->rtData().rqResetFinish, false);
Expand Down
11 changes: 9 additions & 2 deletions binding/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ binding_source = [files(
'plane-binding.cpp',
'window-binding.cpp',
'tilemap-binding.cpp',
'audio-binding.cpp',
'module_rpg.cpp',
'filesystem-binding.cpp',
'windowvx-binding.cpp',
Expand All @@ -39,11 +38,19 @@ binding_source = [files(
'oneshot-niko-binding.cpp',
'oneshot-journal-binding.cpp',
'oneshot-wallpaper-binding.cpp',
'modshot-aleffect-binding.cpp',
'modshot-window-binding.cpp',
'modshot-system-binding.cpp',
)]

audio_binding_source = [files(
'audio-binding.cpp',
'modshot-aleffect-binding.cpp',
)]

if disable_openal == false
binding_source += audio_binding_source
endif

if steamworks == true
binding_source += files('cusl-binding.cpp')
endif
Expand Down
11 changes: 11 additions & 0 deletions binding/oneshot-niko-binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
#define NIKO_Y ((13 * 16) * 2)

#include <filesystem>
#ifdef __WIN32__
#include <process.h>
#else
#include <unistd.h>
#endif

RB_METHOD(nikoPrepare) {
RB_UNUSED_PARAM;
Expand Down Expand Up @@ -49,7 +53,14 @@ RB_METHOD(nikoStart) {
const_cast<char*>(dir.c_str()), const_cast<char*>(window_x.c_str()),
const_cast<char*>(window_y.c_str())};

#ifdef __WIN32__
spawnv(_P_DETACH, dir.c_str(), args);
#else
pid_t pid = fork();
if (pid == 0) {
execv(dir.c_str(), args);
}
#endif

return Qnil;
}
Expand Down
8 changes: 7 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ global_args += '-DMKXPZ_VERSION="@0@"'.format(meson.project_version())
global_args += '-DMKXPZ_GIT_HASH="@0@"'.format(git_hash)
global_args += '-DHAVE_NANOSLEEP'

disable_openal = get_option('disable_openal')
if disable_openal
global_args += '-DMKXPZ_NO_OPENAL'
endif

global_args += '-DZMQ_STATIC'

# ====================
Expand Down Expand Up @@ -193,7 +198,8 @@ executable(exe_name,
objc_args: global_args,
objcpp_args: global_args,
win_subsystem: 'windows',
install: (host_system != 'windows')
install: (host_system != 'windows'),
build_rpath: '$ORIGIN'
)

# Shim for Windows
Expand Down
4 changes: 3 additions & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ option('steamworks_path', type: 'string', value: '', description: 'Path to Steam
option('steam_appid', type: 'string', value: '', description: 'Steam AppID. Set this to use SteamAPI_RestartAppIfNecessary')
option('steamshim_debug', type: 'boolean', value: false, description: 'Whether to show Steamshim debug messages in the console')

option('gfx_backend', type: 'combo', value: 'gl', choices: ['gl', 'gles'], description: 'Graphics rendering API to use.')
option('gfx_backend', type: 'combo', value: 'gl', choices: ['gl', 'gles'], description: 'Graphics rendering API to use.')

option('disable_openal', type: 'boolean', value: false, description: 'Disable OpenAL audio backend')
40 changes: 33 additions & 7 deletions src/display/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

#include "graphics.h"

#ifndef MKXPZ_NO_OPENAL
#include "alstream.h"
#include "audio.h"
#endif
#include "binding.h"
#include "bitmap.h"
#include "config.h"
Expand Down Expand Up @@ -102,24 +104,33 @@ static void closeMovie(THEORAPLAY_Io *io)
struct Movie
{
THEORAPLAY_Decoder *decoder;
const THEORAPLAY_AudioPacket *audio;
const THEORAPLAY_VideoFrame *video;
bool hasVideo;
bool hasAudio;
bool skippable;
Bitmap *videoBitmap;
SDL_RWops srcOps;
#ifndef MKXPZ_NO_OPENAL
bool hasAudio;
SDL_Thread *audioThread;
AtomicFlag audioThreadTermReq;
volatile AudioQueue *audioQueueHead;
volatile AudioQueue *audioQueueTail;
const THEORAPLAY_AudioPacket *audio;
ALuint audioSource;
ALuint alBuffers[STREAM_BUFS];
ALshort audioBuffer[MOVIE_AUDIO_BUFFER_SIZE];
#endif
SDL_mutex *audioMutex;

Movie(bool skippable_)
: decoder(0), audio(0), video(0), skippable(skippable_), videoBitmap(0), audioThread(0)
: decoder(0),
video(0),
skippable(skippable_),
videoBitmap(0)
#ifndef MKXPZ_NO_OPENAL
, audioThread(0),
audio(0)
#endif
{
}
bool preparePlayback()
Expand Down Expand Up @@ -148,9 +159,10 @@ struct Movie
}

// Once we're initialized, we can tell if this file has audio and/or video.
hasAudio = THEORAPLAY_hasAudioStream(decoder);
hasVideo = THEORAPLAY_hasVideoStream(decoder);

#ifndef MKXPZ_NO_OPENAL
hasAudio = THEORAPLAY_hasAudioStream(decoder);
// Queue up the audio
if (hasAudio) {
while ((audio = THEORAPLAY_getAudio(decoder)) == NULL) {
Expand All @@ -160,6 +172,7 @@ struct Movie
SDL_Delay(VIDEO_DELAY);
}
}
#endif

// No video, so no point in doing anything else
if (!hasVideo) {
Expand All @@ -172,22 +185,27 @@ struct Movie
SDL_Delay(VIDEO_DELAY);
}

#ifndef MKXPZ_NO_OPENAL
// Wait until we have audio, if applicable
audio = NULL;
if (hasAudio) {
while ((audio = THEORAPLAY_getAudio(decoder)) == NULL && THEORAPLAY_availableVideo(decoder) < DEF_MAX_VIDEO_FRAMES) {
SDL_Delay(VIDEO_DELAY);
}
}
#endif

// Create this Bitmap without a hires replacement, because we don't
// support hires replacement for Movies yet.
videoBitmap = new Bitmap(video->width, video->height, true);
#ifndef MKXPZ_NO_OPENAL
audioQueueHead = NULL;
audioQueueTail = NULL;
#endif

return true;
}
#ifndef MKXPZ_NO_OPENAL
void queueAudioPacket(const THEORAPLAY_AudioPacket *audio) {
AudioQueue *item = NULL;

Expand Down Expand Up @@ -313,12 +331,14 @@ struct Movie

return true;
}
#endif
void play(float volume)
{
Uint32 frameMs = 0;
Uint32 baseTicks = SDL_GetTicks();
#ifndef MKXPZ_NO_OPENAL
bool openedAudio = false;
#endif
while (THEORAPLAY_isDecoding(decoder)) {
// Check for reset/shutdown input
if(shState->graphics().updateMovieInput(this)) break;
Expand All @@ -335,6 +355,7 @@ struct Movie
video = THEORAPLAY_getVideo(decoder);
}

#ifndef MKXPZ_NO_OPENAL
if (hasAudio) {
if (!audio) {
audio = THEORAPLAY_getAudio(decoder);
Expand All @@ -349,6 +370,7 @@ struct Movie
}

}
#endif

if (video && (video->playms <= now)) {
frameMs = (video->fps == 0.0) ? 0 : ((Uint32) (1000.0 / video->fps));
Expand Down Expand Up @@ -385,14 +407,17 @@ struct Movie
SDL_Delay(VIDEO_DELAY);
}

#ifndef MKXPZ_NO_OPENAL
if (openedAudio) {
bufferMovieAudio(decoder, now);
}
#endif
}
}

~Movie()
{
#ifndef MKXPZ_NO_OPENAL
if (hasAudio) {
if (audioQueueTail) {
THEORAPLAY_freeAudio(audioQueueTail->audio);
Expand All @@ -413,8 +438,9 @@ struct Movie
alDeleteSources(1, &audioSource);
alDeleteBuffers(STREAM_BUFS, alBuffers);
}
if (video) THEORAPLAY_freeVideo(video);
if (audio) THEORAPLAY_freeAudio(audio);
#endif
if (video) THEORAPLAY_freeVideo(video);
if (decoder) THEORAPLAY_stopDecode(decoder);
delete videoBitmap;
}
Expand Down
7 changes: 1 addition & 6 deletions src/display/tilemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,13 +744,8 @@ struct TilemapPrivate

void handleTile(int x, int y, int z)
{
int ox = x + viewpPos.x;
int oy = y + viewpPos.y;
if (!wrapping && (ox < 0 || oy < 0 || ox >= mapData->xSize() || oy >= mapData->ySize()))
return;

int tileInd =
tableGetWrapped(*mapData, ox, oy, z);
tableGetWrapped(*mapData, x + viewpPos.x, y + viewpPos.y, z);

/* Check for empty space */
if (tileInd < 48)
Expand Down
12 changes: 12 additions & 0 deletions src/eventthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
#include <SDL_touch.h>
#include <SDL_rect.h>

#ifndef MKXPZ_NO_OPENAL
#include <al.h>
#include <alc.h>
#include <alext.h>
#endif
#include <cmath>

#include "sharedstate.h"
Expand All @@ -45,7 +47,9 @@
#include "TouchBar.h"
#endif

#ifndef MKXPZ_NO_OPENAL
#include "al-util.h"
#endif
#include "debugwriter.h"

#ifndef __APPLE__
Expand All @@ -54,6 +58,7 @@

#include <string.h>

#ifndef MKXPZ_NO_OPENAL
typedef void (ALC_APIENTRY *LPALCDEVICEPAUSESOFT) (ALCdevice *device);
typedef void (ALC_APIENTRY *LPALCDEVICERESUMESOFT) (ALCdevice *device);

Expand Down Expand Up @@ -82,6 +87,7 @@ initALCFunctions(ALCdevice *alcDev)
}

#define HAVE_ALC_DEVICE_PAUSE alc.DevicePause
#endif

uint8_t EventThread::keyStates[];
EventThread::ControllerState EventThread::controllerState;
Expand Down Expand Up @@ -156,7 +162,9 @@ void EventThread::process(RGSSThreadData &rtData)
UnidirMessage<Vec2i> &windowSizeMsg = rtData.windowSizeMsg;
UnidirMessage<Vec2i> &drawableSizeMsg = rtData.drawableSizeMsg;

#ifndef MKXPZ_NO_OPENAL
initALCFunctions(rtData.alcDev);
#endif

// XXX this function breaks input focus on OSX
#ifndef __APPLE__
Expand Down Expand Up @@ -642,8 +650,10 @@ int EventThread::eventFilter(void *data, SDL_Event *event)
case SDL_APP_WILLENTERBACKGROUND :
Debug() << "SDL_APP_WILLENTERBACKGROUND";

#ifndef MKXPZ_NO_OPENAL
if (HAVE_ALC_DEVICE_PAUSE)
alc.DevicePause(rtData.alcDev);
#endif

rtData.syncPoint.haltThreads();

Expand All @@ -660,8 +670,10 @@ int EventThread::eventFilter(void *data, SDL_Event *event)
case SDL_APP_DIDENTERFOREGROUND :
Debug() << "SDL_APP_DIDENTERFOREGROUND";

#ifndef MKXPZ_NO_OPENAL
if (HAVE_ALC_DEVICE_PAUSE)
alc.DeviceResume(rtData.alcDev);
#endif

rtData.syncPoint.resumeThreads();

Expand Down
Loading

0 comments on commit bc1608b

Please sign in to comment.