-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
344 lines (284 loc) · 11.6 KB
/
Copy pathmain.cpp
File metadata and controls
344 lines (284 loc) · 11.6 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* This project uses the following third-party libraries:
*
* 1. SFML - Simple and Fast Multimedia Library
* Copyright (c) Laurent Gomila
* Licensed under the zlib/libpng license
* https://www.sfml-dev.org/license.php
*
* 2. Steam Audio
* Copyright (c) Valve Corporation
* Licensed under the BSD 3-Clause "New" or "Revised" License
* https://github.com/ValveSoftware/steam-audio/blob/master/LICENSE.md
*
* This project itself is released under CC0 1.0 Universal (CC0 1.0) Public Domain Dedication.
* To the extent possible under law, the author(s) have dedicated all copyright and related
* and neighboring rights to this software to the public domain worldwide.
* This software is distributed without any warranty.
* You should have received a copy of the CC0 Public Domain Dedication along with this software.
* If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.*/
#include <iostream>
#include <cmath>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <vector>
#include "phonon.h"
#include "steamaudiomanager.h"
#include "PerlinNoise.hpp"
// Screen Size
const int sW {1920};
const int sH {1080};
// Other trivial constants
const float piVal {3.14159265358979323846};
const float movementSpeed {300.f};
const float radarSpeed {1500.f};
// Keyboard input method
void InputMovement(sf::Vector2f& ballPos, float deltaTime) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) ballPos.y -= movementSpeed * deltaTime;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) ballPos.y += movementSpeed * deltaTime;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) ballPos.x += movementSpeed * deltaTime;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) ballPos.x -= movementSpeed * deltaTime;
}
// Focus shape modifier and color initialize
void UpdateFocusShape(sf::VertexArray& shape, sf::Vector2f center, float radius, float startAngle, float endAngle, sf::Color color)
{
shape[0].position = center;
shape[0].color = color;
for(size_t i = 1; i < shape.getVertexCount(); ++i)
{
float angle = startAngle + (endAngle - startAngle) * (i - 1) / (shape.getVertexCount() - 2);
shape[i].position = center + sf::Vector2f(std::cos(angle) * radius, std::sin(angle) * radius);
shape[i].color = sf::Color(0, 0, 0, 0);
}
}
// Radar circle action
void UpdateRadarShape(sf::CircleShape& radarCircle, float& radarRadius, float maxRadarRadius, float deltaTime, bool& isRadarExpanding)
{
if (isRadarExpanding)
{
radarRadius += radarSpeed * deltaTime;
float alphaPercentage = 1.0f - (radarRadius / maxRadarRadius);
int alpha = static_cast<int>(255 * alphaPercentage);
alpha = std::max(0, std::min(50, alpha));
radarCircle.setFillColor(sf::Color(255, 255, 255, alpha));
if (radarRadius >= maxRadarRadius)
{
isRadarExpanding = false;
radarRadius = 10.f;
}
radarCircle.setRadius(radarRadius);
radarCircle.setOrigin(radarRadius, radarRadius);
}
}
// Focus shape modifier and color initialize
void DrawGridInstance(sf::VertexArray& shape, sf::Vector2f center, float rotationAngle)
{
sf::Color gridColor = sf::Color(255, 255, 255, 100);
sf::Transform transform;
shape[0].position = center;
shape[0].color = gridColor;
transform.rotate(rotationAngle, center);
for(size_t i = 1; i < shape.getVertexCount(); ++i)
{
shape[i].position = transform.transformPoint(sf::Vector2f(center.x, center.y + i));
shape[i].color = gridColor;
}
}
int main()
{
// Sfml window initialization and frame limit
sf::RenderWindow window(sf::VideoMode(sW, sH), "Audio Actor Test!");
window.setFramerateLimit(60);
// Sound buffer initialization and wave sound file load with sfml interface
sf::SoundBuffer radarBuffer;
if(!radarBuffer.loadFromFile("assets/audiofiles/radarSFX.wav"))
{
std::cerr << "Failed to load audio file." << std::endl;
return 1;
}
if (radarBuffer.getChannelCount() != 1)
{
std::cerr << "Error: Sound file must be mono for binaural effect!" << std::endl;
}
sf::Sound radarSound(radarBuffer);
std::vector<float> radarFloatBuffer;
radarFloatBuffer.reserve(radarBuffer.getSampleCount() * radarBuffer.getChannelCount());
for (size_t i = 0; i < radarBuffer.getSampleCount() * radarBuffer.getChannelCount(); ++i)
{
radarFloatBuffer.push_back(static_cast<float>(radarBuffer.getSamples()[i]) / 32767.f);
}
// Steam Audio Inıtialize
SteamAudioManager steamAudio;
steamAudio.Initialize();
steamAudio.DebugPrint();
IPLAudioSettings audioSettings{};
audioSettings.samplingRate = 44100;
audioSettings.frameSize = 512;
// Base clock and fps counter variables
sf::Clock clock;
float currentElapsedTime = 0.0f;
int frameCount = 0;
float fpsUpdateTime = 0.0f;
float fpsUpdateInterval = 0.2f;
float fpsVal = 0.f;
// Sfml font load
sf::Font font;
if(!font.loadFromFile("assets/fonts/ARIAL.TTF"))
{
std::cerr << "Failed to load font file." << std::endl;
return -1;
}
// Drawable shape colors definitions
sf::Color circleColor(100, 100, 100);
sf::Color focusColor(140, 10, 60);
// Informative text
sf::Text mousePosText;
mousePosText.setFont(font);
mousePosText.setCharacterSize(20);
mousePosText.setFillColor(sf::Color::White);
mousePosText.setPosition(20, 20);
sf::Text fpsText;
fpsText.setFont(font);
fpsText.setCharacterSize(20);
fpsText.setFillColor(sf::Color::White);
fpsText.setPosition(1750, 20);
// Main Actor shape
sf::Vector2f ballPos = {sW/2, sH/2};
sf::CircleShape circleShape(20.f);
circleShape.setFillColor(circleColor);
circleShape.setOrigin(20.f, 20.f);
// Focus shape vertex array variable declarations
float outerRadius = 200.f;
float maxRadius = 1000.f;
float minRadius = 60.f;
float startAngle;
float endAngle;
int segments = 100;
sf::VertexArray focusShape(sf::PrimitiveType::TriangleFan, segments);
// Radar shape
float radarRadius = 10.f;
float maxRadarRadius = 600.f;
sf::CircleShape radarCircle(radarRadius);
radarCircle.setOrigin(radarRadius, radarRadius);
bool isRadarExpanding = false;
// Perlin background grid
int gridReso = 20;
int gCols = sW / gridReso;
int gRows = sH / gridReso;
std::vector<float> rotationAngles(gCols * gRows);
sf::VertexArray gridShape(sf::PrimitiveType::Points, gridReso);
// Perlin Noise initialize
siv::PerlinNoise perlin;
double scale = 0.05;
for (int y = 0; y < gRows; ++y)
{
for (int x = 0; x < gCols; ++x)
{
// Sample from the Perlin noise
double noiseValue = perlin.noise2D(x * scale, y * scale);
// Map noise value from [-1, 1] to [0, 1]
noiseValue = (noiseValue + 1.0) / 2.0;
// Map noise value to a rotation angle [0, 360] degrees
int index = y * gCols + x;
rotationAngles[index] = noiseValue * 360.0;
}
}
sf::Mouse mouse;
// ----------------- MAIN GAME LOOP ----------------------
while(window.isOpen())
{
sf::Vector2i mousePosINT = mouse.getPosition(window);
IPLVector3 dirVector = {(float)mousePosINT.x, 0, (float)mousePosINT.y};
std::vector<float> outputBuffer = steamAudio.ProcessAudio(radarFloatBuffer, dirVector);
// Convert back to SFML format
std::vector<sf::Int16> processedInt16(outputBuffer.size());
for (size_t i = 0; i < outputBuffer.size(); ++i)
{
processedInt16[i] = static_cast<sf::Int16>(outputBuffer[i] * 32767.f);
}
sf::SoundBuffer processedBuffer;
processedBuffer.loadFromSamples(processedInt16.data(), processedInt16.size(), 2, audioSettings.samplingRate);
sf::Sound processedSound(processedBuffer);
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
// Keyboard click event check and calls
if(event.type == sf::Event::KeyPressed)
{
if(event.key.code == sf::Keyboard::F)
{
processedSound.play();
//radarSound.play();
std::cout << "Spatialized Radar Pulse played." << std::endl;
isRadarExpanding = true;
radarRadius = 10.f;
}
if(event.key.code == sf::Keyboard::Space)
{
radarSound.play();
std::cout << "Non-Spatialized Radar Pulse played." << std::endl;
isRadarExpanding = true;
radarRadius = 10.f;
}
}
}
// Delta time and frame per second calculation
float deltaTime = clock.restart().asSeconds();
frameCount++;
currentElapsedTime += deltaTime;
if (currentElapsedTime - fpsUpdateTime >= fpsUpdateInterval)
{
fpsVal = frameCount / (currentElapsedTime - fpsUpdateTime);
fpsUpdateTime = currentElapsedTime;
frameCount = 0;
}
// Mouse position and angle calculation with main actor
sf::Vector2i mousePos = mouse.getPosition(window);
float mouseBallDistance = std::sqrt(std::pow(mousePos.x - ballPos.x, 2) + std::pow(mousePos.y - ballPos.y, 2));
float maxDistance = 800.f;
float focusRadian = std::atan2f(mousePos.y - ballPos.y, mousePos.x - ballPos.x);
float focusDegree = (focusRadian * 180) / piVal;
float normalizedDistance = std::min(mouseBallDistance, maxDistance) / maxDistance;
float invertedNormalizedDistance = 1.0f - normalizedDistance;
// Focus shape sector width
outerRadius = minRadius + normalizedDistance * (maxRadius - minRadius);
float minSectorWidth = 5.0f * (piVal / 180.0f);
float maxSectorWidth = 240.0f * (piVal / 180.0f);
float sectorWidth = minSectorWidth + invertedNormalizedDistance * (maxSectorWidth - minSectorWidth);
startAngle = focusRadian - (sectorWidth * 0.5f);
endAngle = focusRadian + (sectorWidth * 0.5f);
// Defined method calls for every iteration
InputMovement(ballPos, deltaTime);
UpdateFocusShape(focusShape, (sf::Vector2f){ballPos}, outerRadius, startAngle, endAngle, focusColor);
UpdateRadarShape(radarCircle, radarRadius, maxRadarRadius, deltaTime, isRadarExpanding);
window.clear(sf::Color::Black);
// Building grid;
for(int y = 0; y < gRows; ++y)
{
for(int x = 0; x < gCols; ++x)
{
int index = y * gCols + x;
float rotationAngle = rotationAngles[index];
sf::Vector2f cellCenter(4 + (sW / gCols) * x, 4 + (sH / gRows) * y);
DrawGridInstance(gridShape, cellCenter, rotationAngle + focusDegree);
window.draw(gridShape);
}
}
// Screen text insert
mousePosText.setString("Mouse Position: x = " + std::to_string(mousePos.x) + " y = " + std::to_string(mousePos.y));
fpsText.setString("FPS: " + std::to_string(fpsVal));
// Main actor position change
circleShape.setPosition(ballPos);
radarCircle.setPosition(ballPos);
// Sfml draw calls
window.draw(focusShape);
window.draw(radarCircle);
window.draw(circleShape);
window.draw(mousePosText);
window.draw(fpsText);
window.display();
}
steamAudio.CleanUp();
return 0;
}