SFML drawing

Hi, I try to make a text editor sort of in SFML. funny huh? anyway just testing the sf::Text class. But this code does not display the character on the screen. I am sure the problem is in the event loop.

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
/********************************************************************
aim:
-
********************************************************************/
//headers
#include <SFML/Graphics.hpp>

//namespaces

//other definitions and declarations


//main function
int main()
{
    sf::RenderWindow *mainWnd=new sf::RenderWindow(sf::VideoMode(800,600),L"Prog-2: Main Window");

    if(mainWnd!=NULL)
    {
        sf::Event evtMainWnd;
        sf::Text msg;
        sf::String tmp;

        msg.setColor(sf::Color(0,255,0));
        msg.setCharacterSize((unsigned)30);

        while(mainWnd->isOpen())
        {
            while(mainWnd->pollEvent(evtMainWnd))
            {
                switch(evtMainWnd.type)
                {
                case sf::Event::Closed:
                    mainWnd->close();
                    break;
                case sf::Event::KeyPressed:
                    if(evtMainWnd.key.code==sf::Keyboard::Q)
                    {
                        tmp+=msg.getString();
                        msg.setString(tmp);
                    }
                default:
                    break;
                }
            }
            mainWnd->clear();
            mainWnd->draw(msg);
            mainWnd->display();
        }
        delete mainWnd;
    }

    return 0;
}


Aceix.
...

Aceix.
I believe the standard Ariel font was removed a while ago, maybe you should add a font. See the SFML documentation for more information.
Thanks a lot! thought it was still there as in v1.6(SFML).

Aceix.
...Added a font but still won't show on screen. any idea?

I did this:
1
2
3
sf::Font monotypeCorsiva;
monotypeCorsiva.loadFromFile("MTCORSVA.TTF");
msg.setFonf(monotypeCorsiva);


I also made sure that the object is still alive, and the font file is present. compiles and shows no error, but still doesn't work!

Aceix.
Last edited on
I think the problem is in lines 39 and 40. You add the string of msg to tmp, and then set msg's string to tmp. But since msg's string is empty, tmp will be empty true and msg will stay empty.

And by the way, when dealing with entered text in SFML it's much handier to use the TextEntered event: http://sfml-dev.org/tutorials/2.0/window-events.php , scroll down to 'The TextEntered event'.
Last edited on
Thank you Fransje, I'll try that.

Aceix.
Ok so it worked. Thanks to you all!

Here is the final 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/********************************************************************
aim:
-
********************************************************************/
//headers
#include <SFML/Graphics.hpp>

//namespaces

//other definitions and declarations


