From 0929abf3a3c3679bca3343df0b070ea7347eb0b4 Mon Sep 17 00:00:00 2001 From: onpon4 Date: Sun, 10 Jan 2016 23:56:26 -0500 Subject: [PATCH] Added a Y component to audio_playSound, and reduction of volume by distance. This makes it possible to remove code that made mines that exploded off-screen completely inaudible. --- src/alien.cpp | 16 ++++++++-------- src/audio.cpp | 17 ++++++++++------- src/audio.h | 2 +- src/collectable.cpp | 4 +--- src/game.cpp | 36 ++++++++++++++++++------------------ src/player.cpp | 2 +- src/ship.cpp | 14 +++++++------- 7 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/alien.cpp b/src/alien.cpp index a169209..5a10c44 100644 --- a/src/alien.cpp +++ b/src/alien.cpp @@ -1643,7 +1643,7 @@ void alien_move(object *alien) alien->hit = 3; alien->dx *= -1; alien->dy *= -1; - audio_playSound(SFX_HIT, alien->x); + audio_playSound(SFX_HIT, alien->x, alien->y); } } } @@ -1659,9 +1659,9 @@ void alien_move(object *alien) if ((!engine.cheatShield) && (engine.missionCompleteTimer == 0)) player.shield -= alien->shield; alien->shield = 0; - audio_playSound(SFX_EXPLOSION, alien->x); + audio_playSound(SFX_EXPLOSION, alien->x, alien->y); player.hit = 5; - audio_playSound(SFX_HIT, player.x); + audio_playSound(SFX_HIT, player.x, player.y); } if (alien->classDef == CD_ASTEROID2) @@ -1669,9 +1669,9 @@ void alien_move(object *alien) if ((!engine.cheatShield) && (engine.missionCompleteTimer == 0)) player.shield -= alien->shield; alien->shield = 0; - audio_playSound(SFX_EXPLOSION, alien->x); + audio_playSound(SFX_EXPLOSION, alien->x, alien->y); player.hit = 5; - audio_playSound(SFX_HIT, player.x); + audio_playSound(SFX_HIT, player.x, player.y); } if (alien->classDef == CD_BARRIER) @@ -1679,7 +1679,7 @@ void alien_move(object *alien) if ((!engine.cheatShield) && (engine.missionCompleteTimer == 0)) player.shield--; player.hit = 5; - audio_playSound(SFX_HIT, player.x); + audio_playSound(SFX_HIT, player.x, player.y); } } } @@ -1690,7 +1690,7 @@ Fill in later... */ void alien_destroy(object *alien, object *attacker) { - audio_playSound(SFX_EXPLOSION, alien->x); + audio_playSound(SFX_EXPLOSION, alien->x, alien->y); if (alien->flags & FL_FRIEND) { @@ -1841,7 +1841,7 @@ void alien_hurt(object *alien, object *attacker, int damage, bool ion) alien->flags |= FL_LEAVESECTOR; } - audio_playSound(SFX_HIT, alien->x); + audio_playSound(SFX_HIT, alien->x, alien->y); if (alien->AIType == AI_EVASIVE) alien->thinktime = 0; diff --git a/src/audio.cpp b/src/audio.cpp index 4f7b742..6be9e0e 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -42,13 +42,17 @@ void audio_loadSounds() sound[SFX_PLASMA3] = Mix_LoadWAV("sound/plasma3.ogg"); } -void audio_playSound(int sid, float x) +void audio_playSound(int sid, float x, float y) { - if ((!engine.useSound) || (!engine.useAudio)) - return; - int channel = -1; static int freechannel = 4; + 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)); + int volume = MIX_MAX_VOLUME - (MIX_MAX_VOLUME * distance / (3 * screen->w)); + + if ((!engine.useSound) || (!engine.useAudio) || (volume <= 0)) + return; switch(sid) { @@ -86,9 +90,7 @@ void audio_playSound(int sid, float x) freechannel = 4; } - int angle = atanf((x - (screen->w / 2)) / (screen->w / 2)) * 180 / M_PI; - int attenuation = fabsf(x - (screen->w / 2)) / 40; - + angle %= 360; if (angle < 0) angle += 360; @@ -96,6 +98,7 @@ void audio_playSound(int sid, float x) attenuation = 255; Mix_SetPosition(channel, angle, attenuation); + Mix_Volume(channel, volume); Mix_PlayChannel(channel, sound[sid], 0); } diff --git a/src/audio.h b/src/audio.h index bc8fb04..68402ab 100644 --- a/src/audio.h +++ b/src/audio.h @@ -21,7 +21,7 @@ along with this program. If not, see . #define AUDIO_H void audio_loadSounds(); -void audio_playSound(int sid, float x); +void audio_playSound(int sid, float x, float y); void audio_haltMusic(); void audio_pauseMusic(); void audio_resumeMusic(); diff --git a/src/collectable.cpp b/src/collectable.cpp index eea383a..83960f4 100644 --- a/src/collectable.cpp +++ b/src/collectable.cpp @@ -229,9 +229,7 @@ bool collectable_collision(collectables *collectable, object *ship) void collectable_explode(collectables *collectable) { - if ((collectable->x >= 0) && (collectable->x <= screen->w) && - (collectable->y >= 0) && (collectable->y <= screen->h)) - audio_playSound(SFX_EXPLOSION, collectable->x); + audio_playSound(SFX_EXPLOSION, collectable->x, collectable->y); for (int i = 0 ; i < 10 ; i++) explosion_add(RANDRANGE(collectable->x - 25, collectable->x + 25), diff --git a/src/game.cpp b/src/game.cpp index 68341ca..646bd36 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -143,9 +143,9 @@ void game_init() static void game_addDebris(int x, int y, int amount) { if ((rand() % 2) == 0) - audio_playSound(SFX_DEBRIS, x); + audio_playSound(SFX_DEBRIS, x, y); else - audio_playSound(SFX_DEBRIS2, x); + audio_playSound(SFX_DEBRIS2, x, y); object *debris; @@ -450,9 +450,9 @@ static void game_doCollectables() { setInfoLine(temp, FONT_WHITE); if (collectable->type == P_SHIELD) - audio_playSound(SFX_SHIELDUP, player.x); + audio_playSound(SFX_SHIELDUP, player.x, player.y); else - audio_playSound(SFX_PICKUP, player.x); + audio_playSound(SFX_PICKUP, player.x, player.y); } } @@ -648,7 +648,7 @@ static void game_doBullets() { bullet->active = false; bullet->shield = 0; - audio_playSound(SFX_EXPLOSION, bullet->x); + audio_playSound(SFX_EXPLOSION, bullet->x, bullet->y); for (int i = 0 ; i < 10 ; i++) explosion_add(bullet->x + RANDRANGE(-35, 35), bullet->y + RANDRANGE(-35, 35), @@ -700,7 +700,7 @@ static void game_doBullets() { bullet->active = false; bullet->shield = 0; - audio_playSound(SFX_EXPLOSION, bullet->x); + audio_playSound(SFX_EXPLOSION, bullet->x, bullet->y); for (int i = 0 ; i < 10 ; i++) explosion_add(bullet->x + RANDRANGE(-35, 35), bullet->y + RANDRANGE(-35, 35), SP_BIG_EXPLOSION); @@ -712,7 +712,7 @@ static void game_doBullets() bullet->shield = 0; } - audio_playSound(SFX_HIT, player.x); + audio_playSound(SFX_HIT, player.x, player.y); if (bullet->id == WT_ROCKET) explosion_add(bullet->x, bullet->y, SP_BIG_EXPLOSION); @@ -733,11 +733,11 @@ static void game_doBullets() { bullet->active = false; explosion_add(bullet->x, bullet->y, SP_SMALL_EXPLOSION); - audio_playSound(SFX_HIT, cargo[j].x); + audio_playSound(SFX_HIT, cargo[j].x, cargo[j].y); if (cargo[j].collectType != P_PHOEBE) { cargo[j].active = false; - audio_playSound(SFX_EXPLOSION, cargo[j].x); + audio_playSound(SFX_EXPLOSION, cargo[j].x, cargo[j].y); for (int i = 0 ; i < 10 ; i++) explosion_add(cargo[j].x + RANDRANGE(-15, 15), cargo[j].y + RANDRANGE(-15, 15), @@ -803,7 +803,7 @@ static void game_doBullets() { if (bullet->flags & WF_TIMEDEXPLOSION) { - audio_playSound(SFX_EXPLOSION, bullet->x); + audio_playSound(SFX_EXPLOSION, bullet->x, bullet->y); for (int i = 0 ; i < 10 ; i++) explosion_add(bullet->x + RANDRANGE(-35, 35), bullet->y + RANDRANGE(-35, 35), SP_BIG_EXPLOSION); @@ -987,7 +987,7 @@ static void game_doAliens() aliens[i].flags -= FL_ISCLOAKED; else aliens[i].flags += FL_ISCLOAKED; - audio_playSound(SFX_CLOAK, aliens[i].x); + audio_playSound(SFX_CLOAK, aliens[i].x, aliens[i].y); } if (aliens[i].classDef == CD_BARRIER) @@ -1046,7 +1046,7 @@ static void game_doAliens() (aliens[i].ammo[0] >= 250)) { aliens[i].flags += FL_FIRERAY; - audio_playSound(SFX_ENERGYRAY, aliens[i].x); + audio_playSound(SFX_ENERGYRAY, aliens[i].x, aliens[i].y); } } } @@ -1306,7 +1306,7 @@ static void game_doPlayer() if ((engine.done == 0) && (engine.gameSection == SECTION_GAME) && (currentMission.remainingObjectives1 == 0)) { - audio_playSound(SFX_FLY, screen->w / 2); + audio_playSound(SFX_FLY, screen->w / 2, screen->h / 2); engine.done = 2; engine.missionCompleteTimer = (SDL_GetTicks() - 1); } @@ -1431,8 +1431,8 @@ static void game_doPlayer() aliens[i].flags |= FL_LEAVESECTOR; } - audio_playSound(SFX_DEATH, player.x); - audio_playSound(SFX_EXPLOSION, player.x); + audio_playSound(SFX_DEATH, player.x, player.y); + audio_playSound(SFX_EXPLOSION, player.x, player.y); } engine.keyState[KEY_UP] = engine.keyState[KEY_DOWN] = engine.keyState[KEY_LEFT] = engine.keyState[KEY_RIGHT] = 0; @@ -1754,7 +1754,7 @@ static void game_doHud() { if ((engine.seconds > 1) && (engine.seconds <= 11) && (engine.minutes == 0)) { - audio_playSound(SFX_CLOCK, screen->w / 2); + audio_playSound(SFX_CLOCK, screen->w / 2, screen->h / 2); } if (engine.seconds > 0) @@ -2333,14 +2333,14 @@ int game_mainLoop() if ((game.area == MISN_MOEBO) && (aliens[ALIEN_BOSS].flags & FL_ESCAPED)) { - audio_playSound(SFX_DEATH, aliens[ALIEN_BOSS].x); + audio_playSound(SFX_DEATH, aliens[ALIEN_BOSS].x, aliens[ALIEN_BOSS].y); screen_clear(white); renderer_update(); for (int i = 0 ; i < 300 ; i++) { SDL_Delay(10); if ((rand() % 25) == 0) - audio_playSound(SFX_EXPLOSION, aliens[ALIEN_BOSS].x); + audio_playSound(SFX_EXPLOSION, aliens[ALIEN_BOSS].x, aliens[ALIEN_BOSS].y); } SDL_Delay(1000); break; diff --git a/src/player.cpp b/src/player.cpp index 684fac6..27ae2e3 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -320,7 +320,7 @@ void leaveSector() if (player.x <= -100) { engine.done = 2; - audio_playSound(SFX_FLY, screen->w / 2); + audio_playSound(SFX_FLY, screen->w / 2, screen->h / 2); } } diff --git a/src/ship.cpp b/src/ship.cpp index 3470979..20c0c5d 100644 --- a/src/ship.cpp +++ b/src/ship.cpp @@ -61,16 +61,16 @@ void ship_fireBullet(object *ship, int weaponType) case WT_PLASMA: case WT_SPREAD: case WT_DIRECTIONAL: - audio_playSound(SFX_PLASMA, ship->x); + audio_playSound(SFX_PLASMA, ship->x, ship->y); break; case WT_ROCKET: - audio_playSound(SFX_MISSILE, ship->x); + audio_playSound(SFX_MISSILE, ship->x, ship->y); break; case WT_LASER: - audio_playSound(SFX_LASER, ship->x); + audio_playSound(SFX_LASER, ship->x, ship->y); break; case WT_CHARGER: - audio_playSound(SFX_PLASMA3, ship->x); + audio_playSound(SFX_PLASMA3, ship->x, ship->y); break; } @@ -177,11 +177,11 @@ void ship_fireRay(object *ship) player.shield--; explosion_add(player.x, player.y, SP_SMALL_EXPLOSION); - audio_playSound(SFX_HIT, player.x); + audio_playSound(SFX_HIT, player.x, player.y); if (player.shield < 1) { - audio_playSound(SFX_DEATH, player.x); - audio_playSound(SFX_EXPLOSION, player.x); + audio_playSound(SFX_DEATH, player.x, player.y); + audio_playSound(SFX_EXPLOSION, player.x, player.y); } } }