inline void get_newlines(string *text, fontOpt font_opt, int x, int y) { int w(0), h(0); //REPLACING "_" BY "\n" int pos = text->find("_"); while(pos != string::npos) { text->erase(pos, 1); text->insert(pos, "\n"); pos = text->find("_"); } string line; string word(""); int last_pos = 0; for(int i(0); i < text->size(); i++) { if( (*text)[i] == '\n') { word.clear(); line.clear(); } else if( (*text)[i] == ' ') { line.append(word); line += ' '; TTF_SizeText(font_opt.font, line.c_str(), &w, &h); if(w > x) { if(last_pos != 0) { text->insert(last_pos + 1,"\n"); line.clear(); line.append(word); line += ' '; } else { printf("Word to print was too big\n"); text->insert(i + 1,"\n"); line.clear(); } } word.clear(); last_pos = i; } else { word.push_back((*text)[i]); } } text->push_back('\n'); }