So, same result?
I'd guess, then, that it could very well be something in the remove_last_char() and insert_last_char() functions.
Can we see those?
Sure
here the whole code:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <math.h>
#include <iostream>
#include <sstream>
//#include "include/Collision.h"
#define textsize 200
inline int give_letter_size(char &letter, sf::Font &font, int font_size)
{
return font.getGlyph(int(letter), font_size, false).advance;
}
std::string &word_wrap(std::string &text, int width, int font_size, sf::Font &font)
{
int s_width = 0;
int last_word_position = 0;
bool position_used = false;
for(int i = 0; i <= text.size(); i++)
{
s_width += give_letter_size(text[i], font, font_size);
if(s_width >= width)
{
if(!position_used)
{
text.replace(last_word_position, 1, "\n");
i = last_word_position;
position_used = true;
s_width = 0;
}
else
{
text.insert(i, "\n");
s_width = 0;
}
}
if(text[i] == ' ')
{
last_word_position = i;
position_used = false;
}
}
return text;
}
std::string &remove_last_char(std::string &text)
{
text.erase(text.size(), 1);
return text;
}
std::string &insert_last_char(std::string &text, char char_)
{
text.insert(text.size(), 1, char_);
return text;
}
int main()
{
sf::RenderWindow window(sf::VideoMode::getDesktopMode() , "SFML App", sf::Style::Default);
sf::Font font;
if(!font.loadFromFile("arial.ttf"))
std::cout << "Schriftart konnte nicht geladen werden" << std::endl;
std::string text = "Ich habe etwas zu programmieren, und das ist toll nicht wahr? Ich liebe es wenn etwas bei mir funktioniert. Das ist immer gut. sdfgsghdfhdrtzdfhgdfjhdgsdfgsdfgghfgdhghdgjhgfghjsdfgsgfhgfhhj";
sf::Text showing_text;
text = word_wrap(text, textsize, 20, font);
showing_text.setString(text);
showing_text.setCharacterSize(20);
showing_text.setFont(font);
showing_text.setPosition(0,0);
showing_text.setColor(sf::Color::Red);
std::string angaben = "Copyright by jakob, made for jakeos. Maybe the worst Solution that exist for SFML";
angaben = word_wrap(angaben, textsize, 20, font);
sf::Text show;
show.setString(angaben);
show.setCharacterSize(20);
show.setFont(font);
show.setPosition(1720, 500);
show.setColor(sf::Color::Green);
int width = showing_text.getGlobalBounds().width;
std::cout << width << std::endl;
//bool backspace = false;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
else if(event.type == sf::Event::TextEntered)
{
if(event.text.unicode != 0x00000008)
{
insert_last_char(text, event.text.unicode);
showing_text.setString(text);
}
}
else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::BackSpace)
{
remove_last_char(text);
showing_text.setString(text);
}
}
window.clear();
window.draw(showing_text);
window.draw(show);
window.display();
}
return 0;
}