Skip to content

Instantly share code, notes, and snippets.

@Foaly
Created January 1, 2016 20:07
Show Gist options
  • Save Foaly/0d12d3414838de5cea30 to your computer and use it in GitHub Desktop.
Save Foaly/0d12d3414838de5cea30 to your computer and use it in GitHub Desktop.
Character and line spacing
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Character Spacing Example");
window.setFramerateLimit(60);
// load font
sf::Font font;
if(!font.loadFromFile("data/fonts/BilboSwashCaps-Regular.otf"))
{
std::cerr << "Failed to load font!" << std::endl;
return 1;
}
sf::Text sizeText("M", font);
sizeText.setCharacterSize(100);
sf::FloatRect size = sizeText.getLocalBounds();
std::cout << "x: " << size.left << " y: " << size.top << " width: " << size.width << " height: " << size.height << std::endl;
// setup the texts
sf::Text normalText("SFML rocks! 123", font);
normalText.setCharacterSize(100);
normalText.setPosition(30, 10);
normalText.setStyle(sf::Text::Style::Bold);
sf::Text smallText("SFML rocks! 123", font);
smallText.setCharacterSize(30);
smallText.setPosition(30, 150);
smallText.setStyle(sf::Text::Style::Bold);
sf::Text stretchedText("SFML rocks! 123", font);
stretchedText.setCharacterSize(100);
stretchedText.setPosition(30, 200);
stretchedText.setStyle(sf::Text::Style::Bold);
stretchedText.setLetterSpacing(6.f);
sf::Text smallStretchedText("SFML rocks! 123", font);
smallStretchedText.setCharacterSize(30);
smallStretchedText.setPosition(30, 350);
smallStretchedText.setStyle(sf::Text::Style::Bold);
smallStretchedText.setLetterSpacing(1.f);
sf::Text multiplineText("SFML \nrockx! \n123", font);
multiplineText.setCharacterSize(30);
multiplineText.setPosition(30, 400);
multiplineText.setStyle(sf::Text::Style::Bold | sf::Text::Style::Underlined | sf::Text::Style::StrikeThrough);
multiplineText.setLineSpacing(-10.f);
// run the program as long as the window is open
while (window.isOpen())
{
// check all the windows events
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::KeyReleased) {
// close if escape key was pressed
if (event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
}
// clear the window to black
window.clear();
// draw text
window.draw(normalText);
window.draw(smallText);
window.draw(stretchedText);
window.draw(smallStretchedText);
window.draw(multiplineText);
// display the windows content
window.display();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment