OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
sound.cpp
Go to the documentation of this file.
1#include "sound.h"
2
3#include "world/world.h"
4#include "utils/fileext.h"
5#include "gothic.h"
6
9
10Sound::Sound(World& world, Sound::Type type, std::string_view s, const Tempest::Vec3& pos, float range, bool freeSlot) {
11 if(range<=0.f)
12 range = 3500.f;
13
14 auto& owner = *world.sound();
15 if(!owner.isInListenerRange(pos,range))
16 return;
17
18 const auto cname = std::string(s);
19 if(freeSlot) {
20 std::lock_guard<std::mutex> guard(owner.sync);
21 auto slot = owner.freeSlot.find(cname);
22 if(slot!=owner.freeSlot.end() && !slot->second->eff.isFinished())
23 return;
24 }
25
26 SoundFx* snd = nullptr;
27 if(FileExt::hasExt(s,"WAV"))
28 snd = Gothic::inst().loadSoundWavFx(s); else
29 snd = Gothic::inst().loadSoundFx(s);
30
31 if(snd==nullptr)
32 return;
33
34 *this = owner.implAddSound(*snd,pos,range);
35 if(isEmpty())
36 return;
37
38 std::lock_guard<std::mutex> guard(owner.sync);
39 owner.initSlot(*val);
40 switch(type) {
41 case T_Regular:{
42 if(freeSlot)
43 owner.freeSlot[cname] = val; else
44 owner.effect.emplace_back(val);
45 break;
46 }
47 case T_3D:{
48 owner.effect3d.emplace_back(val);
49 break;
50 }
51 }
52 }
53
55 :val(other.val){
56 other.val = nullptr;
57 }
58
60 std::swap(val,other.val);
61 return *this;
62 }
63
66
67Sound::Sound(const std::shared_ptr<WorldSound::Effect>& val)
68 :val(val) {
69 }
70
71Tempest::Vec3 Sound::position() const {
72 return pos;
73 }
74
75bool Sound::isEmpty() const {
76 return val==nullptr ? true : val->eff.isEmpty();
77 }
78
79bool Sound::isFinished() const {
80 return val==nullptr ? true : val->eff.isFinished();
81 }
82
83void Sound::setOcclusion(float occ) {
84 if(val!=nullptr)
85 val->setOcclusion(occ);
86 }
87
88void Sound::setVolume(float v) {
89 if(val!=nullptr)
90 val->setVolume(v);
91 }
92
93float Sound::volume() const {
94 if(val!=nullptr)
95 return val->vol;
96 return 0;
97 }
98
99void Sound::setPosition(const Tempest::Vec3& pos) {
100 setPosition(pos.x,pos.y,pos.z);
101 }
102
103void Sound::setPosition(float x, float y, float z) {
104 if(pos.x==x && pos.y==y && pos.z==z)
105 return;
106 if(val!=nullptr)
107 val->eff.setPosition(x,y,z);
108 pos = {x,y,z};
109 }
110
111void Sound::setLooping(bool l) {
112 if(val!=nullptr)
113 val->loop = l;
114 }
115
116void Sound::setAmbient(bool a) {
117 if(val!=nullptr)
118 val->ambient = a;
119 }
120
121void Sound::setActive(bool a) {
122 if(val!=nullptr)
123 val->active = a;
124 }
125
127 if(val!=nullptr)
128 val->eff.play();
129 }
130
132 if(val!=nullptr)
133 return val->eff.timeLength();
134 return 0;
135 }
static Gothic & inst()
Definition gothic.cpp:249
SoundFx * loadSoundFx(std::string_view name)
Definition gothic.cpp:343
SoundFx * loadSoundWavFx(std::string_view name)
Definition gothic.cpp:364
Definition sound.h:5
Type
Definition sound.h:7
@ T_3D
Definition sound.h:9
@ T_Regular
Definition sound.h:8
~Sound()
Definition sound.cpp:64
Tempest::Vec3 position() const
Definition sound.cpp:71
void setVolume(float v)
Definition sound.cpp:88
Sound()
Definition sound.cpp:7
Sound & operator=(Sound &&other)
Definition sound.cpp:59
uint64_t effectPrefferedTime() const
Definition sound.cpp:131
void setOcclusion(float occ)
Definition sound.cpp:83
void play()
Definition sound.cpp:126
void setAmbient(bool a)
Definition sound.cpp:116
void setLooping(bool l)
Definition sound.cpp:111
void setActive(bool a)
Definition sound.cpp:121
void setPosition(const Tempest::Vec3 &pos)
Definition sound.cpp:99
bool isFinished() const
Definition sound.cpp:79
float volume() const
Definition sound.cpp:93
bool isEmpty() const
Definition sound.cpp:75
Definition world.h:31
WorldSound * sound()
Definition world.h:80
bool hasExt(std::string_view s, const char *extIn)
Definition fileext.h:8