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

Author Topic: Backspace not erasing last char?  (Read 3991 times)

0 Members and 1 Guest are viewing this topic.

lmsmi1

  • Newbie
  • *
  • Posts: 13
  • Console Obsession
    • View Profile
Backspace not erasing last char?
« 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?

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Backspace not erasing last char?
« Reply #1 on: June 24, 2013, 12:19:34 am »
You're not clearing the window when you should.
I use the latest build of SFML2

lmsmi1

  • Newbie
  • *
  • Posts: 13
  • Console Obsession
    • View Profile
Re: Backspace not erasing last char?
« Reply #2 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.

Dante12129

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Backspace not erasing last char?
« Reply #3 on: June 24, 2013, 04:23:37 am »
You should clear the window in the game loop before you draw anything.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Backspace not erasing last char?
« Reply #4 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)
Laurent Gomila - SFML developer

lmsmi1

  • Newbie
  • *
  • Posts: 13
  • Console Obsession
    • View Profile
Re: Backspace not erasing last char?
« Reply #5 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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Backspace not erasing last char?
« Reply #6 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)
« Last Edit: June 24, 2013, 08:26:05 pm by G. »