Skip to content

Commit

Permalink
Replace factory functions with throwing constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
binary1248 authored and ChrisThrasher committed Aug 8, 2024
1 parent 698f265 commit e185f6d
Show file tree
Hide file tree
Showing 67 changed files with 1,994 additions and 1,897 deletions.
6 changes: 3 additions & 3 deletions doc/mainpage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
/// sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML window");
///
/// // Load a sprite to display
/// const auto texture = sf::Texture::loadFromFile("cute_image.jpg").value();
/// const sf::Texture texture("cute_image.jpg");
/// sf::Sprite sprite(texture);
///
/// // Create a graphical text to display
/// const auto font = sf::Font::openFromFile("arial.ttf").value();
/// const sf::Font font("arial.ttf");
/// sf::Text text(font, "Hello SFML", 50);
///
/// // Load a music to play
/// auto music = sf::Music::openFromFile("nice_music.ogg").value();
/// sf::Music music("nice_music.ogg");
///
/// // Play the music
/// music.play();
Expand Down
4 changes: 2 additions & 2 deletions examples/android/app/src/main/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ int main(int argc, char* argv[])
sf::RenderWindow window(screen, "");
window.setFramerateLimit(30);

const auto texture = sf::Texture::createFromFile("image.png").value();
const sf::Texture texture("image.png");

sf::Sprite image(texture);
image.setPosition(sf::Vector2f(screen.size) / 2.f);
image.setOrigin(sf::Vector2f(texture.getSize()) / 2.f);

const auto font = sf::Font::createFromFile("tuffy.ttf").value();
const sf::Font font("tuffy.ttf");

sf::Text text(font, "Tap anywhere to move the logo.", 64);
text.setFillColor(sf::Color::Black);
Expand Down
4 changes: 2 additions & 2 deletions examples/cocoa/CocoaAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@

std::filesystem::path resPath{[[[NSBundle mainBundle] resourcePath] tostdstring]};
sf::RenderWindow renderWindow;
sf::Font font{sf::Font::createFromFile(resPath / "tuffy.ttf").value()};
sf::Font font{resPath / "tuffy.ttf"};
sf::Text text{font};
sf::Texture logo{sf::Texture::createFromFile(resPath / "logo.png").value()};
sf::Texture logo{resPath / "logo.png"};
sf::Sprite sprite{logo};
sf::Color background{sf::Color::Blue};
};
Expand Down
2 changes: 1 addition & 1 deletion examples/event_handling/EventHandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class Application
// Member data
////////////////////////////////////////////////////////////
sf::RenderWindow m_window{sf::VideoMode({800u, 600u}), "SFML Event Handling", sf::Style::Titlebar | sf::Style::Close};
const sf::Font m_font{sf::Font::createFromFile("resources/tuffy.ttf").value()};
const sf::Font m_font{"resources/tuffy.ttf"};
sf::Text m_logText{m_font, "", 20};
sf::Text m_handlerText{m_font, "Current Handler: Classic", 24};
sf::Text m_instructions{m_font, "Press Enter to change handler type", 24};
Expand Down
23 changes: 12 additions & 11 deletions examples/island/Island.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ int main()
sf::RenderWindow window(sf::VideoMode({windowWidth, windowHeight}), "SFML Island", sf::Style::Titlebar | sf::Style::Close);
window.setVerticalSyncEnabled(true);

const auto font = sf::Font::createFromFile("resources/tuffy.ttf").value();
const sf::Font font("resources/tuffy.ttf");

// Create all of our graphics resources
sf::Text hudText(font);
sf::Text statusText(font);
std::optional<sf::Shader> terrainShader;
sf::RenderStates terrainStates;
sf::VertexBuffer terrain(sf::PrimitiveType::Triangles, sf::VertexBuffer::Usage::Static);
sf::Text hudText(font);
sf::Text statusText(font);
sf::Shader terrainShader;
sf::RenderStates terrainStates;
sf::VertexBuffer terrain(sf::PrimitiveType::Triangles, sf::VertexBuffer::Usage::Static);

// Set up our text drawables
statusText.setCharacterSize(28);
Expand All @@ -120,7 +120,7 @@ int main()
{
statusText.setString("Shaders and/or Vertex Buffers Unsupported");
}
else if (!(terrainShader = sf::Shader::createFromFile("resources/terrain.vert", "resources/terrain.frag")))
else if (!terrainShader.loadFromFile("resources/terrain.vert", "resources/terrain.frag"))
{
statusText.setString("Failed to load shader program");
}
Expand Down Expand Up @@ -148,7 +148,7 @@ int main()
statusText.setString("Generating Terrain...");

