Core: Add SignalHandlerAppComponent

This commit is contained in:
SirLynix 2023-06-07 13:36:13 +02:00 committed by Jérôme Leclercq
parent 8eefb2f101
commit 40bb69bc60
9 changed files with 157 additions and 26 deletions

View File

@ -12,10 +12,8 @@
#include <Nazara/Core/Application.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Modules.hpp>
#include <Nazara/Core/SignalHandlerAppComponent.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Platform/Keyboard.hpp>
#include <Nazara/Platform/Platform.hpp>
#include <Nazara/Platform/Window.hpp>
#include <chrono>
#include <iostream>
#include <thread>
@ -27,6 +25,7 @@ int main()
resourceDir = "../.." / resourceDir;
Nz::Application<Nz::Audio> app;
app.AddComponent<Nz::SignalHandlerAppComponent>();
Nz::Sound sound;
if (!sound.LoadFromFile(resourceDir / "Audio/siren.wav"))
@ -36,44 +35,33 @@ int main()
return 1;
}
std::cout << "Demonstration de l'effet doppler avec Nazara" << std::endl;
std::cout << "Appuyez sur entree pour demarrer" << std::endl;
std::cout << "Appuyez sur echap pour arreter" << std::endl;
std::cout << "Doppler effect demo" << std::endl;
std::cout << "Press enter to start" << std::endl;
std::getchar();
// On fait en sorte de répéter le son
// Make a repeating sound, located to the left (and a bit forward so it doesn't switch from left to right speaker brutally) with a right velocity
sound.EnableLooping(true);
// La source du son se situe vers la gauche (Et un peu en avant)
sound.SetPosition(Nz::Vector3f::Left() * 50.f + Nz::Vector3f::Forward() * 5.f);
// Et possède une vitesse de 10 par seconde vers la droite
sound.SetVelocity(Nz::Vector3f::Right() * 10.f);
// On joue le son
sound.Play();
Nz::MillisecondClock clock;
app.AddUpdaterFunc([&]
app.AddUpdaterFunc([&](Nz::Time elapsedTime)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000 / 30));
if (sound.GetStatus() != Nz::SoundStatus::Playing)
{
// On arrête le son et l'application
sound.Stop();
app.Quit();
}
// On bouge la source du son en fonction du temps depuis chaque mise à jour
Nz::Vector3f pos = sound.GetPosition() + sound.GetVelocity() * clock.Restart().AsSeconds();
// Move sound position according to its velocity
Nz::Vector3f pos = sound.GetPosition() + sound.GetVelocity() * elapsedTime.AsSeconds();
sound.SetPosition(pos);
std::cout << "Position: " << pos.x << std::endl;
// Si la position de la source atteint une certaine position, ou si l'utilisateur appuie sur echap
if (pos.x > Nz::Vector3f::Right().x * 50.f || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Escape))
// Stop once far enough
if (pos.x > Nz::Vector3f::Right().x * 50.f)
{
sound.Stop();
app.Quit();

View File

@ -1,3 +1,3 @@
target("DopplerEffect")
add_deps("NazaraAudio", "NazaraPlatform")
add_files("main.cpp")
add_deps("NazaraAudio")
add_files("main.cpp")

View File

@ -6,6 +6,7 @@
#include <Nazara/Core/Application.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Modules.hpp>
#include <Nazara/Core/SignalHandlerAppComponent.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Platform/Keyboard.hpp>
#include <Nazara/Platform/Platform.hpp>
@ -20,6 +21,7 @@ int main()
resourceDir = "../.." / resourceDir;
Nz::Application<Nz::Audio> app;
app.AddComponent<Nz::SignalHandlerAppComponent>();
Nz::SoundStreamParams streamParams;
streamParams.forceMono = false;

View File

@ -27,7 +27,7 @@ namespace Nz
ApplicationBase(int argc, const Pointer<const char>* argv);
ApplicationBase(const ApplicationBase&) = delete;
ApplicationBase(ApplicationBase&&) = delete;
~ApplicationBase() = default;
~ApplicationBase();
inline void AddUpdater(std::unique_ptr<ApplicationUpdater>&& functor);
template<typename F> void AddUpdaterFunc(F&& functor);
@ -48,6 +48,8 @@ namespace Nz
ApplicationBase& operator=(const ApplicationBase&) = delete;
ApplicationBase& operator=(ApplicationBase&&) = delete;
static inline ApplicationBase* Instance();
protected:
template<typename T, typename... Args> T& AddComponent(Args&&... args);
@ -66,6 +68,8 @@ namespace Nz
std::vector<Updater> m_updaters;
HighPrecisionClock m_clock;
Time m_currentTime;
static ApplicationBase* s_instance;
};
}

