Need Help with sfml

hey guys,

Using backspace in this program will not work for me.
it will move back a character but will not erase the character.

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
51
 
#include <iostream>
#include <string>

using namespace std;

sf::Event event;
int main()
{
	sf::Vector2i screenDimension(800, 800);
	sf::RenderWindow window;
	window.create(sf::VideoMode(screenDimension.x, screenDimension.y), "My First SFML Game");
				
	sf::Font font;
	if (!font.loadFromFile("Arimo-Regular.ttf"))
		cout << "Can't find the font file." << endl;

	sf::String sentence;
	sf::Text text;
	text.setFont(font);
	text.setCharacterSize(50);
	text.setString(sentence);
	text.setColor(sf::Color(44, 127, 255));
	
	while (window.isOpen())
	{		
		while (window.pollEvent(event))
		{
			switch (event.type)
			{
			case sf::Event::Closed:
				window.close();
				break;
			case sf::Event::KeyPressed:
				if (event.key.code == sf::Keyboard::Escape)
					window.close();
				break;
			case sf::Event::TextEntered:
				if (event.text.unicode >= 32 && event.text.unicode <= 126)
					sentence += (char)event.text.unicode;
				else if (event.text.unicode == 8 && sentence.getSize() > 0)
					sentence.erase(sentence.getSize() - 1, sentence.getSize());
				text.setString(sentence);
				break;
			}
			
			window.display();
			window.draw(text);
		}
	 }
}
closed account (2UD8vCM9)
There are two major issues with your code.

The first issue, is you don't understand how the erase function in a string object works.

You called it like so
sentence.erase(sentence.getSize() - 1, sentence.getSize());

The first argument is the position you want to start at. The second argument is the number of characters you want to erase. Your second argument should be 1, and not the string's total size. That doesn't make any sense.

The next problem is when you're calling to render and the fact that you're not clearing your screen before rendering.

If I render something, and I update that text field that is being rendered, but I never clear my screen, then that text will remain there until I draw something over it in which case it would have to cover every pixel to be gone.

You shouldn't be rendering in your poll loop, you should be rendering in your
while (window.isOpen()) loop.

Here is the updated code. Please study SFML more and try to use google and check out the SFML documentation to get an idea of how certain functions work if you are not sure.

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
#include <iostream>
#include <string>

using namespace std;

sf::Event event;
int main()
{
	sf::Vector2i screenDimension(800, 800);
	sf::RenderWindow window;
	window.create(sf::VideoMode(screenDimension.x, screenDimension.y), "My First SFML Game");
	sf::Font font;
	if (!font.loadFromFile("Arimo-Regular.ttf"))
		cout << "Can't find the font file." << endl;
	sf::String sentence;
	sf::Text text;
	text.setFont(font);
	text.setCharacterSize(50);
	text.setString(sentence);
	text.setColor(sf::Color(44, 127, 255));
	while (window.isOpen())
	{
		while (window.pollEvent(event))
		{
			switch (event.type)
			{
			case sf::Event::Closed:
				window.close();
				break;
			case sf::Event::KeyPressed:
				if (event.key.code == sf::Keyboard::Escape)
					window.close();
				break;
			case sf::Event::TextEntered:
				if (event.text.unicode >= 32 && event.text.unicode <= 126)
					sentence += (char)event.text.unicode;
				else if (event.text.unicode == 8 && sentence.getSize() > 0)			
					sentence.erase(sentence.getSize() - 1,1);
				text.setString(sentence);
				break;
			}
		}
		window.clear();
		window.draw(text);
		window.display();
	}
}
Last edited on
Topic archived. No new replies allowed.