From 6cfc54f019ea793c75c012af9c8249eac936cfac Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 11 Mar 2022 15:33:28 -0500 Subject: Added music and lamp popping sound --- src/game.h | 2 ++ src/main.cpp | 19 ++++++++++++++++ src/muxer.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/muxer.h | 35 ++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 src/muxer.cpp create mode 100644 src/muxer.h (limited to 'src') diff --git a/src/game.h b/src/game.h index 200b691..f0385ee 100644 --- a/src/game.h +++ b/src/game.h @@ -6,6 +6,7 @@ #include #include #include "map.h" +#include "muxer.h" const int GAME_WIDTH = 640*2; const int GAME_HEIGHT = 480*2; @@ -80,6 +81,7 @@ public: } std::mt19937& rng; + Muxer muxer; Map map; std::list kickups; diff --git a/src/main.cpp b/src/main.cpp index 4b13051..25da94f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,10 @@ void incrementIfSet(Game& game, int& count, int x, int y, Tile val = Tile::Wall) } } +int getZoomLevel(const Game& game) { + return (game.curZoom - INIT_ZOOM) / 2; +} + void tick( Game& game, int x1, @@ -96,11 +100,14 @@ bool movePlayer(Game& game, int x, int y) game.player_x = x; game.player_y = y; + game.muxer.setPlayerLoc(x, y); game.dirtyLighting = true; return true; } else { + //game.muxer.playSoundAtPosition("bump", game.player_x, game.player_y); + return false; } } @@ -241,6 +248,8 @@ void kickUpDust(Game& game, int x, int y, size_t chain) void popLamp(Game& game, int x, int y, size_t chain) { + game.muxer.playSoundAtPosition("pop", x, y); + if (game.map.at(x,y).tile == Tile::Lamp) { game.numLamps--; @@ -400,6 +409,15 @@ void setZoom(Game& game, size_t zoom) { game.curBoundY = game.map.getBottom() - zoom * ZOOM_Y_FACTOR; } + + int zoomLevel = getZoomLevel(game); + if (zoomLevel == 0) { + game.muxer.setMusicLevel(0); + } else if (zoomLevel < 3) { + game.muxer.setMusicLevel(1); + } else { + game.muxer.setMusicLevel(2); + } } int main(int, char**) @@ -731,6 +749,7 @@ int main(int, char**) zoomAcc -= zoomDt; } + game.muxer.update(); renderer.render(game, true); } } catch (const sdl_error& ex) diff --git a/src/muxer.cpp b/src/muxer.cpp new file mode 100644 index 0000000..5b09ef8 --- /dev/null +++ b/src/muxer.cpp @@ -0,0 +1,70 @@ +#include "muxer.h" +#include +#include + +void ERRCHECK_fn(FMOD_RESULT result, const char* file, int line) { + if (result != FMOD_OK) { + std::cout << file << "(" << line << "): FMOD error " << result << " - " << FMOD_ErrorString(result); + abort(); + } +} + +#define ERRCHECK(_result) ERRCHECK_fn(_result, __FILE__, __LINE__) + +Muxer::Muxer() { + FMOD::Studio::System* system_holder = nullptr; + ERRCHECK(FMOD::Studio::System::create(&system_holder)); + system_.reset(system_holder); + + FMOD::System* coreSystem = nullptr; + ERRCHECK(system_->getCoreSystem(&coreSystem)); + ERRCHECK(coreSystem->setSoftwareFormat(0, FMOD_SPEAKERMODE_5POINT1, 0)); + ERRCHECK(system_->initialize(1024, FMOD_STUDIO_INIT_NORMAL, FMOD_INIT_NORMAL, nullptr)); + + FMOD::Studio::Bank* masterBank = nullptr; + ERRCHECK(system_->loadBankFile("../res/fmod/Build/Desktop/Master Bank.bank", FMOD_STUDIO_LOAD_BANK_NORMAL, &masterBank)); + + FMOD::Studio::Bank* stringsBank = nullptr; + ERRCHECK(system_->loadBankFile("../res/fmod/Build/Desktop/Master Bank.strings.bank", FMOD_STUDIO_LOAD_BANK_NORMAL, &stringsBank)); + + FMOD::Studio::EventDescription* exploration_desc = nullptr; + ERRCHECK(system_->getEvent("event:/exploration", &exploration_desc)); + ERRCHECK(exploration_desc->createInstance(&exploration_event_)); + ERRCHECK(exploration_event_->start()); +} + +void Muxer::setPlayerLoc(int x, int y) { + FMOD_3D_ATTRIBUTES attributes = {{0}}; + attributes.forward.z = 1.0f; + attributes.up.y = 1.0f; + attributes.position.x = x; + attributes.position.y = y; + ERRCHECK(system_->setListenerAttributes(0, &attributes)); +} + +void Muxer::playSoundAtPosition(std::string name, float x, float y) { + std::string eventPath = std::string("event:/") + name; + + FMOD::Studio::EventDescription* eventDescription = nullptr; + ERRCHECK(system_->getEvent(eventPath.c_str(), &eventDescription)); + + FMOD::Studio::EventInstance* eventInstance = nullptr; + ERRCHECK(eventDescription->createInstance(&eventInstance)); + + FMOD_3D_ATTRIBUTES attributes = {{0}}; + attributes.forward.z = 1.0f; + attributes.up.y = 1.0f; + attributes.position.x = x; + attributes.position.y = y; + ERRCHECK(eventInstance->set3DAttributes(&attributes)); + ERRCHECK(eventInstance->start()); + ERRCHECK(eventInstance->release()); +} + +void Muxer::setMusicLevel(int level) { + ERRCHECK(exploration_event_->setParameterByName("level", level)); +} + +void Muxer::update() { + ERRCHECK(system_->update()); +} diff --git a/src/muxer.h b/src/muxer.h new file mode 100644 index 0000000..f99cce4 --- /dev/null +++ b/src/muxer.h @@ -0,0 +1,35 @@ +#ifndef MUXER_H_3146C802 +#define MUXER_H_3146C802 + +#include +#include +#include +#include + +class fmod_system_deleter { +public: + void operator()(FMOD::Studio::System* val) { + val->release(); + } +}; + +using fmod_system_ptr = std::unique_ptr; + +class Muxer { +public: + Muxer(); + + void setPlayerLoc(int x, int y); + + void playSoundAtPosition(std::string name, float x, float y); + + void setMusicLevel(int level); + + void update(); + +private: + fmod_system_ptr system_; + FMOD::Studio::EventInstance* exploration_event_; +}; + +#endif /* end of include guard: MUXER_H_3146C802 */ -- cgit 1.4.1