WOWCube Docs logo
WOWCube Docs
Mission Control
Section Shortcuts
APIExamplesSourceWOWConnectChangelog
Filters
SDK and language defaults persist in cookies.
SDK version
Navigation Tree
Collapsed by default, focused on the active path.
Made byMcKay Seamons
GitHub
  1. Home
  2. Docs
  3. Source
  4. Sound.cpp
Mission NodeSDK 6.2C++sound.cpp

Sound.cpp

SDK Source File: Sound.cpp

Source / SDK 6.2 / C++ / Core

Sound.cpp

Sound.cpp
CPP
1/* Copyright Statement:
2 *
3 * (C) 2021-2024 Cubios Inc. All rights reserved.
4 */
5
6#include "Sound.h"
7
8namespace Cubios
9{
10 Sound::Sound(const std::string& name)
11 {
12 id = SND_getAssetId(name.c_str());
13 }
14
15 Sound::~Sound() { }
16
17 void Sound::Play(uint8_t volume)
18 {
19 SND_play(id, volume);
20 }
21
22 void Sound::Stop()
23 {
24 SND_stop();
25 }
26
27 bool Sound::IsPlaying()
28 {
29 return SND_isPlaying();
30 }
31
32
33 SoundCollection::SoundCollection(std::vector<std::string>& names):currentSound(nullptr),delay(0)
34 {
35 this->numSounds = names.size();
36 this->external = false;
37
38 for(size_t i=0;i<this->numSounds;i++)
39 {
40 this->sounds.push_back(new Sound(names.at(i)));
41 }
42 }
43
44 SoundCollection::SoundCollection(std::vector<Cubios::Sound*>& sounds):currentSound(nullptr),delay(0)
45 {
46 this->numSounds = sounds.size();
47 this->external = true;
48
49 for(size_t i=0;i<this->numSounds;i++)
50 {
51 this->sounds.push_back(sounds.at(i));
52 }
53
54 }
55
56 SoundCollection::~SoundCollection()
57 {
58 if(!this->external)
59 {
60 for(size_t i=0;i<this->numSounds;i++)
61 {
62 Cubios::Sound* s = this->sounds.at(i);
63 if(s!=nullptr)
64 {
65 if(s->IsPlaying()) s->Stop();
66 delete s;
67 }
68 }
69 }
70 }
71
72 void SoundCollection::Stop()
73 {
74 if(this->currentSound!=nullptr)
75 {
76 this->currentSound->Stop();
77 this->currentSound = nullptr;
78 this->delay = 0;
79 }
80 }
81
82 bool SoundCollection::IsPlaying()
83 {
84 if(this->currentSound!=nullptr)
85 {
86 return this->currentSound->IsPlaying();
87 }
88 else return false;
89 }
90
91 bool SoundCollection::PlayRandomSound(uint8_t volume, bool stopCurrent)
92 {
93 if(this->currentSound!=nullptr)
94 {
95 if(this->currentSound->IsPlaying())
96 {
97 if(stopCurrent)
98 {
99 this->currentSound->Stop();
100 this->currentSound = nullptr;
101 }
102 else
103 {
104 return false;
105 }
106 }
107 }
108
109 this->currentSound = this->sounds.at(Cubios::random(0, this->numSounds-1));
110 this->currentSound->Play(volume);
111 return true;
112 }
113
114 bool SoundCollection::RepeatRandomSounds(uint8_t volume, int delayMin, int delayMax, int deltaTime)
115 {
116 if(this->currentSound!=nullptr)
117 if(this->currentSound->IsPlaying()) return false;
118
119 if(this->delay<=0)
120 {
121 this->delay = Cubios::random(delayMin, delayMax);
122 }
123 else
124 {
125 this->delay-=deltaTime;
126 return false;
127 }
128
129 this->currentSound = this->sounds.at(Cubios::random(0, this->numSounds-1));
130 this->currentSound->Play(volume);
131 return true;
132 }
133
134} // namespace Cubios
135
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.
Context Rail

Related nodes

AppManager.cpp
Source / SDK 6.2 / C++ / Core
AppManager.h
Source / SDK 6.2 / C++ / Core
Gfx.h
Source / SDK 6.2 / C++ / Core
native access.h
Source / SDK 6.2 / C++ / Core
Previous Node
Screen.h
Source / SDK 6.2 / C++ / Core
Next Node
Sound.h
Source / SDK 6.2 / C++ / Core