//main function
int main()
{
    sf::RenderWindow *mainWnd=new sf::RenderWindow(sf::VideoMode(800,600),L"Prog-2: Main Window");

    if(mainWnd!=NULL)
    {
        sf::Event evtMainWnd;
        sf::Text msg;
        sf::String tmp;
        sf::Font monotypeCorsiva;

        monotypeCorsiva.loadFromFile("MTCORSVA.TTF");
        msg.setColor(sf::Color(0,255,0));
        msg.setCharacterSize((unsigned)30);
        msg.setFont(monotypeCorsiva);

        while(mainWnd->isOpen())
        {
            while(mainWnd->pollEvent(evtMainWnd))
            {
                switch(evtMainWnd.type)
                {
                case sf::Event::Closed:
                    mainWnd->close();
                    break;
                case sf::Event::KeyPressed:
                    if(evtMainWnd.key.code==sf::Keyboard::Q)
                    {
                        tmp=msg.getString();
                        tmp+="q";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::W)
                    {
                        tmp=msg.getString();
                        tmp+="w";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::E)
                    {
                        tmp=msg.getString();
                        tmp+="e";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::R)
                    {
                        tmp=msg.getString();
                        tmp+="r";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::T)
                    {
                        tmp=msg.getString();
                        tmp+="t";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::Y)
                    {
                        tmp=msg.getString();
                        tmp+="y";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::U)
                    {
                        tmp=msg.getString();
                        tmp+="u";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::I)
                    {
                        tmp=msg.getString();
                        tmp+="i";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::O)
                    {
                        tmp=msg.getString();
                        tmp+="o";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::P)
                    {
                        tmp=msg.getString();
                        tmp+="p";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::A)
                    {
                        tmp=msg.getString();
                        tmp+="a";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::S)
                    {
                        tmp=msg.getString();
                        tmp+="s";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::D)
                    {
                        tmp=msg.getString();
                        tmp+="d";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::F)
                    {
                        tmp=msg.getString();
                        tmp+="f";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::G)
                    {
                        tmp=msg.getString();
                        tmp+="g";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::H)
                    {
                        tmp=msg.getString();
                        tmp+="h";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::J)
                    {
                        tmp=msg.getString();
                        tmp+="j";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::K)
                    {
                        tmp=msg.getString();
                        tmp+="k";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::L)
                    {
                        tmp=msg.getString();
                        tmp+="l";
                        msg.setString(tmp);
                    }if(evtMainWnd.key.code==sf::Keyboard::Z)
                    {
                        tmp=msg.getString();
                        tmp+="z";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::X)
                    {
                        tmp=msg.getString();
                        tmp+="x";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::C)
                    {
                        tmp=msg.getString();
                        tmp+="c";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::V)
                    {
                        tmp=msg.getString();
                        tmp+="v";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::B)
                    {
                        tmp=msg.getString();
                        tmp+="b";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::N)
                    {
                        tmp=msg.getString();
                        tmp+="n";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::M)
                    {
                        tmp=msg.getString();
                        tmp+="m";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::Space)
                    {
                        tmp=msg.getString();
                        tmp+=" ";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::Return)
                    {
                        tmp=msg.getString();
                        tmp+="\n";
                        msg.setString(tmp);
                    }
                    if(evtMainWnd.key.code==sf::Keyboard::BackSpace)
                    {
                        tmp=msg.getString();
                        std::string tmp2(tmp.toAnsiString());
                        if(!tmp2.empty())
                            tmp2.pop_back();
                        tmp=tmp2;
                        msg.setString(tmp);
                    }
                default:
                    break;
                }
            }
            mainWnd->clear();
            mainWnd->draw(msg);
            mainWnd->display();
        }
        delete mainWnd;
    }

    return 0;
}


Aceix.
Lines 40-206 can be drastically simplified with std::map<decltype(sf::Keyboard::A), std::string>

16
17
18
19
    sf::RenderWindow *mainWnd=new sf::RenderWindow(sf::VideoMode(800,600),L"Prog-2: Main Window");

    if(mainWnd!=NULL)
    {
The if statement will always be true, new will never return null. You're supposed to just do this:
16
17
sf::RenderWindow mainWnd (sf::VideoMode(800,600),L"Prog-2: Main Window");
//No need to use pointers 
Last edited on
Thanks. I'll take that into consideration.
...but how do I use the map in this case? an example will be cool.

Aceix.
Last edited on
17
18
19
20
21
22
23
24
25
26
27
std::map<decltype(sf::Keyboard::A), std::string> keymap
{
    {sf::Keyboard::A, "a"},
    {sf::Keyboard::B, "b"},
    {sf::Keyboard::C, "c"},
    {sf::Keyboard::D, "d"},
    {sf::Keyboard::E, "e"},
    {sf::Keyboard::F, "f"},
    {sf::Keyboard::G, "g"},
    //...
};
39
40
41
42
43
44
45
46
47
                case sf::Event::KeyPressed:
                    auto it = keymap.find(evtMainWnd.key.code);
                    if(it != keymap.end())
                    {
                        tmp = msg.getString();
                        tmp += it->second;
                        msg.setString(tmp);
                    }
                    break;
Even more simplified:
1
2
3
4
5
6
7
8
case sf::Event::TextEntered:
    if ( evtMainWnd.text.unicode < 128 ) // check if it's a printable character
    {
        tmp += static_cast < char > ( evtMainWnd.text.unicode );
        msg.setString( tmp );
    }

    break;
Thanks to you all.

Aceix.
Topic archived. No new replies allowed.