Goal: I would like to dynamically change a sf::text object.
Details: I guess I'm trying to use it to represent a score meaning that it is constantly changing depending on what the user does.
Description of Issue: However, whenever I use setString(std::String(score)); inside of the loops where I detect an event, it causes a dll error that says:
Debug Assertion Failed!
Program: ...MS
....\FILEPATH\File.exe
Line: 1424
Expression: _pFirstBlock == pHead
For information on how your program can cause an assertion failure, see the Visual C++ documentation
(Press Retry to debug the application)
When I press retry it says that File.exe has stopped working. and its just a horrible mess. I'm not really sure why can't use setString. I tried never setting it outside and only setting the string of the sf::Text inside. but that hasn't worked. It will only let me set the string outside of the loop where its going to set once and never change.
Code:
int score = 30;
sf::RenderWindow gui(sf::VideoMode(120, 100), "TMS Control GUI");
//creating the light up effect
sf::RectangleShape light(sf::Vector2f(55, 44));
light.setPosition(bottomLeft.x, bottomLeft.y);//for bottom left
currentClick = &bottomLeft;
light.setFillColor(sf::Color(0, 0, 0, 0));//make transparent
light.setOutlineColor(sf::Color(255, 255, 255, 255));
light.setOutlineThickness(4);
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
{
// error...
printf("ariel.ttf not found");
}
//create word that says stim
sf::Text stimText;
stimText.setFont(font); // select the font
stimText.setString("Stim "); // set the string to display
stimText.setCharacterSize(16); // in pixels, not points!
stimText.setPosition(3, 52);
stimText.setColor(sf::Color(255, 255, 255, 255)); // set the color
}
//actually adding everything we created to the window
gui.clear(sf::Color(0, 0, 0, 255)); // clear tms control gui
gui.draw(light, sf::RenderStates());
gui.draw(stimText, sf::RenderStates());
gui.display(); //show
sf::Text intense;
intense.setFont(font); //select the font
//tried setting the string here
intense.setCharacterSize(16); // in pixels, not points!
intense.setPosition(3, 75); //moves it to the bottom left box below the word stim
intense.setColor(sf::Color(255, 255, 255, 255)); // set the color
//will use later to display stimulation intesities on the gui
//gui.draw(intense, sf::RenderStates());
while (gui.isOpen())
{
sf::Event event;
while (gui.pollEvent(event))
{
if (event.type == sf::Event::Closed)
gui.close();
if (event.type == sf::Event::KeyPressed) { //was a key pressed?
if (event.key.code == sf::Keyboard::Up) //was up pressed?
{
score = score + 1;
}
if (event.key.code == sf::Keyboard::Down) //was down pressed
{
score = score - 1;
}
if (event.key.code == sf::Keyboard::PageUp) // was pageUp pressed
{
score = score + 10;
}
if (event.key.code == sf::Keyboard::PageDown) //was pageDown pressed
{
score = score - 10;
}
if (event.key.code == sf::Keyboard::Space)
{
score = 0;
}
} //end of if there was a keyboard press
if (event.type == sf::Event::TextEntered) { //was text typed
//do stuff
}
}
if (event.type == sf::Event::MouseButtonReleased)
{
// left mouse button is pressed
sf::Vector2i localPosition = sf::Mouse::getPosition(gui);
if (localPosition.x >0 && localPosition.x < 61
&& localPosition.y > 0 && localPosition.y<50){
//top left box
currentClick = &topLeft;
light.setPosition(topLeft.x, topLeft.y);
}
if (localPosition.x >60 && localPosition.x < 120
&& localPosition.y > 0 && localPosition.y<50) {
//top right box :
currentClick = &topRight;
light.setPosition(topRight.x, topRight.y);
//can also do light.setPosition((*currentClick).x, (*currentClick).y);
}
if (localPosition.x >61 && localPosition.x < 120
&& localPosition.y > 50 && localPosition.y<100) {
//bottom right box :arm tms 2
currentClick = &bottomRight;
light.setPosition(bottomRight.x, bottomRight.y);
}
if (localPosition.x >0 && localPosition.x < 61
&& localPosition.y > 50 && localPosition.y<100) {
//for bottom left
currentClick = &bottomLeft;
light.setPosition(bottomLeft.x, bottomLeft.y);
}
//if you think of what to do in the bottom left corner add here
}//end of if mouse click
//AN ATTEMPT TO DYNAMICALLY CHANGE THE INTENSITY
intense.setString("A.");
intense.setString("B.");
gui.clear(sf::Color(0, 0, 0, 255)); // black screen
gui.draw(light, sf::RenderStates());
gui.draw(stimText, sf::RenderStates());
gui.draw(intense, sf::RenderStates());
gui.display(); //NOTE: anytime you display you have to redraw EVERYTHING you want on the screen
//don't expect anythign to stay
} //end gui window while loop
Note: I tried to simply the code that i have down to the basics and only include the parts that I thought would be relevant to this problem. In my program there are no compile/build/basic errors.
Potential Issue that may or may not be relevant: There is a problem that may be related to this issue or its just a problem with sfml. I find that using sf::Text in general has some weird requirements. For example, I can only start my string with a capital letter or @ symbol and I can only end it with a space/period. If I end it with a number, I get the same error I said above even if I only set the string once in a completely legal way. I've never heard of a programming language where the contents of the string have the ability to cause an error so I find that this is the strangest thing about sfml.
Please help. I've been struggling with this for quite some time now and I could really use assistance.