From 66c072c9c09ab1b75d4d89d56ba206e92b1967f6 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Mon, 10 May 2021 17:48:45 +0200 Subject: [PATCH 1/4] action can be set to happen once on press or on release --- Engine/Engine.cpp | 4 +-- Engine/Engine.h | 2 +- Engine/Source.cpp | 2 +- Engine/cameras/Controller.cpp | 46 ++++++++++++++++++++++++++--------- Engine/cameras/Controller.h | 15 +++++++++--- 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/Engine/Engine.cpp b/Engine/Engine.cpp index de01b89..e6988c0 100644 --- a/Engine/Engine.cpp +++ b/Engine/Engine.cpp @@ -80,9 +80,9 @@ const std::shared_ptr Engine::CreateGameObject(const std::string& ob return go; } -void Engine::BindKeyToFunc(const int& key, std::function& func) +void Engine::BindKeyToFunc(const int& key, std::function& func, const ActionType& type) { - mp_controller->SetKeyToFunc(key, func); + mp_controller->SetKeyToFunc(key, func, type); } const std::shared_ptr Engine::GetMainCamera() diff --git a/Engine/Engine.h b/Engine/Engine.h index d4a25e0..4b49583 100644 --- a/Engine/Engine.h +++ b/Engine/Engine.h @@ -28,7 +28,7 @@ class Engine const std::shared_ptr CreateGameObject(const std::string& object_file); // CONTROLLER - void BindKeyToFunc(const int& key, std::function& func); + void BindKeyToFunc(const int& key, std::function& func, const ActionType& type = ActionType::R3D_PRESS); // CAMERA const std::shared_ptr GetMainCamera(); diff --git a/Engine/Source.cpp b/Engine/Source.cpp index c3e4d50..e6e7c7c 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -27,7 +27,7 @@ int main() engine.setScene(scene); std::function my_func = []() {std::clog << "Test d'un key bind" << std::endl; }; - engine.BindKeyToFunc(GLFW_KEY_Q, my_func); + engine.BindKeyToFunc(GLFW_KEY_Q, my_func, ActionType::R3D_PRESS); int init = 0; // running loop diff --git a/Engine/cameras/Controller.cpp b/Engine/cameras/Controller.cpp index 6df04ac..769928f 100644 --- a/Engine/cameras/Controller.cpp +++ b/Engine/cameras/Controller.cpp @@ -7,22 +7,22 @@ Controller::Controller(std::shared_ptr p_camera) // Set default controls std::function default_func = [this]() { mp_camera->MoveForward(2.0f); }; - SetKeyToFunc(GLFW_KEY_W, default_func); + SetKeyToFunc(GLFW_KEY_W, default_func, ActionType::R3D_HOLD); default_func = [this]() { mp_camera->MoveBackward(2.0f); }; - SetKeyToFunc(GLFW_KEY_S, default_func); + SetKeyToFunc(GLFW_KEY_S, default_func, ActionType::R3D_HOLD); default_func = [this]() { mp_camera->MoveLeft(2.0f); }; - SetKeyToFunc(GLFW_KEY_A, default_func); + SetKeyToFunc(GLFW_KEY_A, default_func, ActionType::R3D_HOLD); default_func = [this]() { mp_camera->MoveRight(2.0f); }; - SetKeyToFunc(GLFW_KEY_D, default_func); + SetKeyToFunc(GLFW_KEY_D, default_func, ActionType::R3D_HOLD); default_func = [this]() { mp_camera->MoveUp(2.0f); }; - SetKeyToFunc(GLFW_KEY_LEFT_SHIFT, default_func); + SetKeyToFunc(GLFW_KEY_LEFT_SHIFT, default_func, ActionType::R3D_HOLD); default_func = [this]() { mp_camera->MoveDown(2.0f); }; - SetKeyToFunc(GLFW_KEY_LEFT_CONTROL, default_func); + SetKeyToFunc(GLFW_KEY_LEFT_CONTROL, default_func, ActionType::R3D_HOLD); } @@ -35,16 +35,40 @@ void Controller::setInput(int32_t key, int32_t scancode, int32_t mods, int32_t a if (action == GLFW_PRESS) { m_keyboard_press.set(key, true); + + for (size_t i = 0; i < m_onpress_actions.size(); i++) + { + m_onpress_actions[i].func(); + } } else if (action == GLFW_RELEASE) { m_keyboard_press.set(key, false); + + for (size_t i = 0; i < m_onrelease_actions.size(); i++) + { + m_onrelease_actions[i].func(); + } } } -void Controller::SetKeyToFunc(const int32_t& key, std::function& func) +void Controller::SetKeyToFunc(const int32_t& key, std::function& func, const ActionType& type) { - m_actions.push_back({ key, func }); + switch (type) + { + case ActionType::RED_RELEASE: + m_onrelease_actions.push_back({ key, func }); + break; + case ActionType::R3D_PRESS: + m_onpress_actions.push_back({ key, func }); + break; + case ActionType::R3D_HOLD: + m_hold_actions.push_back({ key, func }); + break; + default: + m_hold_actions.push_back({ key, func }); + break; + } } void Controller::updateRotation(const double& xpos, const double& ypos) @@ -59,11 +83,11 @@ void Controller::updateRotation(const double& xpos, const double& ypos) void Controller::Update(const float& dt) { - for (size_t i = 0; i < m_actions.size(); i++) + for (size_t i = 0; i < m_hold_actions.size(); i++) { - if (m_keyboard_press[m_actions[i].key]) + if (m_keyboard_press[m_hold_actions[i].key]) { - m_actions[i].func(); + m_hold_actions[i].func(); } } diff --git a/Engine/cameras/Controller.h b/Engine/cameras/Controller.h index 216821c..cc12fff 100644 --- a/Engine/cameras/Controller.h +++ b/Engine/cameras/Controller.h @@ -14,6 +14,13 @@ struct Actions const std::function func; }; +enum class ActionType +{ + RED_RELEASE, + R3D_PRESS, + R3D_HOLD +}; + class Controller { public: @@ -21,17 +28,19 @@ class Controller ~Controller(); void setInput(int32_t key, int32_t scancode, int32_t mods, int32_t action); - void SetKeyToFunc(const int32_t& key, std::function& func); + void SetKeyToFunc(const int32_t& key, std::function& func, const ActionType& type); void Update(const float& dt); void updateRotation(const double& xpos, const double& ypos); uint32_t getLoadRadius(); private: - std::vector m_actions; - std::bitset<348> m_keyboard_press; + std::vector m_hold_actions; + std::vector m_onpress_actions; + std::vector m_onrelease_actions; uint32_t m_load_radius = 4; + std::bitset<348> m_keyboard_press; std::shared_ptr mp_camera; }; From 18de9d1b500070b919037108e60e4872b7a1df88 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Mon, 10 May 2021 18:19:29 +0200 Subject: [PATCH 2/4] can bind key without latency issues --- Engine/Engine.cpp | 15 +++++++++++++++ Engine/Engine.h | 5 +++++ Engine/Source.cpp | 11 +++++++++-- Engine/cameras/Controller.cpp | 18 ++++++++---------- Engine/cameras/Controller.h | 4 ++-- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Engine/Engine.cpp b/Engine/Engine.cpp index e6988c0..3f38c54 100644 --- a/Engine/Engine.cpp +++ b/Engine/Engine.cpp @@ -90,6 +90,21 @@ const std::shared_ptr Engine::GetMainCamera() return mp_main_camera; } +void Engine::SetWireframeMode() +{ + mp_renderer->SetPolygonFillingMode(VK_POLYGON_MODE_LINE); +} + +void Engine::SetPointMode() +{ + mp_renderer->SetPolygonFillingMode(VK_POLYGON_MODE_POINT); +} + +void Engine::SetFillMode() +{ + mp_renderer->SetPolygonFillingMode(VK_POLYGON_MODE_FILL); +} + const bool& Engine::shouldClose() { return glfwWindowShouldClose(&mp_window->getHandle()); diff --git a/Engine/Engine.h b/Engine/Engine.h index 4b49583..35b4f60 100644 --- a/Engine/Engine.h +++ b/Engine/Engine.h @@ -33,6 +33,11 @@ class Engine // CAMERA const std::shared_ptr GetMainCamera(); + // VIEWING + void SetWireframeMode(); + void SetPointMode(); + void SetFillMode(); + const bool& shouldClose(); private: diff --git a/Engine/Source.cpp b/Engine/Source.cpp index e6e7c7c..007cc84 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -26,8 +26,15 @@ int main() engine.setScene(scene); - std::function my_func = []() {std::clog << "Test d'un key bind" << std::endl; }; - engine.BindKeyToFunc(GLFW_KEY_Q, my_func, ActionType::R3D_PRESS); + std::function wireframemode = [&engine]() { + engine.SetWireframeMode(); + }; + engine.BindKeyToFunc(GLFW_KEY_Q, wireframemode, ActionType::R3D_PRESS); + + std::function fillmode = [&engine]() { + engine.SetFillMode(); + }; + engine.BindKeyToFunc(GLFW_KEY_E, fillmode, ActionType::R3D_PRESS); int init = 0; // running loop diff --git a/Engine/cameras/Controller.cpp b/Engine/cameras/Controller.cpp index 769928f..04ee62f 100644 --- a/Engine/cameras/Controller.cpp +++ b/Engine/cameras/Controller.cpp @@ -23,6 +23,10 @@ Controller::Controller(std::shared_ptr p_camera) default_func = [this]() { mp_camera->MoveDown(2.0f); }; SetKeyToFunc(GLFW_KEY_LEFT_CONTROL, default_func, ActionType::R3D_HOLD); + + default_func = []() { return; }; // empty func so not to crash upon call + m_onpress_actions.fill(default_func); + m_onrelease_actions.fill(default_func); } @@ -36,19 +40,13 @@ void Controller::setInput(int32_t key, int32_t scancode, int32_t mods, int32_t a { m_keyboard_press.set(key, true); - for (size_t i = 0; i < m_onpress_actions.size(); i++) - { - m_onpress_actions[i].func(); - } + m_onpress_actions[key](); } else if (action == GLFW_RELEASE) { m_keyboard_press.set(key, false); - for (size_t i = 0; i < m_onrelease_actions.size(); i++) - { - m_onrelease_actions[i].func(); - } + m_onrelease_actions[key](); } } @@ -57,10 +55,10 @@ void Controller::SetKeyToFunc(const int32_t& key, std::function& func, c switch (type) { case ActionType::RED_RELEASE: - m_onrelease_actions.push_back({ key, func }); + m_onrelease_actions[key] = func; break; case ActionType::R3D_PRESS: - m_onpress_actions.push_back({ key, func }); + m_onpress_actions[key] = func; break; case ActionType::R3D_HOLD: m_hold_actions.push_back({ key, func }); diff --git a/Engine/cameras/Controller.h b/Engine/cameras/Controller.h index cc12fff..9824b10 100644 --- a/Engine/cameras/Controller.h +++ b/Engine/cameras/Controller.h @@ -36,8 +36,8 @@ class Controller uint32_t getLoadRadius(); private: std::vector m_hold_actions; - std::vector m_onpress_actions; - std::vector m_onrelease_actions; + std::array, 348> m_onpress_actions; + std::array, 348> m_onrelease_actions; uint32_t m_load_radius = 4; std::bitset<348> m_keyboard_press; From c6f61f458867478718dbc1e1dfb53b67356faddb Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Mon, 10 May 2021 18:26:09 +0200 Subject: [PATCH 3/4] added non fill mode features --- Engine/Source.cpp | 5 +++++ Engine/renderer/VulkanDevice.cpp | 1 + 2 files changed, 6 insertions(+) diff --git a/Engine/Source.cpp b/Engine/Source.cpp index 007cc84..6ba6b89 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -36,6 +36,11 @@ int main() }; engine.BindKeyToFunc(GLFW_KEY_E, fillmode, ActionType::R3D_PRESS); + std::function pointmode = [&engine]() { + engine.SetPointMode(); + }; + engine.BindKeyToFunc(GLFW_KEY_R, pointmode, ActionType::R3D_PRESS); + int init = 0; // running loop do diff --git a/Engine/renderer/VulkanDevice.cpp b/Engine/renderer/VulkanDevice.cpp index 55d47ba..3deb844 100644 --- a/Engine/renderer/VulkanDevice.cpp +++ b/Engine/renderer/VulkanDevice.cpp @@ -74,6 +74,7 @@ void VulkanDevice::createLogicalDevice() VkPhysicalDeviceFeatures deviceFeatures = {}; deviceFeatures.samplerAnisotropy = VK_TRUE; + deviceFeatures.fillModeNonSolid = VK_TRUE; VkDeviceCreateInfo device_info = {}; From 30a04e6208e18467ba704b11e6030e28c3010697 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Mon, 10 May 2021 18:41:48 +0200 Subject: [PATCH 4/4] array of func is wrapped in ptr | if condition to check if func is assigned --- Engine/cameras/Controller.cpp | 15 ++++++++------- Engine/cameras/Controller.h | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Engine/cameras/Controller.cpp b/Engine/cameras/Controller.cpp index 04ee62f..008f465 100644 --- a/Engine/cameras/Controller.cpp +++ b/Engine/cameras/Controller.cpp @@ -24,9 +24,8 @@ Controller::Controller(std::shared_ptr p_camera) default_func = [this]() { mp_camera->MoveDown(2.0f); }; SetKeyToFunc(GLFW_KEY_LEFT_CONTROL, default_func, ActionType::R3D_HOLD); - default_func = []() { return; }; // empty func so not to crash upon call - m_onpress_actions.fill(default_func); - m_onrelease_actions.fill(default_func); + m_onpress_actions = std::make_unique, 348>>(); + m_onrelease_actions = std::make_unique, 348>>(); } @@ -40,13 +39,15 @@ void Controller::setInput(int32_t key, int32_t scancode, int32_t mods, int32_t a { m_keyboard_press.set(key, true); - m_onpress_actions[key](); + if(m_onpress_actions->at(key)) + m_onpress_actions->at(key)(); } else if (action == GLFW_RELEASE) { m_keyboard_press.set(key, false); - m_onrelease_actions[key](); + if(m_onrelease_actions->at(key)) + m_onrelease_actions->at(key)(); } } @@ -55,10 +56,10 @@ void Controller::SetKeyToFunc(const int32_t& key, std::function& func, c switch (type) { case ActionType::RED_RELEASE: - m_onrelease_actions[key] = func; + m_onrelease_actions->at(key) = func; break; case ActionType::R3D_PRESS: - m_onpress_actions[key] = func; + m_onpress_actions->at(key) = func; break; case ActionType::R3D_HOLD: m_hold_actions.push_back({ key, func }); diff --git a/Engine/cameras/Controller.h b/Engine/cameras/Controller.h index 9824b10..2b3f247 100644 --- a/Engine/cameras/Controller.h +++ b/Engine/cameras/Controller.h @@ -36,8 +36,8 @@ class Controller uint32_t getLoadRadius(); private: std::vector m_hold_actions; - std::array, 348> m_onpress_actions; - std::array, 348> m_onrelease_actions; + std::unique_ptr, 348>> m_onpress_actions; + std::unique_ptr, 348>> m_onrelease_actions; uint32_t m_load_radius = 4; std::bitset<348> m_keyboard_press;