Remove default sf::Text constructor

This commit is contained in:
Chris Thrasher 2022-09-05 23:18:29 -06:00
parent 57a40c531f
commit 2c99b3343a
15 changed files with 61 additions and 80 deletions

View File

@ -30,7 +30,7 @@
/// sf::Font font; /// sf::Font font;
/// if (!font.loadFromFile("arial.ttf")) /// if (!font.loadFromFile("arial.ttf"))
/// return EXIT_FAILURE; /// return EXIT_FAILURE;
/// sf::Text text("Hello SFML", font, 50); /// sf::Text text(font, "Hello SFML", 50);
/// ///
/// // Load a music to play /// // Load a music to play
/// sf::Music music; /// sf::Music music;

View File

@ -94,7 +94,7 @@ int main(int argc, char* argv[])
if (!font.loadFromFile("tuffy.ttf")) if (!font.loadFromFile("tuffy.ttf"))
return EXIT_FAILURE; return EXIT_FAILURE;
sf::Text text("Tap anywhere to move the logo.", font, 64); sf::Text text(font, "Tap anywhere to move the logo.", 64);
text.setFillColor(sf::Color::Black); text.setFillColor(sf::Color::Black);
text.setPosition({10, 10}); text.setPosition({10, 10});

View File

@ -37,7 +37,7 @@
// Our PIMPL // Our PIMPL
struct SFMLmainWindow struct SFMLmainWindow
{ {
SFMLmainWindow(sf::WindowHandle win) : renderWindow(win), background(sf::Color::Blue) SFMLmainWindow(sf::WindowHandle win) : renderWindow(win), text(font), background(sf::Color::Blue)
{ {
std::string resPath = [[[NSBundle mainBundle] resourcePath] tostdstring]; std::string resPath = [[[NSBundle mainBundle] resourcePath] tostdstring];
if (!logo.loadFromFile(resPath + "/logo.png")) if (!logo.loadFromFile(resPath + "/logo.png"))
@ -59,7 +59,6 @@ struct SFMLmainWindow
NSLog(@"Couldn't load the font"); NSLog(@"Couldn't load the font");
text.setFillColor(sf::Color::White); text.setFillColor(sf::Color::White);
text.setFont(font);
} }
sf::RenderWindow renderWindow; sf::RenderWindow renderWindow;

View File

@ -95,20 +95,18 @@ int main()
return EXIT_FAILURE; return EXIT_FAILURE;
// Create all of our graphics resources // Create all of our graphics resources
sf::Text hudText; sf::Text hudText(font);
sf::Text statusText; sf::Text statusText(font);
sf::Shader terrainShader; sf::Shader terrainShader;
sf::RenderStates terrainStates(&terrainShader); sf::RenderStates terrainStates(&terrainShader);
sf::VertexBuffer terrain(sf::PrimitiveType::Triangles, sf::VertexBuffer::Static); sf::VertexBuffer terrain(sf::PrimitiveType::Triangles, sf::VertexBuffer::Static);
// Set up our text drawables // Set up our text drawables
statusText.setFont(font);
statusText.setCharacterSize(28); statusText.setCharacterSize(28);
statusText.setFillColor(sf::Color::White); statusText.setFillColor(sf::Color::White);
statusText.setOutlineColor(sf::Color::Black); statusText.setOutlineColor(sf::Color::Black);
statusText.setOutlineThickness(2.0f); statusText.setOutlineThickness(2.0f);
hudText.setFont(font);
hudText.setCharacterSize(14); hudText.setCharacterSize(14);
hudText.setFillColor(sf::Color::White); hudText.setFillColor(sf::Color::White);
hudText.setOutlineColor(sf::Color::Black); hudText.setOutlineColor(sf::Color::Black);

View File

@ -41,8 +41,9 @@ void updateIdentification(unsigned int index)
{ {
sstr.str(""); sstr.str("");
sstr << "Joystick " << index << ":"; sstr << "Joystick " << index << ":";
texts.at("ID").label.setString(sstr.str()); auto& [label, value] = texts.at("ID");
texts.at("ID").value.setString(sf::Joystick::getIdentification(index).name); label.setString(sstr.str());
value.setString(sf::Joystick::getIdentification(index).name);
} }
// Update joystick axes // Update joystick axes
@ -103,37 +104,40 @@ int main()
sstr.setf(std::ios::fixed | std::ios::boolalpha); sstr.setf(std::ios::fixed | std::ios::boolalpha);
// Set up our joystick identification sf::Text objects // Set up our joystick identification sf::Text objects
texts.emplace("ID", JoystickObject{{"<Not Connected>", font}, {"", font}}); {
texts.at("ID").label.setPosition({5.f, 5.f}); auto [it, success] = texts.emplace("ID", JoystickObject{{font, "<Not Connected>"}, {font}});
texts.at("ID").value.setPosition({80.f, 5.f}); auto& [label, value] = it->second;
label.setPosition({5.f, 5.f});
value.setPosition({80.f, 5.f});
}
// Set up our threshold sf::Text objects // Set up our threshold sf::Text objects
sstr.str(""); sstr.str("");
sstr << threshold << " (Change with up/down arrow keys)"; sstr << threshold << " (Change with up/down arrow keys)";
{
texts.emplace("Threshold", JoystickObject{{"Threshold:", font}, {sstr.str(), font}}); auto [it, success] = texts.emplace("Threshold", JoystickObject{{font, "Threshold:"}, {font, sstr.str()}});
texts.at("Threshold").label.setPosition({5.f, 5.f + 2 * font.getLineSpacing(14)}); auto& [label, value] = it->second;
texts.at("Threshold").value.setPosition({80.f, 5.f + 2 * font.getLineSpacing(14)}); label.setPosition({5.f, 5.f + 2 * font.getLineSpacing(14)});
value.setPosition({80.f, 5.f + 2 * font.getLineSpacing(14)});
}
// Set up our label-value sf::Text objects // Set up our label-value sf::Text objects
for (unsigned int i = 0; i < sf::Joystick::AxisCount; ++i) for (unsigned int i = 0; i < sf::Joystick::AxisCount; ++i)
{ {
auto& object = texts.insert({axislabels[i], {{axislabels[i] + ":", font}, {"N/A", font}}}).first->second; auto [it, success] = texts.emplace(axislabels[i], JoystickObject{{font, axislabels[i] + ":"}, {font, "N/A"}});
auto& [label, value] = it->second;
object.label.setPosition({5.f, 5.f + (static_cast<float>(i + 4) * font.getLineSpacing(14))}); label.setPosition({5.f, 5.f + (static_cast<float>(i + 4) * font.getLineSpacing(14))});
object.value.setPosition({80.f, 5.f + (static_cast<float>(i + 4) * font.getLineSpacing(14))}); value.setPosition({80.f, 5.f + (static_cast<float>(i + 4) * font.getLineSpacing(14))});
} }
for (unsigned int i = 0; i < sf::Joystick::ButtonCount; ++i) for (unsigned int i = 0; i < sf::Joystick::ButtonCount; ++i)
{ {
sstr.str(""); sstr.str("");
sstr << "Button " << i; sstr << "Button " << i;
auto& object = texts.insert({sstr.str(), {{sstr.str() + ":", font}, {"N/A", font}}}).first->second; auto [it, success] = texts.emplace(sstr.str(), JoystickObject{{font, sstr.str() + ":"}, {font, "N/A"}});
auto& [label, value] = it->second;
object.label.setPosition( label.setPosition({5.f, 5.f + (static_cast<float>(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))});
{5.f, 5.f + (static_cast<float>(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))}); value.setPosition({80.f, 5.f + (static_cast<float>(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))});
object.value.setPosition(
{80.f, 5.f + (static_cast<float>(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))});
} }
for (auto& [label, joystickObject] : texts) for (auto& [label, joystickObject] : texts)
@ -176,8 +180,9 @@ int main()
for (auto& [label, joystickObject] : texts) for (auto& [label, joystickObject] : texts)
joystickObject.value.setString("N/A"); joystickObject.value.setString("N/A");
texts.at("ID").label.setString("<Not Connected>"); auto& [label, value] = texts.at("ID");
texts.at("ID").value.setString(""); label.setString("<Not Connected>");
value.setString("");
sstr.str(""); sstr.str("");
sstr << threshold << " (Change with up/down arrow keys)"; sstr << threshold << " (Change with up/down arrow keys)";

View File

@ -62,9 +62,9 @@ int main()
if (!font.loadFromFile(resourcesDir() / "tuffy.ttf")) if (!font.loadFromFile(resourcesDir() / "tuffy.ttf"))
return EXIT_FAILURE; return EXIT_FAILURE;
sf::Text text("SFML / OpenGL demo", font); sf::Text text(font, "SFML / OpenGL demo");
sf::Text sRgbInstructions("Press space to toggle sRGB conversion", font); sf::Text sRgbInstructions(font, "Press space to toggle sRGB conversion");
sf::Text mipmapInstructions("Press return to toggle mipmapping", font); sf::Text mipmapInstructions(font, "Press return to toggle mipmapping");
text.setFillColor(sf::Color(255, 255, 255, 170)); text.setFillColor(sf::Color(255, 255, 255, 170));
sRgbInstructions.setFillColor(sf::Color(255, 255, 255, 170)); sRgbInstructions.setFillColor(sf::Color(255, 255, 255, 170));
mipmapInstructions.setFillColor(sf::Color(255, 255, 255, 170)); mipmapInstructions.setFillColor(sf::Color(255, 255, 255, 170));

View File

@ -46,7 +46,7 @@ public:
} }
else else
{ {
sf::Text error("Shader not\nsupported", getFont()); sf::Text error(getFont(), "Shader not\nsupported");
error.setPosition({320.f, 200.f}); error.setPosition({320.f, 200.f});
error.setCharacterSize(36); error.setCharacterSize(36);
target.draw(error, states); target.draw(error, states);

View File

@ -66,7 +66,7 @@ private:
class WaveBlur : public Effect class WaveBlur : public Effect
{ {
public: public:
WaveBlur() : Effect("Wave + Blur") WaveBlur() : Effect("Wave + Blur"), m_text(getFont())
{ {
} }
@ -92,7 +92,6 @@ public:
"Mauris ultricies dolor sed massa convallis sed aliquet augue fringilla.\n" "Mauris ultricies dolor sed massa convallis sed aliquet augue fringilla.\n"
"Duis erat eros, porta in accumsan in, blandit quis sem.\n" "Duis erat eros, porta in accumsan in, blandit quis sem.\n"
"In hac habitasse platea dictumst. Etiam fringilla est id odio dapibus sit amet semper dui laoreet.\n"); "In hac habitasse platea dictumst. Etiam fringilla est id odio dapibus sit amet semper dui laoreet.\n");
m_text.setFont(getFont());
m_text.setCharacterSize(22); m_text.setCharacterSize(22);
m_text.setPosition({30.f, 20.f}); m_text.setPosition({30.f, 20.f});
@ -378,12 +377,12 @@ int main()
textBackground.setColor(sf::Color(255, 255, 255, 200)); textBackground.setColor(sf::Color(255, 255, 255, 200));
// Create the description text // Create the description text
sf::Text description("Current effect: " + effects[current]->getName(), font, 20); sf::Text description(font, "Current effect: " + effects[current]->getName(), 20);
description.setPosition({10.f, 530.f}); description.setPosition({10.f, 530.f});
description.setFillColor(sf::Color(80, 80, 80)); description.setFillColor(sf::Color(80, 80, 80));
// Create the instructions text // Create the instructions text
sf::Text instructions("Press left and right arrows to change the current shader", font, 20); sf::Text instructions(font, "Press left and right arrows to change the current shader", 20);
instructions.setPosition({280.f, 555.f}); instructions.setPosition({280.f, 555.f});
instructions.setFillColor(sf::Color(80, 80, 80)); instructions.setFillColor(sf::Color(80, 80, 80));

View File

@ -87,8 +87,7 @@ int main()
return EXIT_FAILURE; return EXIT_FAILURE;
// Initialize the pause message // Initialize the pause message
sf::Text pauseMessage; sf::Text pauseMessage(font);
pauseMessage.setFont(font);
pauseMessage.setCharacterSize(40); pauseMessage.setCharacterSize(40);
pauseMessage.setPosition({170.f, 200.f}); pauseMessage.setPosition({170.f, 200.f});
pauseMessage.setFillColor(sf::Color::White); pauseMessage.setFillColor(sf::Color::White);

View File

@ -480,14 +480,12 @@ private:
/// } /// }
/// ///
/// // Create a text which uses our font /// // Create a text which uses our font
/// sf::Text text1; /// sf::Text text1(font);
/// text1.setFont(font);
/// text1.setCharacterSize(30); /// text1.setCharacterSize(30);
/// text1.setStyle(sf::Text::Regular); /// text1.setStyle(sf::Text::Regular);
/// ///
/// // Create another text using the same font, but with different parameters /// // Create another text using the same font, but with different parameters
/// sf::Text text2; /// sf::Text text2(font);
/// text2.setFont(font);
/// text2.setCharacterSize(50); /// text2.setCharacterSize(50);
/// text2.setStyle(sf::Text::Italic); /// text2.setStyle(sf::Text::Italic);
/// \endcode /// \endcode

View File

@ -244,7 +244,12 @@ private:
/// ///
/// // Create a sprite and a text to display /// // Create a sprite and a text to display
/// sf::Sprite sprite; /// sf::Sprite sprite;
/// sf::Text text; /// sf::Font font;
/// if (!font.loadFromFile("arial.ttf"))
/// {
/// // error...
/// }
/// sf::Text text(font);
/// ... /// ...
/// ///
/// // Perform OpenGL initializations /// // Perform OpenGL initializations

View File

@ -62,14 +62,6 @@ public:
StrikeThrough = 1 << 3 //!< Strike through characters StrikeThrough = 1 << 3 //!< Strike through characters
}; };
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
/// Creates an empty text.
///
////////////////////////////////////////////////////////////
Text();
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Construct the text from a string, font and size /// \brief Construct the text from a string, font and size
/// ///
@ -85,13 +77,13 @@ public:
/// \param characterSize Base size of characters, in pixels /// \param characterSize Base size of characters, in pixels
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Text(const String& string, const Font& font, unsigned int characterSize = 30); Text(const Font& font, const String& string = "", unsigned int characterSize = 30);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Disallow construction from a temporary font /// \brief Disallow construction from a temporary font
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Text(const String& string, Font&& font, unsigned int characterSize = 30) = delete; Text(Font&& font, const String& string = "", unsigned int characterSize = 30) = delete;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Copy constructor /// \brief Copy constructor
@ -499,7 +491,7 @@ private:
/// font.loadFromFile("arial.ttf"); /// font.loadFromFile("arial.ttf");
/// ///
/// // Create a text /// // Create a text
/// sf::Text text("hello", font); /// sf::Text text(font, "hello");
/// text.setCharacterSize(30); /// text.setCharacterSize(30);
/// text.setStyle(sf::Text::Bold); /// text.setStyle(sf::Text::Bold);
/// text.setFillColor(sf::Color::Red); /// text.setFillColor(sf::Color::Red);

