2011-12-14 00:23:17 +03:00
|
|
|
#pragma once
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
#include "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-10-12 08:00:28 +03:00
|
|
|
|
|
|
|
// Sound infos for creatures in combat
|
|
|
|
struct CreaturesBattleSounds {
|
|
|
|
soundBase::soundID attack;
|
|
|
|
soundBase::soundID defend;
|
|
|
|
soundBase::soundID killed; // was killed or died
|
|
|
|
soundBase::soundID move;
|
|
|
|
soundBase::soundID shoot; // range attack
|
|
|
|
soundBase::soundID wince; // attacked but did not die
|
|
|
|
soundBase::soundID ext1; // creature specific extension
|
|
|
|
soundBase::soundID ext2; // creature specific extension
|
|
|
|
soundBase::soundID startMoving; // usually same as ext1
|
|
|
|
soundBase::soundID endMoving; // usually same as ext2
|
|
|
|
|
|
|
|
CreaturesBattleSounds(): attack(soundBase::invalid),
|
|
|
|
defend(soundBase::invalid),
|
|
|
|
killed(soundBase::invalid),
|
|
|
|
move(soundBase::invalid),
|
|
|
|
shoot(soundBase::invalid),
|
|
|
|
wince(soundBase::invalid),
|
|
|
|
ext1(soundBase::invalid),
|
|
|
|
ext2(soundBase::invalid),
|
|
|
|
startMoving(soundBase::invalid),
|
|
|
|
endMoving(soundBase::invalid) {};
|
|
|
|
};
|
|
|
|
|
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:
|
2011-08-20 04:15:37 +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);
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
//have entry for every currently active channel
|
|
|
|
//boost::function will be NULL if callback was not set
|
|
|
|
std::map<int, boost::function<void()> > callbacks;
|
|
|
|
|
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 initCreaturesSounds(const std::vector<ConstTransitivePtr<CCreature> > &creatures);
|
|
|
|
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);
|
|
|
|
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
|
|
|
|
|
|
|
void setCallback(int channel, boost::function<void()> function);
|
|
|
|
void soundFinishedCallback(int channel);
|
|
|
|
|
2009-10-12 08:00:28 +03:00
|
|
|
std::vector <struct CreaturesBattleSounds> CBattleSounds;
|
2010-12-19 16:39:56 +02:00
|
|
|
std::map<const CSpell*, soundBase::soundID> 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
|
|
|
};
|
|
|
|
|
2009-10-12 08:00:28 +03:00
|
|
|
// Helper
|
2010-12-19 16:39:56 +02:00
|
|
|
#define battle_sound(creature,what_sound) CCS->soundh->CBattleSounds[(creature)->idNumber].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-06 10:34:37 +03:00
|
|
|
bool looped;
|
|
|
|
//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();
|
|
|
|
void stop(int fade_ms=0);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|