Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Dynamic Text  (Read 4476 times)

0 Members and 1 Guest are viewing this topic.

arsh

  • Newbie
  • *
  • Posts: 5
    • View Profile
Dynamic Text
« on: July 12, 2016, 08:58:18 pm »

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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Dynamic Text
« Reply #1 on: July 12, 2016, 09:12:44 pm »
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.
Does this mean that the code you have shown here doesn't cause you any problem? Can you show the code that does?

Just as an aside, setString(std::String(score)); doesn't make sense because an std::string doesn't take an integer as a parameter to its constructor.
You probably want (and maybe are already using): setString(std::to_string(score));
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Dynamic Text
« Reply #2 on: July 13, 2016, 09:20:16 pm »
Any chance you're mixing library versions? Also do you have SFML built specifically for the compiler/version you're running? For example, don't try to link release libraries to a debug build and don't try to use Visual Studio 2008 binaries with Visual Studio 2015.

arsh

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Dynamic Text
« Reply #3 on: July 21, 2016, 04:16:53 pm »
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.
Does this mean that the code you have shown here doesn't cause you any problem? Can you show the code that does?

Just as an aside, setString(std::String(score)); doesn't make sense because an std::string doesn't take an integer as a parameter to its constructor.
You probably want (and maybe are already using): setString(std::to_string(score));


sorry I guess I'm doing:
intense.setString(scoreString);
in multiple locations where I change the scoreString. because I want to change the text dynamically. The problem is when I use the above line of code twice (in 2 diff locations), it crashes. and If i just use it once its fine.

note. that if I just use it once and the scoreString is "50A" it'll crash. but if it is "@50." it'll be okay. Never seen a programming language where the contents of the string affect the program so this is super weird to me.

arsh

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Dynamic Text
« Reply #4 on: July 21, 2016, 04:34:50 pm »
Any chance you're mixing library versions? Also do you have SFML built specifically for the compiler/version you're running? For example, don't try to link release libraries to a debug build and don't try to use Visual Studio 2008 binaries with Visual Studio 2015.

I'm not sure. However, everything regarding sfml besides this appears to work just fine so I feel like I did all the linking and stuff properly.