Advertisement
Guest User

Untitled

a guest
Sep 14th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "engine/component.h"
  4. #include "engine/system.h"
  5. #include "engine/touchable.h"
  6.  
  7. #include <list>
  8. #include <memory>
  9.  
  10. namespace luabind {
  11. class scope;
  12. }
  13.  
  14. namespace OgreOggSound {
  15. class OgreOggISound;
  16. }
  17.  
  18. namespace thrive {
  19.  
  20. /**
  21. * @brief Represents a single sound
  22. */
  23. class Sound {
  24.  
  25. public:
  26.  
  27. /**
  28. * @brief Lua bindings
  29. *
  30. * Exposes:
  31. * - Sound(name, filename)
  32. * - @link m_properties Properties @endlink
  33. * - Properties
  34. * - Properties::playState
  35. * - Properties::loop
  36. * - Properties::volume
  37. * - Properties::maxDistance
  38. * - Properties::rolloffFactor
  39. * - Properties::referenceDistance
  40. * - Properties::priority
  41. * - Enum PlayState
  42. * - PlayState::Play
  43. * - PlayState::Pause
  44. * - PlayState::Stop
  45. * - Sound::name
  46. * - Sound::pause
  47. * - Sound::play
  48. * - Sound::stop
  49. *
  50. * @return
  51. */
  52. static luabind::scope
  53. luaBindings();
  54.  
  55. /**
  56. * @brief Play mode of the sound
  57. */
  58. enum PlayState {
  59. Play,
  60. Pause,
  61. Stop
  62. };
  63.  
  64. /**
  65. * @brief Sound properties
  66. */
  67. struct Properties : public Touchable {
  68. PlayState playState = PlayState::Stop;
  69. bool loop = false;
  70. float volume = 1.0f;
  71. float maxDistance = -1.0f;
  72. float rolloffFactor = -1.0f;
  73. float referenceDistance = 100.0f;
  74. uint8_t priority = 0;
  75. };
  76.  
  77. /**
  78. * @brief Default constructor for loading
  79. */
  80. Sound();
  81.  
  82. /**
  83. * @brief Constructor
  84. *
  85. * @param name
  86. * The name of the sound (must be unique)
  87. * @param filename
  88. * The name of the sound file
  89. */
  90. Sound(
  91. std::string name,
  92. std::string filename
  93. );
  94.  
  95. /**
  96. * @brief The file that the sound is playing
  97. *
  98. * @return
  99. */
  100. std::string
  101. filename() const;
  102.  
  103. /**
  104. * @brief Loads a sound from storage
  105. *
  106. * @param storage
  107. */
  108. void
  109. load(
  110. const StorageContainer& storage
  111. );
  112.  
  113. /**
  114. * @brief The name of the sound
  115. *
  116. * @return
  117. */
  118. std::string
  119. name() const;
  120.  
  121. /**
  122. * @brief Pauses the sound during the next frame
  123. */
  124. void
  125. pause();
  126.  
  127. /**
  128. * @brief Starts (or resumes) playing the sound
  129. */
  130. void
  131. play();
  132.  
  133. /**
  134. * @brief Stops the sound during the next frame
  135. */
  136. void
  137. stop();
  138.  
  139. /**
  140. * @brief Constructs a storage container for serialization
  141. *
  142. * @return
  143. */
  144. StorageContainer
  145. storage() const;
  146.  
  147. /**
  148. * @brief Properties
  149. */
  150. Properties m_properties;
  151.  
  152. /**
  153. * @brief Pointer to internal sound
  154. */
  155. OgreOggSound::OgreOggISound* m_sound = nullptr;
  156.  
  157. private:
  158.  
  159. std::string m_filename;
  160.  
  161. std::string m_name;
  162. };
  163.  
  164. /**
  165. * @brief A component for sound sources
  166. *
  167. */
  168. class SoundSourceComponent : public Component {
  169. COMPONENT(SoundSource)
  170.  
  171. public:
  172.  
  173. /**
  174. * @brief Lua bindings
  175. *
  176. * Exposes:
  177. * - SoundSourceComponent()
  178. * - SoundSourceComponent::addSound()
  179. * - SoundSourceComponent::removeSound()
  180. * - SoundSourceComponent::playSound()
  181. * - SoundSourceComponent::interpose()
  182. * - SoundSourceComponent::queueSound()
  183. * - SoundSourceComponent::volumeMultiplier
  184. * - SoundSourceComponent::ambientSoundSource
  185. *
  186. * @return
  187. **/
  188. static luabind::scope
  189. luaBindings();
  190.  
  191. /**
  192. * @brief Adds a new sound
  193. *
  194. * @param name
  195. * The name of the sound (must be unique)
  196. * @param filename
  197. * The file to play
  198. *
  199. * @return A reference to the new sound
  200. */
  201. Sound*
  202. addSound(
  203. std::string name,
  204. std::string filename
  205. );
  206.  
  207. /**
  208. * @brief Removes a sound by name
  209. *
  210. * @param name
  211. */
  212. void
  213. removeSound(
  214. std::string name
  215. );
  216.  
  217. /**
  218. * @brief Plays a sound
  219. * This is equivalent to playing the sound directly
  220. *
  221. * @param name
  222. * The name of the sound
  223. */
  224. void
  225. playSound(
  226. std::string name
  227. );
  228.  
  229.  
  230. /**
  231. * @brief Interrupts the current ambient sound and interposes a new one
  232. * Does nothing for non-ambient sound sources
  233. *
  234. * @param name
  235. * The name of the song that is to interpose
  236. *
  237. * @param fadeTime
  238. * The time the transition should take
  239. */
  240. void
  241. interpose(
  242. std::string name,
  243. int fadeTime
  244. );
  245.  
  246. /**
  247. * @brief Queues up an ambient sound to be played after the current one
  248. * Does nothing for non-ambient sound sources
  249. *
  250. * @param name
  251. * The name of the song to queue up
  252. */
  253. void
  254. queueSound(
  255. std::string name
  256. );
  257.  
  258.  
  259. void
  260. load(
  261. const StorageContainer& storage
  262. ) override;
  263.  
  264.  
  265. StorageContainer
  266. storage() const override;
  267.  
  268. /**
  269. * @brief Whether this source as an ambient soundsource with no 3d position
  270. */
  271. TouchableValue<bool> m_ambientSoundSource = false;
  272.  
  273. /**
  274. * @brief Whether this source auto loops
  275. *
  276. * Auto looping sound sources will automatically keep playing sounds from the added sounds and fade between them
  277. */
  278. TouchableValue<bool> m_autoLoop = false;
  279.  
  280. /**
  281. * @brief Volume multiplier that is applied to all associated sounds
  282. */
  283. TouchableValue<float> m_volumeMultiplier = 1.0f;
  284.  
  285.  
  286. private:
  287.  
  288. friend class SoundSourceSystem;
  289.  
  290. std::list<Sound*> m_addedSounds;
  291.  
  292. std::list<Sound*> m_removedSounds;
  293.  
  294. Sound* m_autoActiveSound = nullptr;
  295. Sound* m_queuedSound = nullptr;
  296. int m_autoSoundCountdown = 0;
  297. bool m_isTransitioningAuto = false;
  298.  
  299. std::unordered_map<std::string, std::unique_ptr<Sound>> m_sounds;
  300.  
  301. };
  302.  
  303.  
  304. /**
  305. * @brief Creates, updates and removes sounds
  306. */
  307. class SoundSourceSystem : public System {
  308.  
  309. public:
  310.  
  311. /**
  312. * @brief Lua bindings
  313. *
  314. * Exposes:
  315. * - SoundSourceSystem()
  316. *
  317. * @return
  318. **/
  319. static luabind::scope
  320. luaBindings();
  321.  
  322. /**
  323. * @brief Constructor
  324. */
  325. SoundSourceSystem();
  326.  
  327. /**
  328. * @brief Destructor
  329. */
  330. ~SoundSourceSystem();
  331.  
  332. void
  333. activate() override;
  334.  
  335. void
  336. deactivate() override;
  337.  
  338. /**
  339. * @brief Initializes the system
  340. *
  341. */
  342. void init(GameState* gameState) override;
  343.  
  344. /**
  345. * @brief Shuts the system down
  346. */
  347. void shutdown() override;
  348.  
  349. /**
  350. * @brief Updates the system
  351. */
  352. void update(int) override;
  353.  
  354. private:
  355.  
  356. struct Implementation;
  357. std::unique_ptr<Implementation> m_impl;
  358. };
  359.  
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement