-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
192 lines (169 loc) · 6.37 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <bits/stdc++.h>
using namespace std;
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <imgui.h>
#include <imgui-SFML.h>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "2D Sound");
ImGui::SFML::Init(window);
sf::Clock deltaClock;
bool running = true;
bool followMouse = false;
sf::SoundBuffer soundBuffer;
if(!soundBuffer.loadFromFile("assets/peko_mono.ogg"))
{
return 1;
}
// Listener
sf::Listener::setPosition(0.f, 0.f, 0.f);
// sf::Listener::setDirection(0.f, 1.f, 0.f);
sf::Listener::setUpVector(0.f, 0.f, 1.f);
sf::Listener::setGlobalVolume(50.f);
float sound_distance = 5.f;
float sound_attenuation = 1.f;
// Sound
sf::Sound sound;
sound.setBuffer(soundBuffer);
sound.setMinDistance(sound_distance);
sound.setAttenuation(sound_attenuation);
sound.setLoop(true);
sound.setRelativeToListener(true);
sound.play();
sf::Vector3f pos;
float radius = 10.f;
float t = 0;
float angular_velocity = 1.0f;
sf::Clock clk;
auto GetSoundFactor = [&](float dis)
{
return sound_distance / (sound_distance + sound_attenuation * (max(dis, sound_distance) - sound_distance));
};
// run the program as long as the window is open
while (running)
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
ImGui::SFML::ProcessEvent(event);
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
running = false;
else if(event.type == sf::Event::KeyPressed)
{
}
}
ImGui::SFML::Update(window, deltaClock.restart());
ImGui::Begin("Debug");
{
ImGui::Checkbox("Follow mouse", &followMouse);
ImGui::Text("Sound Pos = %.2f %.2f %.2f", pos.x, pos.y, pos.z);
ImGui::DragFloat("Radius", &radius);
ImGui::DragFloat("Vel", &angular_velocity);
if (ImGui::DragFloat("MinDistance", &sound_distance))
{
if (sound_distance <= 0.f)
sound_distance = 0.001f;
sound.pause();
sound.setMinDistance(sound_distance);
sound.play();
}
if (ImGui::DragFloat("Attenuation", &sound_attenuation, 0.001))
{
if(sound_attenuation <= 0.f)
sound_attenuation = 0.f;
sound.pause();
sound.setAttenuation(sound_attenuation);
sound.play();
}
ImGui::Separator();
float dis = sqrt(pow(pos.x,2)+pow(pos.y,2));
ImGui::Text("Volume = %.2f", GetSoundFactor(dis) );
ImGui::Separator();
ImGui::Text("%.2f/%.2f s, %d/%d ms",
sound.getPlayingOffset().asSeconds(),
sound.getBuffer()->getDuration().asSeconds(),
sound.getPlayingOffset().asMilliseconds(),
sound.getBuffer()->getDuration().asMilliseconds());
float nowProgress = sound.getPlayingOffset().asSeconds();
if(ImGui::SliderFloat("Now", &nowProgress, 0, sound.getBuffer()->getDuration().asSeconds()))
{
sound.pause();
sound.setPlayingOffset(sf::seconds(nowProgress));
sound.play();
}
float volume = sound.getVolume();
if(ImGui::SliderFloat("Volume", &volume, 0.f, 100.f))
{
sound.setVolume(volume);
}
}
ImGui::End();
// Update
{
sf::Time dt = clk.restart();
if(!followMouse)
{
// Update rotation
t += dt.asSeconds() * angular_velocity;
pos.x = radius * std::cos(t);
pos.y = radius * std::sin(t);
pos.z = 0.f;
}
else
{
// Follow the mouse
auto m = sf::Mouse::getPosition(window);
auto siz = window.getSize();
// Convert origin to center
m.x = m.x - siz.x / 2.f;
m.y = m.y - siz.y / 2.f;
pos.x = m.x;
pos.y = m.y;
}
sound.setPosition(pos);
}
// Draw
{
window.clear();
sf::Vector2f centerPos = {window.getSize().x / 2.f, window.getSize().y / 2.f};
// Center
sf::CircleShape center{1.f};
center.setOrigin(center.getRadius(), center.getRadius());
center.setPosition(window.getSize().x / 2.f, window.getSize().y / 2.f);
center.setFillColor(sf::Color::Red);
window.draw(center);
// Draw the Min Distance circle
sf::CircleShape minDistanceCir{sound_distance};
minDistanceCir.setOrigin(minDistanceCir.getRadius(), minDistanceCir.getRadius());
minDistanceCir.setPosition(centerPos.x, centerPos.y);
minDistanceCir.setFillColor(sf::Color::Transparent);
minDistanceCir.setOutlineThickness(1.f);
minDistanceCir.setOutlineColor(sf::Color::Blue);
window.draw(minDistanceCir);
// Draw the attenuation circles
for (float i = 1000.f; i >= 0; i -= 1.f)
{
float radius = sound_distance + i;
sf::CircleShape attenuationCir{radius};
float factor = GetSoundFactor(radius);
attenuationCir.setOrigin(attenuationCir.getRadius(), attenuationCir.getRadius());
attenuationCir.setPosition(centerPos.x, centerPos.y);
attenuationCir.setFillColor(sf::Color(factor * 255, factor * 255, factor * 255));
window.draw(attenuationCir);
}
// Sound shape
sf::CircleShape soundCir{1.f};
soundCir.setOrigin(soundCir.getRadius(), soundCir.getRadius());
soundCir.setPosition(centerPos.x + pos.x, centerPos.y + pos.y);
soundCir.setFillColor(sf::Color::Green);
window.draw(soundCir);
window.pushGLStates();
ImGui::SFML::Render(window);
window.popGLStates();
window.display();
}
}
}