/*
Copyright (C) 2003, 2010 - Wolfire Games
-Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
+Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)
This file is part of Lugaru.
chan->position[2] = z;
OPENAL_SAMPLE* sptr = chan->sample;
- if (sptr == NULL)
+ if (sptr == NULL) {
return;
+ }
const ALuint sid = chan->sid;
const bool no_attenuate = sptr->is2d;
AL_API void OPENAL_3D_Listener_SetAttributes(const float* pos, const float*, float fx, float fy, float fz, float tx, float ty, float tz)
{
- if (!initialized)
+ if (!initialized) {
return;
+ }
if (pos != NULL) {
alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
listener_position[0] = pos[0];
AL_API signed char OPENAL_3D_SetAttributes(int channel, const float* pos)
{
- if (!initialized)
+ if (!initialized) {
return false;
- if ((channel < 0) || (channel >= (int)num_channels))
+ }
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return false;
+ }
- if (pos != NULL)
+ if (pos != NULL) {
set_channel_position(channel, pos[0], pos[1], -pos[2]);
+ }
// we ignore velocity, since doppler's broken in the Linux AL at the moment...
AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ& pos)
{
- if (!initialized)
+ if (!initialized) {
return false;
- if ((channel < 0) || (channel >= (int)num_channels))
+ }
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return false;
+ }
set_channel_position(channel, pos.x, pos.y, -pos.z);
AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
{
- if (initialized)
+ if (initialized) {
return false;
- if (maxsoftwarechannels == 0)
+ }
+ if (maxsoftwarechannels == 0) {
return false;
+ }
- if (flags != 0) // unsupported.
+ if (flags != 0) { // unsupported.
return false;
+ }
ALCdevice* dev = alcOpenDevice(NULL);
- if (!dev)
+ if (!dev) {
return false;
+ }
ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
ALCcontext* ctx = alcCreateContext(dev, caps);
num_channels = maxsoftwarechannels;
impl_channels = new OPENAL_Channels[maxsoftwarechannels];
memset(impl_channels, '\0', sizeof(OPENAL_Channels) * num_channels);
- for (unsigned i = 0; i < num_channels; i++)
+ for (unsigned i = 0; i < num_channels; i++) {
alGenSources(1, &impl_channels[i].sid); // !!! FIXME: verify this didn't fail!
+ }
initialized = true;
return true;
AL_API void OPENAL_Close()
{
- if (!initialized)
+ if (!initialized) {
return;
+ }
ALCcontext* ctx = alcGetCurrentContext();
if (ctx) {
static OPENAL_SAMPLE* OPENAL_GetCurrentSample(int channel)
{
- if (!initialized)
+ if (!initialized) {
return NULL;
- if ((channel < 0) || (channel >= (int)num_channels))
+ }
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return NULL;
+ }
return impl_channels[channel].sample;
}
static signed char OPENAL_GetPaused(int channel)
{
- if (!initialized)
+ if (!initialized) {
return false;
- if ((channel < 0) || (channel >= (int)num_channels))
+ }
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return false;
- if (impl_channels[channel].startpaused)
+ }
+ if (impl_channels[channel].startpaused) {
return (true);
+ }
ALint state = 0;
alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
static unsigned int OPENAL_GetLoopMode(int channel)
{
- if (!initialized)
+ if (!initialized) {
return 0;
- if ((channel < 0) || (channel >= (int)num_channels))
+ }
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return 0;
+ }
ALint loop = 0;
alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
- if (loop)
+ if (loop) {
return (OPENAL_LOOP_NORMAL);
+ }
return OPENAL_LOOP_OFF;
}
static signed char OPENAL_IsPlaying(int channel)
{
- if (!initialized)
+ if (!initialized) {
return false;
- if ((channel < 0) || (channel >= (int)num_channels))
+ }
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return false;
+ }
ALint state = 0;
alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
return ((state == AL_PLAYING) ? true : false);
static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE* sptr, OPENAL_DSPUNIT* dsp, signed char startpaused)
{
- if (!initialized)
+ if (!initialized) {
return -1;
- if (sptr == NULL)
+ }
+ if (sptr == NULL) {
return -1;
- if (dsp != NULL)
+ }
+ if (dsp != NULL) {
return -1;
+ }
if (channel == OPENAL_FREE) {
for (unsigned i = 0; i < num_channels; i++) {
ALint state = 0;
}
}
- if ((channel < 0) || (channel >= (int)num_channels))
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return -1;
+ }
alSourceStop(impl_channels[channel].sid);
impl_channels[channel].sample = sptr;
alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
set_channel_position(channel, 0.0f, 0.0f, 0.0f);
impl_channels[channel].startpaused = ((startpaused) ? true : false);
- if (!startpaused)
+ if (!startpaused) {
alSourcePlay(impl_channels[channel].sid);
+ }
return channel;
}
char* fname = (char*)alloca(strlen(_fname) + 16);
strcpy(fname, _fname);
char* ptr = strchr(fname, '.');
- if (ptr)
+ if (ptr) {
*ptr = '\0';
+ }
strcat(fname, ".ogg");
// just in case...
FILE* io = fopen(fname, "rb");
- if (io == NULL)
+ if (io == NULL) {
return NULL;
+ }
ALubyte* retval = NULL;
AL_API OPENAL_SAMPLE* OPENAL_Sample_Load(int index, const char* name_or_data, unsigned int mode, int offset, int length)
{
- if (!initialized)
+ if (!initialized) {
return NULL;
- if (index != OPENAL_FREE)
+ }
+ if (index != OPENAL_FREE) {
return NULL; // this is all the game does...
- if (offset != 0)
+ }
+ if (offset != 0) {
return NULL; // this is all the game does...
- if (length != 0)
+ }
+ if (length != 0) {
return NULL; // this is all the game does...
- if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D))
+ }
+ if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D)) {
return NULL; // this is all the game does...
+ }
OPENAL_SAMPLE* retval = NULL;
ALenum format = AL_NONE;
ALsizei size = 0;
ALuint frequency = 0;
void* data = decode_to_pcm(name_or_data, format, size, frequency);
- if (data == NULL)
+ if (data == NULL) {
return NULL;
+ }
ALuint bid = 0;
alGetError();
retval->mode = OPENAL_LOOP_OFF;
retval->is2d = (mode == OPENAL_2D);
retval->name = new char[strlen(name_or_data) + 1];
- if (retval->name)
+ if (retval->name) {
strcpy(retval->name, name_or_data);
+ }
}
free(data);
AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE* sptr)
{
- if (!initialized)
+ if (!initialized) {
return;
+ }
if (sptr) {
for (unsigned i = 0; i < num_channels; i++) {
if (impl_channels[i].sample == sptr) {
static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE* sptr, unsigned int mode)
{
- if (!initialized)
+ if (!initialized) {
return false;
- if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF))
+ }
+ if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF)) {
return false;
- if (!sptr)
+ }
+ if (!sptr) {
return false;
+ }
sptr->mode = mode;
return true;
}
AL_API signed char OPENAL_SetFrequency(int channel, bool slomo)
{
- if (!initialized)
+ if (!initialized) {
return false;
+ }
if (channel == OPENAL_ALL) {
- for (unsigned i = 0; i < num_channels; i++)
+ for (unsigned i = 0; i < num_channels; i++) {
OPENAL_SetFrequency(i, slomo);
+ }
return true;
}
- if ((channel < 0) || (channel >= (int)num_channels))
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return false;
- if (slomo)
+ }
+ if (slomo) {
alSourcef(impl_channels[channel].sid, AL_PITCH, ((ALfloat)slomofreq) / 44100.0f);
- else
+ } else {
alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
+ }
return true;
}
AL_API signed char OPENAL_SetVolume(int channel, int vol)
{
- if (!initialized)
+ if (!initialized) {
return false;
+ }
if (channel == OPENAL_ALL) {
- for (unsigned i = 0; i < num_channels; i++)
+ for (unsigned i = 0; i < num_channels; i++) {
OPENAL_SetVolume(i, vol);
+ }
return true;
}
- if ((channel < 0) || (channel >= (int)num_channels))
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return false;
+ }
- if (vol < 0)
+ if (vol < 0) {
vol = 0;
- else if (vol > 255)
+ } else if (vol > 255) {
vol = 255;
+ }
ALfloat gain = ((ALfloat)vol) / 255.0f;
alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
return true;
AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
{
- if (!initialized)
+ if (!initialized) {
return false;
+ }
if (channel == OPENAL_ALL) {
- for (unsigned i = 0; i < num_channels; i++)
+ for (unsigned i = 0; i < num_channels; i++) {
OPENAL_SetPaused(i, paused);
+ }
return true;
}
- if ((channel < 0) || (channel >= (int)num_channels))
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return false;
+ }
ALint state = 0;
- if (impl_channels[channel].startpaused)
+ if (impl_channels[channel].startpaused) {
state = AL_PAUSED;
- else
+ } else {
alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
+ }
- if ((paused) && (state == AL_PLAYING))
+ if ((paused) && (state == AL_PLAYING)) {
alSourcePause(impl_channels[channel].sid);
- else if ((!paused) && (state == AL_PAUSED)) {
+ } else if ((!paused) && (state == AL_PAUSED)) {
alSourcePlay(impl_channels[channel].sid);
impl_channels[channel].startpaused = false;
}
AL_API void OPENAL_SetSFXMasterVolume(int volume)
{
- if (!initialized)
+ if (!initialized) {
return;
+ }
ALfloat gain = ((ALfloat)volume) / 255.0f;
alListenerf(AL_GAIN, gain);
}
AL_API signed char OPENAL_StopSound(int channel)
{
- if (!initialized)
+ if (!initialized) {
return false;
+ }
if (channel == OPENAL_ALL) {
- for (unsigned i = 0; i < num_channels; i++)
+ for (unsigned i = 0; i < num_channels; i++) {
OPENAL_StopSound(i);
+ }
return true;
}
- if ((channel < 0) || (channel >= (int)num_channels))
+ if ((channel < 0) || (channel >= (int)num_channels)) {
return false;
+ }
alSourceStop(impl_channels[channel].sid);
impl_channels[channel].startpaused = false;
return true;
static OPENAL_SAMPLE* OPENAL_Stream_GetSample(OPENAL_STREAM* stream)
{
- if (!initialized)
+ if (!initialized) {
return NULL;
+ }
return (OPENAL_SAMPLE*)stream;
}
static signed char OPENAL_Stream_Stop(OPENAL_STREAM* stream)
{
- if (!initialized)
+ if (!initialized) {
return false;
+ }
for (unsigned i = 0; i < num_channels; i++) {
if (impl_channels[i].sample == (OPENAL_SAMPLE*)stream) {
alSourceStop(impl_channels[i].sid);
AL_API void OPENAL_Update()
{
- if (!initialized)
+ if (!initialized) {
return;
+ }
alcProcessContext(alcGetCurrentContext());
}