Skip to content

Commit

Permalink
Remove default empty state of sf::Font
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed May 19, 2024
1 parent 1a40f01 commit 8f0939c
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 185 deletions.
4 changes: 1 addition & 3 deletions doc/mainpage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
/// sf::Sprite sprite(texture);
///
/// // Create a graphical text to display
/// sf::Font font;
/// if (!font.loadFromFile("arial.ttf"))
/// return EXIT_FAILURE;
/// const auto font = sf::Font::loadFromFile("arial.ttf").value();
/// sf::Text text(font, "Hello SFML", 50);
///
/// // Load a music to play
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 @@ -94,9 +94,7 @@ int main(int argc, char* argv[])
image.setPosition(sf::Vector2f(screen.size) / 2.f);
image.setOrigin(sf::Vector2f(texture.getSize()) / 2.f);

sf::Font font;
if (!font.loadFromFile("tuffy.ttf"))
return EXIT_FAILURE;
const auto font = sf::Font::loadFromFile("tuffy.ttf").value();

sf::Text text(font, "Tap anywhere to move the logo.", 64);
text.setFillColor(sf::Color::Black);
Expand Down
17 changes: 7 additions & 10 deletions examples/cocoa/CocoaAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
{
SFMLmainWindow(sf::WindowHandle win) : renderWindow(win)
{
const std::filesystem::path resPath = [[[NSBundle mainBundle] resourcePath] tostdstring];
if (!logo.loadFromFile(resPath / "logo.png"))
NSLog(@"Couldn't load the logo image");

Expand All @@ -51,18 +50,16 @@
sprite.scale({0.3f, 0.3f});
sprite.setPosition(sf::Vector2f(renderWindow.getSize()) / 2.f);

if (!font.loadFromFile(resPath / "tuffy.ttf"))
NSLog(@"Couldn't load the font");

text.setFillColor(sf::Color::White);
}

sf::RenderWindow renderWindow;
sf::Font font;
sf::Text text{font};
sf::Texture logo;
sf::Sprite sprite{logo};
sf::Color background{sf::Color::Blue};
std::filesystem::path resPath{[[[NSBundle mainBundle] resourcePath] tostdstring]};
sf::RenderWindow renderWindow;
sf::Font font{sf::Font::loadFromFile(resPath / "tuffy.ttf").value()};
sf::Text text{font};
sf::Texture logo;
sf::Sprite sprite{logo};
sf::Color background{sf::Color::Blue};
};

// Private stuff
Expand Down
4 changes: 1 addition & 3 deletions examples/island/Island.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ int main()
sf::RenderWindow window(sf::VideoMode({windowWidth, windowHeight}), "SFML Island", sf::Style::Titlebar | sf::Style::Close);
window.setVerticalSyncEnabled(true);

sf::Font font;
if (!font.loadFromFile("resources/tuffy.ttf"))
return EXIT_FAILURE;
const auto font = sf::Font::loadFromFile("resources/tuffy.ttf").value();

// Create all of our graphics resources
sf::Text hudText(font);
Expand Down
4 changes: 1 addition & 3 deletions examples/joystick/Joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ int main()
window.setVerticalSyncEnabled(true);

// Load the text font
sf::Font font;
if (!font.loadFromFile("resources/tuffy.ttf"))
return EXIT_FAILURE;
const auto font = sf::Font::loadFromFile("resources/tuffy.ttf").value();

// Set up our string conversion parameters
sstr.precision(2);
Expand Down
4 changes: 1 addition & 3 deletions examples/opengl/OpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ int main()
const sf::Sprite background(backgroundTexture);

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

sf::Text text(font, "SFML / OpenGL demo");
sf::Text sRgbInstructions(font, "Press space to toggle sRGB conversion");
Expand Down
4 changes: 1 addition & 3 deletions examples/shader/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,7 @@ int main()
window.setVerticalSyncEnabled(true);

// Load the application font and pass it to the Effect class
sf::Font font;
if (!font.loadFromFile("resources/tuffy.ttf"))
return EXIT_FAILURE;
const auto font = sf::Font::loadFromFile("resources/tuffy.ttf").value();
Effect::setFont(font);

// Create the effects
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 @@ -1051,9 +1051,7 @@ int main()
window.setVerticalSyncEnabled(true);

// Load the application font and pass it to the Effect class
sf::Font font;
if (!font.loadFromFile(resourcesDir() / "tuffy.ttf"))
return EXIT_FAILURE;
const auto font = sf::Font::loadFromFile(resourcesDir() / "tuffy.ttf").value();
Effect::setFont(font);