View File

@ -74,7 +74,12 @@ namespace Nz
{
m_running = false;
}
inline ApplicationBase* ApplicationBase::Instance()
{
return s_instance;
}
template<typename T, typename... Args>
T& ApplicationBase::AddComponent(Args&&... args)
{

View File

@ -0,0 +1,35 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_SIGNALHANDLERAPPCOMPONENT_HPP
#define NAZARA_CORE_SIGNALHANDLERAPPCOMPONENT_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Core/ApplicationComponent.hpp>
namespace Nz
{
class NAZARA_CORE_API SignalHandlerAppComponent : public ApplicationComponent
{
public:
inline SignalHandlerAppComponent(ApplicationBase& app);
SignalHandlerAppComponent(const SignalHandlerAppComponent&) = delete;
SignalHandlerAppComponent(SignalHandlerAppComponent&&) = delete;
~SignalHandlerAppComponent() = default;
SignalHandlerAppComponent& operator=(const SignalHandlerAppComponent&) = delete;
SignalHandlerAppComponent& operator=(SignalHandlerAppComponent&&) = delete;
private:
void InstallSignalHandler();
static void HandleInterruptSignal(const char* signalName);
};
}
#include <Nazara/Core/SignalHandlerAppComponent.inl>
#endif // NAZARA_CORE_SIGNALHANDLERAPPCOMPONENT_HPP

View File

@ -0,0 +1,16 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
SignalHandlerAppComponent::SignalHandlerAppComponent(ApplicationBase& app) :
ApplicationComponent(app)
{
InstallSignalHandler();
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@ -15,6 +15,14 @@ namespace Nz
m_running(true),
m_currentTime(Time::Zero())
{
NazaraAssert(s_instance == nullptr, "only one instance of ApplicationBase can exist at a given time");
s_instance = this;
}
ApplicationBase::~ApplicationBase()
{
assert(s_instance == this);
s_instance = nullptr;
}
int ApplicationBase::Run()
@ -90,4 +98,6 @@ namespace Nz
return m_running;
}
ApplicationBase* ApplicationBase::s_instance = nullptr;
}

View File

@ -0,0 +1,71 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/SignalHandlerAppComponent.hpp>
#include <Nazara/Core/ApplicationBase.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Windows.h>
#elif defined(NAZARA_PLATFORM_POSIX)
#include <signal.h>
#include <string.h>
#endif
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
void SignalHandlerAppComponent::InstallSignalHandler()
{
bool succeeded = false;
#if defined(NAZARA_PLATFORM_WINDOWS)
succeeded = ::SetConsoleCtrlHandler([](DWORD ctrlType) -> BOOL
{
switch (ctrlType)
{
case CTRL_C_EVENT: HandleInterruptSignal("CTRL_C"); break;
case CTRL_BREAK_EVENT: HandleInterruptSignal("CTRL_BREAK"); break;
case CTRL_CLOSE_EVENT: HandleInterruptSignal("CTRL_CLOSE"); break;
case CTRL_LOGOFF_EVENT: HandleInterruptSignal("CTRL_LOGOFF"); break;
case CTRL_SHUTDOWN_EVENT: HandleInterruptSignal("CTRL_SHUTDOWN"); break;
default:
{
std::string signalName = "<unknown CTRL signal " + std::to_string(ctrlType) + ">";
HandleInterruptSignal(signalName.c_str());
}
}
return TRUE;
}, TRUE);
#elif defined(NAZARA_PLATFORM_POSIX)
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
action.sa_handler = [](int sig)
{
HandleInterruptSignal(strsignal(sig));
};
if (sigaction(SIGINT, &action, nullptr) != 0)
succeeded = false;
if (sigaction(SIGTERM, &action, nullptr) != 0)
succeeded = false;
#endif
if (!succeeded)
NazaraError("failed to install interruption signal handlers");
}
void SignalHandlerAppComponent::HandleInterruptSignal(const char* signalName)
{
assert(ApplicationBase::Instance());
NazaraNotice("received interruption signal " + std::string(signalName) + ", exiting...");
ApplicationBase::Instance()->Quit();
}
}