0

So I've got this program that is supposed to imitate a console (with a little coding help from this user):

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

sf::Color fontColor;
sf::Font mainFont;
sf::Clock myClock;

bool showCursor = true;

void LoadFont() {
    mainFont.loadFromFile("dos.ttf");
    fontColor.r = 0;
    fontColor.g = 203;
    fontColor.b = 0;
}

int main() {
    sf::RenderWindow wnd(sf::VideoMode(1366, 768), "SFML Console");
    wnd.setSize(sf::Vector2u(1366, 768));

    LoadFont();

    sf::Text myTxt;
    myTxt.setColor(fontColor);
    myTxt.setString("System Module:");
    myTxt.setFont(mainFont);
    myTxt.setCharacterSize(18);
    myTxt.setStyle(sf::Text::Regular);
    myTxt.setPosition(0, 0);

    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();
                }
            }
        }

            wnd.clear();

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

                if(showCursor == true) {
                    myTxt.setString("System Module:_");
                } else {
                    myTxt.setString("System Module:");
                }
            }

            wnd.draw(myTxt);
            wnd.display();
    }
}

I need to be able to let the user type a key on the keyboard, and then render that key on the screen. I'm thinking about using an std::vector of sf::Keyboard::Key, and use a while loop to check what the key is (looping through the std::vector<sf::Keyboard::Key>) without using a whole bunch of if statements, but I don't exactly know how to handle that yet, so I'd like to know if there is an easier way to accomplish my main goal. Suggestions? Comments?

Thank you for your time, ~Mike

4

1 に答える 1

2

SFML には、このための優れた機能がありますsf::Event::TextEntered(チュートリアル)。それは通常あなたが望むものであり、ユーザーが入力したテキストを解釈するためにクレイジーなことをすることを避けます. すべての文字を に追加して、入力したテキストをストックしますsf::String( ではなくstd::string、sfml の unicode タイプをより適切に処理できる可能性があります。確かではありませんが、少し確認する必要があります) sf::Text::setString

docsを参照することを躊躇しないでください。すべてのクラスのページに詳細なドキュメントがあります。

例:

sf::String userInput;
// ...
while( wnd.pollEvent(event))
{
    if(event.type == sf::Event::TextEntered)
    {
        /* Choose one of the 2 following, and note that the insert method
           may be more efficient, as it avoids creating a new string by
           concatenating and then copying into userInput.
        */
        // userInput += event.text.unicode;
        userInput.insert(userInput.getSize(), event.text.unicode);
    }
    else if(event.type == sf::Event::KeyPressed)
    {
        if(event.key.code == sf::Keyboard::BackSpace) // delete the last character
        { 
            userInput.erase(userInput.getSize() - 1);
        }
    }
}
于 2013-06-22T23:35:17.967 に答える