diff --git a/qcpl_dll.bat b/qcpl_dll.bat index 722f062de0..a548ad602b 100644 --- a/qcpl_dll.bat +++ b/qcpl_dll.bat @@ -39,7 +39,7 @@ cd "%scriptdir%\res\dep" xcopy /s "%cd%" %lnkdir% echo Dependencies copied... cd %lnkdir% -g++ -o topaz_test_dependent.exe test.o "%scriptdir%\res\exe\topaz_test.res" -L. -L%libdir% -lSDL2_mixer -ltopaz -lmdl +g++ -o topaz_test_dependent.exe test.o "%scriptdir%\res\exe\topaz_test.res" -L. -L%libdir% -ltopaz -lmdl move test.o "%cpldir%" explorer %lnkdir% diff --git a/res/runtime/music/noise.wav b/res/runtime/music/noise.wav new file mode 100644 index 0000000000..67ce2d50e2 Binary files /dev/null and b/res/runtime/music/noise.wav differ diff --git a/res/runtime/resources.data b/res/runtime/resources.data index a2de4618cd..f045f1825d 100644 --- a/res/runtime/resources.data +++ b/res/runtime/resources.data @@ -101,4 +101,4 @@ torus.name: Complete Torus cylinder.path: ../../../res/runtime/models/cylinder.obj cylinder.name: Complete Cylinder speed: 2000 -played: 162331 +played: 166716 diff --git a/src/audio.cpp b/src/audio.cpp new file mode 100644 index 0000000000..2a08185e4e --- /dev/null +++ b/src/audio.cpp @@ -0,0 +1,52 @@ +#include "audio.hpp" + +AudioClip::AudioClip(std::string filename): filename(filename) +{ + this->audioHandle = Mix_LoadWAV(this->filename.c_str()); +} + +AudioClip::~AudioClip() +{ + Mix_FreeChunk(this->audioHandle); +} + +void AudioClip::play() +{ + this->channel = Mix_PlayChannel(-1, this->audioHandle, 0); +} + +int AudioClip::getChannel() const +{ + return this->channel; +} + +AudioMusic::AudioMusic(std::string filename): filename(filename), paused(false) +{ + this->audioHandle = Mix_LoadMUS(this->filename.c_str()); +} + +AudioMusic::~AudioMusic() +{ + Mix_FreeMusic(this->audioHandle); +} + +void AudioMusic::play(bool priority) +{ + // if its a priority or we're not playing music, play this. + if(priority || Mix_PlayingMusic() != 0) + Mix_PlayMusic(this->audioHandle, -1); +} + +void AudioMusic::setPaused(bool pause = true) +{ + this->paused = pause; + if(this->paused) + Mix_PauseMusic(); + else + Mix_ResumeMusic(); +} + +void AudioMusic::togglePaused() +{ + this->setPaused(!this->paused); +} \ No newline at end of file diff --git a/src/audio.hpp b/src/audio.hpp new file mode 100644 index 0000000000..50c8b02bf5 --- /dev/null +++ b/src/audio.hpp @@ -0,0 +1,33 @@ +#ifndef AUDIO_HPP +#define AUDIO_HPP +#include +#include "SDL_mixer.h" + +class AudioClip +{ +public: + AudioClip(std::string filename); + ~AudioClip(); + void play(); + int getChannel() const; +private: + int channel; + const std::string filename; + Mix_Chunk* audioHandle; +}; + +class AudioMusic +{ +public: + AudioMusic(std::string filename); + ~AudioMusic(); + void play(bool priority = true); + void setPaused(bool pause); + void togglePaused(); +private: + bool paused; + const std::string filename; + Mix_Music* audioHandle; +}; + +#endif \ No newline at end of file diff --git a/src/command.cpp b/src/command.cpp index f3a722faf6..99cb4b27b2 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -1,6 +1,7 @@ #include "command.hpp" std::vector CommandCache::aliasArgs = std::vector(); +std::vector> CommandCache::clips = std::vector>(); void CommandCache::updateAlias(std::vector aliasArgs) { @@ -12,6 +13,24 @@ std::vector CommandCache::getAlias() return CommandCache::aliasArgs; } +void CommandCache::addAudioClip(std::unique_ptr&& clip) +{ + CommandCache::clips.push_back(std::move(clip)); +} + +void CommandCache::destroyChannelClips(int channel) +{ + for(std::unique_ptr& clip : CommandCache::clips) + { + unsigned int prevSize = CommandCache::clips.size(); + auto lambda = [channel](const std::unique_ptr& clip) -> bool{return clip->getChannel() == channel;}; + auto rem = std::remove_if(CommandCache::clips.begin(), CommandCache::clips.end(), lambda); + CommandCache::clips.erase(rem, CommandCache::clips.end()); + if(CommandCache::clips.size() != prevSize) + std::cout << "Clip belonging to the channel " << channel << " has been destroyed.\n"; + } +} + void Commands::inputCommand(std::string cmd, std::shared_ptr& world, Player& player) { std::vector args; @@ -49,6 +68,8 @@ void Commands::inputCommand(std::string cmd, std::shared_ptr& world, Play Commands::setSpawnPoint(args, world, true); else if(cmdName == "spawnorientation") Commands::setSpawnOrientation(args, world, true); + else if(cmdName == "play") + Commands::playAudio(args, true); else std::cout << "Unknown command. Maybe you made a typo?\n"; } @@ -295,4 +316,16 @@ void Commands::setSpawnOrientation(std::vector args, std::shared_pt world->setSpawnOrientation(spawn); if(printResults) std::cout << "Set spawnorientation of the world '" << world->getFileName() << "' to [" << spawn.getX() << "," << spawn.getY() << "," << spawn.getZ() << "]\n"; +} + +void Commands::playAudio(std::vector args, bool printResults) +{ + std::string filename = RES_POINT + "/music/"; + for(unsigned int i = 1; i < args.size(); i++) + filename += args.at(i); + std::unique_ptr clip(new AudioClip(filename)); + clip->play(); + CommandCache::addAudioClip(std::move(clip)); + Mix_ChannelFinished(CommandCache::destroyChannelClips); + std::cout << "Playing the audio clip with the file-path " << filename << "\n"; } \ No newline at end of file diff --git a/src/command.hpp b/src/command.hpp index fc83620d94..b4485fce13 100644 --- a/src/command.hpp +++ b/src/command.hpp @@ -2,6 +2,7 @@ #define COMMAND_HPP #include "world.hpp" #include "player.hpp" +#include "audio.hpp" class CommandCache { @@ -9,6 +10,10 @@ class CommandCache static void updateAlias(std::vector aliasArgs); static std::vector getAlias(); static std::vector aliasArgs; + + static std::vector> clips; + static void addAudioClip(std::unique_ptr&& clip); + static void destroyChannelClips(int channel); }; namespace Commands @@ -28,6 +33,7 @@ namespace Commands void setGravity(std::vector args, std::shared_ptr& world, bool printResults); void setSpawnPoint(std::vector args, std::shared_ptr& world, bool printResults); void setSpawnOrientation(std::vector args, std::shared_ptr& world, bool printResults); + void playAudio(std::vector args, bool printResults); } #endif \ No newline at end of file diff --git a/src/test.cpp b/src/test.cpp index 1cbdaff83a..42697287c1 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -1,6 +1,7 @@ #include "listeners.hpp" #include "timekeeper.hpp" #include "command.hpp" +#include "audio.hpp" #include //Global Heap Variables @@ -33,12 +34,16 @@ int main() TimeProfiler tp; unsigned int fps = 1000; + AudioMusic music(RES_POINT + "/music/music.wav"); + music.play(); + /* Mix_Music* music = NULL; music = Mix_LoadMUS((RES_POINT + "/music/music.wav").c_str()); if(music != NULL) Mix_PlayMusic(music, -1); else std::cerr << "Couldn't play music: Did not load properly.\n"; + */ std::vector processingThisFrame; @@ -79,6 +84,6 @@ int main() std::ostringstream strum; strum << secondsLifetime; timeStorage.editTag("played", strum.str()); - Mix_FreeMusic(music); + //Mix_FreeMusic(music); return 0; } \ No newline at end of file