Coordinate Issues

I'm writing an application (obviously, duh) and coordinates are getting very, very confusing now that I've added SFML to my application. This program of mine is supposed to let the user press RETURN and then have the window reprint "System$~>" to the screen and then have it just like the first line was on the screen. However when the user presses RETURN twice, it doesn't make a new line like the first time. Instead when the user types text, it prints the typed character over the first character typed before RETURN was pressed a second time. Is this just my PC, or is it something in the source? Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

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

bool showCursor = false;
bool newLine = false;

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 myChar;
    myChar.setColor(fontColor);
    myChar.setFont(mainFont);
    myChar.setCharacterSize(18);
    myChar.setStyle(sf::Text::Regular);

    sf::String mainString = "System$~> ";
    sf::Text myTxt;
    myTxt.setColor(fontColor);
    myTxt.setString(mainString);
    myTxt.setFont(mainFont);
    myTxt.setCharacterSize(18);
    myTxt.setStyle(sf::Text::Regular);
    myTxt.setPosition(0, 0);

    sf::String cursor = "_";
    sf::Text myCursor;
    myCursor.setColor(fontColor);
    myCursor.setFont(mainFont);
    myCursor.setCharacterSize(18);
    myCursor.setStyle(sf::Text::Regular);
    myCursor.setPosition(myTxt.getGlobalBounds().width + 1, 0);

    sf::String bText;

    sf::Text buffer;
    buffer.setColor(fontColor);
    buffer.setFont(mainFont);
    buffer.setCharacterSize(18);
    buffer.setStyle(sf::Text::Regular);
    buffer.setPosition(myTxt.getGlobalBounds().width + 1, 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();
                }
                if (myEvent.key.code == sf::Keyboard::BackSpace) {
                    if (!bText.isEmpty()) {
                        bText.erase(bText.getSize(), 1);
                    }
                }
                if (myEvent.key.code == sf::Keyboard::Return) {
                    newLine = true;
                }
            }

            if (myEvent.type == sf::Event::TextEntered) {
                if (myEvent.text.unicode < 128) {
                    bText.insert(bText.getSize(), myEvent.text.unicode);
                    buffer.setString(bText);
                }
            }
        }

        if (newLine == true) {
            myTxt.setPosition(0, myTxt.getGlobalBounds().height + 1);
            bText.clear();
            buffer.setString(bText);
            buffer.setPosition((myTxt.getGlobalBounds().width + myCursor.getGlobalBounds().width) + 1, myTxt.getGlobalBounds().height + 1);
            newLine = false;
        }

        wnd.draw(myTxt);
        wnd.draw(buffer);

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

        if(showCursor == true) {
            cursor.insert(cursor.getSize(), "_");
            myCursor.setPosition((myTxt.getGlobalBounds().width + buffer.getGlobalBounds().width) + 1, 0);
            wnd.draw(myCursor);
        } else {
            cursor.erase(cursor.getSize(), 1);
        }

        wnd.display();
    }
}
1
2
        if (newLine == true) {
            myTxt.setPosition(0, myTxt.getGlobalBounds().height + 1);


getGlobalBounds().height gives you the height of one line of text I assume.

Which means that whenever the user presses Return, you are not moving down 1 line, as you intended... instead you are moving down 1 line from the top of the screen.

If you want to move down a line from where you are, you need to include where you are in the math:

 
myTxt.setPosition(0, myTxt.getPosition().y + myTxt.getGlobalBounds().height + 1);
Last edited on
Topic archived. No new replies allowed.