From 4668a1d1581a3feeb78fc69a7fe62ad736fd59b6 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Sat, 21 Jan 2023 19:10:07 +0100 Subject: [PATCH] Update examples and tests code --- examples/DeferredShading/main.cpp | 142 +++++++-------- examples/PhysicallyBasedRendering/main.cpp | 112 ++++++------ examples/Physics2DDemo/main.cpp | 77 +++----- examples/PhysicsDemo/main.cpp | 199 ++++++++++----------- examples/Showcase/main.cpp | 165 ++++++++--------- examples/Tut00/main.cpp | 4 +- examples/Tut01/main.cpp | 5 +- examples/Tut02/main.cpp | 46 +++-- examples/WidgetDemo/main.cpp | 61 ++----- tests/ComputeTest/main.cpp | 13 +- tests/GraphicsTest/main.cpp | 127 +++++++------ 11 files changed, 418 insertions(+), 533 deletions(-) diff --git a/examples/DeferredShading/main.cpp b/examples/DeferredShading/main.cpp index 74a794ea1..5612d48ca 100644 --- a/examples/DeferredShading/main.cpp +++ b/examples/DeferredShading/main.cpp @@ -82,13 +82,11 @@ int main() else rendererConfig.preferredAPI = Nz::RenderAPI::OpenGL; - Nz::Modules nazara(rendererConfig); + Nz::Application app(rendererConfig); nzsl::ShaderWriter::States states; states.shaderModuleResolver = Nz::Graphics::Instance()->GetShaderModuleResolver(); - Nz::RenderWindow window; - Nz::MeshParams meshParams; meshParams.center = true; meshParams.vertexRotation = Nz::EulerAnglesf(0.f, -90.f, 0.f); @@ -98,12 +96,11 @@ int main() std::shared_ptr device = Nz::Graphics::Instance()->GetRenderDevice(); const Nz::RenderDeviceInfo& deviceInfo = device->GetDeviceInfo(); + auto& windowing = app.AddComponent(); + std::string windowTitle = "Graphics Test"; - if (!window.Create(device, Nz::VideoMode(1920, 1080, 32), windowTitle)) - { - std::cout << "Failed to create Window" << std::endl; - return __LINE__; - } + Nz::Window& window = windowing.CreateWindow(Nz::VideoMode(1920, 1080), windowTitle); + Nz::WindowSwapchain windowSwapchain(device, window); std::shared_ptr spaceship = Nz::Mesh::LoadFromFile(resourceDir / "Spaceship/spaceship.obj", meshParams); if (!spaceship) @@ -1061,8 +1058,6 @@ int main() Nz::EulerAnglesf camAngles(-30.f, 0.f, 0.f); Nz::Quaternionf camQuat(camAngles); - window.EnableEventPolling(true); - Nz::MillisecondClock updateClock; Nz::MillisecondClock fpsClock; unsigned int fps = 0; @@ -1093,76 +1088,69 @@ int main() return Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, elapsedTime * rotationSpeed, 0.f)) * direction; }; - while (window.IsOpen()) + window.GetEventHandler().OnEvent.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent& event) { - Nz::Time now = Nz::GetElapsedNanoseconds(); - if (lightAnimation) - elapsedTime += now - time; - time = now; - - Nz::WindowEvent event; - while (window.PollEvent(&event)) + switch (event.type) { - switch (event.type) + case Nz::WindowEventType::MouseMoved: // La souris a bougé { - case Nz::WindowEventType::Quit: - window.Close(); - break; + // Gestion de la caméra free-fly (Rotation) + float sensitivity = 0.3f; // Sensibilité de la souris - case Nz::WindowEventType::MouseMoved: // La souris a bougé - { - // Gestion de la caméra free-fly (Rotation) - float sensitivity = 0.3f; // Sensibilité de la souris + // On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris + camAngles.yaw = camAngles.yaw - event.mouseMove.deltaX*sensitivity; + camAngles.yaw.Normalize(); - // On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris - camAngles.yaw = camAngles.yaw - event.mouseMove.deltaX*sensitivity; - camAngles.yaw.Normalize(); + // Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles + camAngles.pitch = Nz::Clamp(camAngles.pitch - event.mouseMove.deltaY*sensitivity, -89.f, 89.f); - // Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles - camAngles.pitch = Nz::Clamp(camAngles.pitch - event.mouseMove.deltaY*sensitivity, -89.f, 89.f); - - camQuat = camAngles; - break; - } - - case Nz::WindowEventType::KeyPressed: - { - if (event.key.scancode == Nz::Keyboard::Scancode::Space) - { - float elapsedSeconds = elapsedTime.AsSeconds(); - float rotationSpeed = ComputeLightAnimationSpeed(viewerPos); - - auto& spotLight = spotLights.emplace_back(); - spotLight.color = Nz::Color(0.4f, 0.4f, 1.f); - spotLight.radius = 5.f; - spotLight.position = AnimateLightPosition(viewerPos, rotationSpeed, -elapsedSeconds); - spotLight.direction = AnimateLightDirection(camQuat * Nz::Vector3f::Forward(), rotationSpeed, -elapsedSeconds); - - lightUpdate = true; - } - else if (event.key.virtualKey == Nz::Keyboard::VKey::F) - forwardEnabled = !forwardEnabled; - else if (event.key.virtualKey == Nz::Keyboard::VKey::A) - lightAnimation = !lightAnimation; - else if (event.key.virtualKey == Nz::Keyboard::VKey::B) - bloomEnabled = !bloomEnabled; - else if (event.key.virtualKey == Nz::Keyboard::VKey::E) - modelInstance1.UpdateWorldMatrix(Nz::Matrix4f::Transform(viewerPos, camQuat)); - - break; - } - - case Nz::WindowEventType::Resized: - { - Nz::Vector2ui newSize = window.GetSize(); - viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newSize.x) / newSize.y, 0.1f, 1000.f)); - break; - } - - default: - break; + camQuat = camAngles; + break; } + + case Nz::WindowEventType::KeyPressed: + { + if (event.key.scancode == Nz::Keyboard::Scancode::Space) + { + float elapsedSeconds = elapsedTime.AsSeconds(); + float rotationSpeed = ComputeLightAnimationSpeed(viewerPos); + + auto& spotLight = spotLights.emplace_back(); + spotLight.color = Nz::Color(0.4f, 0.4f, 1.f); + spotLight.radius = 5.f; + spotLight.position = AnimateLightPosition(viewerPos, rotationSpeed, -elapsedSeconds); + spotLight.direction = AnimateLightDirection(camQuat * Nz::Vector3f::Forward(), rotationSpeed, -elapsedSeconds); + + lightUpdate = true; + } + else if (event.key.virtualKey == Nz::Keyboard::VKey::F) + forwardEnabled = !forwardEnabled; + else if (event.key.virtualKey == Nz::Keyboard::VKey::A) + lightAnimation = !lightAnimation; + else if (event.key.virtualKey == Nz::Keyboard::VKey::B) + bloomEnabled = !bloomEnabled; + else if (event.key.virtualKey == Nz::Keyboard::VKey::E) + modelInstance1.UpdateWorldMatrix(Nz::Matrix4f::Transform(viewerPos, camQuat)); + + break; + } + + case Nz::WindowEventType::Resized: + { + Nz::Vector2ui newSize = window.GetSize(); + viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newSize.x) / newSize.y, 0.1f, 1000.f)); + break; + } + + default: + break; } + }); + + app.AddUpdater([&](Nz::Time deltaTime) + { + if (lightAnimation) + elapsedTime += deltaTime; if (std::optional deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60))) { @@ -1194,11 +1182,11 @@ int main() viewerInstance.UpdateViewMatrix(Nz::Matrix4f::TransformInverse(viewerPos, camQuat)); } - Nz::RenderFrame frame = window.AcquireFrame(); + Nz::RenderFrame frame = windowSwapchain.AcquireFrame(); if (!frame) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); - continue; + return; } currentFrame = &frame; @@ -1535,7 +1523,7 @@ int main() bakedGraph.Execute(frame); - const Nz::RenderTarget* windowRT = window.GetRenderTarget(); + const Nz::RenderTarget* windowRT = &windowSwapchain.GetSwapchain(); frame.Execute([&](Nz::CommandBufferBuilder& builder) { Nz::Recti windowRenderRect(0, 0, window.GetSize().x, window.GetSize().y); @@ -1572,7 +1560,7 @@ int main() window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS"); fps = 0; } - } + }); - return EXIT_SUCCESS; + return app.Run(); } diff --git a/examples/PhysicallyBasedRendering/main.cpp b/examples/PhysicallyBasedRendering/main.cpp index 63fb12a17..5e53e1dc5 100644 --- a/examples/PhysicallyBasedRendering/main.cpp +++ b/examples/PhysicallyBasedRendering/main.cpp @@ -21,9 +21,15 @@ int main() else rendererConfig.preferredAPI = Nz::RenderAPI::OpenGL; - Nz::Modules nazara(rendererConfig); + Nz::Application app(rendererConfig); - Nz::RenderWindow window; + std::shared_ptr device = Nz::Graphics::Instance()->GetRenderDevice(); + + auto& windowing = app.AddComponent(); + + std::string windowTitle = "Physically Based Rendering Test"; + Nz::Window& window = windowing.CreateWindow(Nz::VideoMode(1920, 1080), windowTitle); + Nz::WindowSwapchain windowSwapchain(device, window); Nz::MeshParams meshParams; meshParams.center = true; @@ -31,15 +37,6 @@ int main() meshParams.vertexScale = Nz::Vector3f(0.002f); meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV_Tangent); - std::shared_ptr device = Nz::Graphics::Instance()->GetRenderDevice(); - - std::string windowTitle = "Physically Based Rendering Test"; - if (!window.Create(device, Nz::VideoMode(1920, 1080, 32), windowTitle)) - { - std::cout << "Failed to create Window" << std::endl; - return __LINE__; - } - std::shared_ptr sphereMesh = std::make_shared(); sphereMesh->CreateStatic(); sphereMesh->BuildSubMesh(Nz::Primitive::UVSphere(1.f, 50, 50)); @@ -74,7 +71,7 @@ int main() Nz::Vector2ui windowSize = window.GetSize(); - Nz::Camera camera(window.GetRenderTarget()); + Nz::Camera camera(&windowSwapchain.GetSwapchain()); //camera.UpdateClearColor(Nz::Color::Gray); Nz::ViewerInstance& viewerInstance = camera.GetViewerInstance(); @@ -102,65 +99,58 @@ int main() Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f); Nz::Quaternionf camQuat(camAngles); - window.EnableEventPolling(true); - Nz::MillisecondClock updateClock; Nz::MillisecondClock fpsClock; unsigned int fps = 0; Nz::Mouse::SetRelativeMouseMode(true); - while (window.IsOpen()) + window.GetEventHandler().OnEvent.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent& event) { - Nz::WindowEvent event; - while (window.PollEvent(&event)) + switch (event.type) { - switch (event.type) + case Nz::WindowEventType::KeyPressed: + if (event.key.virtualKey == Nz::Keyboard::VKey::N) + { + if (materialInstance->GetTextureProperty(normalMapProperty)) + materialInstance->SetTextureProperty(normalMapProperty, {}); + else + materialInstance->SetTextureProperty(normalMapProperty, normalMap); + } + break; + + case Nz::WindowEventType::MouseMoved: // La souris a bougé { - case Nz::WindowEventType::Quit: - window.Close(); - break; + // Gestion de la caméra free-fly (Rotation) + float sensitivity = 0.3f; // Sensibilité de la souris - case Nz::WindowEventType::KeyPressed: - if (event.key.virtualKey == Nz::Keyboard::VKey::N) - { - if (materialInstance->GetTextureProperty(normalMapProperty)) - materialInstance->SetTextureProperty(normalMapProperty, {}); - else - materialInstance->SetTextureProperty(normalMapProperty, normalMap); - } - break; + // On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris + camAngles.yaw = camAngles.yaw - event.mouseMove.deltaX * sensitivity; + camAngles.yaw.Normalize(); - case Nz::WindowEventType::MouseMoved: // La souris a bougé - { - // Gestion de la caméra free-fly (Rotation) - float sensitivity = 0.3f; // Sensibilité de la souris + // Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles + camAngles.pitch = Nz::Clamp(camAngles.pitch - event.mouseMove.deltaY*sensitivity, -89.f, 89.f); - // On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris - camAngles.yaw = camAngles.yaw - event.mouseMove.deltaX * sensitivity; - camAngles.yaw.Normalize(); - - // Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles - camAngles.pitch = Nz::Clamp(camAngles.pitch - event.mouseMove.deltaY*sensitivity, -89.f, 89.f); - - camQuat = camAngles; - //light->UpdateRotation(camQuat); - break; - } - - case Nz::WindowEventType::Resized: - { - Nz::Vector2ui newWindowSize = window.GetSize(); - viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newWindowSize.x) / newWindowSize.y, 0.1f, 1000.f)); - viewerInstance.UpdateTargetSize(Nz::Vector2f(newWindowSize)); - break; - } - - default: - break; + camQuat = camAngles; + //light->UpdateRotation(camQuat); + break; } - } + case Nz::WindowEventType::Resized: + { + Nz::Vector2ui newWindowSize = window.GetSize(); + viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newWindowSize.x) / newWindowSize.y, 0.1f, 1000.f)); + viewerInstance.UpdateTargetSize(Nz::Vector2f(newWindowSize)); + break; + } + + default: + break; + } + }); + + app.AddUpdater([&](Nz::Time elapsedTime) + { if (std::optional deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60))) { float cameraSpeed = 2.f * deltaTime->AsSeconds(); @@ -191,11 +181,11 @@ int main() //light->UpdatePosition(viewerPos); } - Nz::RenderFrame frame = window.AcquireFrame(); + Nz::RenderFrame frame = windowSwapchain.AcquireFrame(); if (!frame) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); - continue; + return; } viewerInstance.UpdateViewMatrix(Nz::Matrix4f::TransformInverse(viewerPos, camAngles)); @@ -213,7 +203,7 @@ int main() window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS"); fps = 0; } - } + }); - return EXIT_SUCCESS; + return app.Run(); } diff --git a/examples/Physics2DDemo/main.cpp b/examples/Physics2DDemo/main.cpp index c35aeabf5..83a842ac0 100644 --- a/examples/Physics2DDemo/main.cpp +++ b/examples/Physics2DDemo/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -36,27 +37,28 @@ int main() std::cin.ignore(std::numeric_limits::max(), '\n'); - Nz::Modules nazara(rendererConfig); + Nz::Application app(rendererConfig); + + auto& windowing = app.AddComponent(); + + auto& ecs = app.AddComponent(); + Nz::Physics2DSystem& physSytem = ecs.AddSystem(); + Nz::RenderSystem& renderSystem = ecs.AddSystem(); + + std::string windowTitle = "Physics 2D"; + Nz::Window& window = windowing.CreateWindow(Nz::VideoMode(1920, 1080, 32), windowTitle); + Nz::WindowSwapchain& windowSwapchain = renderSystem.CreateSwapchain(window); std::shared_ptr device = Nz::Graphics::Instance()->GetRenderDevice(); - entt::registry registry; - - Nz::SystemGraph systemGraph(registry); - Nz::Physics2DSystem& physSytem = systemGraph.AddSystem(); - Nz::RenderSystem& renderSystem = systemGraph.AddSystem(); - - std::string windowTitle = "Graphics Test"; - Nz::RenderWindow& window = renderSystem.CreateWindow(device, Nz::VideoMode(1920, 1080), windowTitle); - Nz::Vector2f windowSize = Nz::Vector2f(window.GetSize()); physSytem.GetPhysWorld().SetGravity({ 0.f, -98.1f }); - entt::entity viewer = registry.create(); + entt::handle viewer = ecs.CreateEntity(); { - registry.emplace(viewer); - auto& cameraComponent = registry.emplace(viewer, window.GetRenderTarget(), Nz::ProjectionType::Orthographic); + viewer.emplace(); + auto& cameraComponent = viewer.emplace(&windowSwapchain.GetSwapchain(), Nz::ProjectionType::Orthographic); cameraComponent.UpdateRenderMask(1); cameraComponent.UpdateClearColor(Nz::Color(0.5f, 0.5f, 0.5f)); } @@ -75,23 +77,23 @@ int main() { for (std::size_t x = 0; x < 30; ++x) { - entt::entity spriteEntity = registry.create(); + entt::handle spriteEntity = ecs.CreateEntity(); { std::shared_ptr sprite = std::make_shared(spriteMaterial); sprite->SetSize({ 32.f, 32.f }); sprite->SetOrigin({ 0.5f, 0.5f }); - registry.emplace(spriteEntity).SetPosition(windowSize.x * 0.5f + x * 48.f - 15.f * 48.f, windowSize.y / 2 + y * 48.f); + spriteEntity.emplace().SetPosition(windowSize.x * 0.5f + x * 48.f - 15.f * 48.f, windowSize.y / 2 + y * 48.f); - registry.emplace(spriteEntity).AttachRenderable(sprite, 1); - auto& rigidBody = registry.emplace(spriteEntity, physSytem.CreateRigidBody(50.f, std::make_shared(Nz::Vector2f(32.f, 32.f)))); + spriteEntity.emplace().AttachRenderable(sprite, 1); + auto& rigidBody = spriteEntity.emplace(physSytem.CreateRigidBody(50.f, std::make_shared(Nz::Vector2f(32.f, 32.f)))); rigidBody.SetFriction(0.9f); //rigidBody.SetElasticity(0.99f); } } } - entt::entity groundEntity = registry.create(); + entt::handle groundEntity = ecs.CreateEntity(); { std::shared_ptr tilemap = std::make_shared(Nz::Vector2ui(40, 20), Nz::Vector2f(64.f, 64.f), 18); tilemap->SetOrigin({ 0.5f, 0.5f }); @@ -111,17 +113,15 @@ int main() } } - registry.emplace(groundEntity).SetPosition(windowSize.x * 0.5f, -windowSize.y * 0.2f); - registry.emplace(groundEntity).AttachRenderable(tilemap, 1); - auto& rigidBody = registry.emplace(groundEntity, physSytem.CreateRigidBody(0.f, std::make_shared(tilemap->GetSize()))); + groundEntity.emplace().SetPosition(windowSize.x * 0.5f, -windowSize.y * 0.2f); + groundEntity.emplace().AttachRenderable(tilemap, 1); + auto& rigidBody = groundEntity.emplace(physSytem.CreateRigidBody(0.f, std::make_shared(tilemap->GetSize()))); rigidBody.SetFriction(0.9f); } Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f); Nz::Quaternionf camQuat(camAngles); - window.EnableEventPolling(true); - Nz::MillisecondClock secondClock; unsigned int fps = 0; @@ -132,39 +132,16 @@ int main() Nz::PidController headingController(0.5f, 0.f, 0.05f); Nz::PidController upController(1.f, 0.f, 0.1f); - bool showColliders = false; - while (window.IsOpen()) + app.AddUpdater([&](Nz::Time elapsedTime) { - Nz::WindowEvent event; - while (window.PollEvent(&event)) - { - switch (event.type) - { - case Nz::WindowEventType::Quit: - window.Close(); - break; - - case Nz::WindowEventType::KeyPressed: - break; - - case Nz::WindowEventType::MouseMoved: - break; - - default: - break; - } - } - - systemGraph.Update(); - fps++; if (secondClock.RestartIfOver(Nz::Time::Second())) { - window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(registry.alive()) + " entities"); + window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(ecs.GetRegistry().alive()) + " entities"); fps = 0; } - } + }); - return EXIT_SUCCESS; + return app.Run(); } diff --git a/examples/PhysicsDemo/main.cpp b/examples/PhysicsDemo/main.cpp index 951a1dda9..92db9b288 100644 --- a/examples/PhysicsDemo/main.cpp +++ b/examples/PhysicsDemo/main.cpp @@ -1,10 +1,11 @@ #include +#include #include #include #include -#include #include #include +#include #include #include #include @@ -36,7 +37,17 @@ int main() std::cin.ignore(std::numeric_limits::max(), '\n'); - Nz::Modules nazara(rendererConfig); + Nz::Application app(rendererConfig); + + auto& windowing = app.AddComponent(); + + auto& ecs = app.AddComponent(); + Nz::Physics3DSystem& physSytem = ecs.AddSystem(); + Nz::RenderSystem& renderSystem = ecs.AddSystem(); + + std::string windowTitle = "Physics 3D"; + Nz::Window& window = windowing.CreateWindow(Nz::VideoMode(1920, 1080, 32), windowTitle); + Nz::WindowSwapchain& windowSwapchain = renderSystem.CreateSwapchain(window); std::shared_ptr device = Nz::Graphics::Instance()->GetRenderDevice(); @@ -94,21 +105,12 @@ int main() Nz::VertexMapper vertexMapper(*spaceshipMesh->GetSubMesh(0)); Nz::SparsePtr vertices = vertexMapper.GetComponentPtr(Nz::VertexComponent::Position); - entt::registry registry; - - Nz::SystemGraph systemGraph(registry); - Nz::Physics3DSystem& physSytem = systemGraph.AddSystem(); - Nz::RenderSystem& renderSystem = systemGraph.AddSystem(); - - std::string windowTitle = "Graphics Test"; - Nz::RenderWindow& window = renderSystem.CreateWindow(device, Nz::VideoMode(1920, 1080, 32), windowTitle); - Nz::Vector2ui windowSize = window.GetSize(); - entt::entity viewer = registry.create(); + entt::handle viewer = ecs.CreateEntity(); { - registry.emplace(viewer); - auto& cameraComponent = registry.emplace(viewer, window.GetRenderTarget()); + viewer.emplace(); + auto& cameraComponent = viewer.emplace(&windowSwapchain.GetSwapchain()); cameraComponent.UpdateRenderMask(1); cameraComponent.UpdateClearColor(Nz::Color(0.5f, 0.5f, 0.5f)); } @@ -136,40 +138,40 @@ int main() colliderModel->SetMaterial(i, colliderMat); } - entt::entity textEntity = registry.create(); + entt::handle textEntity = ecs.CreateEntity(); { - auto& entityGfx = registry.emplace(textEntity); + auto& entityGfx = textEntity.emplace(); entityGfx.AttachRenderable(sprite, 1); - auto& entityNode = registry.emplace(textEntity); + auto& entityNode = textEntity.emplace(); entityNode.SetPosition(0.f, 5.f, 0.f); } - entt::entity playerEntity = registry.create(); + entt::handle playerEntity = ecs.CreateEntity(); - entt::entity headingEntity = registry.create(); + entt::handle headingEntity = ecs.CreateEntity(); { - auto& entityLight = registry.emplace(playerEntity); + auto& entityLight = playerEntity.emplace(); auto& spotLight = entityLight.AddLight(1); spotLight.EnableShadowCasting(true); spotLight.UpdateShadowMapSize(1024); - auto& entityGfx = registry.emplace(playerEntity); + auto& entityGfx = playerEntity.emplace(); entityGfx.AttachRenderable(model, 1); - auto& entityNode = registry.emplace(playerEntity); + auto& entityNode = playerEntity.emplace(); entityNode.SetPosition(Nz::Vector3f(12.5f, 0.f, 25.f)); - auto& entityPhys = registry.emplace(playerEntity, physSytem.CreateRigidBody(shipCollider)); + auto& entityPhys = playerEntity.emplace(physSytem.CreateRigidBody(shipCollider)); entityPhys.SetMass(50.f); entityPhys.SetAngularDamping(Nz::Vector3f::Zero()); - auto& headingNode = registry.emplace(headingEntity); + auto& headingNode = headingEntity.emplace(); headingNode.SetInheritRotation(false); headingNode.SetParent(entityNode); } - registry.get(viewer).SetParent(entt::handle(registry, headingEntity)); - registry.get(viewer).SetPosition(Nz::Vector3f::Backward() * 2.5f + Nz::Vector3f::Up() * 1.f); + viewer.get().SetParent(headingEntity); + viewer.get().SetPosition(Nz::Vector3f::Backward() * 2.5f + Nz::Vector3f::Up() * 1.f); for (std::size_t x = 0; x < 3; ++x) { @@ -177,15 +179,15 @@ int main() { for (std::size_t z = 0; z < 3; ++z) { - entt::entity entity = registry.create(); - auto& entityGfx = registry.emplace(entity); + entt::handle entity = ecs.CreateEntity(); + auto& entityGfx = entity.emplace(); entityGfx.AttachRenderable(model, 1); - auto& entityNode = registry.emplace(entity); + auto& entityNode = entity.emplace(); entityNode.SetPosition(Nz::Vector3f(x * 2.f, y * 1.5f, z * 2.f)); entityNode.SetRotation(Nz::EulerAnglesf(0.f, Nz::TurnAnglef(0.5f), 0.f)); - auto& entityPhys = registry.emplace(entity, physSytem.CreateRigidBody(shipCollider)); + auto& entityPhys = entity.emplace(physSytem.CreateRigidBody(shipCollider)); entityPhys.SetMass(1.f); entityPhys.SetAngularDamping(Nz::Vector3f::Zero()); entityPhys.SetLinearDamping(0.f); @@ -196,8 +198,6 @@ int main() Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f); Nz::Quaternionf camQuat(camAngles); - window.EnableEventPolling(true); - Nz::MillisecondClock updateClock; Nz::MillisecondClock fpsClock; unsigned int fps = 0; @@ -208,82 +208,69 @@ int main() Nz::PidController upController(1.f, 0.f, 0.1f); bool showColliders = false; - while (window.IsOpen()) + + Nz::WindowEventHandler& eventHandler = window.GetEventHandler(); + eventHandler.OnKeyPressed.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent::KeyEvent& event) { - Nz::WindowEvent event; - while (window.PollEvent(&event)) + if (event.virtualKey == Nz::Keyboard::VKey::A) { - switch (event.type) + bool alphaTestEnabled = std::get(*material->GetValueProperty("AlphaTest")); + material->SetValueProperty("AlphaTest", !alphaTestEnabled); + } + else if (event.virtualKey == Nz::Keyboard::VKey::B) + { + showColliders = !showColliders; + if (showColliders) { - case Nz::WindowEventType::Quit: - window.Close(); - break; - - case Nz::WindowEventType::KeyPressed: - if (event.key.virtualKey == Nz::Keyboard::VKey::A) - { - bool alphaTestEnabled = std::get(*material->GetValueProperty("AlphaTest")); - material->SetValueProperty("AlphaTest", !alphaTestEnabled); - } - else if (event.key.virtualKey == Nz::Keyboard::VKey::B) - { - showColliders = !showColliders; - if (showColliders) - { - auto view = registry.view(); - for (auto [entity, gfxComponent, _] : view.each()) - gfxComponent.AttachRenderable(colliderModel, 1); - } - else - { - auto view = registry.view(); - for (auto [entity, gfxComponent, _] : view.each()) - gfxComponent.DetachRenderable(colliderModel); - } - } - else if (event.key.virtualKey == Nz::Keyboard::VKey::Space) - { - entt::entity entity = registry.create(); - auto& entityGfx = registry.emplace(entity); - entityGfx.AttachRenderable(model, 1); - if (showColliders) - entityGfx.AttachRenderable(colliderModel, 1); - - registry.emplace(entity); - - auto& entityPhys = registry.emplace(entity, physSytem.CreateRigidBody(shipCollider)); - entityPhys.SetMass(1.f); - entityPhys.SetAngularDamping(Nz::Vector3f::Zero()); - entityPhys.SetLinearDamping(0.f); - } - - break; - - case Nz::WindowEventType::MouseMoved: - { - float sensitivity = 0.3f; - - camAngles.yaw = camAngles.yaw - event.mouseMove.deltaX * sensitivity; - camAngles.pitch = camAngles.pitch - event.mouseMove.deltaY * sensitivity; - - camAngles.Normalize(); - - camQuat = camAngles; - - registry.get(headingEntity).SetRotation(camQuat); - break; - } - - default: - break; + auto view = ecs.GetRegistry().view(); + for (auto [entity, gfxComponent, _] : view.each()) + gfxComponent.AttachRenderable(colliderModel, 1); + } + else + { + auto view = ecs.GetRegistry().view(); + for (auto [entity, gfxComponent, _] : view.each()) + gfxComponent.DetachRenderable(colliderModel); } } + else if (event.virtualKey == Nz::Keyboard::VKey::Space) + { + entt::handle entity = ecs.CreateEntity(); + auto& entityGfx = entity.emplace(); + entityGfx.AttachRenderable(model, 1); + if (showColliders) + entityGfx.AttachRenderable(colliderModel, 1); + entity.emplace(); + + auto& entityPhys = entity.emplace(physSytem.CreateRigidBody(shipCollider)); + entityPhys.SetMass(1.f); + entityPhys.SetAngularDamping(Nz::Vector3f::Zero()); + entityPhys.SetLinearDamping(0.f); + } + }); + + eventHandler.OnMouseMoved.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event) + { + float sensitivity = 0.3f; + + camAngles.yaw = camAngles.yaw - event.deltaX * sensitivity; + camAngles.pitch = camAngles.pitch - event.deltaY * sensitivity; + + camAngles.Normalize(); + + camQuat = camAngles; + + headingEntity.get().SetRotation(camQuat); + }); + + app.AddUpdater([&](Nz::Time elapsedTime) + { if (std::optional deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60))) { float elapsedTime = deltaTime->AsSeconds(); - auto spaceshipView = registry.view(); + auto spaceshipView = ecs.GetRegistry().view(); for (auto&& [entity, node, _] : spaceshipView.each()) { if (entity == playerEntity) @@ -291,17 +278,17 @@ int main() Nz::Vector3f spaceshipPos = node.GetPosition(Nz::CoordSys::Global); if (spaceshipPos.GetSquaredLength() > Nz::IntegralPow(20.f, 2)) - registry.destroy(entity); + ecs.GetRegistry().destroy(entity); } - Nz::RigidBody3DComponent& playerShipBody = registry.get(playerEntity); + Nz::RigidBody3DComponent& playerShipBody = playerEntity.get(); Nz::Quaternionf currentRotation = playerShipBody.GetRotation(); - Nz::Vector3f desiredHeading = registry.get(headingEntity).GetForward(); + Nz::Vector3f desiredHeading = headingEntity.get().GetForward(); Nz::Vector3f currentHeading = currentRotation * Nz::Vector3f::Forward(); Nz::Vector3f headingError = currentHeading.CrossProduct(desiredHeading); - Nz::Vector3f desiredUp = registry.get(headingEntity).GetUp(); + Nz::Vector3f desiredUp = headingEntity.get().GetUp(); Nz::Vector3f currentUp = currentRotation * Nz::Vector3f::Up(); Nz::Vector3f upError = currentUp.CrossProduct(desiredUp); @@ -329,16 +316,14 @@ int main() playerShipBody.AddForce(Nz::Vector3f::Down() * 3.f * mass, Nz::CoordSys::Local); } - systemGraph.Update(); - fps++; if (fpsClock.RestartIfOver(Nz::Time::Second())) { - window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(registry.alive()) + " entities"); + window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(ecs.GetRegistry().alive()) + " entities"); fps = 0; } - } + }); - return EXIT_SUCCESS; + return app.Run(); } diff --git a/examples/Showcase/main.cpp b/examples/Showcase/main.cpp index 77e45cbed..9591066c8 100644 --- a/examples/Showcase/main.cpp +++ b/examples/Showcase/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -37,47 +38,49 @@ int main() else rendererConfig.preferredAPI = Nz::RenderAPI::OpenGL; - Nz::Modules nazara(rendererConfig); + Nz::Application app(rendererConfig); Nz::PluginLoader loader; Nz::Plugin assimp = loader.Load(); std::shared_ptr device = Nz::Graphics::Instance()->GetRenderDevice(); - entt::registry registry; - Nz::SystemGraph systemGraph(registry); - systemGraph.AddSystem(); + auto& ecs = app.AddComponent(); + ecs.AddSystem(); - Nz::Physics3DSystem& physSytem = systemGraph.AddSystem(); - Nz::RenderSystem& renderSystem = systemGraph.AddSystem(); + Nz::Physics3DSystem& physSytem = ecs.AddSystem(); + Nz::RenderSystem& renderSystem = ecs.AddSystem(); + + auto& windowing = app.AddComponent(); std::string windowTitle = "Skinning test"; - Nz::RenderWindow& window = renderSystem.CreateWindow(device, Nz::VideoMode(1280, 720, 32), windowTitle); + Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1280, 720), windowTitle); + auto& windowSwapchain = renderSystem.CreateSwapchain(mainWindow); physSytem.GetPhysWorld().SetGravity({ 0.f, -9.81f, 0.f }); Nz::TextureParams texParams; texParams.renderDevice = device; - entt::entity playerEntity = registry.create(); - entt::entity playerRotation = registry.create(); - entt::entity playerCamera = registry.create(); + entt::handle playerEntity = ecs.CreateEntity(); + entt::handle playerRotation = ecs.CreateEntity(); + entt::handle playerCamera = ecs.CreateEntity(); { - auto& playerNode = registry.emplace(playerEntity); + auto& playerNode = playerEntity.emplace(); playerNode.SetPosition(0.f, 1.8f, 1.f); - auto& playerBody = registry.emplace(playerEntity, &physSytem.GetPhysWorld()); + auto& playerBody = playerEntity.emplace(&physSytem.GetPhysWorld()); playerBody.SetMass(42.f); playerBody.SetGeom(std::make_shared(Nz::Vector3f::Unit())); - auto& playerRotNode = registry.emplace(playerRotation); + auto& playerRotNode = playerRotation.emplace(); playerRotNode.SetParent(playerNode); - auto& cameraNode = registry.emplace(playerCamera); + auto& cameraNode = playerCamera.emplace(); cameraNode.SetPosition(Nz::Vector3f::Up() * 2.f + Nz::Vector3f::Backward()); //cameraNode.SetParent(playerRotNode); - auto& cameraComponent = registry.emplace(playerCamera, window.GetRenderTarget()); + auto& cameraComponent = playerCamera.emplace(&windowSwapchain.GetSwapchain()); cameraComponent.UpdateZNear(0.2f); cameraComponent.UpdateZFar(10000.f); cameraComponent.UpdateRenderMask(1); @@ -163,14 +166,14 @@ int main() } }*/ - entt::handle bobEntity = entt::handle(registry, registry.create()); - entt::entity lightEntity1 = registry.create(); + entt::handle bobEntity = ecs.CreateEntity(); + entt::handle lightEntity1 = ecs.CreateEntity(); { - auto& lightNode = registry.emplace(lightEntity1); + auto& lightNode = lightEntity1.emplace(); lightNode.SetPosition(Nz::Vector3f::Up() * 3.f + Nz::Vector3f::Backward() * 1.f); lightNode.SetRotation(Nz::EulerAnglesf(-70.f, 0.f, 0.f)); - auto& cameraLight = registry.emplace(lightEntity1); + auto& cameraLight = lightEntity1.emplace(); auto& spotLight = cameraLight.AddLight(0xFFFFFFFF); spotLight.UpdateColor(Nz::Color::Red()); @@ -179,20 +182,21 @@ int main() spotLight.EnableShadowCasting(true); } - entt::entity lightEntity2 = registry.create(); + entt::handle lightEntity2 = ecs.CreateEntity(); { - auto& lightNode = registry.emplace(lightEntity2); + auto& lightNode = lightEntity2.emplace(); lightNode.SetPosition(Nz::Vector3f::Up() * 3.5f + Nz::Vector3f::Right() * 1.5f); lightNode.SetRotation(Nz::EulerAnglesf(-70.f, 90.f, 0.f)); - auto& cameraLight = registry.emplace(lightEntity2); + auto& cameraLight = lightEntity2.emplace(); auto& spotLight = cameraLight.AddLight(0xFFFFFFFF); spotLight.UpdateColor(Nz::Color::Green()); spotLight.EnableShadowCasting(true); spotLight.UpdateShadowMapSize(1024); } - entt::entity lightEntity3 = registry.create(); + + entt::handle lightEntity3 = ecs.CreateEntity(); { auto& bobNode = bobEntity.emplace(); @@ -205,8 +209,7 @@ int main() auto& sharedSkeleton = bobEntity.emplace(skeleton); - - entt::entity sphereEntity = registry.create(); + entt::handle sphereEntity = ecs.CreateEntity(); { std::shared_ptr sphereMesh = std::make_shared(); sphereMesh->CreateStatic(); @@ -227,35 +230,35 @@ int main() for (std::size_t i = 0; i < sphereModel->GetSubMeshCount(); ++i) sphereModel->SetMaterial(i, sphereMat); - auto& sphereNode = registry.emplace(sphereEntity); + auto& sphereNode = sphereEntity.emplace(); sphereNode.SetScale(0.1f); sphereNode.SetInheritScale(false); sphereNode.SetParentJoint(bobEntity, "RightHand"); - auto& sphereBody = registry.emplace(sphereEntity, &physSytem.GetPhysWorld()); + auto& sphereBody = sphereEntity.emplace(&physSytem.GetPhysWorld()); sphereBody.SetGeom(std::make_shared(0.1f)); - auto& sphereGfx = registry.emplace(sphereEntity); + auto& sphereGfx = sphereEntity.emplace(); sphereGfx.AttachRenderable(sphereModel, 0xFFFFFFFF); } - entt::entity smallBobEntity = registry.create(); - auto& smallBobNode = registry.emplace(smallBobEntity); + entt::handle smallBobEntity = ecs.CreateEntity(); + auto& smallBobNode = smallBobEntity.emplace(); smallBobNode.SetParentJoint(bobEntity, "LeftHand"); - auto& smallBobGfx = registry.emplace(smallBobEntity); + auto& smallBobGfx = smallBobEntity.emplace(); smallBobGfx.AttachRenderable(bobModel, 0xFFFFFFFF); - registry.emplace(smallBobEntity, skeleton); + smallBobEntity.emplace(skeleton); { - auto& lightNode = registry.emplace(lightEntity3); + auto& lightNode = lightEntity3.emplace(); //lightNode.SetPosition(Nz::Vector3f::Up() * 4.f); lightNode.SetPosition(Nz::Vector3f::Down() * 7.5f + Nz::Vector3f::Backward() * 2.5f); //lightNode.SetRotation(Nz::EulerAnglesf(-45.f, 180.f, 0.f)); lightNode.SetParentJoint(bobEntity, "Spine2"); - auto& cameraLight = registry.emplace(lightEntity3); + auto& cameraLight = lightEntity3.emplace(); auto& pointLight = cameraLight.AddLight(0xFFFFFFFF); pointLight.UpdateColor(Nz::Color::Blue()); @@ -294,17 +297,17 @@ int main() sprite->UpdateRenderLayer(1); sprite->Update(Nz::SimpleTextDrawer::Draw("Shadow-mapping !", 72), 0.002f); - entt::entity textEntity = registry.create(); + entt::handle textEntity = ecs.CreateEntity(); { - auto& entityGfx = registry.emplace(textEntity); + auto& entityGfx = textEntity.emplace(); entityGfx.AttachRenderable(sprite, 1); - auto& entityNode = registry.emplace(textEntity); + auto& entityNode = textEntity.emplace(); entityNode.SetPosition(Nz::Vector3f::Up() * 0.5f + Nz::Vector3f::Backward() * 0.66f + Nz::Vector3f::Left() * 0.5f); entityNode.SetRotation(Nz::EulerAnglesf(-45.f, 0.f, 0.f)); } - entt::entity planeEntity = registry.create(); + entt::handle planeEntity = ecs.CreateEntity(); Nz::Boxf floorBox; { Nz::MeshParams meshPrimitiveParams; @@ -332,12 +335,12 @@ int main() std::shared_ptr planeModel = std::make_shared(std::move(planeMeshGfx), planeMesh.GetAABB()); planeModel->SetMaterial(0, planeMat); - auto& planeGfx = registry.emplace(planeEntity); + auto& planeGfx = planeEntity.emplace(); planeGfx.AttachRenderable(planeModel, 0xFFFFFFFF); - registry.emplace(planeEntity); + planeEntity.emplace(); - auto& planeBody = registry.emplace(planeEntity, &physSytem.GetPhysWorld()); + auto& planeBody = planeEntity.emplace(&physSytem.GetPhysWorld()); planeBody.SetGeom(std::make_shared(Nz::Vector3f(planeSize.x, 0.5f, planeSize.y), Nz::Vector3f(0.f, -0.25f, 0.f))); Nz::Mesh boxMesh; @@ -350,13 +353,11 @@ int main() std::shared_ptr boxModel = std::make_shared(std::move(boxMeshGfx), boxMesh.GetAABB()); boxModel->SetMaterial(0, planeMat); - entt::entity boxEntity = registry.create(); - registry.emplace(boxEntity).SetPosition(Nz::Vector3f(0.f, 0.25f, -0.5f)); - registry.emplace(boxEntity).AttachRenderable(boxModel, 0xFFFFFFFF); + entt::handle boxEntity = ecs.CreateEntity(); + boxEntity.emplace().SetPosition(Nz::Vector3f(0.f, 0.25f, -0.5f)); + boxEntity.emplace().AttachRenderable(boxModel, 0xFFFFFFFF); } - window.EnableEventPolling(true); - Nz::MillisecondClock fpsClock, updateClock; float incr = 0.f; unsigned int currentFrame = 0; @@ -365,49 +366,33 @@ int main() Nz::UInt64 fps = 0; bool paused = false; - while (window.IsOpen()) + Nz::WindowEventHandler& eventHandler = mainWindow.GetEventHandler(); + eventHandler.OnKeyPressed.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent::KeyEvent& event) { - Nz::WindowEvent event; - while (window.PollEvent(&event)) - { - switch (event.type) - { - case Nz::WindowEventType::Quit: - window.Close(); - break; + if (event.virtualKey == Nz::Keyboard::VKey::P) + paused = !paused; + }); - case Nz::WindowEventType::KeyPressed: - { - if (event.type == Nz::WindowEventType::KeyPressed && event.key.virtualKey == Nz::Keyboard::VKey::P) - paused = !paused; + eventHandler.OnMouseMoved.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event) + { + // Gestion de la caméra free-fly (Rotation) + float sensitivity = 0.3f; // Sensibilité de la souris - break; - } + // On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris + camAngles.yaw = camAngles.yaw - event.deltaX * sensitivity; + camAngles.yaw.Normalize(); - case Nz::WindowEventType::MouseMoved: - { - // Gestion de la caméra free-fly (Rotation) - float sensitivity = 0.3f; // Sensibilité de la souris + // Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles + camAngles.pitch = Nz::Clamp(camAngles.pitch - event.deltaY * sensitivity, -89.f, 89.f); - // On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris - camAngles.yaw = camAngles.yaw - event.mouseMove.deltaX * sensitivity; - camAngles.yaw.Normalize(); - - // Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles - camAngles.pitch = Nz::Clamp(camAngles.pitch - event.mouseMove.deltaY * sensitivity, -89.f, 89.f); - - /*auto& playerRotNode = registry.get(playerRotation); - playerRotNode.SetRotation(camAngles);*/ - auto& playerRotNode = registry.get(playerCamera); - playerRotNode.SetRotation(camAngles); - break; - } - - default: - break; - } - } + /*auto& playerRotNode = registry.get(playerRotation); + playerRotNode.SetRotation(camAngles);*/ + auto& playerRotNode = playerCamera.get(); + playerRotNode.SetRotation(camAngles); + }); + app.AddUpdater([&](Nz::Time elapsedTime) + { if (std::optional deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60))) { float updateTime = deltaTime->AsSeconds(); @@ -433,7 +418,7 @@ int main() float cameraSpeed = 2.f; - auto& cameraNode = registry.get(playerCamera); + auto& cameraNode = playerCamera.get(); if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space)) cameraNode.Move(Nz::Vector3f::Up() * cameraSpeed * updateTime, Nz::CoordSys::Global); @@ -517,9 +502,9 @@ int main() playerShipBody.AddForce(Nz::Vector3f::Down() * 3.f * mass, Nz::CoordSys::Local);*/ } - + Nz::DebugDrawer& debugDrawer = renderSystem.GetFramePipeline().GetDebugDrawer(); - auto& lightNode = registry.get(lightEntity3); + auto& lightNode = lightEntity3.get(); //debugDrawer.DrawLine(lightNode.GetPosition(Nz::CoordSys::Global), lightNode.GetForward() * 10.f, Nz::Color::Blue()); Nz::Vector3f pos = lightNode.GetPosition(Nz::CoordSys::Global); debugDrawer.DrawBox(Nz::Boxf(pos.x - 0.05f, pos.y - 0.05f, pos.z - 0.05f, 0.1f, 0.1f, 0.1f), Nz::Color::Blue()); @@ -528,16 +513,16 @@ int main() if (floorBox.Intersect(test, &intersection)) debugDrawer.DrawBox(intersection, Nz::Color::Green());*/ - systemGraph.Update(); - fps++; if (fpsClock.RestartIfOver(Nz::Time::Second())) { - window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(registry.alive()) + " entities"); + mainWindow.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(ecs.GetRegistry().alive()) + " entities"); fps = 0; } - } + }); + + return app.Run(); return EXIT_SUCCESS; } diff --git a/examples/Tut00/main.cpp b/examples/Tut00/main.cpp index 4453a1c63..0edf08690 100644 --- a/examples/Tut00/main.cpp +++ b/examples/Tut00/main.cpp @@ -11,8 +11,8 @@ int main(int argc, char* argv[]) { // This "example" has only one purpose: Giving an empty project for you to test whatever you want - // If you wish to have multiple test projects, you only have to copy/paste this directory and change the name in the build.lua - Nz::Modules nazara; + // If you wish to have multiple test projects, you only have to copy/paste this directory and change the name in the xmake.lua + Nz::Application app; return EXIT_SUCCESS; } diff --git a/examples/Tut01/main.cpp b/examples/Tut01/main.cpp index 5cd9fa069..77078ac57 100644 --- a/examples/Tut01/main.cpp +++ b/examples/Tut01/main.cpp @@ -10,17 +10,14 @@ #include #include #include -#include -#include #include -#include int main() { Nz::Application app; auto& windowing = app.AddComponent(); - Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1280, 720), "Hello world"); + Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1280, 720), "Tut01 - Hello world"); auto& ecs = app.AddComponent(); diff --git a/examples/Tut02/main.cpp b/examples/Tut02/main.cpp index 82896c5fc..375aa2d79 100644 --- a/examples/Tut02/main.cpp +++ b/examples/Tut02/main.cpp @@ -1,30 +1,34 @@ // Sources pour https://github.com/NazaraEngine/NazaraEngine/wiki/(FR)-Tutoriel:-%5B02%5D-Gestion-des-événements +#include +#include #include #include #include #include +#include #include #include #include -#include -#include #include -#include int main() { - Nz::Modules nazara; + Nz::Application app; - entt::registry registry; - Nz::SystemGraph systemGraph(registry); - Nz::RenderSystem& renderSystem = systemGraph.AddSystem(); - Nz::RenderWindow& mainWindow = renderSystem.CreateWindow(Nz::Graphics::Instance()->GetRenderDevice(), Nz::VideoMode(1280, 720), "Tut02 - Events"); + auto& windowing = app.AddComponent(); + Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1280, 720), "Tut02 - Hello world"); - entt::entity cameraEntity = registry.create(); + auto& ecs = app.AddComponent(); + + Nz::RenderSystem& renderSystem = ecs.AddSystem(); + auto& windowSwapchain = renderSystem.CreateSwapchain(mainWindow); + + entt::handle cameraEntity = ecs.CreateEntity(); { - registry.emplace(cameraEntity); - auto& cameraComponent = registry.emplace(cameraEntity, mainWindow.GetRenderTarget(), Nz::ProjectionType::Orthographic); + cameraEntity.emplace(); + + auto& cameraComponent = cameraEntity.emplace(&windowSwapchain.GetSwapchain(), Nz::ProjectionType::Orthographic); cameraComponent.UpdateClearColor(Nz::Color(0.46f, 0.48f, 0.84f, 1.f)); } @@ -36,10 +40,10 @@ int main() std::shared_ptr textSprite = std::make_shared(); textSprite->Update(textDrawer); - entt::entity textEntity = registry.create(); + entt::handle textEntity = ecs.CreateEntity(); { - auto& nodeComponent = registry.emplace(textEntity); - auto& gfxComponent = registry.emplace(textEntity); + auto& nodeComponent = textEntity.emplace(); + auto& gfxComponent = textEntity.emplace(); gfxComponent.AttachRenderable(textSprite, 0xFFFFFFFF); Nz::Boxf textBox = textSprite->GetAABB(); @@ -47,8 +51,8 @@ int main() nodeComponent.SetPosition(windowSize.x / 2 - textBox.width / 2, windowSize.y / 2 - textBox.height / 2); } - Nz::EventHandler& eventHandler = mainWindow.GetEventHandler(); - eventHandler.OnKeyPressed.Connect([&](const Nz::EventHandler*, const Nz::WindowEvent::KeyEvent& e) + Nz::WindowEventHandler& eventHandler = mainWindow.GetEventHandler(); + eventHandler.OnKeyPressed.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent::KeyEvent& e) { textDrawer.SetText("You pressed " + Nz::Keyboard::GetKeyName(e.virtualKey)); textSprite->Update(textDrawer); @@ -56,7 +60,7 @@ int main() Nz::Boxf textBox = textSprite->GetAABB(); Nz::Vector2ui windowSize = mainWindow.GetSize(); - auto& nodeComponent = registry.get(textEntity); + auto& nodeComponent = textEntity.get(); nodeComponent.SetPosition(windowSize.x / 2 - textBox.width / 2, windowSize.y / 2 - textBox.height / 2); // Profitons-en aussi pour nous donner un moyen de quitter le programme @@ -64,11 +68,5 @@ int main() mainWindow.Close(); // Cette ligne casse la boucle de la fenêtre }); - while (mainWindow.IsOpen()) - { - mainWindow.ProcessEvents(); - systemGraph.Update(); - } - - return EXIT_SUCCESS; + return app.Run(); } diff --git a/examples/WidgetDemo/main.cpp b/examples/WidgetDemo/main.cpp index aeb78a9e2..fd6b87cd9 100644 --- a/examples/WidgetDemo/main.cpp +++ b/examples/WidgetDemo/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -37,18 +38,17 @@ int main() std::cin.ignore(std::numeric_limits::max(), '\n'); - Nz::Modules nazara(rendererConfig); + Nz::Application app(rendererConfig); - std::shared_ptr device = Nz::Graphics::Instance()->GetRenderDevice(); + auto& windowing = app.AddComponent(); + Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1920, 1080), "Widget demo"); - std::string windowTitle = "Widget Test"; + auto& ecs = app.AddComponent(); - entt::registry registry; - Nz::SystemGraph systemGraph(registry); - Nz::RenderSystem& renderSystem = systemGraph.AddSystem(); - Nz::RenderWindow& mainWindow = renderSystem.CreateWindow(device, Nz::VideoMode(1920, 1080), windowTitle); + Nz::RenderSystem& renderSystem = ecs.AddSystem(); + auto& windowSwapchain = renderSystem.CreateSwapchain(mainWindow); - Nz::Canvas canvas2D(registry, mainWindow.GetEventHandler(), mainWindow.GetCursorController().CreateHandle(), 0xFFFFFFFF); + Nz::Canvas canvas2D(ecs.GetRegistry(), mainWindow.GetEventHandler(), mainWindow.GetCursorController().CreateHandle(), 0xFFFFFFFF); canvas2D.Resize(Nz::Vector2f(mainWindow.GetSize())); Nz::LabelWidget* labelWidget = canvas2D.Add(); @@ -72,7 +72,7 @@ int main() samplerInfo.anisotropyLevel = 8; Nz::TextureParams texParams; - texParams.renderDevice = device; + texParams.renderDevice = Nz::Graphics::Instance()->GetRenderDevice(); texParams.loadFormat = Nz::PixelFormat::RGBA8_SRGB; std::shared_ptr materialInstance = material->Instantiate(); @@ -118,44 +118,13 @@ int main() textAreaWidget2->SetBackgroundColor(Nz::Color::White()); textAreaWidget2->SetTextColor(Nz::Color::Black());*/ - entt::entity viewer2D = registry.create(); + entt::handle viewer2D = ecs.CreateEntity(); { - registry.emplace(viewer2D); - auto& cameraComponent = registry.emplace(viewer2D, mainWindow.GetRenderTarget(), Nz::ProjectionType::Orthographic); - cameraComponent.UpdateClearColor(Nz::Color(0.678f, 0.847f, 0.9f, 1.f)); + viewer2D.emplace(); + + auto& cameraComponent = viewer2D.emplace(&windowSwapchain.GetSwapchain(), Nz::ProjectionType::Orthographic); + cameraComponent.UpdateClearColor(Nz::Color(0.46f, 0.48f, 0.84f, 1.f)); } - mainWindow.EnableEventPolling(true); - - Nz::MillisecondClock fpsClock; - unsigned int fps = 0; - - while (mainWindow.IsOpen()) - { - Nz::WindowEvent event; - while (mainWindow.PollEvent(&event)) - { - switch (event.type) - { - case Nz::WindowEventType::Quit: - mainWindow.Close(); - break; - - default: - break; - } - } - - systemGraph.Update(); - - fps++; - - if (fpsClock.RestartIfOver(Nz::Time::Second())) - { - mainWindow.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(registry.alive()) + " entities"); - fps = 0; - } - } - - return EXIT_SUCCESS; + return app.Run(); } diff --git a/tests/ComputeTest/main.cpp b/tests/ComputeTest/main.cpp index 970c76c89..1766368bf 100644 --- a/tests/ComputeTest/main.cpp +++ b/tests/ComputeTest/main.cpp @@ -6,10 +6,7 @@ #include #include #include -#include -#include #include -#include NAZARA_REQUEST_DEDICATED_GPU() @@ -123,13 +120,13 @@ int main() }); std::string windowTitle = "Compute test"; - - Nz::RenderWindow window; - if (!window.Create(device, Nz::VideoMode(1280, 720, 32), windowTitle)) + Nz::Window window; + if (!window.Create(Nz::VideoMode(1280, 720), windowTitle)) { std::cout << "Failed to create Window" << std::endl; std::abort(); } + Nz::WindowSwapchain windowSwapchain(device, window); Nz::Vector2ui windowSize = window.GetSize(); constexpr float textureSize = 512.f; @@ -146,7 +143,7 @@ int main() { window.ProcessEvents(); - Nz::RenderFrame frame = window.AcquireFrame(); + Nz::RenderFrame frame = windowSwapchain.AcquireFrame(); if (!frame) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); @@ -168,7 +165,7 @@ int main() } } - const Nz::RenderTarget* windowRT = window.GetRenderTarget(); + const Nz::RenderTarget* windowRT = &windowSwapchain.GetSwapchain(); frame.Execute([&](Nz::CommandBufferBuilder& builder) { builder.BeginDebugRegion("Compute part", Nz::Color::Blue()); diff --git a/tests/GraphicsTest/main.cpp b/tests/GraphicsTest/main.cpp index 92c6459f5..422eb8d9f 100644 --- a/tests/GraphicsTest/main.cpp +++ b/tests/GraphicsTest/main.cpp @@ -25,8 +25,6 @@ int main() Nz::Modules nazara(rendererConfig); - Nz::RenderWindow window; - Nz::MeshParams meshParams; meshParams.center = true; meshParams.vertexRotation = Nz::EulerAnglesf(0.f, -90.f, 0.f); @@ -36,11 +34,13 @@ int main() std::shared_ptr device = Nz::Graphics::Instance()->GetRenderDevice(); std::string windowTitle = "Graphics Test"; - if (!window.Create(device, Nz::VideoMode(1920, 1080, 32), windowTitle)) + Nz::Window window; + if (!window.Create(Nz::VideoMode(1280, 720), windowTitle)) { std::cout << "Failed to create Window" << std::endl; - return __LINE__; + std::abort(); } + Nz::WindowSwapchain windowSwapchain(device, window); std::shared_ptr spaceshipMesh = Nz::Mesh::LoadFromFile(resourceDir / "Spaceship/spaceship.obj", meshParams); if (!spaceshipMesh) @@ -80,7 +80,7 @@ int main() Nz::Vector2ui windowSize = window.GetSize(); - Nz::Camera camera(window.GetRenderTarget()); + Nz::Camera camera(&windowSwapchain.GetSwapchain()); camera.UpdateClearColor(Nz::Color::Gray()); Nz::ViewerInstance& viewerInstance = camera.GetViewerInstance(); @@ -114,72 +114,71 @@ int main() Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f); Nz::Quaternionf camQuat(camAngles); - window.EnableEventPolling(true); - Nz::MillisecondClock updateClock; Nz::MillisecondClock fpsClock; unsigned int fps = 0; Nz::Mouse::SetRelativeMouseMode(true); + window.GetEventHandler().OnEvent.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent& event) + { + switch (event.type) + { + case Nz::WindowEventType::Quit: + window.Close(); + break; + + case Nz::WindowEventType::KeyPressed: + if (event.key.virtualKey == Nz::Keyboard::VKey::A) + { + for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i) + model.SetMaterial(i, materialInstance); + } + else if (event.key.virtualKey == Nz::Keyboard::VKey::B) + { + for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i) + model.SetMaterial(i, materialInstance2); + } + else if (event.key.virtualKey == Nz::Keyboard::VKey::Space) + { + modelInstance->UpdateWorldMatrix(Nz::Matrix4f::Translate(viewerPos)); + } + + break; + + case Nz::WindowEventType::MouseMoved: // La souris a bougé + { + // Gestion de la caméra free-fly (Rotation) + float sensitivity = 0.3f; // Sensibilité de la souris + + // On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris + camAngles.yaw = camAngles.yaw - event.mouseMove.deltaX * sensitivity; + camAngles.yaw.Normalize(); + + // Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles + camAngles.pitch = Nz::Clamp(camAngles.pitch - event.mouseMove.deltaY*sensitivity, -89.f, 89.f); + + camQuat = camAngles; + light->UpdateRotation(camQuat); + break; + } + + case Nz::WindowEventType::Resized: + { + Nz::Vector2ui newWindowSize = window.GetSize(); + viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newWindowSize.x) / newWindowSize.y, 0.1f, 1000.f)); + viewerInstance.UpdateTargetSize(Nz::Vector2f(newWindowSize)); + break; + } + + default: + break; + } + }); + while (window.IsOpen()) { - Nz::WindowEvent event; - while (window.PollEvent(&event)) - { - switch (event.type) - { - case Nz::WindowEventType::Quit: - window.Close(); - break; - - case Nz::WindowEventType::KeyPressed: - if (event.key.virtualKey == Nz::Keyboard::VKey::A) - { - for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i) - model.SetMaterial(i, materialInstance); - } - else if (event.key.virtualKey == Nz::Keyboard::VKey::B) - { - for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i) - model.SetMaterial(i, materialInstance2); - } - else if (event.key.virtualKey == Nz::Keyboard::VKey::Space) - { - modelInstance->UpdateWorldMatrix(Nz::Matrix4f::Translate(viewerPos)); - } - - break; - - case Nz::WindowEventType::MouseMoved: // La souris a bougé - { - // Gestion de la caméra free-fly (Rotation) - float sensitivity = 0.3f; // Sensibilité de la souris - - // On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris - camAngles.yaw = camAngles.yaw - event.mouseMove.deltaX * sensitivity; - camAngles.yaw.Normalize(); - - // Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles - camAngles.pitch = Nz::Clamp(camAngles.pitch - event.mouseMove.deltaY*sensitivity, -89.f, 89.f); - - camQuat = camAngles; - light->UpdateRotation(camQuat); - break; - } - - case Nz::WindowEventType::Resized: - { - Nz::Vector2ui newWindowSize = window.GetSize(); - viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newWindowSize.x) / newWindowSize.y, 0.1f, 1000.f)); - viewerInstance.UpdateTargetSize(Nz::Vector2f(newWindowSize)); - break; - } - - default: - break; - } - } + Nz::Window::ProcessEvents(); if (std::optional deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60))) { @@ -211,7 +210,7 @@ int main() light->UpdatePosition(viewerPos); } - Nz::RenderFrame frame = window.AcquireFrame(); + Nz::RenderFrame frame = windowSwapchain.AcquireFrame(); if (!frame) { std::this_thread::sleep_for(std::chrono::milliseconds(1));