SFML community forums

Help => Window => Topic started by: lmsmi1 on June 24, 2013, 12:00:57 am

Title: Backspace not erasing last char?
Post by: lmsmi1 on June 24, 2013, 12:00:57 am
So I've got this little loop in my code:

    while(wnd.isOpen()) {
        sf::Event myEvent;

        while (wnd.pollEvent(myEvent)) {
            if (myEvent.type == sf::Event::Closed) {
                wnd.close();
            }

            if (myEvent.type == sf::Event::KeyPressed) {
                if (myEvent.key.code == sf::Keyboard::Escape) {
                    wnd.close();
                }
                if (myEvent.key.code == sf::Keyboard::Return) {
                    newLine = true;
                }
            }

            if (myEvent.type == sf::Event::TextEntered) {
                if (myEvent.text.unicode == '\b') {
                    if (!bText.isEmpty()) {
                        bText.erase(bText.getSize() - 1, 1);
                        buffer.setString(bText);
                    }
                } else {
                    bText.insert(bText.getSize(), myEvent.text.unicode);
                    buffer.setString(bText);
                }
            }
        }

        if (newLine == true) {
            myTxt.setPosition(0, myTxt.getPosition().y + myTxt.getGlobalBounds().height + 1);
            bText.clear();
            buffer.setString(bText);
            buffer.setPosition(myTxt.getGlobalBounds().width + 1, myTxt.getPosition().y + 1);
            newLine = !newLine;
        }

        wnd.draw(myTxt);
        wnd.draw(buffer);

        /*

        if (myClock.getElapsedTime() >= sf::milliseconds(500)) {
            myClock.restart();
            showCursor = !showCursor;
        }

        if(showCursor == true) {
            cursor = "_";
            myCursor.setPosition(myTxt.getGlobalBounds().width + buffer.getGlobalBounds().width + 1, myTxt.getPosition().y + 1);
        } else {
            cursor.clear();
        }

        wnd.draw(myCursor);

        */

        wnd.display();
    }

It works like a charm. However the application doesn't erase the last character printed on the screen when [BACKSPACE] is pressed. Can anyone explain why this is happening and how to fix this?
Title: Re: Backspace not erasing last char?
Post by: OniLinkPlus on June 24, 2013, 12:19:34 am
You're not clearing the window when you should.
Title: Re: Backspace not erasing last char?
Post by: lmsmi1 on June 24, 2013, 03:39:39 am
What? Do you mean I'm not refreshing the window when I should? I got my code to erase the last char without using sf::RenderWindow::clear() a few hours ago, but for some reason something I added/removed/edited removed the effect.
Title: Re: Backspace not erasing last char?
Post by: Dante12129 on June 24, 2013, 04:23:37 am
You should clear the window in the game loop before you draw anything.
Title: Re: Backspace not erasing last char?
Post by: Laurent on June 24, 2013, 08:00:16 am
http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php

Quote
This clear/draw/display cycle is the only good way to draw things. Don't try other strategies, such as keeping pixels from the previous frame, "erasing" pixels, or drawing once and calling display multiple times. You'll get strange results due to double-buffering.
Modern graphics chipsets and APIs are really made for repeated clear/draw/display cycles where everything is completely refreshed at each iteration of the main loop. Don't be scared to draw 1000 sprites 60 times per second, you're far below the millions of triangles that your computer can handle.

(yes, it's in red also in the tutorial)
Title: Re: Backspace not erasing last char?
Post by: lmsmi1 on June 24, 2013, 07:18:34 pm
http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php

Quote
This clear/draw/display cycle is the only good way to draw things. Don't try other strategies, such as keeping pixels from the previous frame, "erasing" pixels, or drawing once and calling display multiple times. You'll get strange results due to double-buffering.
Modern graphics chipsets and APIs are really made for repeated clear/draw/display cycles where everything is completely refreshed at each iteration of the main loop. Don't be scared to draw 1000 sprites 60 times per second, you're far below the millions of triangles that your computer can handle.

(yes, it's in red also in the tutorial)

So the only logical thing to do would be to add the typed sf::String to the end of a std::vector<sf::String>, then render them in order on the screen from the 0 position to the end of the vector?
Title: Re: Backspace not erasing last char?
Post by: G. on June 24, 2013, 08:24:26 pm
Sounds like a way to do it yeah. (or use a vector of sf::Text, so that you don't have to uselessly compute positions every frame)