2011-08-24 14:14:44 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2003 Parallel Realities
|
2015-03-01 21:37:32 +01:00
|
|
|
Copyright (C) 2011, 2012 Guus Sliepen
|
2020-12-26 06:10:10 +01:00
|
|
|
Copyright (C) 2015-2020 The Diligent Circle <diligentcircle@riseup.net>
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
2015-02-26 17:20:36 +01:00
|
|
|
as published by the Free Software Foundation; either version 3
|
2011-08-24 14:14:44 +02:00
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-02-26 17:20:36 +01:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2015-02-26 17:20:36 +01:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-24 14:14:44 +02:00
|
|
|
*/
|
|
|
|
|
2016-11-26 00:01:36 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2016-11-26 00:01:36 +01:00
|
|
|
#include "SDL_mixer.h"
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2016-11-26 00:01:36 +01:00
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "structs.h"
|
|
|
|
|
2021-04-03 18:13:44 +02:00
|
|
|
#include "alien.h"
|
2016-11-26 00:01:36 +01:00
|
|
|
#include "game.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "screen.h"
|
2011-08-26 21:29:04 +02:00
|
|
|
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2015-03-08 15:38:58 +01:00
|
|
|
static Mix_Chunk *sound[SFX_MAX];
|
|
|
|
static Mix_Music *music = NULL;
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2015-03-08 15:38:58 +01:00
|
|
|
void audio_loadSounds()
|
|
|
|
{
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2015-03-08 15:38:58 +01:00
|
|
|
sound[SFX_EXPLOSION] = Mix_LoadWAV("sound/explode.ogg");
|
|
|
|
sound[SFX_HIT] = Mix_LoadWAV("sound/explode2.ogg");
|
|
|
|
sound[SFX_DEATH] = Mix_LoadWAV("sound/maledeath.ogg");
|
|
|
|
sound[SFX_MISSILE] = Mix_LoadWAV("sound/missile.ogg");
|
|
|
|
sound[SFX_PLASMA] = Mix_LoadWAV("sound/plasma.ogg");
|
|
|
|
sound[SFX_CLOCK] = Mix_LoadWAV("sound/clock.ogg");
|
|
|
|
sound[SFX_FLY] = Mix_LoadWAV("sound/flyby.ogg");
|
|
|
|
sound[SFX_ENERGYRAY] = Mix_LoadWAV("sound/beamLaser.ogg");
|
|
|
|
sound[SFX_PICKUP] = Mix_LoadWAV("sound/item.ogg");
|
|
|
|
sound[SFX_SHIELDUP] = Mix_LoadWAV("sound/shield.ogg");
|
|
|
|
sound[SFX_CLOAK] = Mix_LoadWAV("sound/cloak.ogg");
|
|
|
|
sound[SFX_DEBRIS] = Mix_LoadWAV("sound/explode3.ogg");
|
|
|
|
sound[SFX_DEBRIS2] = Mix_LoadWAV("sound/explode4.ogg");
|
|
|
|
sound[SFX_LASER] = Mix_LoadWAV("sound/laser.ogg");
|
|
|
|
sound[SFX_PLASMA2] = Mix_LoadWAV("sound/plasma2.ogg");
|
|
|
|
sound[SFX_PLASMA3] = Mix_LoadWAV("sound/plasma3.ogg");
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2015-03-08 15:38:58 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 05:56:26 +01:00
|
|
|
void audio_playSound(int sid, float x, float y)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2012-03-11 15:11:15 +01:00
|
|
|
int channel = -1;
|
2012-03-02 23:00:35 +01:00
|
|
|
static int freechannel = 4;
|
2016-01-11 06:10:55 +01:00
|
|
|
static int channelVolume[4] = {0, 0, 0, 0};
|
2016-01-11 05:56:26 +01:00
|
|
|
int angle = atanf((x - (screen->w / 2)) / (screen->w / 2)) * 180 / M_PI;
|
|
|
|
int attenuation = fabsf(x - (screen->w / 2)) / (screen->w / 20);
|
|
|
|
float distance = sqrtf(powf(fabsf(x - (screen->w / 2)), 2) + powf(fabsf(y - (screen->h / 2)), 2));
|
2021-07-26 06:34:47 +02:00
|
|
|
const int max_volume = MIX_MAX_VOLUME;
|
2021-04-02 06:08:55 +02:00
|
|
|
int volume = max_volume - (max_volume * distance / (3 * screen->w));
|
2016-01-11 05:56:26 +01:00
|
|
|
|
|
|
|
if ((!engine.useSound) || (!engine.useAudio) || (volume <= 0))
|
|
|
|
return;
|
2012-03-02 23:00:35 +01:00
|
|
|
|
2011-08-24 14:14:44 +02:00
|
|
|
switch(sid)
|
|
|
|
{
|
|
|
|
case SFX_DEATH:
|
|
|
|
case SFX_CLOCK:
|
|
|
|
case SFX_FLY:
|
|
|
|
case SFX_SHIELDUP:
|
|
|
|
case SFX_PICKUP:
|
|
|
|
case SFX_CLOAK:
|
|
|
|
case SFX_PLASMA2:
|
|
|
|
case SFX_PLASMA3:
|
2012-03-02 23:00:35 +01:00
|
|
|
channel = -1;
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
case SFX_PLASMA:
|
|
|
|
case SFX_LASER:
|
2012-03-02 23:00:35 +01:00
|
|
|
channel = 0;
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
case SFX_ENERGYRAY:
|
|
|
|
case SFX_MISSILE:
|
2012-03-02 23:00:35 +01:00
|
|
|
channel = 1;
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
case SFX_HIT:
|
2012-03-02 23:00:35 +01:00
|
|
|
channel = 2;
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
case SFX_EXPLOSION:
|
|
|
|
case SFX_DEBRIS:
|
|
|
|
case SFX_DEBRIS2:
|
2012-03-02 23:00:35 +01:00
|
|
|
channel = 3;
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-03-02 23:00:35 +01:00
|
|
|
|
2016-01-11 06:10:55 +01:00
|
|
|
if (channel == -1)
|
|
|
|
{
|
2012-03-02 23:00:35 +01:00
|
|
|
channel = freechannel++;
|
2016-01-11 06:10:55 +01:00
|
|
|
if (freechannel >= 8)
|
2012-03-02 23:00:35 +01:00
|
|
|
freechannel = 4;
|
|
|
|
}
|
2016-01-11 06:10:55 +01:00
|
|
|
else
|
|
|
|
{
|
2021-04-02 06:08:55 +02:00
|
|
|
if (Mix_Playing(channel) && (volume <= max_volume / 4)
|
|
|
|
&& (channelVolume[channel] >= max_volume * 3 / 4))
|
2016-01-11 06:10:55 +01:00
|
|
|
return;
|
|
|
|
else
|
|
|
|
channelVolume[channel] = volume;
|
|
|
|
}
|
2012-03-02 23:00:35 +01:00
|
|
|
|
2016-01-11 05:56:26 +01:00
|
|
|
angle %= 360;
|
2015-03-08 15:38:58 +01:00
|
|
|
if (angle < 0)
|
2012-03-02 23:00:35 +01:00
|
|
|
angle += 360;
|
|
|
|
|
2015-03-08 15:38:58 +01:00
|
|
|
if (attenuation > 255)
|
2012-03-02 23:00:35 +01:00
|
|
|
attenuation = 255;
|
|
|
|
|
|
|
|
Mix_SetPosition(channel, angle, attenuation);
|
2016-01-11 05:56:26 +01:00
|
|
|
Mix_Volume(channel, volume);
|
2012-03-02 23:00:35 +01:00
|
|
|
Mix_PlayChannel(channel, sound[sid], 0);
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
2015-03-08 15:38:58 +01:00
|
|
|
void audio_haltMusic()
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2015-03-08 15:38:58 +01:00
|
|
|
if (Mix_PlayingMusic())
|
2015-04-26 13:45:58 +02:00
|
|
|
{
|
|
|
|
Mix_ResumeMusic();
|
2015-03-08 15:38:58 +01:00
|
|
|
Mix_HaltMusic();
|
2015-04-26 13:45:58 +02:00
|
|
|
}
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2015-03-08 15:38:58 +01:00
|
|
|
if (music != NULL)
|
|
|
|
{
|
|
|
|
Mix_FreeMusic(music);
|
|
|
|
music = NULL;
|
|
|
|
}
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
2015-04-26 13:45:58 +02:00
|
|
|
void audio_pauseMusic()
|
|
|
|
{
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2015-04-26 13:45:58 +02:00
|
|
|
if (Mix_PlayingMusic() && !Mix_PausedMusic())
|
|
|
|
Mix_PauseMusic();
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2015-04-26 13:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void audio_resumeMusic()
|
|
|
|
{
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2015-04-26 13:45:58 +02:00
|
|
|
Mix_ResumeMusic();
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2015-04-26 13:45:58 +02:00
|
|
|
}
|
|
|
|
|
2015-03-08 15:38:58 +01:00
|
|
|
void audio_setMusicVolume(int volume)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2015-03-08 15:38:58 +01:00
|
|
|
if (engine.useMusic && engine.useAudio)
|
|
|
|
Mix_VolumeMusic(volume);
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2015-03-08 15:38:58 +01:00
|
|
|
}
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2021-04-03 18:13:44 +02:00
|
|
|
void audio_setMusicPosition(double position)
|
|
|
|
{
|
|
|
|
#ifndef NOSOUND
|
|
|
|
if (engine.useMusic && engine.useAudio)
|
|
|
|
Mix_SetMusicPosition(position);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-26 06:34:47 +02:00
|
|
|
void audio_playMusic(const char *filename, int loops)
|
2015-03-08 15:38:58 +01:00
|
|
|
{
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2015-03-08 15:38:58 +01:00
|
|
|
if (engine.useMusic && engine.useAudio)
|
|
|
|
{
|
|
|
|
audio_haltMusic();
|
|
|
|
music = Mix_LoadMUS(filename);
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_setMusicVolume(MIX_MAX_VOLUME);
|
2015-03-08 15:38:58 +01:00
|
|
|
Mix_PlayMusic(music, loops);
|
|
|
|
}
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
2015-03-08 15:38:58 +01:00
|
|
|
void audio_playRandomTrack()
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2011-08-26 22:48:52 +02:00
|
|
|
if ((!engine.useMusic) || (!engine.useAudio))
|
2011-08-24 14:14:44 +02:00
|
|
|
return;
|
|
|
|
|
2020-12-26 19:33:15 +01:00
|
|
|
#ifdef OLD_MUSIC
|
|
|
|
int tracks = 0;
|
|
|
|
char track[][PATH_MAX] = {
|
|
|
|
"music/Frantic.mod", "music/Artificial.mod", "music/Lunatic.mod",
|
|
|
|
"music/ToxicFriend.mod", "music/DigitalInferno.mod",
|
|
|
|
"music/TempoTrance.mod", "music/IntoTheMachine.mod"
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (game.system)
|
|
|
|
{
|
|
|
|
case SYSTEM_SPIRIT:
|
|
|
|
tracks = 3;
|
|
|
|
break;
|
|
|
|
case SYSTEM_EYANANTH:
|
|
|
|
tracks = 5;
|
|
|
|
break;
|
|
|
|
case SYSTEM_MORDOR:
|
|
|
|
case SYSTEM_SOL:
|
|
|
|
tracks = 7;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tracks = 3;
|
|
|
|
}
|
|
|
|
#else
|
2015-03-03 07:08:47 +01:00
|
|
|
int tracks = 4;
|
2020-12-26 19:33:15 +01:00
|
|
|
char track[][PATH_MAX] = {
|
2015-02-27 04:30:26 +01:00
|
|
|
"music/railjet_short.ogg", "music/space_dimensions.ogg",
|
2015-03-03 07:08:47 +01:00
|
|
|
"music/frozen_jam.ogg", "music/sound_and_silence.ogg"
|
2011-08-24 14:14:44 +02:00
|
|
|
};
|
2020-12-26 19:33:15 +01:00
|
|
|
#endif
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2020-12-26 19:33:15 +01:00
|
|
|
switch (game.area)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2020-12-26 19:33:15 +01:00
|
|
|
#ifndef OLD_MUSIC
|
2015-03-29 19:11:12 +02:00
|
|
|
case MISN_START:
|
2021-04-03 17:17:04 +02:00
|
|
|
case MISN_INTERCEPTION:
|
2021-04-03 18:13:44 +02:00
|
|
|
if ((aliens[ALIEN_KLINE].classDef == CD_KLINE)
|
|
|
|
&& aliens[ALIEN_KLINE].active)
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic("music/last_cyber_dance.ogg", -1);
|
2021-04-03 18:13:44 +02:00
|
|
|
else if ((game.system == SYSTEM_MORDOR)
|
|
|
|
&& (aliens[ALIEN_BOSS].classDef == CD_CLOAKFIGHTER)
|
|
|
|
&& aliens[ALIEN_BOSS].active)
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic("music/space_dimensions.ogg", -1);
|
2021-04-03 18:13:44 +02:00
|
|
|
else
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic("music/railjet_short.ogg", -1);
|
2015-03-03 07:08:47 +01:00
|
|
|
break;
|
2021-04-03 17:17:04 +02:00
|
|
|
case MISN_HAIL:
|
|
|
|
case MISN_JOLDAR:
|
|
|
|
case MISN_DORIM:
|
|
|
|
case MISN_SIVEDI:
|
|
|
|
case MISN_NEPTUNE:
|
|
|
|
case MISN_SATURN:
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic("music/frozen_jam.ogg", -1);
|
2021-04-03 17:17:04 +02:00
|
|
|
break;
|
|
|
|
case MISN_CERADSE:
|
|
|
|
case MISN_ALLEZ:
|
|
|
|
case MISN_URUSOR:
|
|
|
|
case MISN_FELLON:
|
|
|
|
case MISN_POSWIC:
|
|
|
|
case MISN_MARS:
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic("music/sound_and_silence.ogg", -1);
|
2021-04-03 17:17:04 +02:00
|
|
|
break;
|
|
|
|
case MISN_HINSTAG:
|
|
|
|
case MISN_NEROD:
|
|
|
|
case MISN_ODEON:
|
|
|
|
case MISN_ALMARTHA:
|
|
|
|
case MISN_PLUTO:
|
|
|
|
case MISN_URANUS:
|
|
|
|
case MISN_JUPITER:
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic("music/space_dimensions.ogg", -1);
|
2021-04-03 17:17:04 +02:00
|
|
|
break;
|
2020-12-26 19:33:15 +01:00
|
|
|
#endif
|
2015-03-29 19:11:12 +02:00
|
|
|
case MISN_MOEBO:
|
|
|
|
case MISN_ELAMALE:
|
|
|
|
case MISN_ELLESH:
|
|
|
|
case MISN_EARTH:
|
2020-12-26 19:33:15 +01:00
|
|
|
#ifdef OLD_MUSIC
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic("music/HardTranceDub.mod", -1);
|
2020-12-26 19:33:15 +01:00
|
|
|
#else
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic("music/orbital_colossus.ogg", -1);
|
2020-12-26 19:33:15 +01:00
|
|
|
#endif
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
2015-03-29 19:11:12 +02:00
|
|
|
case MISN_VENUS:
|
2020-12-26 19:33:15 +01:00
|
|
|
#ifdef OLD_MUSIC
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic("music/LoopsAndTings.mod", -1);
|
2020-12-26 19:33:15 +01:00
|
|
|
#else
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic("music/androids_last_dance.ogg", -1);
|
2020-12-26 19:33:15 +01:00
|
|
|
#endif
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
default:
|
2021-07-26 06:34:47 +02:00
|
|
|
audio_playMusic(track[rand() % tracks], -1);
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2015-03-08 15:38:58 +01:00
|
|
|
}
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2015-03-08 15:38:58 +01:00
|
|
|
void audio_free()
|
|
|
|
{
|
2017-01-22 06:11:13 +01:00
|
|
|
#ifndef NOSOUND
|
2015-03-08 15:38:58 +01:00
|
|
|
for (int i = 0 ; i < SFX_MAX ; i++)
|
|
|
|
{
|
|
|
|
if (sound[i] != NULL)
|
|
|
|
{
|
|
|
|
Mix_FreeChunk(sound[i]);
|
|
|
|
sound[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (music != NULL)
|
|
|
|
{
|
|
|
|
Mix_FreeMusic(music);
|
|
|
|
music = NULL;
|
|
|
|
}
|
2017-01-22 06:11:13 +01:00
|
|
|
#endif
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|