Skip to content

Instantly share code, notes, and snippets.

@MarioLiebisch
Last active May 12, 2018 16:54
Show Gist options
  • Save MarioLiebisch/75636d5e2a3cb465dcf986802b7f4a93 to your computer and use it in GitHub Desktop.
Save MarioLiebisch/75636d5e2a3cb465dcf986802b7f4a93 to your computer and use it in GitHub Desktop.
A minimalistic example to print text in a circle using SFML. (Example output: https://i.imgur.com/2M2OURz.png)
#include <SFML/Graphics.hpp>
int main(int argc, char **argv) {
sf::RenderWindow window(sf::VideoMode(800, 600), "Circle Text", sf::Style::Default);
window.setVerticalSyncEnabled(true);
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut ultrices ante. Aliquam ornare eu enim eget accumsan.", font);
text.setPosition(400, 300);
sf::Shader shader;
shader.loadFromMemory(R"SHADER( void main() {
// Let's define an angle depending on X coordinate.
// This basically defines how "wrap around" the text is.
// The correct factor depends on font, font size, etc.
float a = gl_Vertex.x / 260;
// Next we'll need the radius of our circle.
// This is based on the text's baseline.
float r = 200;
// Calculate the new vertex coordinates.
// Note how I completely ignore the original
// X coordinate (it's included in a).
gl_Vertex.x = (gl_Vertex.y + r) * sin(a);
gl_Vertex.y = (gl_Vertex.y + r) * cos(a);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
gl_FrontColor = gl_Color;
} )SHADER", sf::Shader::Vertex);
sf::RenderStates states; states.shader = &shader;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
}
}
window.clear(sf::Color(64, 127, 192));
window.draw(text, states);
window.display();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment