diff --git a/include/SFML/Graphics/Texture.hpp b/include/SFML/Graphics/Texture.hpp index 1ecc1e716..f49763bb6 100644 --- a/include/SFML/Graphics/Texture.hpp +++ b/include/SFML/Graphics/Texture.hpp @@ -69,6 +69,12 @@ public: //////////////////////////////////////////////////////////// Texture(); + //////////////////////////////////////////////////////////// + /// \brief Destructor + /// + //////////////////////////////////////////////////////////// + ~Texture(); + //////////////////////////////////////////////////////////// /// \brief Copy constructor /// @@ -78,10 +84,22 @@ public: Texture(const Texture& copy); //////////////////////////////////////////////////////////// - /// \brief Destructor + /// \brief Copy assignment operator /// //////////////////////////////////////////////////////////// - ~Texture(); + Texture& operator=(const Texture&); + + //////////////////////////////////////////////////////////// + /// \brief Move constructor + /// + //////////////////////////////////////////////////////////// + Texture(Texture&&) noexcept; + + //////////////////////////////////////////////////////////// + /// \brief Move assignment operator + /// + //////////////////////////////////////////////////////////// + Texture& operator=(Texture&&) noexcept; //////////////////////////////////////////////////////////// /// \brief Create the texture @@ -502,16 +520,6 @@ public: //////////////////////////////////////////////////////////// [[nodiscard]] bool generateMipmap(); - //////////////////////////////////////////////////////////// - /// \brief Overload of assignment operator - /// - /// \param right Instance to assign - /// - /// \return Reference to self - /// - //////////////////////////////////////////////////////////// - Texture& operator=(const Texture& right); - //////////////////////////////////////////////////////////// /// \brief Swap the contents of this texture with those of another /// diff --git a/src/SFML/Graphics/Texture.cpp b/src/SFML/Graphics/Texture.cpp index ed1346015..89e314612 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -38,6 +38,7 @@ #include #include #include +#include namespace @@ -99,6 +100,49 @@ Texture::~Texture() } } +//////////////////////////////////////////////////////////// +Texture::Texture(Texture&& right) noexcept : +m_size(std::exchange(right.m_size, {})), +m_actualSize(std::exchange(right.m_actualSize, {})), +m_texture(std::exchange(right.m_texture, 0)), +m_isSmooth(std::exchange(right.m_isSmooth, false)), +m_sRgb(std::exchange(right.m_sRgb, false)), +m_isRepeated(std::exchange(right.m_isRepeated, false)), +m_fboAttachment(std::exchange(right.m_fboAttachment, false)), +m_cacheId(std::exchange(right.m_cacheId, 0)) +{ +} + +//////////////////////////////////////////////////////////// +Texture& Texture::operator=(Texture&& right) noexcept +{ + // Catch self-moving. + if (&right == this) + { + return *this; + } + + // Destroy the OpenGL texture + if (m_texture) + { + TransientContextLock lock; + + GLuint texture = m_texture; + glCheck(glDeleteTextures(1, &texture)); + } + + // Move old to new. + m_size = std::exchange(right.m_size, {}); + m_actualSize = std::exchange(right.m_actualSize, {}); + m_texture = std::exchange(right.m_texture, 0); + m_isSmooth = std::exchange(right.m_isSmooth, false); + m_sRgb = std::exchange(right.m_sRgb, false); + m_isRepeated = std::exchange(right.m_isRepeated, false); + m_fboAttachment = std::exchange(right.m_fboAttachment, false); + m_cacheId = std::exchange(right.m_cacheId, 0); + return *this; +} + //////////////////////////////////////////////////////////// bool Texture::create(const Vector2u& size) diff --git a/test/Graphics/Texture.test.cpp b/test/Graphics/Texture.test.cpp index 729aebe66..168b1bacc 100644 --- a/test/Graphics/Texture.test.cpp +++ b/test/Graphics/Texture.test.cpp @@ -7,10 +7,8 @@ static_assert(std::is_copy_constructible_v); static_assert(std::is_copy_assignable_v); -static_assert(std::is_move_constructible_v); -static_assert(!std::is_nothrow_move_constructible_v); -static_assert(std::is_move_assignable_v); -static_assert(!std::is_nothrow_move_assignable_v); +static_assert(std::is_nothrow_move_constructible_v); +static_assert(std::is_nothrow_move_assignable_v); static_assert(std::is_nothrow_swappable_v); TEST_CASE("[Graphics] sf::Texture" * doctest::skip(skipDisplayTests))