mirror of
https://github.com/SFML/SFML.git
synced 2024-11-28 22:31:09 +08:00
Removed the built-in default font
This commit is contained in:
parent
a3357d9c10
commit
a0c1f5f50f
@ -18,6 +18,11 @@ int main()
|
||||
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML OpenGL", sf::Style::Default, sf::ContextSettings(32));
|
||||
window.setVerticalSyncEnabled(true);
|
||||
|
||||
// Load a font for drawing some text
|
||||
sf::Font font;
|
||||
if (!font.loadFromFile("resources/sansation.ttf"))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Create a sprite for the background
|
||||
sf::Texture backgroundTexture;
|
||||
if (!backgroundTexture.loadFromFile("resources/background.jpg"))
|
||||
@ -140,7 +145,7 @@ int main()
|
||||
|
||||
// Draw some text on top of our OpenGL object
|
||||
window.pushGLStates();
|
||||
sf::Text text("SFML / OpenGL demo");
|
||||
sf::Text text("SFML / OpenGL demo", font);
|
||||
text.setColor(sf::Color(255, 255, 255, 170));
|
||||
text.setPosition(250.f, 450.f);
|
||||
window.draw(text);
|
||||
|
BIN
examples/opengl/resources/sansation.ttf
Normal file
BIN
examples/opengl/resources/sansation.ttf
Normal file
Binary file not shown.
@ -5,6 +5,7 @@
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
|
||||
|
||||
@ -19,6 +20,11 @@ public :
|
||||
{
|
||||
}
|
||||
|
||||
static void setFont(const sf::Font& font)
|
||||
{
|
||||
s_font = &font;
|
||||
}
|
||||
|
||||
const std::string& getName() const
|
||||
{
|
||||
return m_name;
|
||||
@ -43,7 +49,7 @@ public :
|
||||
}
|
||||
else
|
||||
{
|
||||
sf::Text error("Shader not\nsupported");
|
||||
sf::Text error("Shader not\nsupported", getFont());
|
||||
error.setPosition(320.f, 200.f);
|
||||
error.setCharacterSize(36);
|
||||
target.draw(error, states);
|
||||
@ -58,6 +64,12 @@ protected :
|
||||
{
|
||||
}
|
||||
|
||||
static const sf::Font& getFont()
|
||||
{
|
||||
assert(s_font != NULL);
|
||||
return *s_font;
|
||||
}
|
||||
|
||||
private :
|
||||
|
||||
// Virtual functions to be implemented in derived effects
|
||||
@ -69,6 +81,8 @@ private :
|
||||
|
||||
std::string m_name;
|
||||
bool m_isLoaded;
|
||||
|
||||
static const sf::Font* s_font;
|
||||
};
|
||||
|
||||
#endif // EFFECT_HPP
|
||||
|
@ -8,6 +8,8 @@
|
||||
#include <cmath>
|
||||
|
||||
|
||||
const sf::Font* Effect::s_font = NULL;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// "Pixelate" fragment shader
|
||||
////////////////////////////////////////////////////////////
|
||||
@ -87,6 +89,7 @@ public :
|
||||
"Mauris ultricies dolor sed massa convallis sed aliquet augue fringilla.\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");
|
||||
m_text.setFont(getFont());
|
||||
m_text.setCharacterSize(22);
|
||||
m_text.setPosition(30, 20);
|
||||
|
||||
@ -268,6 +271,12 @@ int main()
|
||||
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Shader");
|
||||
window.setVerticalSyncEnabled(true);
|
||||
|
||||
// Load the application font and pass it to the Effect class
|
||||
sf::Font font;
|
||||
if (!font.loadFromFile("resources/sansation.ttf"))
|
||||
return EXIT_FAILURE;
|
||||
Effect::setFont(font);
|
||||
|
||||
// Create the effects
|
||||
std::vector<Effect*> effects;
|
||||
effects.push_back(new Pixelate);
|
||||
@ -288,11 +297,6 @@ int main()
|
||||
textBackground.setPosition(0, 520);
|
||||
textBackground.setColor(sf::Color(255, 255, 255, 200));
|
||||
|
||||
// Load the messages font
|
||||
sf::Font font;
|
||||
if (!font.loadFromFile("resources/sansation.ttf"))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Create the description text
|
||||
sf::Text description("Current effect: " + effects[current]->getName(), font, 20);
|
||||
description.setPosition(10, 530);
|
||||
|
@ -197,20 +197,6 @@ public :
|
||||
////////////////////////////////////////////////////////////
|
||||
Font& operator =(const Font& right);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Return the default built-in font
|
||||
///
|
||||
/// This font is provided for convenience, it is used by
|
||||
/// sf::Text instances by default. It is provided so that
|
||||
/// users don't have to provide and load a font file in order
|
||||
/// to display text on screen.
|
||||
/// The font used is Arial.
|
||||
///
|
||||
/// \return Reference to the built-in default font
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
static const Font& getDefaultFont();
|
||||
|
||||
private :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
@ -316,7 +302,7 @@ private :
|
||||
/// the loadFromFile function for the complete list of supported formats.
|
||||
///
|
||||
/// Once it is loaded, a sf::Font instance provides three
|
||||
/// types of informations about the font:
|
||||
/// types of information about the font:
|
||||
/// \li Global metrics, such as the line spacing
|
||||
/// \li Per-glyph metrics, such as bounding box or kerning
|
||||
/// \li Pixel representation of glyphs
|
||||
|
@ -70,14 +70,14 @@ public :
|
||||
Text();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Construct the string from a string, font and size
|
||||
/// \brief Construct the text from a string, font and size
|
||||
///
|
||||
/// \param string Text assigned to the string
|
||||
/// \param font Font used to draw the string
|
||||
/// \param characterSize Base size of characters, in pixels
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
explicit Text(const String& string, const Font& font = Font::getDefaultFont(), unsigned int characterSize = 30);
|
||||
explicit Text(const String& string, const Font& font, unsigned int characterSize = 30);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Set the text's string
|
||||
@ -109,8 +109,6 @@ public :
|
||||
/// a pointer to the one that you passed to this function.
|
||||
/// If the font is destroyed and the text tries to
|
||||
/// use it, the behaviour is undefined.
|
||||
/// Texts have a valid font by default, which the built-in
|
||||
/// Font::getDefaultFont().
|
||||
///
|
||||
/// \param font New font
|
||||
///
|
||||
@ -179,15 +177,16 @@ public :
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Get the text's font
|
||||
///
|
||||
/// If the text has no font attached, a NULL pointer is returned.
|
||||
/// The returned reference is const, which means that you
|
||||
/// cannot modify the font when you get it from this function.
|
||||
///
|
||||
/// \return Text's font
|
||||
/// \return Pointer to the text's font
|
||||
///
|
||||
/// \see setFont
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
const Font& getFont() const;
|
||||
const Font* getFont() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Get the character size
|
||||
@ -348,10 +347,6 @@ private :
|
||||
/// window.draw(text);
|
||||
/// \endcode
|
||||
///
|
||||
/// Note that you don't need to load a font to draw text,
|
||||
/// SFML comes with a built-in font that is implicitely used
|
||||
/// by default.
|
||||
///
|
||||
/// \see sf::Font, sf::Transformable
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -326,28 +326,6 @@ Font& Font::operator =(const Font& right)
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
const Font& Font::getDefaultFont()
|
||||
{
|
||||
static Font font;
|
||||
static bool loaded = false;
|
||||
|
||||
// Load the default font on first call
|
||||
if (!loaded)
|
||||
{
|
||||
static const signed char data[] =
|
||||
{
|
||||
#include <SFML/Graphics/Arial.hpp>
|
||||
};
|
||||
|
||||
font.loadFromMemory(data, sizeof(data));
|
||||
loaded = true;
|
||||
}
|
||||
|
||||
return font;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
void Font::cleanup()
|
||||
{
|
||||
|
@ -36,7 +36,7 @@ namespace sf
|
||||
////////////////////////////////////////////////////////////
|
||||
Text::Text() :
|
||||
m_string (),
|
||||
m_font (&Font::getDefaultFont()),
|
||||
m_font (NULL),
|
||||
m_characterSize(30),
|
||||
m_style (Regular),
|
||||
m_color (255, 255, 255),
|
||||
@ -122,10 +122,9 @@ const String& Text::getString() const
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
const Font& Text::getFont() const
|
||||
const Font* Text::getFont() const
|
||||
{
|
||||
assert(m_font != NULL); // can never be NULL, always &Font::getDefaultFont() by default
|
||||
return *m_font;
|
||||
return m_font;
|
||||
}
|
||||
|
||||
|
||||
@ -153,7 +152,9 @@ const Color& Text::getColor() const
|
||||
////////////////////////////////////////////////////////////
|
||||
Vector2f Text::findCharacterPos(std::size_t index) const
|
||||
{
|
||||
assert(m_font != NULL);
|
||||
// Make sure that we have a valid font
|
||||
if (!m_font)
|
||||
return Vector2f();
|
||||
|
||||
// Adjust the index if it's out of range
|
||||
if (index > m_string.getSize())
|
||||
@ -212,23 +213,26 @@ FloatRect Text::getGlobalBounds() const
|
||||
////////////////////////////////////////////////////////////
|
||||
void Text::draw(RenderTarget& target, RenderStates states) const
|
||||
{
|
||||
assert(m_font != NULL);
|
||||
|
||||
states.transform *= getTransform();
|
||||
states.blendMode = BlendAlpha; // alpha blending is mandatory for proper text rendering
|
||||
states.texture = &m_font->getTexture(m_characterSize);
|
||||
target.draw(m_vertices, states);
|
||||
if (m_font)
|
||||
{
|
||||
states.transform *= getTransform();
|
||||
states.blendMode = BlendAlpha; // alpha blending is mandatory for proper text rendering
|
||||
states.texture = &m_font->getTexture(m_characterSize);
|
||||
target.draw(m_vertices, states);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
void Text::updateGeometry()
|
||||
{
|
||||
assert(m_font != NULL);
|
||||
|
||||
// Clear the previous geometry
|
||||
m_vertices.clear();
|
||||
|
||||
// No font: nothing to draw
|
||||
if (!m_font)
|
||||
return;
|
||||
|
||||
// No text: nothing to draw
|
||||
if (m_string.isEmpty())
|
||||
return;
|
||||
|
Loading…
Reference in New Issue
Block a user