diff --git a/src/application/context/contexthelper.cpp b/src/application/context/contexthelper.cpp index 2863661..2b92cd7 100644 --- a/src/application/context/contexthelper.cpp +++ b/src/application/context/contexthelper.cpp @@ -17,10 +17,7 @@ namespace rythe::application bool ContextHelper::init() { - glfwSetErrorCallback([](int code, rsl::cstring desc) - { - log::error("GLFW ERROR {}: {}", code, desc); - }); + glfwSetErrorCallback([](int code, rsl::cstring desc) { log::error("GLFW ERROR {}: {}", code, desc); }); bool success = glfwInit(); if (success) @@ -86,9 +83,8 @@ namespace rythe::application mw = mode->width; mh = mode->height; - overlap = - math::max(0, math::min(wx + ww, mx + mw) - math::max(wx, mx)) * - math::max(0, math::min(wy + wh, my + mh) - math::max(wy, my)); + overlap = math::max(0, math::min(wx + ww, mx + mw) - math::max(wx, mx)) * + math::max(0, math::min(wy + wh, my + mh) - math::max(wy, my)); if (bestoverlap < overlap) { @@ -100,7 +96,9 @@ namespace rythe::application return bestmonitor; } - void ContextHelper::setWindowMonitor(GLFWwindow* window, GLFWmonitor* monitor, math::int2 pos, math::int2 size, int refreshRate) + void ContextHelper::setWindowMonitor( + GLFWwindow* window, GLFWmonitor* monitor, math::int2 pos, math::int2 size, int refreshRate + ) { glfwSetWindowMonitor(window, monitor, pos.x, pos.y, size.x, size.y, refreshRate); } @@ -125,7 +123,8 @@ namespace rythe::application return glfwCreateWindow(dim.x, dim.y, title, monitor, share); } - GLFWwindow* ContextHelper::createWindow(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share) + GLFWwindow* + ContextHelper::createWindow(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share) { return glfwCreateWindow(width, height, title, monitor, share); } @@ -139,9 +138,12 @@ namespace rythe::application { GLFWwindow* focus = newFocus.load(std::memory_order_acquire); if (!focus) + { return; + } - while (!newFocus.compare_exchange_weak(focus, nullptr, std::memory_order_release, std::memory_order_relaxed)); + while (!newFocus.compare_exchange_weak(focus, nullptr, std::memory_order_release, std::memory_order_relaxed)) + ; glfwHideWindow(focus); glfwShowWindow(focus); @@ -156,7 +158,9 @@ namespace rythe::application void ContextHelper::setWindowShouldClose(GLFWwindow* window, int value) { if (window) + { glfwSetWindowShouldClose(window, value); + } } int ContextHelper::windowShouldClose(GLFWwindow* window) @@ -194,7 +198,9 @@ namespace rythe::application void ContextHelper::destroyWindow(GLFWwindow* window) { if (initialized()) + { glfwDestroyWindow(window); + } } math::int2 ContextHelper::getFramebufferSize(GLFWwindow* window) @@ -207,7 +213,9 @@ namespace rythe::application void ContextHelper::swapBuffers(GLFWwindow* window) { if (initialized()) + { glfwSwapBuffers(window); + } } void ContextHelper::swapInterval(int interval) @@ -305,12 +313,14 @@ namespace rythe::application return glfwSetWindowMaximizeCallback(window, callback); } - GLFWframebuffersizefun ContextHelper::setFramebufferSizeCallback(GLFWwindow* window, GLFWframebuffersizefun callback) + GLFWframebuffersizefun + ContextHelper::setFramebufferSizeCallback(GLFWwindow* window, GLFWframebuffersizefun callback) { return glfwSetFramebufferSizeCallback(window, callback); } - GLFWwindowcontentscalefun ContextHelper::setWindowContentScaleCallback(GLFWwindow* window, GLFWwindowcontentscalefun callback) + GLFWwindowcontentscalefun + ContextHelper::setWindowContentScaleCallback(GLFWwindow* window, GLFWwindowcontentscalefun callback) { return glfwSetWindowContentScaleCallback(window, callback); } @@ -323,7 +333,9 @@ namespace rythe::application int ContextHelper::getGamepadSate(int jid, GLFWgamepadstate* state) { if (initialized()) + { return glfwGetGamepadState(jid, state); + } return 0; } diff --git a/src/application/context/contexthelper.hpp b/src/application/context/contexthelper.hpp index ace88b4..81eecd6 100644 --- a/src/application/context/contexthelper.hpp +++ b/src/application/context/contexthelper.hpp @@ -47,12 +47,16 @@ namespace rythe::application static int getError(rsl::cstring* desc); static GLFWmonitor* getPrimaryMonitor(); static GLFWmonitor* getCurrentMonitor(GLFWwindow* window); - static void setWindowMonitor(GLFWwindow* window, GLFWmonitor* monitor, math::int2 pos, math::int2 size, int refreshRate); + static void + setWindowMonitor(GLFWwindow* window, GLFWmonitor* monitor, math::int2 pos, math::int2 size, int refreshRate); static const GLFWvidmode* getPrimaryVideoMode(); static const GLFWvidmode* getVideoMode(GLFWmonitor* monitor); static void windowHint(int hint, int value); - static GLFWwindow* createWindow(math::int2 dim, const char* title, GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr); - static GLFWwindow* createWindow(int width, int height, const char* title, GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr); + static GLFWwindow* + createWindow(math::int2 dim, const char* title, GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr); + static GLFWwindow* createWindow( + int width, int height, const char* title, GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr + ); /**@brief Request the input thread to give focus to this window. */ static void showWindow(GLFWwindow* window); @@ -89,7 +93,8 @@ namespace rythe::application static GLFWwindowiconifyfun setWindowIconifyCallback(GLFWwindow* window, GLFWwindowiconifyfun callback); static GLFWwindowmaximizefun setWindowMaximizeCallback(GLFWwindow* window, GLFWwindowmaximizefun callback); static GLFWframebuffersizefun setFramebufferSizeCallback(GLFWwindow* window, GLFWframebuffersizefun callback); - static GLFWwindowcontentscalefun setWindowContentScaleCallback(GLFWwindow* window, GLFWwindowcontentscalefun callback); + static GLFWwindowcontentscalefun + setWindowContentScaleCallback(GLFWwindow* window, GLFWwindowcontentscalefun callback); static GLFWjoystickfun setJoystickCallback(GLFWjoystickfun callback); static int getGamepadSate(int jid, GLFWgamepadstate* state); static void updateGamepadMappings(const char* name); diff --git a/src/application/events/windowevents.hpp b/src/application/events/windowevents.hpp index 55d3475..64e426e 100644 --- a/src/application/events/windowevents.hpp +++ b/src/application/events/windowevents.hpp @@ -129,7 +129,9 @@ namespace rythe::application : windowHandle(windowHandle) { for (int i = 0; i < count; i++) + { paths.push_back(cpaths[i]); + } } }; diff --git a/src/application/input/inputmap.hpp b/src/application/input/inputmap.hpp index 477bc63..7383ae8 100644 --- a/src/application/input/inputmap.hpp +++ b/src/application/input/inputmap.hpp @@ -21,32 +21,17 @@ namespace rythe::application MAX_SIZE = 16 + JOYSTICK0 }; - static uint8_t value(modifier_keys m) - { - return static_cast(m); - } + static uint8_t value(modifier_keys m) { return static_cast(m); } - friend uint8_t operator-(modifier_keys lhs, modifier_keys rhs) - { - return value(lhs) - value(rhs); - } + friend uint8_t operator-(modifier_keys lhs, modifier_keys rhs) { return value(lhs) - value(rhs); } - friend uint8_t operator+(modifier_keys lhs, modifier_keys rhs) - { - return value(lhs) + value(rhs); - } + friend uint8_t operator+(modifier_keys lhs, modifier_keys rhs) { return value(lhs) + value(rhs); } friend modifier_keys operator+(modifier_keys lhs, uint8_t rhs) { return static_cast(value(lhs) + rhs); } - friend uint8_t operator&(modifier_keys lhs, modifier_keys rhs) - { - return value(lhs) & value(rhs); - } - friend uint8_t operator|(modifier_keys lhs, modifier_keys rhs) - { - return value(lhs) | value(rhs); - } + friend uint8_t operator&(modifier_keys lhs, modifier_keys rhs) { return value(lhs) & value(rhs); } + friend uint8_t operator|(modifier_keys lhs, modifier_keys rhs) { return value(lhs) | value(rhs); } enum class method { @@ -219,53 +204,54 @@ namespace rythe::application static constexpr bool is_axis(method m) { return m == method::MOUSE || m == method::JOY || m == method::SCROLL || m == method::GAMEPAD_LEFT_TRIGGER || - m == method::GAMEPAD_RIGHT_TRIGGER || m == method::GAMEPAD_LEFT_AXIS || m == method::GAMEPAD_RIGHT_AXIS || - m == method::GAMEPAD_LEFT_X || m == method::GAMEPAD_LEFT_Y || m == method::GAMEPAD_RIGHT_X || - m == method::GAMEPAD_RIGHT_Y || m == method::HSCROLL || m == method::VSCROLL || - m == method::MOUSE_X || m == method::MOUSE_Y; + m == method::GAMEPAD_RIGHT_TRIGGER || m == method::GAMEPAD_LEFT_AXIS || + m == method::GAMEPAD_RIGHT_AXIS || m == method::GAMEPAD_LEFT_X || m == method::GAMEPAD_LEFT_Y || + m == method::GAMEPAD_RIGHT_X || m == method::GAMEPAD_RIGHT_Y || m == method::HSCROLL || + m == method::VSCROLL || m == method::MOUSE_X || m == method::MOUSE_Y; } static constexpr bool is_key(method m) { - return m == method::KEY || m == method::CHAR || m == method::JOY || m == method::MOUSE || m == method::GAMEPAD_A || - m == method::GAMEPAD_B || m == method::GAMEPAD_X || m == method::GAMEPAD_Y || - m == method::GAMEPAD_LEFT_BUMPER || m == method::GAMEPAD_RIGHT_BUMPER || m == method::GAMEPAD_BACK || - m == method::GAMEPAD_START || m == method::GAMEPAD_GUIDE || m == method::GAMEPAD_DPAD_UP || - m == method::GAMEPAD_DPAD_RIGHT || m == method::GAMEPAD_DPAD_DOWN || m == method::GAMEPAD_DPAD_LEFT || - m == method::SPACE || m == method::APOSTROPHE || m == method::COMMA || m == method::MINUS || - m == method::PERIOD || m == method::SLASH || m == method::NUM0 || m == method::NUM1 || - m == method::NUM2 || m == method::NUM3 || m == method::NUM4 || m == method::NUM5 || m == method::NUM6 || - m == method::NUM7 || m == method::NUM8 || m == method::NUM9 || m == method::SEMICOLON || - m == method::EQUAL || m == method::A || m == method::B || m == method::C || m == method::D || - m == method::E || m == method::F || m == method::G || m == method::H || m == method::I || m == method::J || - m == method::K || m == method::L || m == method::M || m == method::N || m == method::O || m == method::P || - m == method::Q || m == method::R || m == method::S || m == method::T || m == method::U || m == method::V || - m == method::W || m == method::X || m == method::Y || m == method::Z || m == method::LEFT_BRACKET || - m == method::BACKSLASH || m == method::RIGHT_BRACKET || m == method::GRAVE_ACCENT || m == method::WORLD_1 || + return m == method::KEY || m == method::CHAR || m == method::JOY || m == method::MOUSE || + m == method::GAMEPAD_A || m == method::GAMEPAD_B || m == method::GAMEPAD_X || + m == method::GAMEPAD_Y || m == method::GAMEPAD_LEFT_BUMPER || m == method::GAMEPAD_RIGHT_BUMPER || + m == method::GAMEPAD_BACK || m == method::GAMEPAD_START || m == method::GAMEPAD_GUIDE || + m == method::GAMEPAD_DPAD_UP || m == method::GAMEPAD_DPAD_RIGHT || m == method::GAMEPAD_DPAD_DOWN || + m == method::GAMEPAD_DPAD_LEFT || m == method::SPACE || m == method::APOSTROPHE || + m == method::COMMA || m == method::MINUS || m == method::PERIOD || m == method::SLASH || + m == method::NUM0 || m == method::NUM1 || m == method::NUM2 || m == method::NUM3 || + m == method::NUM4 || m == method::NUM5 || m == method::NUM6 || m == method::NUM7 || + m == method::NUM8 || m == method::NUM9 || m == method::SEMICOLON || m == method::EQUAL || + m == method::A || m == method::B || m == method::C || m == method::D || m == method::E || + m == method::F || m == method::G || m == method::H || m == method::I || m == method::J || + m == method::K || m == method::L || m == method::M || m == method::N || m == method::O || + m == method::P || m == method::Q || m == method::R || m == method::S || m == method::T || + m == method::U || m == method::V || m == method::W || m == method::X || m == method::Y || + m == method::Z || m == method::LEFT_BRACKET || m == method::BACKSLASH || + m == method::RIGHT_BRACKET || m == method::GRAVE_ACCENT || m == method::WORLD_1 || m == method::WORLD_2 || m == method::ESCAPE || m == method::ENTER || m == method::TAB || m == method::BACKSPACE || m == method::INSERT || m == method::DEL || m == method::RIGHT || m == method::LEFT || m == method::DOWN || m == method::UP || m == method::PAGE_UP || m == method::PAGE_DOWN || m == method::HOME || m == method::END || m == method::CAPS_LOCK || - m == method::SCROLL_LOCK || m == method::NUM_LOCK || m == method::PRINT_SCREEN || m == method::PAUSE || - m == method::F1 || m == method::F2 || m == method::F3 || m == method::F4 || m == method::F5 || - m == method::F6 || m == method::F7 || m == method::F8 || m == method::F9 || m == method::F10 || - m == method::F11 || m == method::F12 || m == method::F13 || m == method::F14 || m == method::F15 || - m == method::F16 || m == method::F17 || m == method::F18 || m == method::F19 || m == method::F20 || - m == method::F21 || m == method::F22 || m == method::F23 || m == method::F24 || m == method::F25 || - m == method::KP_0 || m == method::KP_1 || m == method::KP_2 || m == method::KP_3 || m == method::KP_4 || - m == method::KP_5 || m == method::KP_6 || m == method::KP_7 || m == method::KP_8 || m == method::KP_9 || - m == method::KP_DECIMAL || m == method::KP_DIVIDE || m == method::KP_MULTIPLY || m == method::KP_SUBTRACT || + m == method::SCROLL_LOCK || m == method::NUM_LOCK || m == method::PRINT_SCREEN || + m == method::PAUSE || m == method::F1 || m == method::F2 || m == method::F3 || m == method::F4 || + m == method::F5 || m == method::F6 || m == method::F7 || m == method::F8 || m == method::F9 || + m == method::F10 || m == method::F11 || m == method::F12 || m == method::F13 || m == method::F14 || + m == method::F15 || m == method::F16 || m == method::F17 || m == method::F18 || m == method::F19 || + m == method::F20 || m == method::F21 || m == method::F22 || m == method::F23 || m == method::F24 || + m == method::F25 || m == method::KP_0 || m == method::KP_1 || m == method::KP_2 || + m == method::KP_3 || m == method::KP_4 || m == method::KP_5 || m == method::KP_6 || + m == method::KP_7 || m == method::KP_8 || m == method::KP_9 || m == method::KP_DECIMAL || + m == method::KP_DIVIDE || m == method::KP_MULTIPLY || m == method::KP_SUBTRACT || m == method::KP_ADD || m == method::KP_ENTER || m == method::KP_EQUAL || m == method::LEFT_SHIFT || - m == method::LEFT_CONTROL || m == method::LEFT_ALT || m == method::LEFT_SUPER || m == method::RIGHT_SHIFT || - m == method::RIGHT_CONTROL || m == method::RIGHT_ALT || m == method::RIGHT_SUPER || m == method::MENU || - m == method::MOUSE_LEFT || m == method::MOUSE_MIDDLE || m == method::MOUSE_RIGHT || - m == method::GAMEPAD_RIGHT_THUMB || m == method::GAMEPAD_LEFT_THUMB; + m == method::LEFT_CONTROL || m == method::LEFT_ALT || m == method::LEFT_SUPER || + m == method::RIGHT_SHIFT || m == method::RIGHT_CONTROL || m == method::RIGHT_ALT || + m == method::RIGHT_SUPER || m == method::MENU || m == method::MOUSE_LEFT || + m == method::MOUSE_MIDDLE || m == method::MOUSE_RIGHT || m == method::GAMEPAD_RIGHT_THUMB || + m == method::GAMEPAD_LEFT_THUMB; } - static constexpr bool has_keys_and_axis(method m) - { - return is_key(m) && is_axis(m); - } + static constexpr bool has_keys_and_axis(method m) { return is_key(m) && is_axis(m); } static constexpr bool is_family(method m) { @@ -406,30 +392,43 @@ namespace rythe::application case method::JOY: { return std::set{ - method::GAMEPAD_A, method::GAMEPAD_B, method::GAMEPAD_X, method::GAMEPAD_Y, method::GAMEPAD_LEFT_BUMPER, - method::GAMEPAD_RIGHT_BUMPER, method::GAMEPAD_BACK, method::GAMEPAD_START, method::GAMEPAD_GUIDE, - method::GAMEPAD_DPAD_UP, method::GAMEPAD_DPAD_RIGHT, - method::GAMEPAD_DPAD_DOWN, method::GAMEPAD_DPAD_LEFT, method::GAMEPAD_LEFT_X, method::GAMEPAD_LEFT_Y, - method::GAMEPAD_RIGHT_X, method::GAMEPAD_RIGHT_Y, method::GAMEPAD_LEFT_TRIGGER, method::GAMEPAD_RIGHT_TRIGGER, - method::GAMEPAD_RIGHT_THUMB, method::GAMEPAD_LEFT_THUMB + method::GAMEPAD_A, + method::GAMEPAD_B, + method::GAMEPAD_X, + method::GAMEPAD_Y, + method::GAMEPAD_LEFT_BUMPER, + method::GAMEPAD_RIGHT_BUMPER, + method::GAMEPAD_BACK, + method::GAMEPAD_START, + method::GAMEPAD_GUIDE, + method::GAMEPAD_DPAD_UP, + method::GAMEPAD_DPAD_RIGHT, + method::GAMEPAD_DPAD_DOWN, + method::GAMEPAD_DPAD_LEFT, + method::GAMEPAD_LEFT_X, + method::GAMEPAD_LEFT_Y, + method::GAMEPAD_RIGHT_X, + method::GAMEPAD_RIGHT_Y, + method::GAMEPAD_LEFT_TRIGGER, + method::GAMEPAD_RIGHT_TRIGGER, + method::GAMEPAD_RIGHT_THUMB, + method::GAMEPAD_LEFT_THUMB }; } case method::GAMEPAD_LEFT_AXIS: { - return std::set{ - method::GAMEPAD_LEFT_X, method::GAMEPAD_LEFT_Y - }; + return std::set{method::GAMEPAD_LEFT_X, method::GAMEPAD_LEFT_Y}; } case method::GAMEPAD_RIGHT_AXIS: { - return std::set{ - method::GAMEPAD_RIGHT_X, method::GAMEPAD_RIGHT_Y - }; + return std::set{method::GAMEPAD_RIGHT_X, method::GAMEPAD_RIGHT_Y}; } case method::MOUSE: { - return std::set{method::MOUSE_Y, method::MOUSE_X, method::MOUSE_LEFT, method::MOUSE_MIDDLE, method::MOUSE_RIGHT}; + return std::set{ + method::MOUSE_Y, method::MOUSE_X, method::MOUSE_LEFT, method::MOUSE_MIDDLE, method::MOUSE_RIGHT + }; } case method::SCROLL: diff --git a/src/application/input/inputsystem.hpp b/src/application/input/inputsystem.hpp index 02d8f02..86a344c 100644 --- a/src/application/input/inputsystem.hpp +++ b/src/application/input/inputsystem.hpp @@ -12,7 +12,8 @@ namespace rythe::application class InputSystem : public core::System { - using action_callback = rsl::delegate; + using action_callback = + rsl::delegate; struct action_data { @@ -26,7 +27,8 @@ namespace rythe::application bool repeat; }; - using axis_callback = rsl::delegate; + using axis_callback = + rsl::delegate; struct axis_data { @@ -66,7 +68,8 @@ namespace rythe::application // that are already connected // note that GLFW only supports 16 gamepads! - for (rsl::size_type i = 0; i < inputmap::modifier_keys::MAX_SIZE - inputmap::modifier_keys::JOYSTICK0; ++i) + for (rsl::size_type i = 0; i < inputmap::modifier_keys::MAX_SIZE - inputmap::modifier_keys::JOYSTICK0; + ++i) { if (ContextHelper::joystickPresent(i)) { @@ -78,15 +81,9 @@ namespace rythe::application // ContextHelper::updateGamepadMappings("assets/conf/gamepad.conf"); } - inline static math::double2 getMousePosition() - { - return m_mousePos; - } + inline static math::double2 getMousePosition() { return m_mousePos; } - inline static math::double2 getMouseDelta() - { - return m_mouseDelta; - } + inline static math::double2 getMouseDelta() { return m_mouseDelta; } /** * @brief Creates a Binding of a Key /Axis to the emission of an event in the event bus. @@ -99,7 +96,10 @@ namespace rythe::application template static void createBinding(inputmap::method k, float value = 1) { - static_assert(std::is_base_of_v, Event> || std::is_base_of_v, Event>, "Event needs to either be an input_action or an input_axis"); + static_assert( + std::is_base_of_v, Event> || std::is_base_of_v, Event>, + "Event needs to either be an input_action or an input_axis" + ); if constexpr (std::is_base_of_v, Event>) { @@ -167,7 +167,10 @@ namespace rythe::application template static void removeBinding(inputmap::method met) { - static_assert(std::is_base_of_v, Event> || std::is_base_of_v, Event>, "Event needs to either be an input_action or an input_axis"); + static_assert( + std::is_base_of_v, Event> || std::is_base_of_v, Event>, + "Event needs to either be an input_action or an input_axis" + ); if constexpr (std::is_base_of_v, Event>) { @@ -236,16 +239,15 @@ namespace rythe::application // creates a tuple with default value 0 auto& data = m_actions[m][rsl::typeHash()]; - data.callback = action_callback::create( - [](InputSystem* self, bool state, inputmap::modifier_keys mods, inputmap::method method, float def, float delta) + data.callback = action_callback::create([](InputSystem* self, bool state, inputmap::modifier_keys mods, + inputmap::method method, float def, float delta) { (void)def; Event e; e.input_delta = delta; e.set(state, mods, method); self->raiseEvent(e); - } - ); + }); data.last_method = m; data.last_mods = inputmap::modifier_keys::NONE; data.repeat = false; @@ -256,12 +258,9 @@ namespace rythe::application { auto& data = m_actions[m][rsl::typeHash()]; - data.callback = action_callback::create( - [](InputSystem* self, bool state, inputmap::modifier_keys mods, inputmap::method method, float def, float delta) - { - self->pushCommand(state ? def : 0.0f, mods, method); - } - ); + data.callback = action_callback::create([](InputSystem* self, bool state, inputmap::modifier_keys mods, + inputmap::method method, float def, float delta) + { self->pushCommand(state ? def : 0.0f, mods, method); }); data.trigger_value = value; data.last_method = m; data.last_mods = inputmap::modifier_keys::NONE; @@ -275,15 +274,14 @@ namespace rythe::application auto& data = m_axes[m][rsl::typeHash()]; - data.callback = axis_callback::create( - [](InputSystem* self, float value, inputmap::modifier_keys mods, inputmap::method method, float delta) + data.callback = axis_callback::create([](InputSystem* self, float value, inputmap::modifier_keys mods, + inputmap::method method, float delta) { Event e; e.input_delta = delta; e.set(value > 0.05f || value < -0.05f, mods, method); // convert float range 0-1 to key state false:true self->raiseEvent(e); - } - ); + }); data.last_value = value; data.last_method = m; @@ -296,12 +294,9 @@ namespace rythe::application auto& data = m_axes[m][rsl::typeHash()]; - data.callback = axis_callback::create( - [](InputSystem* self, float value, inputmap::modifier_keys mods, inputmap::method method, float delta) - { - self->pushCommand(value, mods, method); - } - ); + data.callback = axis_callback::create([](InputSystem* self, float value, inputmap::modifier_keys mods, + inputmap::method method, float delta) + { self->pushCommand(value, mods, method); }); data.last_value = value; data.last_method = m; @@ -312,9 +307,13 @@ namespace rythe::application static void onCheckGamepadPresence(int jid, int event) { if (event == GLFW_CONNECTED) + { m_presentGamepads.insert(jid); + } else if (event == GLFW_DISCONNECTED) + { m_presentGamepads.erase(jid); + } } void onUpdate(rsl::time_span deltaTime) @@ -338,7 +337,12 @@ namespace rythe::application for (auto [_, action] : inner_map) { if (action.repeat) - action.callback(this, action.last_state, action.last_mods, action.last_method, action.trigger_value, deltaTime); + { + action.callback( + this, action.last_state, action.last_mods, action.last_method, action.trigger_value, + deltaTime + ); + } } } } @@ -348,7 +352,10 @@ namespace rythe::application onMouseReset(); } - void matchGLFWAxisWithSignalAxis(const GLFWgamepadstate& state, inputmap::modifier_keys joystick, const rsl::size_type glfw, inputmap::method m) + void matchGLFWAxisWithSignalAxis( + const GLFWgamepadstate& state, inputmap::modifier_keys joystick, const rsl::size_type glfw, + inputmap::method m + ) { const float value = state.axes[glfw]; for (auto [_, axis] : m_axes[m]) @@ -385,45 +392,121 @@ namespace rythe::application GLFWgamepadstate state; if (!ContextHelper::getGamepadSate(glfw_joystick_id, &state)) + { continue; + } const auto joystick = mods::JOYSTICK0 + glfw_joystick_id; for (auto [_, action] : m_actions[method::GAMEPAD_A]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_A], joystick, method::GAMEPAD_A, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_A], joystick, method::GAMEPAD_A, action.trigger_value, + dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_B]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_B], joystick, method::GAMEPAD_B, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_B], joystick, method::GAMEPAD_B, action.trigger_value, + dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_X]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_X], joystick, method::GAMEPAD_X, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_X], joystick, method::GAMEPAD_X, action.trigger_value, + dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_Y]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_Y], joystick, method::GAMEPAD_Y, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_Y], joystick, method::GAMEPAD_Y, action.trigger_value, + dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_LEFT_BUMPER]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER], joystick, method::GAMEPAD_LEFT_BUMPER, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER], joystick, method::GAMEPAD_LEFT_BUMPER, + action.trigger_value, dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_RIGHT_BUMPER]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER], joystick, method::GAMEPAD_RIGHT_BUMPER, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER], joystick, method::GAMEPAD_RIGHT_BUMPER, + action.trigger_value, dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_BACK]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_BACK], joystick, method::GAMEPAD_BACK, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_BACK], joystick, method::GAMEPAD_BACK, + action.trigger_value, dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_START]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_START], joystick, method::GAMEPAD_START, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_START], joystick, method::GAMEPAD_START, + action.trigger_value, dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_DPAD_UP]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_UP], joystick, method::GAMEPAD_DPAD_UP, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_UP], joystick, method::GAMEPAD_DPAD_UP, + action.trigger_value, dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_DPAD_RIGHT]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_RIGHT], joystick, method::GAMEPAD_DPAD_RIGHT, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_RIGHT], joystick, method::GAMEPAD_DPAD_RIGHT, + action.trigger_value, dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_DPAD_LEFT]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_LEFT], joystick, method::GAMEPAD_DPAD_LEFT, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_LEFT], joystick, method::GAMEPAD_DPAD_LEFT, + action.trigger_value, dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_DPAD_DOWN]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_DOWN], joystick, method::GAMEPAD_DPAD_DOWN, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_DOWN], joystick, method::GAMEPAD_DPAD_DOWN, + action.trigger_value, dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_LEFT_THUMB]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_LEFT_THUMB], joystick, method::GAMEPAD_LEFT_THUMB, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_LEFT_THUMB], joystick, method::GAMEPAD_LEFT_THUMB, + action.trigger_value, dt + ); + } for (auto [_, action] : m_actions[method::GAMEPAD_RIGHT_THUMB]) - action.callback(this, state.buttons[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB], joystick, method::GAMEPAD_RIGHT_THUMB, action.trigger_value, dt); + { + action.callback( + this, state.buttons[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB], joystick, method::GAMEPAD_RIGHT_THUMB, + action.trigger_value, dt + ); + } matchGLFWAxisWithSignalAxis(state, joystick, GLFW_GAMEPAD_AXIS_LEFT_X, method::GAMEPAD_LEFT_X); matchGLFWAxisWithSignalAxis(state, joystick, GLFW_GAMEPAD_AXIS_LEFT_Y, method::GAMEPAD_LEFT_Y); - matchGLFWAxisWithSignalAxis(state, joystick, GLFW_GAMEPAD_AXIS_LEFT_TRIGGER, method::GAMEPAD_LEFT_TRIGGER); + matchGLFWAxisWithSignalAxis( + state, joystick, GLFW_GAMEPAD_AXIS_LEFT_TRIGGER, method::GAMEPAD_LEFT_TRIGGER + ); matchGLFWAxisWithSignalAxis(state, joystick, GLFW_GAMEPAD_AXIS_RIGHT_X, method::GAMEPAD_RIGHT_X); matchGLFWAxisWithSignalAxis(state, joystick, GLFW_GAMEPAD_AXIS_RIGHT_Y, method::GAMEPAD_RIGHT_Y); - matchGLFWAxisWithSignalAxis(state, joystick, GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, method::GAMEPAD_RIGHT_TRIGGER); + matchGLFWAxisWithSignalAxis( + state, joystick, GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, method::GAMEPAD_RIGHT_TRIGGER + ); } } @@ -433,13 +516,19 @@ namespace rythe::application mods result = mods::NONE; if (glfw_mods & GLFW_MOD_ALT) + { result = static_cast(result & inputmap::modifier_keys::ALT); + } if (glfw_mods & GLFW_MOD_SHIFT) + { result = static_cast(result & inputmap::modifier_keys::SHIFT); + } if (glfw_mods & GLFW_MOD_CONTROL) + { result = static_cast(result & inputmap::modifier_keys::CTRL); + } return result; } @@ -453,7 +542,11 @@ namespace rythe::application action.last_mods = translateModifierKeys(window_key_event.mods); action.last_method = m; if (!action.repeat) - action.callback(this, action.last_state, action.last_mods, action.last_method, action.trigger_value, 0.0f); + { + action.callback( + this, action.last_state, action.last_mods, action.last_method, action.trigger_value, 0.0f + ); + } } } @@ -462,10 +555,14 @@ namespace rythe::application m_mouseDelta = window_mouse_event.position - m_mousePos; m_mousePos = window_mouse_event.position; if (math::abs(m_mouseDelta.x) < 0.0001) + { m_mouseDelta.x = 0.0; + } if (math::abs(m_mouseDelta.y) < 0.0001) + { m_mouseDelta.y = 0.0; + } for (auto [_, axis] : m_axes[inputmap::method::MOUSE_X]) { @@ -493,7 +590,12 @@ namespace rythe::application action.last_mods = translateModifierKeys(window_mouse_event.mods); action.last_method = inputmap::method::MOUSE_LEFT; if (!action.repeat) - action.callback(this, action.last_state, action.last_mods, action.last_method, action.trigger_value, 0.0f); + { + action.callback( + this, action.last_state, action.last_mods, action.last_method, action.trigger_value, + 0.0f + ); + } } break; } @@ -505,7 +607,12 @@ namespace rythe::application action.last_mods = translateModifierKeys(window_mouse_event.mods); action.last_method = inputmap::method::MOUSE_MIDDLE; if (!action.repeat) - action.callback(this, action.last_state, action.last_mods, action.last_method, action.trigger_value, 0.0f); + { + action.callback( + this, action.last_state, action.last_mods, action.last_method, action.trigger_value, + 0.0f + ); + } } break; } @@ -517,7 +624,12 @@ namespace rythe::application action.last_mods = translateModifierKeys(window_mouse_event.mods); action.last_method = inputmap::method::MOUSE_RIGHT; if (!action.repeat) - action.callback(this, action.last_state, action.last_mods, action.last_method, action.trigger_value, 0.0f); + { + action.callback( + this, action.last_state, action.last_mods, action.last_method, action.trigger_value, + 0.0f + ); + } } break; } diff --git a/src/application/module/applicationmodule.hpp b/src/application/module/applicationmodule.hpp index d1f7d5d..f80dcc9 100644 --- a/src/application/module/applicationmodule.hpp +++ b/src/application/module/applicationmodule.hpp @@ -18,9 +18,6 @@ namespace rythe::application reportSystem(); } - virtual rsl::priority_type priority() override - { - return 100; - } + virtual rsl::priority_type priority() override { return 100; } }; } // namespace rythe::application diff --git a/src/application/window/window.cpp b/src/application/window/window.cpp index 9fd0ea1..579196e 100644 --- a/src/application/window/window.cpp +++ b/src/application/window/window.cpp @@ -52,17 +52,23 @@ namespace rythe::application win.lock->lock(); if (!WindowSystem::windowStillExists(win.handle)) + { return; + } m_contextIsValid = ContextHelper::makeContextCurrent(win); if (!m_contextIsValid) + { log::warn("Context is no longer valid."); + } } context_guard::~context_guard() { if (!m_contextIsValid) + { return; + } ContextHelper::makeContextCurrent(nullptr); m_win.lock->unlock(); diff --git a/src/application/window/windowsystem.cpp b/src/application/window/windowsystem.cpp index b3fe011..cf955d7 100644 --- a/src/application/window/windowsystem.cpp +++ b/src/application/window/windowsystem.cpp @@ -17,7 +17,9 @@ namespace rythe::application void WindowSystem::closeWindow(GLFWwindow* window) { if (!ContextHelper::initialized()) + { return; + } { std::lock_guard guard(m_creationLock); // Lock all creation sensitive data. @@ -27,16 +29,22 @@ namespace rythe::application async::spinlock* lock = nullptr; if (handle.valid()) { - raiseEvent(m_windowComponents[window]); // Trigger any callbacks that want to know about any windows closing. + raiseEvent(m_windowComponents[window] + ); // Trigger any callbacks that want to know about any windows closing. - if (!ContextHelper::windowShouldClose(window)) // If a callback canceled the window destruction then we should cancel. + if (!ContextHelper::windowShouldClose(window + )) // If a callback canceled the window destruction then we should cancel. + { return; + } { lock = handle->lock; - std::lock_guard guard(*lock); // "deleting" the window is technically writing, so we copy the pointer and use that to lock it. - handle.get() = invalid_window; // We mark the window as deleted without deleting it yet. It can cause users to find invalid windows, - // but at least they won't use a destroyed component after the lock unlocks. + std::lock_guard guard(*lock + ); // "deleting" the window is technically writing, so we copy the pointer and use that to lock it. + handle.get() = invalid_window; // We mark the window as deleted without deleting it yet. It can + // cause users to find invalid windows, but at least they won't use a + // destroyed component after the lock unlocks. handle.destroy(); m_windowComponents.erase(window); @@ -44,20 +52,26 @@ namespace rythe::application if (handle.owner == ecs::world) { - raiseEvent(); // If the current window we're closing is the main window we want to close the application. + raiseEvent( + ); // If the current window we're closing is the main window we want to close the application. } // (we might want to leave this up to the user at some point.) - ContextHelper::destroyWindow(window); // After all traces of the window throughout the engine have been erased we actually close the window. + ContextHelper::destroyWindow(window); // After all traces of the window throughout the engine have been + // erased we actually close the window. } if (lock) + { delete lock; + } } } void WindowSystem::onWindowMoved(GLFWwindow* window, int x, int y) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], math::int2(x, y)); + } } void WindowSystem::onWindowResize(GLFWwindow* win, int width, int height) @@ -73,79 +87,108 @@ namespace rythe::application void WindowSystem::onWindowRefresh(GLFWwindow* window) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window]); + } } void WindowSystem::onWindowFocus(GLFWwindow* window, int focused) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], focused); + } } void WindowSystem::onWindowIconify(GLFWwindow* window, int iconified) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], iconified); + } } void WindowSystem::onWindowMaximize(GLFWwindow* window, int maximized) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], maximized); + } } void WindowSystem::onWindowFrameBufferResize(GLFWwindow* window, int width, int height) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], math::int2(width, height)); + } } void WindowSystem::onWindowContentRescale(GLFWwindow* window, float xscale, float yscale) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], math::vec2(xscale, xscale)); + } } void WindowSystem::onItemDroppedInWindow(GLFWwindow* window, int count, const char** paths) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], count, paths); + } } void WindowSystem::onMouseEnterWindow(GLFWwindow* window, int entered) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], entered); + } } void WindowSystem::onKeyInput(GLFWwindow* window, int key, int scancode, int action, int mods) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], key, scancode, action, mods); + } } void WindowSystem::onCharInput(GLFWwindow* window, rsl::uint codepoint) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], codepoint); + } } void WindowSystem::onMouseMoved(GLFWwindow* window, double xpos, double ypos) { if (m_windowComponents.contains(window)) - raiseEvent(m_windowComponents[window], math::double2(xpos, ypos) / (math::dvec2)ContextHelper::getFramebufferSize(window)); + { + raiseEvent( + m_windowComponents[window], + math::double2(xpos, ypos) / (math::dvec2)ContextHelper::getFramebufferSize(window) + ); + } } void WindowSystem::onMouseButton(GLFWwindow* window, int button, int action, int mods) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], button, action, mods); + } } void WindowSystem::onMouseScroll(GLFWwindow* window, double xoffset, double yoffset) { if (m_windowComponents.contains(window)) + { raiseEvent(m_windowComponents[window], math::double2(xoffset, yoffset)); + } } void WindowSystem::onExit(events::exit& event) @@ -165,17 +208,22 @@ namespace rythe::application { lock = win.lock; - std::lock_guard guard(*lock); // "deleting" the window is technically writing, so we copy the pointer and use that to lock it. - win = invalid_window; // We mark the window as deleted without deleting it yet. It can cause users to find invalid windows, - // but at least they won't use a destroyed component after the lock unlocks. + std::lock_guard guard(*lock + ); // "deleting" the window is technically writing, so we copy the pointer and use that to lock it. + win = invalid_window; // We mark the window as deleted without deleting it yet. It can cause users + // to find invalid windows, but at least they won't use a destroyed component + // after the lock unlocks. handle.destroy(); m_windowComponents.erase(win); } - ContextHelper::destroyWindow(win); // After all traces of the window throughout the engine have been erased we actually close the window. + ContextHelper::destroyWindow(win); // After all traces of the window throughout the engine have been + // erased we actually close the window. } if (lock) + { delete lock; + } } m_exit = true; @@ -195,7 +243,9 @@ namespace rythe::application m_iconRequests.emplace_back(entityId, icon); } else + { log::warn("Icon change denied, invalid entity given."); + } } void WindowSystem::requestIconChange(rsl::id_type entityId, const std::string& iconName) @@ -206,7 +256,9 @@ namespace rythe::application m_iconRequests.emplace_back(entityId, iconName); } else + { log::warn("Icon change denied, invalid entity given."); + } } void WindowSystem::requestFullscreenToggle(rsl::id_type entityId, math::int2 position, math::int2 size) @@ -217,10 +269,15 @@ namespace rythe::application m_fullscreenRequests.emplace_back(entityId, position, size); } else + { log::warn("Fullscreen toggle denied, invalid entity given."); + } } - void WindowSystem::requestWindow(rsl::id_type entityId, math::int2 size, const std::string& name, assets::asset icon, GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints) + void WindowSystem::requestWindow( + rsl::id_type entityId, math::int2 size, const std::string& name, assets::asset icon, + GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints + ) { if (entityId) { @@ -228,10 +285,15 @@ namespace rythe::application m_creationRequests.emplace_back(entityId, size, name, icon, monitor, share, swapInterval, hints); } else + { log::warn("Window creation denied, invalid entity given."); + } } - void WindowSystem::requestWindow(rsl::id_type entityId, math::int2 size, const std::string& name, assets::asset icon, GLFWmonitor* monitor, GLFWwindow* share, int swapInterval) + void WindowSystem::requestWindow( + rsl::id_type entityId, math::int2 size, const std::string& name, assets::asset icon, + GLFWmonitor* monitor, GLFWwindow* share, int swapInterval + ) { if (entityId) { @@ -239,10 +301,15 @@ namespace rythe::application m_creationRequests.emplace_back(entityId, size, name, icon, monitor, share, swapInterval); } else + { log::warn("Window creation denied, invalid entity given."); + } } - void WindowSystem::requestWindow(rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints) + void WindowSystem::requestWindow( + rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, + GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints + ) { if (entityId) { @@ -250,10 +317,15 @@ namespace rythe::application m_creationRequests.emplace_back(entityId, size, name, iconName, monitor, share, swapInterval); } else + { log::warn("Window creation denied, invalid entity given."); + } } - void WindowSystem::requestWindow(rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, GLFWmonitor* monitor, GLFWwindow* share, int swapInterval) + void WindowSystem::requestWindow( + rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, + GLFWmonitor* monitor, GLFWwindow* share, int swapInterval + ) { if (entityId) { @@ -261,7 +333,9 @@ namespace rythe::application m_creationRequests.emplace_back(entityId, size, name, iconName, monitor, share, swapInterval); } else + { log::warn("Window creation denied, invalid entity given."); + } } void WindowSystem::setup() @@ -269,20 +343,31 @@ namespace rythe::application using namespace filesystem::literals; m_exit = false; - m_defaultIcon = assets::load("RYTHE Icon", "engine://resources/rythe/icon.png"_view, assets::import_settings{true, true, false}); + m_defaultIcon = assets::load( + "RYTHE Icon", "engine://resources/rythe/icon.png"_view, assets::import_settings{true, true, false} + ); bindToEvent(); - if (m_creationRequests.empty() || (std::find_if(m_creationRequests.begin(), m_creationRequests.end(), [](window_request& r) - { return r.entityId == ecs::world_entity_id; }) == m_creationRequests.end())) - requestWindow(ecs::world, math::int2(1360, 768), "RYTHE Engine", assets::invalid_asset, nullptr, nullptr, 1); // Create the request for the main window. + if (m_creationRequests.empty() || (std::find_if( + m_creationRequests.begin(), m_creationRequests.end(), + [](window_request& r) { return r.entityId == ecs::world_entity_id; } + ) == m_creationRequests.end())) + { + requestWindow( + ecs::world, math::int2(1360, 768), "RYTHE Engine", assets::invalid_asset, nullptr, nullptr, 1 + ); // Create the request for the main window. + } - if (!ContextHelper::initialized()) // Initialize context. + if (!ContextHelper::initialized()) // Initialize context. + { if (!ContextHelper::init()) { exit(); - return; // If we can't initialize we can't create any windows, not creating the main window means the engine should shut down. + return; // If we can't initialize we can't create any windows, not creating the main window means the + // engine should shut down. } + } log::trace("Creating main window."); createWindows(); showMainWindow(); @@ -294,7 +379,9 @@ namespace rythe::application void WindowSystem::createWindows() { if (m_exit) // If the engine is exiting then we can't create new windows. + { return; + } std::lock_guard guard(m_creationRequestLock); for (auto& request : m_creationRequests) @@ -310,7 +397,9 @@ namespace rythe::application if (request.hints.size()) { for (auto& [hint, value] : request.hints) + { ContextHelper::windowHint(hint, value); + } } else // Default window hints. { @@ -322,7 +411,9 @@ namespace rythe::application GLFWmonitor* monitor = request.monitor; if (!request.monitor) + { monitor = ContextHelper::getPrimaryMonitor(); + } const GLFWvidmode* mode = ContextHelper::getVideoMode(monitor); @@ -333,18 +424,25 @@ namespace rythe::application } if (request.size == math::int2(0, 0)) + { request.size = {400, 400}; + } if (request.name.empty()) + { request.name = "RYTHE Engine"; + } assets::asset icon = request.icon; if (icon == assets::invalid_asset) + { icon = m_defaultIcon; + } ContextHelper::windowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); - window win = ContextHelper::createWindow(request.size, request.name.c_str(), request.monitor, request.share); + window win = + ContextHelper::createWindow(request.size, request.name.c_str(), request.monitor, request.share); if (icon->components() == image_components::rgba && icon->format() == channel_format::eight_bit) { @@ -387,8 +485,10 @@ namespace rythe::application { win.lock = new async::spinlock(); - std::lock_guard wguard(*win.lock); // This is the only code that has access to win.lock right now, so there's no deadlock risk. - std::lock_guard cguard(m_creationLock); // Locking them both separately is faster than using a multilock. + std::lock_guard wguard(*win.lock + ); // This is the only code that has access to win.lock right now, so there's no deadlock risk. + std::lock_guard cguard(m_creationLock + ); // Locking them both separately is faster than using a multilock. m_windowComponents.insert(win, handle); handle = ent.add_component(win); @@ -425,7 +525,9 @@ namespace rythe::application void WindowSystem::fullscreenWindows() { if (m_exit) // If the engine is exiting then we can't change any windows. + { return; + } std::lock_guard guard(m_fullscreenRequestLock); for (auto& request : m_fullscreenRequests) @@ -461,7 +563,9 @@ namespace rythe::application GLFWmonitor* monitor = ContextHelper::getCurrentMonitor(win); const GLFWvidmode* mode = ContextHelper::getVideoMode(monitor); - ContextHelper::setWindowMonitor(win, monitor, {0, 0}, math::int2(mode->width, mode->height), mode->refreshRate); + ContextHelper::setWindowMonitor( + win, monitor, {0, 0}, math::int2(mode->width, mode->height), mode->refreshRate + ); win.m_size = math::int2(mode->width, mode->height); ContextHelper::makeContextCurrent(win); ContextHelper::swapInterval(win.m_swapInterval); @@ -477,7 +581,9 @@ namespace rythe::application void WindowSystem::updateWindowIcons() { if (m_exit) // If the engine is exiting then we can't change any windows. + { return; + } std::lock_guard guard(m_iconRequestLock); for (auto& request : m_iconRequests) @@ -497,9 +603,14 @@ namespace rythe::application continue; } - if (request.icon->components() != image_components::rgba || request.icon->format() != channel_format::eight_bit) + if (request.icon->components() != image_components::rgba || + request.icon->format() != channel_format::eight_bit) { - log::warn("Icon change denied, image {} has the wrong format. The needed format is 4 channels with 8 bits per channel.", request.icon.name()); + log::warn( + "Icon change denied, image {} has the wrong format. The needed format is 4 channels with 8 bits " + "per channel.", + request.icon.name() + ); continue; } @@ -513,7 +624,9 @@ namespace rythe::application void WindowSystem::refreshWindows(rsl::time_span deltaTime) { if (!ContextHelper::initialized()) + { return; + } std::lock_guard guard(m_creationLock); static ecs::filter windowFilter; @@ -523,7 +636,9 @@ namespace rythe::application { context_guard guard(win); if (!guard.contextIsValid()) + { continue; + } ContextHelper::swapBuffers(win); } } @@ -536,7 +651,9 @@ namespace rythe::application fullscreenWindows(); if (!ContextHelper::initialized()) + { return; + } ContextHelper::pollEvents(); ContextHelper::updateWindowFocus(); diff --git a/src/application/window/windowsystem.hpp b/src/application/window/windowsystem.hpp index 3595f17..16fa808 100644 --- a/src/application/window/windowsystem.hpp +++ b/src/application/window/windowsystem.hpp @@ -31,7 +31,10 @@ namespace rythe::application int swapInterval; std::vector> hints; - window_request(rsl::id_type entityId, math::int2 size, const std::string& name, assets::asset icon, GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints) + window_request( + rsl::id_type entityId, math::int2 size, const std::string& name, assets::asset icon, + GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints + ) : entityId(entityId), size(size), name(name), @@ -42,7 +45,11 @@ namespace rythe::application hints(hints) { } - window_request(rsl::id_type entityId, math::int2 size = {400, 400}, const std::string& name = "RYTHE Engine", assets::asset icon = assets::invalid_asset, GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr, int swapInterval = 0) + window_request( + rsl::id_type entityId, math::int2 size = {400, 400}, const std::string& name = "RYTHE Engine", + assets::asset icon = assets::invalid_asset, GLFWmonitor* monitor = nullptr, + GLFWwindow* share = nullptr, int swapInterval = 0 + ) : entityId(entityId), size(size), name(name), @@ -53,7 +60,10 @@ namespace rythe::application { } - window_request(rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints) + window_request( + rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, + GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints + ) : entityId(entityId), size(size), name(name), @@ -64,7 +74,10 @@ namespace rythe::application hints(hints) { } - window_request(rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr, int swapInterval = 0) + window_request( + rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, + GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr, int swapInterval = 0 + ) : entityId(entityId), size(size), name(name), @@ -82,7 +95,9 @@ namespace rythe::application math::int2 position; math::int2 size; - fullscreen_toggle_request(rsl::id_type entityId, math::int2 position = {100, 100}, math::int2 size = {400, 400}) + fullscreen_toggle_request( + rsl::id_type entityId, math::int2 position = {100, 100}, math::int2 size = {400, 400} + ) : entityId(entityId), position(position), size(size) @@ -110,17 +125,18 @@ namespace rythe::application static sparse_map> m_windowComponents; static async::spinlock m_creationLock; - bool m_exit; // Keep track of whether the exit event has been raised. - // If any window requests happen after this boolean has been set then they will be denied. + bool m_exit; // Keep track of whether the exit event has been raised. + // If any window requests happen after this boolean has been set then they will be denied. - static async::spinlock m_creationRequestLock; // Lock to keep the creation request list thread-safe. - static std::vector m_creationRequests; // List of requests since the last creation loop. + static async::spinlock m_creationRequestLock; // Lock to keep the creation request list thread-safe. + static std::vector m_creationRequests; // List of requests since the last creation loop. - static async::spinlock m_fullscreenRequestLock; // Lock to keep the fullscreen request list thread-safe. - static std::vector m_fullscreenRequests; // List of requests since the last fullscreen update loop. + static async::spinlock m_fullscreenRequestLock; // Lock to keep the fullscreen request list thread-safe. + static std::vector + m_fullscreenRequests; // List of requests since the last fullscreen update loop. - static async::spinlock m_iconRequestLock; // Lock to keep the icon request list thread-safe. - static std::vector m_iconRequests; // List of requests since the last icon update loop. + static async::spinlock m_iconRequestLock; // Lock to keep the icon request list thread-safe. + static std::vector m_iconRequests; // List of requests since the last icon update loop. // Internal function for closing a window safely. static void closeWindow(GLFWwindow* window); @@ -167,22 +183,30 @@ namespace rythe::application static void requestIconChange(rsl::id_type entityId, assets::asset icon); static void requestIconChange(rsl::id_type entityId, const std::string& iconName); - static void requestFullscreenToggle(rsl::id_type entityId, math::int2 position = {100, 100}, math::int2 size = {400, 400}); - - static void requestWindow(rsl::id_type entityId, math::int2 size, const std::string& name, assets::asset icon, GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints); - static void requestWindow(rsl::id_type entityId, math::int2 size = {400, 400}, const std::string& name = "RYTHE Engine", assets::asset icon = assets::invalid_asset, GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr, int swapInterval = 0); - static void requestWindow(rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints); - static void requestWindow(rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr, int swapInterval = 0); - - void showMainWindow() - { - ContextHelper::showWindow(ecs::world.get_component()->handle); - } - - void exit() - { - raiseEvent(); - } + static void + requestFullscreenToggle(rsl::id_type entityId, math::int2 position = {100, 100}, math::int2 size = {400, 400}); + + static void requestWindow( + rsl::id_type entityId, math::int2 size, const std::string& name, assets::asset icon, + GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints + ); + static void requestWindow( + rsl::id_type entityId, math::int2 size = {400, 400}, const std::string& name = "RYTHE Engine", + assets::asset icon = assets::invalid_asset, GLFWmonitor* monitor = nullptr, + GLFWwindow* share = nullptr, int swapInterval = 0 + ); + static void requestWindow( + rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, + GLFWmonitor* monitor, GLFWwindow* share, int swapInterval, const std::vector>& hints + ); + static void requestWindow( + rsl::id_type entityId, math::int2 size, const std::string& name, const std::string& iconName, + GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr, int swapInterval = 0 + ); + + void showMainWindow() { ContextHelper::showWindow(ecs::world.get_component()->handle); } + + void exit() { raiseEvent(); } virtual void setup();