refactor client main

This commit is contained in:
2024-07-23 01:17:42 +02:00
parent acbc00c6c6
commit 5a16af2696

View File

@@ -1,16 +1,17 @@
#include <iostream> #include <iostream>
#include "Network.h"
#include <Nazara/Core.hpp> #include <Nazara/Core.hpp>
#include <Nazara/Graphics.hpp> #include <Nazara/Graphics.hpp>
#include <Nazara/Renderer.hpp>
#include <Nazara/Platform.hpp>
#include <Nazara/Physics3D.hpp> #include <Nazara/Physics3D.hpp>
#include <Nazara/Platform.hpp>
#include <Nazara/Renderer.hpp>
#include <Nazara/Widgets.hpp>
#include <random> #include <random>
static Nz::Vector3f Get2DDirectionVectorFromRotation(float yaw) constexpr Nz::UInt32 RenderMaskUI = 0x00010000;
{ constexpr Nz::UInt32 RenderMask3D = 0x0000FFFF;
static Nz::Vector3f Get2DDirectionVectorFromRotation(float yaw) {
return { return {
-std::sin(yaw), -std::sin(yaw),
0, 0,
@@ -18,15 +19,14 @@ static Nz::Vector3f Get2DDirectionVectorFromRotation(float yaw)
}; };
} }
static void CreateLight(Nz::EnttWorld &world) static void CreateLight(Nz::EnttWorld& world) {
{
entt::handle lightEntity = world.CreateEntity(); entt::handle lightEntity = world.CreateEntity();
{ {
auto &lightNode = lightEntity.emplace<Nz::NodeComponent>(); auto& lightNode = lightEntity.emplace<Nz::NodeComponent>();
lightNode.SetPosition({0, 5, 0}); lightNode.SetPosition({0, 5, 0});
auto &entityLight = lightEntity.emplace<Nz::LightComponent>(); auto& entityLight = lightEntity.emplace<Nz::LightComponent>();
auto &spotLight = entityLight.AddLight<Nz::PointLight>(1); auto& spotLight = entityLight.AddLight<Nz::PointLight>(RenderMask3D);
spotLight.EnableShadowCasting(true); spotLight.EnableShadowCasting(true);
spotLight.UpdateShadowMapSize(1024); spotLight.UpdateShadowMapSize(1024);
spotLight.UpdateRadius(10.0f); spotLight.UpdateRadius(10.0f);
@@ -34,8 +34,7 @@ static void CreateLight(Nz::EnttWorld &world)
} }
} }
static void CreateBoxes(Nz::EnttWorld &world) static void CreateBoxes(Nz::EnttWorld& world) {
{
constexpr float BoxDims = 1.f; constexpr float BoxDims = 1.f;
std::mt19937 rd(42); std::mt19937 rd(42);
@@ -46,8 +45,7 @@ static void CreateBoxes(Nz::EnttWorld &world)
std::shared_ptr<Nz::GraphicalMesh> boxMesh = Nz::GraphicalMesh::Build(Nz::Primitive::Box(Nz::Vector3f(1.f))); std::shared_ptr<Nz::GraphicalMesh> boxMesh = Nz::GraphicalMesh::Build(Nz::Primitive::Box(Nz::Vector3f(1.f)));
constexpr std::size_t BoxCount = 100; constexpr std::size_t BoxCount = 100;
for (std::size_t i = 0; i < BoxCount; ++i) for (std::size_t i = 0; i < BoxCount; ++i) {
{
float width = lengthDis(rd); float width = lengthDis(rd);
float height = lengthDis(rd); float height = lengthDis(rd);
float depth = lengthDis(rd); float depth = lengthDis(rd);
@@ -64,9 +62,9 @@ static void CreateBoxes(Nz::EnttWorld &world)
std::shared_ptr<Nz::Model> sphereModel = std::make_shared<Nz::Model>(boxMesh); std::shared_ptr<Nz::Model> sphereModel = std::make_shared<Nz::Model>(boxMesh);
sphereModel->SetMaterial(0, std::move(boxMaterial)); sphereModel->SetMaterial(0, std::move(boxMaterial));
boxEntity.emplace<Nz::GraphicsComponent>(std::move(sphereModel)); boxEntity.emplace<Nz::GraphicsComponent>(std::move(sphereModel), RenderMask3D);
auto &ballNode = boxEntity.emplace<Nz::NodeComponent>(); auto& ballNode = boxEntity.emplace<Nz::NodeComponent>();
ballNode.SetPosition({xRandom(rd), yRandom(rd) + 20.0f, zRandom(rd)}); ballNode.SetPosition({xRandom(rd), yRandom(rd) + 20.0f, zRandom(rd)});
ballNode.SetScale({width, height, depth}); ballNode.SetScale({width, height, depth});
@@ -80,8 +78,7 @@ static void CreateBoxes(Nz::EnttWorld &world)
} }
} }
static void CreateModel(Nz::EnttWorld &world) static void CreateModel(Nz::EnttWorld& world) {
{
std::filesystem::path resourceDir = "assets/models"; std::filesystem::path resourceDir = "assets/models";
Nz::MeshParams meshParams; Nz::MeshParams meshParams;
@@ -91,8 +88,7 @@ static void CreateModel(Nz::EnttWorld &world)
meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV_Tangent); meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV_Tangent);
std::shared_ptr<Nz::Mesh> deambuMesh = Nz::Mesh::LoadFromFile(resourceDir / "sol.obj", meshParams); std::shared_ptr<Nz::Mesh> deambuMesh = Nz::Mesh::LoadFromFile(resourceDir / "sol.obj", meshParams);
if (!deambuMesh) if (!deambuMesh) {
{
NazaraError("failed to load model"); NazaraError("failed to load model");
return; return;
} }
@@ -102,22 +98,21 @@ static void CreateModel(Nz::EnttWorld &world)
entt::handle deambEntity = world.CreateEntity(); entt::handle deambEntity = world.CreateEntity();
{ {
auto &entityGfx = deambEntity.emplace<Nz::GraphicsComponent>(); auto& entityGfx = deambEntity.emplace<Nz::GraphicsComponent>(deambModel, RenderMask3D);
entityGfx.AttachRenderable(deambModel); // entityGfx.AttachRenderable(deambModel);
auto &entityNode = deambEntity.emplace<Nz::NodeComponent>(); auto& entityNode = deambEntity.emplace<Nz::NodeComponent>();
entityNode.SetPosition(Nz::Vector3f(0.f, 0.f, 0.f)); entityNode.SetPosition(Nz::Vector3f(0.f, 0.f, 0.f));
} }
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice(); std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
std::shared_ptr<Nz::MaterialInstance> material = Nz::MaterialInstance::Instantiate(Nz::MaterialType::Phong); std::shared_ptr<Nz::MaterialInstance> material = Nz::MaterialInstance::Instantiate(Nz::MaterialType::Phong);
for (std::string_view passName : {"DepthPass", "ForwardPass"}) for (std::string_view passName : {"DepthPass", "ForwardPass"}) {
{ material->UpdatePassStates(passName, [](Nz::RenderStates& states) {
material->UpdatePassStates(passName, [](Nz::RenderStates &states)
{
states.depthClamp = true; states.depthClamp = true;
return true; }); return true;
});
} }
std::mt19937 rd(42); std::mt19937 rd(42);
@@ -141,46 +136,41 @@ static void CreateModel(Nz::EnttWorld &world)
static Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f); static Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f);
static Nz::MillisecondClock updateClock; static Nz::MillisecondClock updateClock;
static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Application<Nz::Graphics, Nz::Physics3D> &app) static void CreateCamera(Nz::EnttWorld& world, Nz::Window& window, Nz::Application<Nz::Graphics, Nz::Physics3D, Nz::Widgets>& app,
{ std::shared_ptr<Nz::RenderWindow>& renderTarget) {
Nz::RenderSystem &renderSystem = world.AddSystem<Nz::RenderSystem>();
Nz::SwapchainParameters params;
params.presentMode.clear();
params.presentMode.push_back(Nz::PresentMode::VerticalSync);
Nz::WindowSwapchain &windowSwapchain = renderSystem.CreateSwapchain(window, params);
// Création de la caméra // Création de la caméra
entt::handle cameraEntity = world.CreateEntity(); entt::handle cameraEntity = world.CreateEntity();
auto &cameraNode = cameraEntity.emplace<Nz::NodeComponent>(); auto& cameraNode = cameraEntity.emplace<Nz::NodeComponent>();
cameraNode.SetPosition({0, 2.5, 0}); cameraNode.SetPosition({0, 2.5, 0});
auto &cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(std::make_shared<Nz::RenderWindow>(windowSwapchain), Nz::ProjectionType::Perspective); auto& cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(renderTarget, Nz::ProjectionType::Perspective);
cameraComponent.UpdateClearColor(Nz::Color(0.3f, 0.8f, 1.0f)); cameraComponent.UpdateClearColor(Nz::Color(0.3f, 0.8f, 1.0f));
cameraComponent.UpdateZNear(0.1f); cameraComponent.UpdateZNear(0.1f);
cameraComponent.UpdateZFar(100.0f); cameraComponent.UpdateZFar(100.0f);
cameraComponent.UpdateRenderMask(RenderMask3D);
cameraComponent.UpdateRenderOrder(-1);
entt::handle playerEntity = world.CreateEntity(); entt::handle playerEntity = world.CreateEntity();
auto &playerNode = playerEntity.emplace<Nz::NodeComponent>(); auto& playerNode = playerEntity.emplace<Nz::NodeComponent>();
playerNode.SetPosition({0, 5, 0}); playerNode.SetPosition({0, 5, 0});
cameraNode.SetParent(playerEntity); cameraNode.SetParent(playerEntity);
window.GetEventHandler().OnMouseMoved.Connect([&](const Nz::WindowEventHandler * /*eventHandler*/, const Nz::WindowEvent::MouseMoveEvent &event) window.GetEventHandler().OnMouseMoved.Connect(
{ [&](const Nz::WindowEventHandler* /*eventHandler*/, const Nz::WindowEvent::MouseMoveEvent& event) {
constexpr float sensitivity = 0.3f; constexpr float sensitivity = 0.3f;
camAngles.yaw -= event.deltaX * sensitivity; camAngles.yaw -= event.deltaX * sensitivity;
camAngles.yaw.Normalize(); camAngles.yaw.Normalize();
camAngles.pitch = Nz::Clamp(camAngles.pitch - event.deltaY * sensitivity, -89.f, 89.f); camAngles.pitch = Nz::Clamp(camAngles.pitch - event.deltaY * sensitivity, -89.f, 89.f);
camAngles.roll = 0.0f; camAngles.roll = 0.0f;
cameraNode.SetRotation(camAngles); cameraNode.SetRotation(camAngles);
}); });
Nz::Vector3f playerSize{0.5, 2, 0.5}; Nz::Vector3f playerSize{0.5, 2, 0.5};
@@ -193,7 +183,7 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Applicati
std::shared_ptr<Nz::Model> sphereModel = std::make_shared<Nz::Model>(boxMesh); std::shared_ptr<Nz::Model> sphereModel = std::make_shared<Nz::Model>(boxMesh);
sphereModel->SetMaterial(0, std::move(boxMaterial)); sphereModel->SetMaterial(0, std::move(boxMaterial));
playerEntity.emplace<Nz::GraphicsComponent>(std::move(sphereModel)); playerEntity.emplace<Nz::GraphicsComponent>(std::move(sphereModel), RenderMask3D);
} }
std::shared_ptr<Nz::BoxCollider3D> boxCollider = std::make_shared<Nz::BoxCollider3D>(playerSize); std::shared_ptr<Nz::BoxCollider3D> boxCollider = std::make_shared<Nz::BoxCollider3D>(playerSize);
@@ -206,18 +196,16 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Applicati
playerEntity.emplace<Nz::RigidBody3DComponent>(settings); playerEntity.emplace<Nz::RigidBody3DComponent>(settings);
app.AddUpdaterFunc([playerEntity]() app.AddUpdaterFunc([playerEntity]() {
{ if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::Milliseconds(30))) {
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::Milliseconds(30)))
{
Nz::Vector3f front = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians()); Nz::Vector3f front = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians());
Nz::Vector3f left = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians() + 3.1415926535/2.0); Nz::Vector3f left = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians() + 3.1415926535 / 2.0);
auto& playerBody = playerEntity.get<Nz::RigidBody3DComponent>(); auto& playerBody = playerEntity.get<Nz::RigidBody3DComponent>();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z)){ if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z)) {
std::cout << front << "\n"; // std::cout << front << "\n";
std::cout << "Pos : " << playerBody.GetPosition() << " \n"; // std::cout << "Pos : " << playerBody.GetPosition() << " \n";
playerBody.AddForce(front * 10.f * playerMass, Nz::CoordSys::Global); playerBody.AddForce(front * 10.f * playerMass, Nz::CoordSys::Global);
} }
@@ -230,8 +218,8 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Applicati
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D)) if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D))
playerBody.AddForce(-left * 10.f * playerMass, Nz::CoordSys::Local); playerBody.AddForce(-left * 10.f * playerMass, Nz::CoordSys::Local);
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space)){ if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space)) {
playerBody.AddForce(Nz::Vector3f::Up() * 15.f * playerMass, Nz::CoordSys::Global); playerBody.AddForce(Nz::Vector3f::Up() * 15.f * playerMass, Nz::CoordSys::Global);
} }
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::R)) { if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::R)) {
@@ -240,28 +228,100 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Applicati
playerBody.SetPosition({0, 10, 0}); playerBody.SetPosition({0, 10, 0});
} }
//playerBody.SetRotation({}); // playerBody.SetRotation({});
} }); }
});
} }
int Video(int argc, char **argv) { static void SetupWidgets(Nz::EnttWorld& world, std::shared_ptr<Nz::RenderWindow> renderTarget, Nz::Window& window) {
Nz::Application<Nz::Graphics, Nz::Physics3D> app(argc, argv); entt::handle cameraEntity = world.CreateEntity();
{
cameraEntity.emplace<Nz::NodeComponent>();
auto &windowing = app.AddComponent<Nz::WindowingAppComponent>(); auto& cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(renderTarget, Nz::ProjectionType::Orthographic);
cameraComponent.UpdateClearColor(Nz::Color(0.3f, 0.8f, 1.0f, 0.0f));
cameraComponent.UpdateRenderMask(RenderMaskUI);
cameraComponent.UpdateRenderOrder(1);
// cameraComponent.UpdateSize({100, 100});
}
Nz::Canvas canvas(world.GetRegistry(), window.GetEventHandler(), window.GetCursorController().CreateHandle(), RenderMaskUI);
canvas.Resize({static_cast<float>(window.GetSize().x), static_cast<float>(window.GetSize().y)});
canvas.EnableBackground(false);
auto widget = canvas.Add<Nz::RichTextAreaWidget>();
widget->EnableBackground(false);
widget->EnableLineWrap(true);
widget->SetBackgroundColor(Nz::Color(0, 0, 0, 50));
widget->SetCharacterSize(22);
widget->SetTextColor(Nz::Color::White());
widget->SetTextOutlineThickness(1.f);
widget->SetReadOnly(true);
auto m_chatEnteringBox = canvas.Add<Nz::TextAreaWidget>();
m_chatEnteringBox->EnableBackground(false);
m_chatEnteringBox->SetBackgroundColor(Nz::Color(255, 255, 255, 150));
m_chatEnteringBox->SetTextColor(Nz::Color::Black());
m_chatEnteringBox->SetText("ceci est un test incroyable");
// m_chatEnteringBox->SetMaximumWidth(m_chatEnteringBox->GetPreferredWidth());
auto versionLabel = canvas.Add<Nz::LabelWidget>();
versionLabel->UpdateText(
Nz::SimpleTextDrawer::Draw(std::to_string(100000000) + "." + std::to_string(0) + "." + std::to_string(0), 14));
versionLabel->Resize({500, 500});
versionLabel->SetPosition({1000, 800});
versionLabel->SetPosition({canvas.GetWidth() - versionLabel->GetWidth(), canvas.GetHeight() - versionLabel->GetHeight()});
// Création d'un texte
Nz::SimpleTextDrawer textDrawer;
textDrawer.SetText("Hello world !");
textDrawer.SetCharacterSize(72);
textDrawer.SetTextOutlineThickness(4.f);
std::shared_ptr<Nz::TextSprite> textSprite = std::make_shared<Nz::TextSprite>();
textSprite->Update(textDrawer);
entt::handle textEntity = world.CreateEntity();
{
auto& nodeComponent = textEntity.emplace<Nz::NodeComponent>();
auto& gfxComponent = textEntity.emplace<Nz::GraphicsComponent>(textSprite, RenderMaskUI);
Nz::Boxf textBox = textSprite->GetAABB();
Nz::Vector2ui windowSize = window.GetSize();
nodeComponent.SetPosition({windowSize.x / 2 - textBox.width / 2, windowSize.y / 2 - textBox.height / 2});
}
}
int Video(int argc, char** argv) {
Nz::Application<Nz::Graphics, Nz::Physics3D, Nz::Widgets> app(argc, argv);
auto& windowing = app.AddComponent<Nz::WindowingAppComponent>();
std::string windowTitle = "Blitz 2"; std::string windowTitle = "Blitz 2";
Nz::Window &window = windowing.CreateWindow(Nz::VideoMode(1920, 1080, 32), windowTitle); Nz::Window& window = windowing.CreateWindow(Nz::VideoMode(1920, 1080, 32), windowTitle);
auto &ecs = app.AddComponent<Nz::EntitySystemAppComponent>(); auto& ecs = app.AddComponent<Nz::EntitySystemAppComponent>();
auto &world = ecs.AddWorld<Nz::EnttWorld>(); auto& world = ecs.AddWorld<Nz::EnttWorld>();
auto &physSystem = world.AddSystem<Nz::Physics3DSystem>(); auto& physSystem = world.AddSystem<Nz::Physics3DSystem>();
physSystem.GetPhysWorld().SetMaxStepCount(1); physSystem.GetPhysWorld().SetMaxStepCount(1);
physSystem.GetPhysWorld().SetStepSize(Nz::Time::Milliseconds(20)); physSystem.GetPhysWorld().SetStepSize(Nz::Time::Milliseconds(20));
physSystem.GetPhysWorld().SetGravity(Nz::Vector3f::Down() * 9.81f); physSystem.GetPhysWorld().SetGravity(Nz::Vector3f::Down() * 9.81f);
CreateCamera(world, window, app); Nz::RenderSystem& renderSystem = world.AddSystem<Nz::RenderSystem>();
Nz::SwapchainParameters params;
params.presentMode.clear();
params.presentMode.push_back(Nz::PresentMode::VerticalSync);
Nz::WindowSwapchain& windowSwapchain = renderSystem.CreateSwapchain(window, params);
auto renderTarget = std::make_shared<Nz::RenderWindow>(windowSwapchain);
CreateCamera(world, window, app, renderTarget);
CreateBoxes(world); CreateBoxes(world);
CreateModel(world); CreateModel(world);
CreateLight(world); CreateLight(world);
@@ -271,20 +331,19 @@ int Video(int argc, char **argv) {
Nz::MillisecondClock fpsClock; Nz::MillisecondClock fpsClock;
unsigned int fps = 0; unsigned int fps = 0;
app.AddUpdaterFunc([&]() app.AddUpdaterFunc([&]() {
{ fps++;
fps++;
if (fpsClock.RestartIfOver(Nz::Time::Second())) if (fpsClock.RestartIfOver(Nz::Time::Second())) {
{ window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " +
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(world.GetAliveEntityCount()) + " entities"); Nz::NumberToString(world.GetAliveEntityCount()) + " entities");
fps = 0; fps = 0;
} }); }
});
return app.Run(); return app.Run();
} }
int main(int argc, char **argv) int main(int argc, char** argv) {
{ return Video(argc, argv);
TestNetwork();
} }