Advertisement

[C++] Clearing Buffer Once

Started by June 25, 2013 06:39 PM
-1 comments, last by lmsmi1 11 years, 5 months ago

So I've got this program to run successfully, but am getting a problem: when I hit the return key, it prints a musical note onto the screen. It does do what I want it to do: make a new line, but it prints the musical note at the beginning of the buffer. How do I get rid of this? Where do I place buf.clear() to get the note character to go away? Here's the full code:

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <vector>

int main() {
    /* Set Variables */
    sf::Color bgColor;
    sf::Font mainFont;
    sf::String pre = "System!>";
    sf::Text prefix;
    sf::String buf;
    sf::Text buffer;
    sf::String lastStr;
    sf::Text lastLine;
    std::vector<sf::String> lineVector;
    int lineCounter = 1;
    int currentVectorPosition = 0;
    int currentPrefixY = 0;
    int i = 0;
    int x = 0;

    bgColor.r = 0;
    bgColor.g = 0;
    bgColor.b = 203;

    sf::RenderWindow rWnd(sf::VideoMode(800, 600), "SFML Window");
    rWnd.setSize(sf::Vector2u(800, 600));
    mainFont.loadFromFile("dos.ttf");

    prefix.setCharacterSize(18);
    prefix.setColor(sf::Color(255, 255, 255));
    prefix.setString(pre);
    prefix.setFont(mainFont);
    prefix.setPosition(0, currentPrefixY);
    prefix.setStyle(sf::Text::Regular);

    buffer.setCharacterSize(18);
    buffer.setColor(sf::Color(255, 255, 255));
    buffer.setFont(mainFont);
    buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, 0);
    buffer.setString(buf);
    buffer.setStyle(sf::Text::Regular);

    lastLine.setCharacterSize(18);
    lastLine.setColor(sf::Color(255, 255, 255));
    lastLine.setFont(mainFont);
    lastLine.setPosition(0, 0);
    lastLine.setString(lastStr);
    lastLine.setStyle(sf::Text::Regular);

    while(rWnd.isOpen()) {
        rWnd.clear(bgColor);

        sf::Event event;
        while(rWnd.pollEvent(event)) {
            if(event.type == sf::Event::Closed) {
                rWnd.close();
            }
            if(event.type == sf::Event::KeyPressed) {
                if(event.key.code == sf::Keyboard::Escape) {
                    rWnd.close();
                }
                if(event.key.code == sf::Keyboard::Return) {
                    lineVector.push_back(buf);
                    buf.clear();
                    lineCounter += 1;
                }
            }
            if(event.type == sf::Event::TextEntered) {
                if(event.text.unicode == '\b') {
                    if(!buf.isEmpty()) {
                        buf.erase(buf.getSize() - 1, 1);
                    }
                } else {
                    buf.insert(buf.getSize(), event.text.unicode);
                }
            }
        }
        /* Somewhere between here */
        if(lineCounter == 1) {
            prefix.setPosition(0, 0);
            buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, 0);
        } else {
            x = 0;
            i = 1;
            currentVectorPosition = 0;
            currentPrefixY = 0;
            while(i < lineCounter) {
                prefix.setPosition(0, currentPrefixY);
                lastLine.setPosition(prefix.getGlobalBounds().width + 1, prefix.getPosition().y);
                x++;
                lastLine.setString(lineVector[currentVectorPosition]);
                rWnd.draw(prefix);
                rWnd.draw(lastLine);
                currentPrefixY = prefix.getPosition().y + prefix.getGlobalBounds().height;
                i += 1;
                currentVectorPosition += 1;
            }
            prefix.setPosition(0, currentPrefixY);
            buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, prefix.getPosition().y/* + prefix.getGlobalBounds().height*/);
        }
        buffer.setString(buf);
        rWnd.draw(prefix);
        rWnd.draw(buffer);
        rWnd.display();
    }
}

This topic is closed to new replies.

Advertisement