1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
int main(){
sf::RenderWindow window(sf::VideoMode(800, 600), "MessageBox");
sf::Font font;
font.loadFromFile("automati.ttf");
sf::Text text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam gravida ipsum hendrerit, dignissim ipsum vitae, mattis est. Cras risus magna, lobortis ut neque quis, bibendum varius mi. Nullam quis diam molestie, maximus nibh sed, hendrerit nibh. Curabitur a consequat ipsum, sed imperdiet sem. Integer et quam lacinia, vulputate diam eu, ultrices ipsum. Morbi et orci nec lectus commodo porta convallis et tellus. Quisque et pulvinar arcu. Duis id libero justo. Duis lacinia ante non eros lobortis luctus. Morbi laoreet id mauris sit amet posuere. ", font, 20);
sf::Vector2f bounds(500, 200);
std::string str = text.getString();
std::vector<std::string> words_vector;
std::stringstream ss(str);
while(ss >> str){
words_vector.push_back(str);
}
label:
text.setString("");
for (int i=0; i<words_vector.size(); i++){
str = text.getString();
text.setString(str + " " + words_vector[i]);
if (text.getLocalBounds().width > bounds.x){
text.setString(str + "\n" + words_vector[i]);
if (text.getLocalBounds().height > bounds.y){
text.setCharacterSize(text.getCharacterSize()*0.95);
goto label;
}
}
}
str = text.getString();
str.erase(0, 1);
text.setString(str);
text.setColor(sf::Color::Black);
while (window.isOpen()){
sf::Event event;
while (window.pollEvent(event)){
if(event.type == sf::Event::Closed){
window.close();
}
}
window.clear(sf::Color::White);
window.draw(text);
window.display();
sf::sleep(sf::milliseconds(10));
}
}
|