diff --git a/headers/enduro2d/math/_math.hpp b/headers/enduro2d/math/_math.hpp index 76027560..f7239870 100644 --- a/headers/enduro2d/math/_math.hpp +++ b/headers/enduro2d/math/_math.hpp @@ -664,15 +664,6 @@ namespace e2d { namespace math return approximately(v, T(0), precision); } - // - // enum_to_number - // - - template < typename E > - constexpr std::underlying_type_t enum_to_number(E e) noexcept { - return static_cast>(e); - } - // // lerp/inverse_lerp // diff --git a/headers/enduro2d/utils/_utils.hpp b/headers/enduro2d/utils/_utils.hpp index 714d8e8b..1ce456a8 100644 --- a/headers/enduro2d/utils/_utils.hpp +++ b/headers/enduro2d/utils/_utils.hpp @@ -136,4 +136,14 @@ namespace e2d { namespace utils E2D_ASSERT(begin <= end); return impl::sdbm_hash_impl(init, begin, end); } + + // + // enum_to_underlying + // + + template < typename E + , typename = std::enable_if::value> > + constexpr std::underlying_type_t enum_to_underlying(E e) noexcept { + return static_cast>(e); + } }} diff --git a/sources/enduro2d/core/dbgui.cpp b/sources/enduro2d/core/dbgui.cpp index afe2fc8f..7c6417ba 100644 --- a/sources/enduro2d/core/dbgui.cpp +++ b/sources/enduro2d/core/dbgui.cpp @@ -36,7 +36,7 @@ namespace void on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept final { E2D_UNUSED(scancode); - auto key_i = math::enum_to_number(key); + auto key_i = utils::enum_to_underlying(key); if ( key_i < E2D_COUNTOF(io_.KeysDown) ) { switch ( act ) { case keyboard_key_action::press: @@ -210,7 +210,7 @@ namespace e2d void setup_key_map_(ImGuiIO& io) noexcept { const auto map_key = [&io](ImGuiKey_ imgui_key, keyboard_key key) noexcept { E2D_ASSERT(imgui_key < ImGuiKey_COUNT); - io.KeyMap[imgui_key] = math::enum_to_number(key); + io.KeyMap[imgui_key] = utils::enum_to_underlying(key); }; map_key(ImGuiKey_Tab, keyboard_key::tab); diff --git a/sources/enduro2d/core/input.cpp b/sources/enduro2d/core/input.cpp index 6fde53c0..c2892fcf 100644 --- a/sources/enduro2d/core/input.cpp +++ b/sources/enduro2d/core/input.cpp @@ -18,14 +18,14 @@ namespace e2d v2f cursor_pos; v2f scroll_delta; bitset< - math::enum_to_number(mouse_button::unknown) + 1> pressed; + utils::enum_to_underlying(mouse_button::unknown) + 1> pressed; bitset< - math::enum_to_number(mouse_button::unknown) + 1> just_pressed; + utils::enum_to_underlying(mouse_button::unknown) + 1> just_pressed; bitset< - math::enum_to_number(mouse_button::unknown) + 1> just_released; + utils::enum_to_underlying(mouse_button::unknown) + 1> just_released; public: std::size_t button_index(mouse_button btn) const noexcept { - const auto index = math::enum_to_number(btn); + const auto index = utils::enum_to_underlying(btn); E2D_ASSERT(index < pressed.size()); return math::numeric_cast(index); } @@ -81,14 +81,14 @@ namespace e2d mutable std::mutex mutex; str32 input_text; bitset< - math::enum_to_number(keyboard_key::unknown) + 1> pressed; + utils::enum_to_underlying(keyboard_key::unknown) + 1> pressed; bitset< - math::enum_to_number(keyboard_key::unknown) + 1> just_pressed; + utils::enum_to_underlying(keyboard_key::unknown) + 1> just_pressed; bitset< - math::enum_to_number(keyboard_key::unknown) + 1> just_released; + utils::enum_to_underlying(keyboard_key::unknown) + 1> just_released; public: std::size_t key_index(keyboard_key key) const noexcept { - const auto index = math::enum_to_number(key); + const auto index = utils::enum_to_underlying(key); E2D_ASSERT(index < pressed.size()); return math::numeric_cast(index); } diff --git a/sources/enduro2d/core/render.cpp b/sources/enduro2d/core/render.cpp index 2708da82..a5705e17 100644 --- a/sources/enduro2d/core/render.cpp +++ b/sources/enduro2d/core/render.cpp @@ -47,7 +47,7 @@ namespace }; const pixel_type_description& get_pixel_type_description(pixel_declaration::pixel_type type) noexcept { - const std::size_t index = math::numeric_cast(math::enum_to_number(type)); + const std::size_t index = math::numeric_cast(utils::enum_to_underlying(type)); E2D_ASSERT(index < E2D_COUNTOF(pixel_type_descriptions)); const pixel_type_description& desc = pixel_type_descriptions[index]; E2D_ASSERT(desc.type == type); diff --git a/sources/enduro2d/core/render_impl/render_opengl.cpp b/sources/enduro2d/core/render_impl/render_opengl.cpp index 450c8674..1c5c4a85 100644 --- a/sources/enduro2d/core/render_impl/render_opengl.cpp +++ b/sources/enduro2d/core/render_impl/render_opengl.cpp @@ -802,12 +802,12 @@ namespace e2d } bool need_color = - math::enum_to_number(external_texture) - & math::enum_to_number(render_target::external_texture::color); + utils::enum_to_underlying(external_texture) + & utils::enum_to_underlying(render_target::external_texture::color); bool need_depth = - math::enum_to_number(external_texture) - & math::enum_to_number(render_target::external_texture::depth); + utils::enum_to_underlying(external_texture) + & utils::enum_to_underlying(render_target::external_texture::depth); texture_ptr color; texture_ptr depth; @@ -925,14 +925,14 @@ namespace e2d E2D_ASSERT(is_in_main_thread()); bool clear_color = - math::enum_to_number(command.clear_buffer()) - & math::enum_to_number(clear_command::buffer::color); + utils::enum_to_underlying(command.clear_buffer()) + & utils::enum_to_underlying(clear_command::buffer::color); bool clear_depth = - math::enum_to_number(command.clear_buffer()) - & math::enum_to_number(clear_command::buffer::depth); + utils::enum_to_underlying(command.clear_buffer()) + & utils::enum_to_underlying(clear_command::buffer::depth); bool clear_stencil = - math::enum_to_number(command.clear_buffer()) - & math::enum_to_number(clear_command::buffer::stencil); + utils::enum_to_underlying(command.clear_buffer()) + & utils::enum_to_underlying(clear_command::buffer::stencil); const render_target_ptr& rt = state_->render_target(); bool has_color = !rt || rt->state().color() || !rt->state().color_rb().empty(); diff --git a/sources/enduro2d/core/render_impl/render_opengl_impl.cpp b/sources/enduro2d/core/render_impl/render_opengl_impl.cpp index 98a7a45d..a75eed08 100644 --- a/sources/enduro2d/core/render_impl/render_opengl_impl.cpp +++ b/sources/enduro2d/core/render_impl/render_opengl_impl.cpp @@ -317,10 +317,10 @@ namespace e2d convert_blending_equation(bs.rgb_equation()), convert_blending_equation(bs.alpha_equation()))); GL_CHECK_CODE(debug_, glColorMask( - (math::enum_to_number(bs.color_mask()) & math::enum_to_number(blending_color_mask::r)) != 0, - (math::enum_to_number(bs.color_mask()) & math::enum_to_number(blending_color_mask::g)) != 0, - (math::enum_to_number(bs.color_mask()) & math::enum_to_number(blending_color_mask::b)) != 0, - (math::enum_to_number(bs.color_mask()) & math::enum_to_number(blending_color_mask::a)) != 0)); + (utils::enum_to_underlying(bs.color_mask()) & utils::enum_to_underlying(blending_color_mask::r)) != 0, + (utils::enum_to_underlying(bs.color_mask()) & utils::enum_to_underlying(blending_color_mask::g)) != 0, + (utils::enum_to_underlying(bs.color_mask()) & utils::enum_to_underlying(blending_color_mask::b)) != 0, + (utils::enum_to_underlying(bs.color_mask()) & utils::enum_to_underlying(blending_color_mask::a)) != 0)); state_block_.blending(bs); return *this; diff --git a/untests/sources/untests_math/_math.cpp b/untests/sources/untests_math/_math.cpp index e2a7bfd9..88f026be 100644 --- a/untests/sources/untests_math/_math.cpp +++ b/untests/sources/untests_math/_math.cpp @@ -451,26 +451,6 @@ TEST_CASE("math") { REQUIRE(math::is_near_zero(0.0001f, 0.001f)); REQUIRE(math::is_near_zero(-0.0001f, 0.001f)); } - { - enum class ee_u8 : u8 { - ee1 = 10, - ee2 = 42 - }; - enum class ee_i16 : i16 { - ee1 = 10, - ee2 = 42 - }; - auto e1 = math::enum_to_number(ee_u8::ee1); - auto e2 = math::enum_to_number(ee_i16::ee2); - REQUIRE(e1 == u8(10)); - REQUIRE(e2 == i16(42)); - static_assert( - std::is_same::value, - "static unit test error"); - static_assert( - std::is_same::value, - "static unit test error"); - } { REQUIRE(math::approximately(math::lerp(1.f, 11.f, 0.f), 1.f)); REQUIRE(math::approximately(math::lerp(1.f, 11.f, 0.5f), 6.f)); diff --git a/untests/sources/untests_utils/_utils.cpp b/untests/sources/untests_utils/_utils.cpp index a367e4ad..79ad6e49 100644 --- a/untests/sources/untests_utils/_utils.cpp +++ b/untests/sources/untests_utils/_utils.cpp @@ -9,6 +9,26 @@ using namespace e2d; TEST_CASE("utils") { + { + enum class ee_u8 : u8 { + ee1 = 10, + ee2 = 42 + }; + enum class ee_i16 : i16 { + ee1 = 10, + ee2 = 42 + }; + auto e1 = utils::enum_to_underlying(ee_u8::ee1); + auto e2 = utils::enum_to_underlying(ee_i16::ee2); + REQUIRE(e1 == u8(10)); + REQUIRE(e2 == i16(42)); + static_assert( + std::is_same::value, + "static unit test error"); + static_assert( + std::is_same::value, + "static unit test error"); + } { REQUIRE(utils::sdbm_hash("") == 0u); REQUIRE(utils::sdbm_hash(1u, "") == 1u);