From e2523097d28b2722fab715e6b8dedf1f2449ad04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 22 Nov 2023 18:12:58 +0100 Subject: [PATCH] TextureTools: add a NoCreate constructor to DistanceField. Should have been there for ages already. --- doc/changelog.dox | 5 ++ src/Magnum/TextureTools/DistanceField.cpp | 2 + src/Magnum/TextureTools/DistanceField.h | 16 +++++ src/Magnum/TextureTools/Test/CMakeLists.txt | 72 ++++++++++--------- .../TextureTools/Test/DistanceFieldTest.cpp | 54 ++++++++++++++ 5 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 src/Magnum/TextureTools/Test/DistanceFieldTest.cpp diff --git a/doc/changelog.dox b/doc/changelog.dox index b0e1d9273..c8481ef81 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -810,6 +810,11 @@ See also: needed in order to prevent aligning pixel-perfect fonts to fractional positions. +@subsubsection changelog-latest-changes-texturetools TextureTools library + +- Added a @ref TextureTools::DistanceField::DistanceField(NoCreateT) + constructor allowing to construct the object without a GL context present + @subsubsection changelog-latest-changes-trade Trade library - A changed signature of the @ref Trade::AbstractImporter::doOpenData(Containers::Array&&, DataFlags) diff --git a/src/Magnum/TextureTools/DistanceField.cpp b/src/Magnum/TextureTools/DistanceField.cpp index 6801f74b5..4c0dc1c9a 100644 --- a/src/Magnum/TextureTools/DistanceField.cpp +++ b/src/Magnum/TextureTools/DistanceField.cpp @@ -171,6 +171,8 @@ DistanceField::DistanceField(const UnsignedInt radius): _state{new State{radius} } } +DistanceField::DistanceField(NoCreateT) noexcept {} + DistanceField::DistanceField(DistanceField&&) noexcept = default; DistanceField::~DistanceField() = default; diff --git a/src/Magnum/TextureTools/DistanceField.h b/src/Magnum/TextureTools/DistanceField.h index 11310b5ce..e145bd77f 100644 --- a/src/Magnum/TextureTools/DistanceField.h +++ b/src/Magnum/TextureTools/DistanceField.h @@ -95,6 +95,22 @@ class MAGNUM_TEXTURETOOLS_EXPORT DistanceField { */ explicit DistanceField(UnsignedInt radius); + /** + * @brief Construct without creating the internal OpenGL state + * @m_since_latest + * + * The constructed instance is equivalent to moved-from state, i.e. no + * APIs can be safely called on the object. Useful in cases where you + * will overwrite the instance later anyway. Move another object over + * it to make it useful. + * + * This function can be safely used for constructing (and later + * destructing) objects even without any OpenGL context being active. + * However note that this is a low-level and a potentially dangerous + * API, see the documentation of @ref NoCreate for alternatives. + */ + explicit DistanceField(NoCreateT) noexcept; + /** @brief Copying is not allowed */ DistanceField(const DistanceField&) = delete; diff --git a/src/Magnum/TextureTools/Test/CMakeLists.txt b/src/Magnum/TextureTools/Test/CMakeLists.txt index dc669a981..063f824ad 100644 --- a/src/Magnum/TextureTools/Test/CMakeLists.txt +++ b/src/Magnum/TextureTools/Test/CMakeLists.txt @@ -87,42 +87,46 @@ else() endif() endif() -if(MAGNUM_BUILD_GL_TESTS) - # Otherwise CMake complains that Corrade::PluginManager is not found, wtf - find_package(Corrade REQUIRED PluginManager) +if(MAGNUM_TARGET_GL) + corrade_add_test(TextureToolsDistanceFieldTest DistanceFieldTest.cpp LIBRARIES MagnumTextureTools) - set(TextureToolsDistanceFieldGLTest_SRCS DistanceFieldGLTest.cpp) - if(CORRADE_TARGET_IOS) - # TODO: do this in a generic way in corrade_add_test() - set_source_files_properties(DistanceFieldGLTestFiles PROPERTIES - MACOSX_PACKAGE_LOCATION Resources) - list(APPEND TextureToolsDistanceFieldGLTest_SRCS DistanceFieldGLTestFiles) - endif() - corrade_add_test(TextureToolsDistanceFieldGLTest ${TextureToolsDistanceFieldGLTest_SRCS} - LIBRARIES - MagnumDebugTools - MagnumGL - MagnumOpenGLTester - MagnumTextureToolsTestLib - MagnumTrade - FILES - DistanceFieldGLTestFiles/input.tga - DistanceFieldGLTestFiles/output.tga) - target_include_directories(TextureToolsDistanceFieldGLTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/$) - if(MAGNUM_BUILD_PLUGINS_STATIC) - if(MAGNUM_WITH_ANYIMAGEIMPORTER) - target_link_libraries(TextureToolsDistanceFieldGLTest PRIVATE AnyImageImporter) - endif() - if(MAGNUM_WITH_TGAIMPORTER) - target_link_libraries(TextureToolsDistanceFieldGLTest PRIVATE TgaImporter) - endif() - else() - # So the plugins get properly built when building the test - if(MAGNUM_WITH_ANYIMAGEIMPORTER) - add_dependencies(TextureToolsDistanceFieldGLTest AnyImageImporter) + if(MAGNUM_BUILD_GL_TESTS) + # Otherwise CMake complains that Corrade::PluginManager is not found, wtf + find_package(Corrade REQUIRED PluginManager) + + set(TextureToolsDistanceFieldGLTest_SRCS DistanceFieldGLTest.cpp) + if(CORRADE_TARGET_IOS) + # TODO: do this in a generic way in corrade_add_test() + set_source_files_properties(DistanceFieldGLTestFiles PROPERTIES + MACOSX_PACKAGE_LOCATION Resources) + list(APPEND TextureToolsDistanceFieldGLTest_SRCS DistanceFieldGLTestFiles) endif() - if(MAGNUM_WITH_TGAIMPORTER) - add_dependencies(TextureToolsDistanceFieldGLTest TgaImporter) + corrade_add_test(TextureToolsDistanceFieldGLTest ${TextureToolsDistanceFieldGLTest_SRCS} + LIBRARIES + MagnumDebugTools + MagnumGL + MagnumOpenGLTester + MagnumTextureToolsTestLib + MagnumTrade + FILES + DistanceFieldGLTestFiles/input.tga + DistanceFieldGLTestFiles/output.tga) + target_include_directories(TextureToolsDistanceFieldGLTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/$) + if(MAGNUM_BUILD_PLUGINS_STATIC) + if(MAGNUM_WITH_ANYIMAGEIMPORTER) + target_link_libraries(TextureToolsDistanceFieldGLTest PRIVATE AnyImageImporter) + endif() + if(MAGNUM_WITH_TGAIMPORTER) + target_link_libraries(TextureToolsDistanceFieldGLTest PRIVATE TgaImporter) + endif() + else() + # So the plugins get properly built when building the test + if(MAGNUM_WITH_ANYIMAGEIMPORTER) + add_dependencies(TextureToolsDistanceFieldGLTest AnyImageImporter) + endif() + if(MAGNUM_WITH_TGAIMPORTER) + add_dependencies(TextureToolsDistanceFieldGLTest TgaImporter) + endif() endif() endif() endif() diff --git a/src/Magnum/TextureTools/Test/DistanceFieldTest.cpp b/src/Magnum/TextureTools/Test/DistanceFieldTest.cpp new file mode 100644 index 000000000..07707c406 --- /dev/null +++ b/src/Magnum/TextureTools/Test/DistanceFieldTest.cpp @@ -0,0 +1,54 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, + 2020, 2021, 2022, 2023 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include + +#include "Magnum/TextureTools/DistanceField.h" + +namespace Magnum { namespace TextureTools { namespace Test { namespace { + +struct DistanceFieldTest: TestSuite::Tester { + explicit DistanceFieldTest(); + + void constructNoCreate(); +}; + +DistanceFieldTest::DistanceFieldTest() { + addTests({&DistanceFieldTest::constructNoCreate}); +} + +void DistanceFieldTest::constructNoCreate() { + DistanceField distanceField{NoCreate}; + + /* Shouldn't crash or try to acces GL */ + CORRADE_VERIFY(true); + + /* Implicit construction is not allowed */ + CORRADE_VERIFY(!std::is_convertible::value); +} + +}}}} + +CORRADE_TEST_MAIN(Magnum::TextureTools::Test::DistanceFieldTest)