MCPcopy Create free account
hub / github.com/NazaraEngine/NazaraEngine / main

Function main

examples/DopplerEffect/main.cpp:19–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17#include <iostream>
18
19int main()
20{
21 // NzKeyboard nécessite l'initialisation du module Utilitaire
22 Nz::Initializer<Nz::Audio, Nz::Platform> audio;
23 if (!audio)
24 {
25 std::cout << "Failed to initialize audio module" << std::endl;
26 std::getchar();
27 return 1;
28 }
29
30 Nz::Sound sound;
31 if (!sound.LoadFromFile("resources/siren.wav"))
32 {
33 std::cout << "Failed to load sound" << std::endl;
34 std::getchar();
35 return 1;
36 }
37
38 std::cout << "Demonstration de l'effet doppler avec Nazara" << std::endl;
39 std::cout << "Appuyez sur entree pour demarrer" << std::endl;
40 std::cout << "Appuyez sur echap pour arreter" << std::endl;
41
42 std::getchar();
43
44 // On fait en sorte de répéter le son
45 sound.EnableLooping(true);
46
47 // La source du son se situe vers la gauche (Et un peu en avant)
48 sound.SetPosition(Nz::Vector3f::Left()*50.f + Nz::Vector3f::Forward()*5.f);
49
50 // Et possède une vitesse de 10 par seconde vers la droite
51 sound.SetVelocity(Nz::Vector3f::Left()*-10.f);
52
53 // On joue le son
54 sound.Play();
55
56 // La boucle du programme (Pour déplacer le son)
57 Nz::Clock clock;
58 while (sound.GetStatus() == Nz::SoundStatus_Playing)
59 {
60 // Comme le son se joue dans un thread séparé, on peut mettre en pause le principal régulièrement
61 int sleepTime = int(1000/60 - clock.GetMilliseconds()); // 60 FPS
62
63 if (sleepTime > 0)
64 Nz::Thread::Sleep(sleepTime);
65
66 // On bouge la source du son en fonction du temps depuis chaque mise à jour
67 Nz::Vector3f pos = sound.GetPosition() + sound.GetVelocity()*clock.GetSeconds();
68 sound.SetPosition(pos);
69
70 std::cout << "Sound position: " << pos << std::endl;
71
72 // Si la position de la source atteint une certaine position, ou si l'utilisateur appuie sur echap
73 if (pos.x > Nz::Vector3f::Left().x*-50.f || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::Escape))
74 sound.Stop(); // On arrête le son (Stoppant également la boucle)
75
76 clock.Restart();

Callers

nothing calls this directly

Calls 12

GetMillisecondsMethod · 0.80
GetSecondsMethod · 0.80
RestartMethod · 0.80
LoadFromFileMethod · 0.45
EnableLoopingMethod · 0.45
SetPositionMethod · 0.45
SetVelocityMethod · 0.45
PlayMethod · 0.45
GetStatusMethod · 0.45
GetPositionMethod · 0.45
GetVelocityMethod · 0.45
StopMethod · 0.45

Tested by

no test coverage detected