Skip to content

Commit

Permalink
sf::Texture no longer has a default constructor in SFML 3
Browse files Browse the repository at this point in the history
  • Loading branch information
texus committed Jun 8, 2024
1 parent 04f108f commit b354b8e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

# If the cmake version is higher than the minimum version then enable all new policies, up to the maximum version specified.
# When cmake is newer than the highest version then its newest policies will still be set to the old behavior for compatibility.
cmake_minimum_required(VERSION 3.16...3.28)
cmake_minimum_required(VERSION 3.16...3.29)

# Include macros such as tgui_set_option
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Macros.cmake)
Expand Down
2 changes: 1 addition & 1 deletion doc/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<script type="text/javascript">
if(window.location.href.endsWith('/namespaces.html') || window.location.href.endsWith('/namespaces') || window.location.href.endsWith('/annotated.html') || window.location.href.endsWith('/annotated'))
{
toggleLevel(2);
dynsection.toggleLevel(2);
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ TGUI_MODULE_EXPORT namespace tgui
///
/// @warning You are not allowed to change the size, pixels or smoothing of the texture
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD sf::Texture& getInternalTexture();
TGUI_NODISCARD sf::Texture* getInternalTexture();


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns a const reference to the internal SFML texture
/// @return Const reference to internal texture
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD const sf::Texture& getInternalTexture() const;
TGUI_NODISCARD const sf::Texture* getInternalTexture() const;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -100,7 +100,7 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:

sf::Texture m_texture;
std::unique_ptr<sf::Texture> m_texture;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ namespace tgui

sf::RenderStates sfStates = convertRenderStates(transformedStates, texture);
TGUI_ASSERT(std::dynamic_pointer_cast<BackendTextureSFML>(sprite.getTexture().getData()->backendTexture), "BackendRenderTargetSFML::drawSprite requires backend texture of type BackendTextureSFML");
sfStates.texture = &std::static_pointer_cast<BackendTextureSFML>(sprite.getTexture().getData()->backendTexture)->getInternalTexture();
sfStates.texture = std::static_pointer_cast<BackendTextureSFML>(sprite.getTexture().getData()->backendTexture)->getInternalTexture();
sfStates.shader = sprite.getTexture().getShader();

#if SFML_VERSION_MAJOR < 3
Expand Down Expand Up @@ -247,7 +247,7 @@ namespace tgui
if (texture)
{
TGUI_ASSERT(std::dynamic_pointer_cast<BackendTextureSFML>(texture), "BackendRenderTargetSFML requires textures of type BackendTextureSFML");
statesSFML.texture = &std::static_pointer_cast<BackendTextureSFML>(texture)->getInternalTexture();
statesSFML.texture = std::static_pointer_cast<BackendTextureSFML>(texture)->getInternalTexture();
}

return statesSFML;
Expand Down
39 changes: 24 additions & 15 deletions src/Backend/Renderer/SFML-Graphics/BackendTextureSFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace tgui

BackendTextureSFML::BackendTextureSFML()
{
m_isSmooth = m_texture.isSmooth(); // Smooth filter is disabled by default in SFML textures
m_isSmooth = false; // TGUI_NEXT: This is only for backwards compatibility, this line can be removed in the future
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -42,20 +42,28 @@ namespace tgui
{
BackendTexture::loadTextureOnly(size, pixels, smooth);

m_texture.setSmooth(smooth);

if (Vector2u{m_texture.getSize()} != size)
if (!m_texture || (Vector2u{m_texture->getSize()} != size))
{
#if SFML_VERSION_MAJOR >= 3
if (!m_texture.create({size.x, size.y}))
auto optionalTexture = sf::Texture::create({size.x, size.y});
if (!optionalTexture)
return false;

m_texture = std::make_unique<sf::Texture>(std::move(*optionalTexture));
#else
if (!m_texture.create(size.x, size.y))
#endif
if (!m_texture)
m_texture = std::make_unique<sf::Texture>();
if (!m_texture->create(size.x, size.y))
return false;
#endif
}

if (pixels)
m_texture.update(pixels);
if (m_texture)
{
m_texture->setSmooth(smooth);
if (pixels)
m_texture->update(pixels);
}

return true;
}
Expand All @@ -65,28 +73,29 @@ namespace tgui
void BackendTextureSFML::setSmooth(bool smooth)
{
BackendTexture::setSmooth(smooth);
m_texture.setSmooth(smooth);
if (m_texture)
m_texture->setSmooth(smooth);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

sf::Texture& BackendTextureSFML::getInternalTexture()
sf::Texture* BackendTextureSFML::getInternalTexture()
{
return m_texture;
return m_texture.get();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

const sf::Texture& BackendTextureSFML::getInternalTexture() const
const sf::Texture* BackendTextureSFML::getInternalTexture() const
{
return m_texture;
return m_texture.get();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void BackendTextureSFML::replaceInternalTexture(const sf::Texture& texture)
{
m_texture = texture;
m_texture = std::make_unique<sf::Texture>(texture);

m_pixels = nullptr;
m_imageSize = {texture.getSize().x, texture.getSize().y};
Expand Down
2 changes: 1 addition & 1 deletion src/Backend/Renderer/SFML-Graphics/CanvasSFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ namespace tgui

TGUI_ASSERT(std::dynamic_pointer_cast<BackendTextureSFML>(sprite.getTexture().getData()->backendTexture),
"CanvasSFML::draw requires sprite to have a backend texture of type BackendTextureSFML");
statesSFML.texture = &std::static_pointer_cast<BackendTextureSFML>(sprite.getTexture().getData()->backendTexture)->getInternalTexture();
statesSFML.texture = std::static_pointer_cast<BackendTextureSFML>(sprite.getTexture().getData()->backendTexture)->getInternalTexture();

#if SFML_VERSION_MAJOR >= 3
statesSFML.coordinateType = sf::CoordinateType::Normalized;
Expand Down
7 changes: 6 additions & 1 deletion tests/Widgets/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ TEST_CASE("[CanvasSFML]")
canvas->setSize({180, 140});
canvas->setPosition({10, 5});

#if SFML_VERSION_MAJOR >= 3
auto optionalTexture = sf::Texture::loadFromFile("resources/image.png");
REQUIRE(optionalTexture);
sf::Texture texture = std::move(*optionalTexture);
#else
sf::Texture texture;
REQUIRE(texture.loadFromFile("resources/image.png"));

#endif
sf::Sprite sprite(texture);
sprite.setScale({150.f / texture.getSize().x, 100.f / texture.getSize().y});
sprite.setPosition({15, 20});
Expand Down
7 changes: 6 additions & 1 deletion tests/Widgets/Picture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@ TEST_CASE("[Picture]")
{
SECTION("from sf::Texture")
{
#if SFML_VERSION_MAJOR >= 3
auto optionalSfTexture = sf::Texture::loadFromFile("resources/image.png");
REQUIRE(optionalSfTexture);
sf::Texture sfTexture = std::move(*optionalSfTexture);
#else
sf::Texture sfTexture;
REQUIRE(sfTexture.loadFromFile("resources/image.png"));

#endif
tgui::Texture tguiTexture;
tguiTexture.loadFromPixelData(sfTexture.getSize(), sfTexture.copyToImage().getPixelsPtr());

Expand Down
4 changes: 2 additions & 2 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ struct TestsWindowDefault : public TestsWindowBase
}

#if SFML_VERSION_MAJOR >= 3
sf::Window window{sf::VideoMode{{windowWidth, windowHeight}}, windowTitle, sf::Style::Default, sf::State::Windowed, sf::ContextSettings(0, 0, 0, 3, 3, sf::ContextSettings::Attribute::Core)};
sf::Window window{sf::VideoMode{{windowWidth, windowHeight}}, windowTitle, sf::Style::Default, sf::State::Windowed, sf::ContextSettings{0, 0, 0, 3, 3, sf::ContextSettings::Attribute::Core}};
#else
sf::Window window{sf::VideoMode{windowWidth, windowHeight}, windowTitle, sf::Style::Default, sf::ContextSettings(0, 0, 0, 3, 3, sf::ContextSettings::Attribute::Core)};
sf::Window window{sf::VideoMode{windowWidth, windowHeight}, windowTitle, sf::Style::Default, sf::ContextSettings{0, 0, 0, 3, 3, sf::ContextSettings::Attribute::Core}};
#endif
};
#endif
Expand Down

0 comments on commit b354b8e

Please sign in to comment.