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

Author Topic: Using an std::vector filled with sf::Strings  (Read 3970 times)

0 Members and 2 Guests are viewing this topic.

lmsmi1

  • Newbie
  • *
  • Posts: 13
  • Console Obsession
    • View Profile
Using an std::vector filled with sf::Strings
« on: June 25, 2013, 05:09:06 am »
I've got a program here that doesn't like to set sf::Text's sf::Strings from an std::vector<sf::String>. I've narrowed it down to this line:

lastLine.setString(lineVector[currentVectorPosition]);

And 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;

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

    sf::RenderWindow rWnd(sf::VideoMode(800, 600), "OpenCMD [Version 2, Beta 1: Private Build]");
    rWnd.setSize(sf::Vector2u(800, 600));
    mainFont.loadFromFile("dos.ttf");

    prefix.setCharacterSize(18);
    prefix.setColor(sf::Color(255, 255, 255));
    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::Return) {
                    lineVector.push_back(buf);
                    lineCounter += 1;
                }
                if(event.key.code == sf::Keyboard::Escape) {
                    rWnd.close();
                }
            }
            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 {
            i = 1; // Counter
            currentVectorPosition = 0;
            currentPrefixY = 0; // Set current text Y to 0
            while(i <= lineCounter) { // while i s less than or equal to the number of lines
                prefix.setPosition(0, currentPrefixY); // set the prefix x to 0, y to 0
                lastLine.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, prefix.getPosition().y + prefix.getGlobalBounds().height);
                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();
    }
}
 

My question is: why is my sf::Text not able to set it's sf::String from an std::vector of sf::Strings?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using an std::vector filled with sf::Strings
« Reply #1 on: June 25, 2013, 07:51:38 am »
Quote
while(i <= lineCounter)
Shouldn't it be while(i < lineCounter)?

And you should learn how to use your debugger ;)
Laurent Gomila - SFML developer

lmsmi1

  • Newbie
  • *
  • Posts: 13
  • Console Obsession
    • View Profile
Re: Using an std::vector filled with sf::Strings
« Reply #2 on: June 25, 2013, 06:58:22 pm »
Thanks, I just figured out that code can be destroyed when just one character is added in the wrong place. GDB is very limited in my opinion, but I'll check the documentation. Oh and, could you explain why I have to compare myEvent.key.code to sf::Keyboard::Return? I don't get the same results when comparing myEvent.text.unicode to '\n' for some odd reason.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using an std::vector filled with sf::Strings
« Reply #3 on: June 25, 2013, 07:39:06 pm »
Quote
GDB is very limited in my opinion
No it's not. Maybe it's just the front-end that you use, or that you know only a limited set of commands.

Quote
Oh and, could you explain why I have to compare myEvent.key.code to sf::Keyboard::Return? I don't get the same results when comparing myEvent.text.unicode to '\n' for some odd reason.
sf::Keyboard::Return is a key identifier, not a character; its value could be anything. It can only be compared to another sf::Keyboard::Code.
'\n' is a regular character.
Laurent Gomila - SFML developer

 

anything