Skip to content

Commit

Permalink
Remove default empty state of sf::Texture
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed May 24, 2024
1 parent 51a7076 commit 3be45d0
Show file tree
Hide file tree
Showing 21 changed files with 201 additions and 304 deletions.
4 changes: 1 addition & 3 deletions doc/mainpage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
/// sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML window");
///
/// // Load a sprite to display
/// sf::Texture texture;
/// if (!texture.loadFromFile("cute_image.jpg"))
/// return EXIT_FAILURE;
/// const auto texture = sf::Texture::loadFromFile("cute_image.jpg").value();
/// sf::Sprite sprite(texture);
///
/// // Create a graphical text to display
Expand Down
4 changes: 1 addition & 3 deletions examples/android/app/src/main/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ int main(int argc, char* argv[])
sf::RenderWindow window(screen, "");
window.setFramerateLimit(30);

sf::Texture texture;
if (!texture.loadFromFile("image.png"))
return EXIT_FAILURE;
const auto texture = sf::Texture::loadFromFile("image.png").value();

sf::Sprite image(texture);
image.setPosition(sf::Vector2f(screen.size) / 2.f);
Expand Down
5 changes: 1 addition & 4 deletions examples/cocoa/CocoaAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
{
SFMLmainWindow(sf::WindowHandle win) : renderWindow(win)
{
if (!logo.loadFromFile(resPath / "logo.png"))
NSLog(@"Couldn't load the logo image");

logo.setSmooth(true);

sprite.setOrigin(sprite.getLocalBounds().getCenter());
Expand All @@ -57,7 +54,7 @@
sf::RenderWindow renderWindow;
sf::Font font{sf::Font::loadFromFile(resPath / "tuffy.ttf").value()};
sf::Text text{font};
sf::Texture logo;
sf::Texture logo{sf::Texture::loadFromFile(resPath / "logo.png").value()};
sf::Sprite sprite{logo};
sf::Color background{sf::Color::Blue};
};
Expand Down
12 changes: 3 additions & 9 deletions examples/opengl/OpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ int main()
window.setMaximumSize(sf::Vector2u(1200, 900));

// Create a sprite for the background
sf::Texture backgroundTexture;
backgroundTexture.setSrgb(sRgb);
if (!backgroundTexture.loadFromFile(resourcesDir() / "background.jpg"))
return EXIT_FAILURE;
const auto backgroundTexture = sf::Texture::loadFromFile(resourcesDir() / "background.jpg", sRgb).value();
const sf::Sprite background(backgroundTexture);

// Create some text to draw on top of our OpenGL object
Expand All @@ -78,9 +75,7 @@ int main()
mipmapInstructions.setPosition({200.f, 550.f});

// Load a texture to apply to our 3D cube
sf::Texture texture;
if (!texture.loadFromFile(resourcesDir() / "logo.png"))
return EXIT_FAILURE;
auto texture = sf::Texture::loadFromFile(resourcesDir() / "logo.png").value();

// Attempt to generate a mipmap for our cube texture
// We don't check the return value here since
Expand Down Expand Up @@ -230,8 +225,7 @@ int main()
if (mipmapEnabled)
{
// We simply reload the texture to disable mipmapping
if (!texture.loadFromFile(resourcesDir() / "logo.png"))
return EXIT_FAILURE;
texture = sf::Texture::loadFromFile(resourcesDir() / "logo.png").value();

mipmapEnabled = false;
}
Expand Down
25 changes: 6 additions & 19 deletions examples/shader/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ class Pixelate : public Effect

bool onLoad() override
{
// Load the texture and initialize the sprite
if (!m_texture.loadFromFile("resources/background.jpg"))
return false;
// Initialize the sprite
m_sprite.emplace(m_texture);

m_shader.setUniform("texture", sf::Shader::CurrentTexture);
Expand All @@ -55,7 +53,7 @@ class Pixelate : public Effect
}

private:
sf::Texture m_texture;
sf::Texture m_texture{sf::Texture::loadFromFile("resources/background.jpg").value()};
std::optional<sf::Sprite> m_sprite;
sf::Shader m_shader{sf::Shader::loadFromFile("resources/pixelate.frag", sf::Shader::Type::Fragment).value()};
};
Expand Down Expand Up @@ -184,12 +182,7 @@ class Edge : public Effect
{
m_surface.setSmooth(true);

// Load the textures
if (!m_backgroundTexture.loadFromFile("resources/sfml.png"))
return false;
m_backgroundTexture.setSmooth(true);
if (!m_entityTexture.loadFromFile("resources/devices.png"))
return false;
m_entityTexture.setSmooth(true);

// Initialize the background sprite
Expand Down Expand Up @@ -240,8 +233,8 @@ class Edge : public Effect

private:
sf::RenderTexture m_surface{sf::RenderTexture::create({800, 600}).value()};
sf::Texture m_backgroundTexture;
sf::Texture m_entityTexture;
sf::Texture m_backgroundTexture{sf::Texture::loadFromFile("resources/sfml.png").value()};
sf::Texture m_entityTexture{sf::Texture::loadFromFile("resources/devices.png").value()};
std::optional<sf::Sprite> m_backgroundSprite;
std::vector<sf::Sprite> m_entities;
sf::Shader m_shader{sf::Shader::loadFromFile("resources/edge.frag", sf::Shader::Type::Fragment).value()};
Expand Down Expand Up @@ -273,10 +266,6 @@ class Geometry : public Effect
m_pointCloud[i].position = {positionDistribution(rng), positionDistribution(rng)};
}

