2011-12-14 00:23:17 +03:00
|
|
|
#pragma once
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2012-09-29 13:59:43 +03:00
|
|
|
#include "../lib/CConfigHandler.h"
|
2009-04-22 21:48:56 +03:00
|
|
|
#include "CSoundBase.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "../lib/CCreatureHandler.h"
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CMusicHandler.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
2009-04-16 14:14:13 +03:00
|
|
|
*/
|
|
|
|
|
2009-05-14 07:49:04 +03:00
|
|
|
class CSpell;
|
2009-05-01 02:25:17 +03:00
|
|
|
struct _Mix_Music;
|
|
|
|
typedef struct _Mix_Music Mix_Music;
|
|
|
|
struct Mix_Chunk;
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
class CAudioBase {
|
|
|
|
protected:
|
|
|
|
bool initialized;
|
|
|
|
int volume; // from 0 (mute) to 100
|
|
|
|
|
|
|
|
public:
|
|
|
|
CAudioBase(): initialized(false), volume(0) {};
|
|
|
|
virtual void init() = 0;
|
|
|
|
virtual void release() = 0;
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
virtual void setVolume(ui32 percent);
|
|
|
|
ui32 getVolume() { return volume; };
|
2009-05-22 07:14:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class CSoundHandler: public CAudioBase
|
2009-04-16 14:14:13 +03:00
|
|
|
{
|
2009-04-22 21:48:56 +03:00
|
|
|
private:
|
2013-02-24 13:05:56 +03:00
|
|
|
//soundBase::soundID getSoundID(const std::string &fileName);
|
2012-01-12 18:23:00 +03:00
|
|
|
//update volume on configuration change
|
|
|
|
SettingsListener listener;
|
|
|
|
void onVolumeChange(const JsonNode &volumeNode);
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2009-04-30 13:53:06 +03:00
|
|
|
std::map<soundBase::soundID, Mix_Chunk *> soundChunks;
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2009-04-30 13:53:06 +03:00
|
|
|
Mix_Chunk *GetSoundChunk(soundBase::soundID soundID);
|
2012-09-20 19:14:23 +03:00
|
|
|
Mix_Chunk *GetSoundChunk(std::string &sound);
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
//have entry for every currently active channel
|
2013-06-26 14:18:27 +03:00
|
|
|
//std::function will be nullptr if callback was not set
|
|
|
|
std::map<int, std::function<void()> > callbacks;
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2009-04-16 14:14:13 +03:00
|
|
|
public:
|
2009-05-22 07:14:59 +03:00
|
|
|
CSoundHandler();
|
|
|
|
|
|
|
|
void init();
|
|
|
|
void release();
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2010-12-19 16:39:56 +02:00
|
|
|
void initSpellsSounds(const std::vector< ConstTransitivePtr<CSpell> > &spells);
|
2011-12-14 00:23:17 +03:00
|
|
|
void setVolume(ui32 percent);
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2009-04-22 21:48:56 +03:00
|
|
|
// Sounds
|
2009-04-30 13:53:06 +03:00
|
|
|
int playSound(soundBase::soundID soundID, int repeats=0);
|
2012-09-17 22:00:26 +03:00
|
|
|
int playSound(std::string sound, int repeats=0);
|
2009-04-30 13:53:06 +03:00
|
|
|
int playSoundFromSet(std::vector<soundBase::soundID> &sound_vec);
|
2009-04-24 00:09:10 +03:00
|
|
|
void stopSound(int handler);
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
void setCallback(int channel, std::function<void()> function);
|
2011-08-19 22:50:24 +03:00
|
|
|
void soundFinishedCallback(int channel);
|
|
|
|
|
2013-02-24 13:05:56 +03:00
|
|
|
std::map<const CSpell*, std::string> spellSounds;
|
2009-04-22 21:50:22 +03:00
|
|
|
|
|
|
|
// Sets
|
2009-05-06 05:39:03 +03:00
|
|
|
std::vector<soundBase::soundID> pickupSounds;
|
2009-04-30 13:53:06 +03:00
|
|
|
std::vector<soundBase::soundID> horseSounds;
|
2011-08-19 22:50:24 +03:00
|
|
|
std::vector<soundBase::soundID> battleIntroSounds;
|
2009-05-06 05:32:36 +03:00
|
|
|
};
|
|
|
|
|
2012-09-17 22:00:26 +03:00
|
|
|
// Helper //now it looks somewhat useless
|
|
|
|
#define battle_sound(creature,what_sound) creature->sounds.what_sound
|
2009-10-12 08:00:28 +03:00
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
class CMusicHandler;
|
|
|
|
|
|
|
|
//Class for handling one music file
|
|
|
|
class MusicEntry
|
|
|
|
{
|
|
|
|
CMusicHandler *owner;
|
|
|
|
Mix_Music *music;
|
2012-08-27 23:45:58 +03:00
|
|
|
int loop; // -1 = indefinite
|
2012-08-06 10:34:37 +03:00
|
|
|
//if not null - set from which music will be randomly selected
|
|
|
|
std::string setName;
|
|
|
|
std::string currentName;
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
|
|
|
|
void load(std::string musicURI);
|
2011-08-19 22:50:24 +03:00
|
|
|
|
|
|
|
public:
|
2012-08-06 10:34:37 +03:00
|
|
|
bool isSet(std::string setName);
|
|
|
|
bool isTrack(std::string trackName);
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
MusicEntry(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped);
|
2011-08-19 22:50:24 +03:00
|
|
|
~MusicEntry();
|
|
|
|
|
|
|
|
bool play();
|
2012-08-27 23:45:58 +03:00
|
|
|
bool stop(int fade_ms=0);
|
2011-08-19 22:50:24 +03:00
|
|
|
};
|
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
class CMusicHandler: public CAudioBase
|
2009-05-06 05:32:36 +03:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
// Because we use the SDL music callback, our music variables must
|
|
|
|
// be protected
|
|
|
|
boost::mutex musicMutex;
|
2012-01-12 18:23:00 +03:00
|
|
|
//update volume on configuration change
|
|
|
|
SettingsListener listener;
|
|
|
|
void onVolumeChange(const JsonNode &volumeNode);
|
2009-05-06 05:32:36 +03:00
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
unique_ptr<MusicEntry> current;
|
|
|
|
unique_ptr<MusicEntry> next;
|
2011-08-19 22:50:24 +03:00
|
|
|
|
|
|
|
void queueNext(MusicEntry *queued);
|
2012-08-06 10:34:37 +03:00
|
|
|
|
|
|
|
std::map<std::string, std::map<int, std::string> > musicsSet;
|
2009-05-06 05:32:36 +03:00
|
|
|
public:
|
2009-05-22 07:14:59 +03:00
|
|
|
CMusicHandler();
|
2009-05-06 05:32:36 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
/// add entry with URI musicURI in set. Track will have ID musicID
|
|
|
|
void addEntryToSet(std::string set, int musicID, std::string musicURI);
|
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
void init();
|
|
|
|
void release();
|
2011-12-14 00:23:17 +03:00
|
|
|
void setVolume(ui32 percent);
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
/// play track by URI, if loop = true music will be looped
|
|
|
|
void playMusic(std::string musicURI, bool loop);
|
|
|
|
/// play random track from this set
|
|
|
|
void playMusicFromSet(std::string musicSet, bool loop);
|
|
|
|
/// play specific track from set
|
|
|
|
void playMusicFromSet(std::string musicSet, int entryID, bool loop);
|
2009-05-01 02:25:17 +03:00
|
|
|
void stopMusic(int fade_ms=1000);
|
|
|
|
void musicFinishedCallback(void);
|
2012-08-06 10:34:37 +03:00
|
|
|
|
|
|
|
friend class MusicEntry;
|
2009-04-22 21:48:56 +03:00
|
|
|
};
|