// Set up the render states
terrainStates = sf::RenderStates(&*terrainShader);
terrainStates = sf::RenderStates(&terrainShader);
}

// Center the status text
Expand Down Expand Up @@ -186,7 +186,8 @@ int main()
}

// Arrow key pressed:
if (terrainShader.has_value() && event->is<sf::Event::KeyPressed>())
// TODO Replace use of getNativeHandle() when validity function is added
if (terrainShader.getNativeHandle() != 0 && event->is<sf::Event::KeyPressed>())
{
switch (event->getIf<sf::Event::KeyPressed>()->code)
{
Expand Down Expand Up @@ -216,7 +217,7 @@ int main()

window.draw(statusText);

if (terrainShader.has_value())
if (terrainShader.getNativeHandle() != 0)
{
{
const std::lock_guard lock(workQueueMutex);
Expand All @@ -236,7 +237,7 @@ int main()
bufferUploadPending = false;
}

terrainShader->setUniform("lightFactor", lightFactor);
terrainShader.setUniform("lightFactor", lightFactor);
window.draw(terrain, terrainStates);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/joystick/Joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ int main()
window.setVerticalSyncEnabled(true);

// Open the text font
const auto font = sf::Font::createFromFile("resources/tuffy.ttf").value();
const sf::Font font("resources/tuffy.ttf");

// Set up our string conversion parameters
sstr.precision(2);
Expand Down
10 changes: 5 additions & 5 deletions examples/opengl/OpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ int main()
window.setMaximumSize(sf::Vector2u(1200, 900));

// Create a sprite for the background
const auto backgroundTexture = sf::Texture::createFromFile(resourcesDir() / "background.jpg", sRgb).value();
const sf::Sprite background(backgroundTexture);
const sf::Texture backgroundTexture(resourcesDir() / "background.jpg", sRgb);
const sf::Sprite background(backgroundTexture);

// Create some text to draw on top of our OpenGL object
const auto font = sf::Font::createFromFile(resourcesDir() / "tuffy.ttf").value();
const sf::Font font(resourcesDir() / "tuffy.ttf");

sf::Text text(font, "SFML / OpenGL demo");
sf::Text sRgbInstructions(font, "Press space to toggle sRGB conversion");
Expand All @@ -75,7 +75,7 @@ int main()
mipmapInstructions.setPosition({200.f, 550.f});

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

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

// Rebind the texture
sf::Texture::bind(&texture);
Expand Down
2 changes: 1 addition & 1 deletion examples/raw_input/RawInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main()
window.setVerticalSyncEnabled(true);

// Open the application font
const auto font = sf::Font::createFromFile("resources/tuffy.ttf").value();
const sf::Font font("resources/tuffy.ttf");

// Create the mouse position text
sf::Text mousePosition(font, "", 20);
Expand Down
75 changes: 35 additions & 40 deletions examples/shader/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,69 +278,66 @@ class Geometry : public Effect
////////////////////////////////////////////////////////////
std::optional<Pixelate> tryLoadPixelate()
{
auto texture = sf::Texture::createFromFile("resources/background.jpg");
if (!texture.has_value())
sf::Texture texture;
if (!texture.loadFromFile("resources/background.jpg"))
return std::nullopt;

auto shader = sf::Shader::createFromFile("resources/pixelate.frag", sf::Shader::Type::Fragment);
if (!shader.has_value())
sf::Shader shader;
if (!shader.loadFromFile("resources/pixelate.frag", sf::Shader::Type::Fragment))
return std::nullopt;

return std::make_optional<Pixelate>(std::move(*texture), std::move(*shader));
return std::make_optional<Pixelate>(std::move(texture), std::move(shader));
}

std::optional<WaveBlur> tryLoadWaveBlur(const sf::Font& font)
{
auto shader = sf::Shader::createFromFile("resources/wave.vert", "resources/blur.frag");
if (!shader.has_value())
sf::Shader shader;
if (!shader.loadFromFile("resources/wave.vert", "resources/blur.frag"))
return std::nullopt;

return std::make_optional<WaveBlur>(font, std::move(*shader));
return std::make_optional<WaveBlur>(font, std::move(shader));
}

std::optional<StormBlink> tryLoadStormBlink()
{
auto shader = sf::Shader::createFromFile("resources/storm.vert", "resources/blink.frag");
if (!shader.has_value())
sf::Shader shader;
if (!shader.loadFromFile("resources/storm.vert", "resources/blink.frag"))
return std::nullopt;

return std::make_optional<StormBlink>(std::move(*shader));
return std::make_optional<StormBlink>(std::move(shader));
}

std::optional<Edge> tryLoadEdge()
{
// Create the off-screen surface
auto surface = sf::RenderTexture::create({800, 600});
if (!surface.has_value())
sf::RenderTexture surface;
if (!surface.resize({800, 600}))
return std::nullopt;

surface->setSmooth(true);
surface.setSmooth(true);

// Load the background texture
auto backgroundTexture = sf::Texture::createFromFile("resources/sfml.png");
if (!backgroundTexture.has_value())
sf::Texture backgroundTexture;
if (!backgroundTexture.loadFromFile("resources/sfml.png"))
return std::nullopt;

backgroundTexture->setSmooth(true);
backgroundTexture.setSmooth(true);

// Load the entity texture
auto entityTexture = sf::Texture::createFromFile("resources/devices.png");
if (!entityTexture.has_value())
sf::Texture entityTexture;
if (!entityTexture.loadFromFile("resources/devices.png"))
return std::nullopt;

entityTexture->setSmooth(true);
entityTexture.setSmooth(true);

// Load the shader
auto shader = sf::Shader::createFromFile("resources/edge.frag", sf::Shader::Type::Fragment);
if (!shader.has_value())
sf::Shader shader;
if (!shader.loadFromFile("resources/edge.frag", sf::Shader::Type::Fragment))
return std::nullopt;

shader->setUniform("texture", sf::Shader::CurrentTexture);
shader.setUniform("texture", sf::Shader::CurrentTexture);

return std::make_optional<Edge>(std::move(*surface),
std::move(*backgroundTexture),
std::move(*entityTexture),
std::move(*shader));
return std::make_optional<Edge>(std::move(surface), std::move(backgroundTexture), std::move(entityTexture), std::move(shader));
}

std::optional<Geometry> tryLoadGeometry()
Expand All @@ -350,25 +347,23 @@ std::optional<Geometry> tryLoadGeometry()
return std::nullopt;

// Load the logo texture
auto logoTexture = sf::Texture::createFromFile("resources/logo.png");
if (!logoTexture.has_value())
sf::Texture logoTexture;
if (!logoTexture.loadFromFile("resources/logo.png"))
return std::nullopt;

logoTexture->setSmooth(true);
logoTexture.setSmooth(true);

// Load the shader
auto shader = sf::Shader::createFromFile("resources/billboard.vert",
"resources/billboard.geom",
"resources/billboard.frag");
if (!shader.has_value())
sf::Shader shader;
if (!shader.loadFromFile("resources/billboard.vert", "resources/billboard.geom", "resources/billboard.frag"))
return std::nullopt;

shader->setUniform("texture", sf::Shader::CurrentTexture);
shader.setUniform("texture", sf::Shader::CurrentTexture);

// Set the render resolution (used for proper scaling)
shader->setUniform("resolution", sf::Vector2f(800, 600));
shader.setUniform("resolution", sf::Vector2f(800, 600));

return std::make_optional<Geometry>(std::move(*logoTexture), std::move(*shader));
return std::make_optional<Geometry>(std::move(logoTexture), std::move(shader));
}

} // namespace
Expand All @@ -394,7 +389,7 @@ int main()
window.setVerticalSyncEnabled(true);

// Open the application font
const auto font = sf::Font::createFromFile("resources/tuffy.ttf").value();
const sf::Font font("resources/tuffy.ttf");

// Create the effects
std::optional pixelateEffect = tryLoadPixelate();
Expand All @@ -418,8 +413,8 @@ int main()
std::size_t current = 0;

// Create the messages background
const auto textBackgroundTexture = sf::Texture::createFromFile("resources/text-background.png").value();
sf::Sprite textBackground(textBackgroundTexture);
const sf::Texture textBackgroundTexture("resources/text-background.png");
sf::Sprite textBackground(textBackgroundTexture);
textBackground.setPosition({0.f, 520.f});
textBackground.setColor(sf::Color(255, 255, 255, 200));

Expand Down
4 changes: 2 additions & 2 deletions examples/sound/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
void playSound()
{
// Load a sound buffer from a wav file
const auto buffer = sf::SoundBuffer::createFromFile("resources/killdeer.wav").value();
const sf::SoundBuffer buffer("resources/killdeer.wav");

// Display sound information
std::cout << "killdeer.wav:" << '\n'
Expand Down Expand Up @@ -46,7 +46,7 @@ void playSound()
void playMusic(const std::filesystem::path& filename)
{
// Load an ogg music file
auto music = sf::Music::createFromFile("resources" / filename).value();
sf::Music music("resources" / filename);

// Display music information
std::cout << filename << ":" << '\n'
Expand Down
Loading

0 comments on commit e185f6d

Please sign in to comment.