Strange symbol at the end of the strings when drawn on the screen

I’m working on a game with a dialogue box that has text show up while you’re reading it, like in an RPG. I got it to work, but my one problem is when I draw it to the screen there’s a weird symbol I didn’t type in and isn’t on the keyboard. I have a gif below to demonstrate.

Here’s my code:

TextRenderer.h

#ifndef TEXTRENDERER_H
#define TEXTRENDERER_H
#include 
#include 

using namespace sf;
using namespace std;

class TextRenderer {
    public:
        Texture *t;
        Sprite sprite;
        string currentString, fullString, eBeforeString;
        int currentWordNum, onTheLine;
        vector words;
        Text drawableText;
        Font font;
        Clock charTime;
        TextRenderer();
        void update();
        void newText(string nText);
};

#endif // TEXTRENDERER_H

TextRenderer.cpp

#include "TextRenderer.h"
#include 
#include 
#include 

using namespace sf;
using namespace std;

TextRenderer::TextRenderer() {
    t = new Texture;
    t->loadFromFile("data/images/talkScreen.png");
    sprite.setTexture(*t);
    sprite.setOrigin(sprite.getGlobalBounds().width/2, 0);
    sprite.setScale(4, 4);
    font.loadFromFile("data/fonts/VCR_OSD_MONO_1.001.ttf");
    drawableText.setFont(font);
    drawableText.setFillColor(Color::White);
    drawableText.setCharacterSize(30);
    currentWordNum = 0; /// Initializing variables
    currentString = "";
    eBeforeString = "";
}

void TextRenderer::update() {
    /// If the current word number (spot in the words vector) is less than
    /// the size of the words vector
    if (currentWordNum < words.size()) {
        if (currentString.length() = 0.02) {
                currentString = words.at(currentWordNum).substr(0, currentString.length() + 1);
                charTime.restart();
                onTheLine += 1;
            }
        } else if (currentString == words.at(currentWordNum)) {
            /// If you just finished typing a word
            if (charTime.getElapsedTime().asSeconds() >= 0.02) {
                eBeforeString += currentString + " ";
                currentString = "";
                currentWordNum += 1;
                onTheLine += 1;
                if (currentWordNum != words.size()) {
                    if (onTheLine + words.at(currentWordNum).length() >= 53) {
                        eBeforeString += "n";
                        onTheLine = 0;
                    }
                }
            }
        }
    }
    drawableText.setString(eBeforeString + currentString);
}

void TextRenderer::newText(string nText) {
    fullString = nText;
    /// Breaking the string into 'words' which I add to a vector
    char eStr[fullString.length()];
    for (unsigned i = 0; i < fullString.size(); i++) {
        eStr[i] = fullString.at(i);
    }
    char *spt = strtok(eStr, " ");
    while (spt != NULL) {
        words.push_back(spt);
        spt = strtok(NULL, " ");
    }
    currentString = "";
    eBeforeString = "";
    currentWordNum = 0;
    onTheLine = 0;
    drawableText.setString(currentString);
    drawableText.setPosition(sprite.getPosition().x + 20 - sprite.getGlobalBounds().width/2, sprite.getPosition().y + 10);
    charTime.restart();
}

And in main.cpp, I just call

textScreen.newText("Look at the symbol at the very end of this string on the other side of this period .");

1201ProgramAlarm’s answer solves the immediate problem, but I have a different view.

strtok is a holdover from C. It has its uses, but it requires you to leave the comfort of std::string for the wild and woolly world of c-strings and either dynamic allocation or the non-standard Variable Length Array used by OP. It also has potential failure cases that stem from being written to handle simpler problems from simpler times. For example, all strtok s use the same internal buffer. The obvious threading implications have been solved with thread-local storage, and the remaining problem of two or more concurrent strtok s in one thread is solved with strtok_r (re-entrant strtok ), but seeing as we are coding in C++, we might as well handle this with C++ streams.

Instead may I suggest

void TextRenderer::newText(string nText) {
    fullString = nText;

// replacement starts here
    istringstream in(nText);
    string word;
    while (in >> word) {
        words.push_back(word);
    }
//end replacement

    currentString = "";
    eBeforeString = "";
    currentWordNum = 0;
    onTheLine = 0;
    drawableText.setString(currentString);
    drawableText.setPosition(sprite.getPosition().x + 20 - sprite.getGlobalBounds().width/2, sprite.getPosition().y + 10);
    charTime.restart();
}

This completely eliminates the need for eStr (which pains me because I like the egg hunt) and reduces the code to an easy to read 5 liner.

Documentation on istringstream.

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Strange symbol at the end of the strings when drawn on the screen

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录