Skip to content

Commit

Permalink
Added a new music file and a short sound clip for testing purposes. A…
Browse files Browse the repository at this point in the history
…dded the ability to play sounds via a command (Now stores the sound in a command cache with highly-optimised move semantics)
  • Loading branch information
harrand committed Apr 9, 2017
1 parent 55ba06c commit 7219d3e
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 3 deletions.
2 changes: 1 addition & 1 deletion qcpl_dll.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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%
Expand Down
Binary file added res/runtime/music/noise.wav
Binary file not shown.
2 changes: 1 addition & 1 deletion res/runtime/resources.data
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 52 additions & 0 deletions src/audio.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
33 changes: 33 additions & 0 deletions src/audio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef AUDIO_HPP
#define AUDIO_HPP
#include <string>
#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
33 changes: 33 additions & 0 deletions src/command.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "command.hpp"

std::vector<std::string> CommandCache::aliasArgs = std::vector<std::string>();
std::vector<std::unique_ptr<AudioClip>> CommandCache::clips = std::vector<std::unique_ptr<AudioClip>>();

void CommandCache::updateAlias(std::vector<std::string> aliasArgs)
{
Expand All @@ -12,6 +13,24 @@ std::vector<std::string> CommandCache::getAlias()
return CommandCache::aliasArgs;
}

void CommandCache::addAudioClip(std::unique_ptr<AudioClip>&& clip)
{
CommandCache::clips.push_back(std::move(clip));
}

void CommandCache::destroyChannelClips(int channel)
{
for(std::unique_ptr<AudioClip>& clip : CommandCache::clips)
{
unsigned int prevSize = CommandCache::clips.size();
auto lambda = [channel](const std::unique_ptr<AudioClip>& 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>& world, Player& player)
{
std::vector<std::string> args;
Expand Down Expand Up @@ -49,6 +68,8 @@ void Commands::inputCommand(std::string cmd, std::shared_ptr<World>& 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";
}
Expand Down Expand Up @@ -295,4 +316,16 @@ void Commands::setSpawnOrientation(std::vector<std::string> 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<std::string> 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<AudioClip> 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";
}
6 changes: 6 additions & 0 deletions src/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
#define COMMAND_HPP
#include "world.hpp"
#include "player.hpp"
#include "audio.hpp"

class CommandCache
{
public:
static void updateAlias(std::vector<std::string> aliasArgs);
static std::vector<std::string> getAlias();
static std::vector<std::string> aliasArgs;

static std::vector<std::unique_ptr<AudioClip>> clips;
static void addAudioClip(std::unique_ptr<AudioClip>&& clip);
static void destroyChannelClips(int channel);
};

namespace Commands
Expand All @@ -28,6 +33,7 @@ namespace Commands
void setGravity(std::vector<std::string> args, std::shared_ptr<World>& world, bool printResults);
void setSpawnPoint(std::vector<std::string> args, std::shared_ptr<World>& world, bool printResults);
void setSpawnOrientation(std::vector<std::string> args, std::shared_ptr<World>& world, bool printResults);
void playAudio(std::vector<std::string> args, bool printResults);
}

#endif
7 changes: 6 additions & 1 deletion src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "listeners.hpp"
#include "timekeeper.hpp"
#include "command.hpp"
#include "audio.hpp"
#include <iostream>

//Global Heap Variables
Expand Down Expand Up @@ -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<float> processingThisFrame;

Expand Down Expand Up @@ -79,6 +84,6 @@ int main()
std::ostringstream strum;
strum << secondsLifetime;
timeStorage.editTag("played", strum.str());
Mix_FreeMusic(music);
//Mix_FreeMusic(music);
return 0;
}

0 comments on commit 7219d3e

Please sign in to comment.