// Load the texture
if (!m_logoTexture.loadFromFile("resources/logo.png"))
return false;

// Load the shader
m_shader = sf::Shader::loadFromFile("resources/billboard.vert",
"resources/billboard.geom",
Expand Down Expand Up @@ -319,7 +308,7 @@ class Geometry : public Effect
}

private:
sf::Texture m_logoTexture;
sf::Texture m_logoTexture{sf::Texture::loadFromFile("resources/logo.png").value()};
sf::Transform m_transform;
std::optional<sf::Shader> m_shader;
sf::VertexArray m_pointCloud;
Expand Down Expand Up @@ -358,9 +347,7 @@ int main()
effect->load();

// Create the messages background
sf::Texture textBackgroundTexture;
if (!textBackgroundTexture.loadFromFile("resources/text-background.png"))
return EXIT_FAILURE;
const auto textBackgroundTexture = sf::Texture::loadFromFile("resources/text-background.png").value();
sf::Sprite textBackground(textBackgroundTexture);
textBackground.setPosition({0.f, 520.f});
textBackground.setColor(sf::Color(255, 255, 255, 200));
Expand Down
4 changes: 1 addition & 3 deletions examples/sound_effects/SoundEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,9 +1080,7 @@ int main()
effects[current]->start();

// Create the messages background
sf::Texture textBackgroundTexture;
if (!textBackgroundTexture.loadFromFile(resourcesDir() / "text-background.png"))
return EXIT_FAILURE;
const auto textBackgroundTexture = sf::Texture::loadFromFile(resourcesDir() / "text-background.png").value();
sf::Sprite textBackground(textBackgroundTexture);
textBackground.setPosition({0.f, 520.f});
textBackground.setColor(sf::Color(255, 255, 255, 200));
Expand Down
4 changes: 1 addition & 3 deletions examples/tennis/Tennis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ int main()
sf::Sound ballSound(ballSoundBuffer);

// Create the SFML logo texture:
sf::Texture sfmlLogoTexture;
if (!sfmlLogoTexture.loadFromFile(resourcesDir() / "sfml_logo.png"))
return EXIT_FAILURE;
const auto sfmlLogoTexture = sf::Texture::loadFromFile(resourcesDir() / "sfml_logo.png").value();
sf::Sprite sfmlLogo(sfmlLogoTexture);
sfmlLogo.setPosition({170.f, 50.f});

Expand Down
6 changes: 2 additions & 4 deletions examples/win32/Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ int main()
sf::RenderWindow sfmlView2(view2);

// Load some textures to display
sf::Texture texture1;
sf::Texture texture2;
if (!texture1.loadFromFile("resources/image1.jpg") || !texture2.loadFromFile("resources/image2.jpg"))
return EXIT_FAILURE;
const auto texture1 = sf::Texture::loadFromFile("resources/image1.jpg").value();
const auto texture2 = sf::Texture::loadFromFile("resources/image2.jpg").value();
sf::Sprite sprite1(texture1);
sf::Sprite sprite2(texture2);
sprite1.setOrigin(sf::Vector2f(texture1.getSize()) / 2.f);
Expand Down
6 changes: 1 addition & 5 deletions include/SFML/Graphics/RenderWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,7 @@ class SFML_GRAPHICS_API RenderWindow : public Window, public RenderTarget
/// sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML OpenGL");
///
/// // Create a sprite and a text to display
/// sf::Texture texture;
/// if (!texture.loadFromFile("circle.png"))
/// {
/// // error...
/// }
/// const auto texture = sf::Texture::loadFromFile("circle.png").value();
/// sf::Sprite sprite(texture);
/// const auto font = sf::Font::loadFromFile("arial.ttf").value();
/// sf::Text text(font);
Expand Down
8 changes: 2 additions & 6 deletions include/SFML/Graphics/Sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,8 @@ class SFML_GRAPHICS_API Sprite : public Drawable, public Transformable
///
/// Usage example:
/// \code
/// // Declare and load a texture
/// sf::Texture texture;
/// if (!texture.loadFromFile("texture.png"))
/// {
/// // Handle error...
/// }
/// // Load a texture
/// const auto texture = sf::Texture::loadFromFile("texture.png").value();
///
/// // Create a sprite
/// sf::Sprite sprite(texture);
Expand Down

0 comments on commit 3be45d0

Please sign in to comment.