View File

@ -94,11 +94,7 @@ void addGlyphQuad(sf::VertexArray& vertices, sf::Vector2f position, const sf::Co
namespace sf namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Text::Text() = default; Text::Text(const Font& font, const String& string, unsigned int characterSize) :
////////////////////////////////////////////////////////////
Text::Text(const String& string, const Font& font, unsigned int characterSize) :
m_string(string), m_string(string),
m_font(&font), m_font(&font),
m_characterSize(characterSize) m_characterSize(characterSize)
@ -301,10 +297,6 @@ float Text::getOutlineThickness() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Vector2f Text::findCharacterPos(std::size_t index) const Vector2f Text::findCharacterPos(std::size_t index) const
{ {
// Make sure that we have a valid font
if (!m_font)
return Vector2f();
// Adjust the index if it's out of range // Adjust the index if it's out of range
if (index > m_string.getSize()) if (index > m_string.getSize())
index = m_string.getSize(); index = m_string.getSize();
@ -371,8 +363,6 @@ FloatRect Text::getGlobalBounds() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Text::draw(RenderTarget& target, const RenderStates& states) const void Text::draw(RenderTarget& target, const RenderStates& states) const
{
if (m_font)
{ {
ensureGeometryUpdate(); ensureGeometryUpdate();
@ -387,15 +377,11 @@ void Text::draw(RenderTarget& target, const RenderStates& states) const
target.draw(m_vertices, statesCopy); target.draw(m_vertices, statesCopy);
} }
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Text::ensureGeometryUpdate() const void Text::ensureGeometryUpdate() const
{ {
if (!m_font)
return;
// Do nothing, if geometry has not changed and the font texture has not changed // Do nothing, if geometry has not changed and the font texture has not changed
if (!m_geometryNeedUpdate && m_font->getTexture(m_characterSize).m_cacheId == m_fontTextureId) if (!m_geometryNeedUpdate && m_font->getTexture(m_characterSize).m_cacheId == m_fontTextureId)
return; return;

View File

@ -47,7 +47,7 @@ int main(int, char const**)
{ {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
sf::Text text("Hello SFML", font, 50); sf::Text text(font, "Hello SFML", 50);
text.setFillColor(sf::Color::Black); text.setFillColor(sf::Color::Black);
// Load a music to play // Load a music to play

View File

@ -45,7 +45,7 @@ int main(int argc, char const** argv)
{ {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
sf::Text text("Hello SFML", font, 50); sf::Text text(font, "Hello SFML", 50);
text.setFillColor(sf::Color::Black); text.setFillColor(sf::Color::Black);
// Load a music to play // Load a music to play