// Create the effects
Expand Down
4 changes: 1 addition & 3 deletions examples/tennis/Tennis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ int main()
ball.setOrigin({ballRadius / 2.f, ballRadius / 2.f});

// Load the text font
sf::Font font;
if (!font.loadFromFile(resourcesDir() / "tuffy.ttf"))
return EXIT_FAILURE;
const auto font = sf::Font::loadFromFile(resourcesDir() / "tuffy.ttf").value();

// Initialize the pause message
sf::Text pauseMessage(font);
Expand Down
35 changes: 15 additions & 20 deletions include/SFML/Graphics/Font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <filesystem>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
Expand Down Expand Up @@ -87,12 +88,12 @@ class SFML_GRAPHICS_API Font
///
/// \param filename Path of the font file to load
///
/// \return True if loading succeeded, false if it failed
/// \return Font if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromMemory, loadFromStream
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromFile(const std::filesystem::path& filename);
[[nodiscard]] static std::optional<Font> loadFromFile(const std::filesystem::path& filename);

////////////////////////////////////////////////////////////
/// \brief Load the font from a file in memory
Expand All @@ -108,12 +109,12 @@ class SFML_GRAPHICS_API Font
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
///
/// \return True if loading succeeded, false if it failed
/// \return Font if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromFile, loadFromStream
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromMemory(const void* data, std::size_t sizeInBytes);
[[nodiscard]] static std::optional<Font> loadFromMemory(const void* data, std::size_t sizeInBytes);

////////////////////////////////////////////////////////////
/// \brief Load the font from a custom stream
Expand All @@ -130,12 +131,12 @@ class SFML_GRAPHICS_API Font
///
/// \param stream Source stream to read from
///
/// \return True if loading succeeded, false if it failed
/// \return Font if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromFile, loadFromMemory
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromStream(InputStream& stream);
[[nodiscard]] static std::optional<Font> loadFromStream(InputStream& stream);

////////////////////////////////////////////////////////////
/// \brief Get the font information
Expand Down Expand Up @@ -322,12 +323,6 @@ class SFML_GRAPHICS_API Font
std::vector<Row> rows; //!< List containing the position of all the existing rows
};

////////////////////////////////////////////////////////////
/// \brief Free all the internal resources
///
////////////////////////////////////////////////////////////
void cleanup();

////////////////////////////////////////////////////////////
/// \brief Find or create the glyphs page corresponding to the given character size
///
Expand Down Expand Up @@ -378,6 +373,12 @@ class SFML_GRAPHICS_API Font
struct FontHandles;
using PageTable = std::unordered_map<unsigned int, Page>; //!< Table mapping a character size to its page (texture)

////////////////////////////////////////////////////////////
/// \brief Create a font from font handles and a family name
///
////////////////////////////////////////////////////////////
Font(std::shared_ptr<FontHandles>&& fontHandles, std::string&& familyName);

////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -430,14 +431,8 @@ class SFML_GRAPHICS_API Font
///
/// Usage example:
/// \code
/// // Declare a new font
/// sf::Font font;
///
/// // Load it from a file
/// if (!font.loadFromFile("arial.ttf"))
/// {
/// // error...
/// }
/// // Load a new font
/// const auto font = sf::Font::loadFromFile("arial.ttf").value();
///
/// // Create a text which uses our font
/// sf::Text text1(font);
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 @@ -272,11 +272,7 @@ class SFML_GRAPHICS_API RenderWindow : public Window, public RenderTarget
/// // error...
/// }
/// sf::Sprite sprite(texture);
/// sf::Font font;
/// if (!font.loadFromFile("arial.ttf"))
/// {
/// // error...
/// }
/// const auto font = sf::Font::loadFromFile("arial.ttf").value();
/// sf::Text text(font);
/// ...
///
Expand Down
8 changes: 2 additions & 6 deletions include/SFML/Graphics/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,8 @@ class SFML_GRAPHICS_API Text : public Drawable, public Transformable
///
/// Usage example:
/// \code
/// // Declare and load a font
/// sf::Font font;
/// if (!font.loadFromFile("arial.ttf"))
/// {
/// // Handle error...
/// }
/// // Load a font
/// const auto font = sf::Font::loadFromFile("arial.ttf").value();
///
/// // Create a text
/// sf::Text text(font, "hello");
Expand Down

0 comments on commit 8f0939c

Please sign in to comment.