Skip to content

Instantly share code, notes, and snippets.

@Bromeon
Created September 20, 2015 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bromeon/d9510ecfd0b6884ce88f to your computer and use it in GitHub Desktop.
Save Bromeon/d9510ecfd0b6884ce88f to your computer and use it in GitHub Desktop.
Demo for SFML text outline feature
// Jan Haller, 20.09.2015
#include <SFML/Graphics.hpp>
#include <locale>
#include <cmath>
int main()
{
sf::RenderWindow window({1000, 600}, "Outline test");
window.setVerticalSyncEnabled(true);
sf::Font font;
font.loadFromFile("C:/Windows/Fonts/arial.ttf");
const std::locale loc("");
sf::String string;
unsigned int style = 0;
float thickness = 4.f;
sf::Text text("Simple editor!\nF1-F4: style\nF5, F6: outline", font, 100u);
text.setPosition({20.f, 20.f});
text.setFillColor(sf::Color::Blue);
text.setOutlineColor(sf::Color::White);
text.setOutlineThickness(thickness);
for (;;)
{
sf::Event e;
while (window.pollEvent(e))
{
if (e.type == sf::Event::Closed)
{
return 0;
}
else if (e.type == sf::Event::TextEntered)
{
sf::Uint32 ch = e.text.unicode;
switch (ch)
{
case '\b':
if (!string.isEmpty())
string.erase(string.getSize() - 1);
break;
case '\r':
string += '\n';
break;
default:
if (std::isprint(ch, loc) || std::isblank(ch, loc) || std::isalnum(ch, loc))
string += ch;
}
text.setString(string);
}
else if (e.type == sf::Event::KeyPressed)
{
switch (e.key.code)
{
case sf::Keyboard::F1: style ^= sf::Text::Bold; break;
case sf::Keyboard::F2: style ^= sf::Text::Italic; break;
case sf::Keyboard::F3: style ^= sf::Text::Underlined; break;
case sf::Keyboard::F4: style ^= sf::Text::StrikeThrough; break;
case sf::Keyboard::F5: thickness = std::min(thickness + 0.2f, 10.f); break;
case sf::Keyboard::F6: thickness = std::max(thickness - 0.2f, 0.f); break;
case sf::Keyboard::Escape: return 0;
}
text.setStyle(style);
text.setOutlineThickness(thickness);
}
}
window.clear();
window.draw(text);
window.display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment