blinking cursor in sfml?

closed account (Dy7SLyTq)
so im trying to make a blinking cursor to give it a terminal feel, but it is speradic, since it is going at cycles instead of seconds. how can i fix this?

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
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow App(sf::VideoMode(900, 750), "Fuck it. Uploads Happen.");
    sf::Font         Font;

    if(!Font.LoadFromFile("font/Exo-Light.otf")) Font = sf::Font::GetDefaultFont();

    App.SetPosition(125, 75);

    sf::String Header("Facebook Uploader", Font, 30),
               Cursor("|",                 Font, 30);

    Header.SetColor(sf::Color::Green);

    Header.SetPosition(10, 10);
    Cursor.SetPosition(275, 10);

    bool Shown = true;

    while(App.IsOpened())
    {
        sf::Event Event;

        Cursor.SetColor( Shown ? sf::Color::Green : sf::Color::Black);
        Shown = !Shown;

        while(App.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed)                                              App.Close();
            if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) App.Close();
        }

        App.Clear();

        App.Draw(Header);
        App.Draw(Cursor);

        App.Display();
    }

    return 0;
}
closed account (D80DSL3A)
Toggle the value of Shown only once every 20 cycles or so? And, only draw Cursor when Shown is true?
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
Cursor.SetColor(sf::Color::Green);// keep constant color
bool Shown = true;
const int blinkFreq = 20;
int blinkCnt = 0;

    while(App.IsOpened())
    {
        sf::Event Event;
    
        // toggle every blinkFreq cycles
        if( ++blinkCnt >= blinkFreq )
        {
            Shown = !Shown;
            blinkCnt = 0;
        }

        while(App.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed)                                              App.Close();
            if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) App.Close();
        }

        App.Clear();

        App.Draw(Header);
        if( Shown ) App.Draw(Cursor);// draw only when Shown

        App.Display();
    }

What controls the render rate? I always set App.UseVerticalSync(true);
so I can count on that 20 cycle delay = 1/3second or so.
Last edited on
Animation can typically be done one of two ways:

1) By "frame"
2) By time.

You seem to be doing it by frame... which is problematic because you don't specify a fixed framerate. Which means faster computers will blink faster than slower computers.


By time is usually the way to go with animation. For a simple blinking cursor.. it might be as easy as checking the current system time to get the current time in seconds... and only draw the cursor if the time is even (draw nothing when odd).
You're using SFML version 1.6 which hasn't been updated in years, isn't currently supported, and has several bugs which will never be fixed. I would strongly suggest updating to the current version. In 2.0/2.1 you could do so using the following 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
#include <SFML/Graphics.hpp>

const char* fontFileName = "font/Exo-Light.otf";

class Cursor : public sf::Drawable
{
public:
    Cursor(sf::Time delay, sf::Font& font, sf::Color color = sf::Color::White,  unsigned size=30, char ch = '|') 
        : _delay(delay), _show(true), _cursor(sf::String(ch), font, size) { _cursor.setColor(color);}

    void setPosition(const sf::Vector2f & pos) { _cursor.setPosition(pos); }

    void update()
    {
        if (_clock.getElapsedTime() >= _delay)
        {
            _show = !_show;
            _clock.restart();
        }
    }

    void draw(sf::RenderTarget& target, sf::RenderStates state) const override
    {
        if (_show)
            target.draw(_cursor, state);
    }

private:

    sf::Clock _clock;
    const sf::Time _delay;
    bool _show;
    sf::Text _cursor;
};


int main()
{
    sf::RenderWindow App(sf::VideoMode(900, 750), "Fuck it. Uploads Happen.");
    sf::Font Font;

    if (!Font.loadFromFile(fontFileName))
        return 1;

    App.setPosition(sf::Vector2i(125, 75));

    sf::Text Header("Facebook Uploader", Font, 30);
    Cursor cursor(sf::milliseconds(400), Font, sf::Color::Green);

    Header.setColor(sf::Color::Green);

    Header.setPosition(sf::Vector2f(10.0f, 10.0f));
    cursor.setPosition(sf::Vector2f(275.0f, 10.0f));

    while ( App.isOpen() )
    {
        sf::Event Event;

        while (App.pollEvent(Event))
        {
            if (Event.type == sf::Event::Closed)
                App.close();

            if (Event.type == sf::Event::KeyPressed && Event.key.code == sf::Keyboard::Escape)
                App.close();
        }

        cursor.update();

        App.clear();

        App.draw(Header);
        App.draw(cursor);

        App.display();
    }

    return 0;
}


I leave it to you to translate to version 1.6 if you must.

[Edit: removed unused variable which was an artifact of using OP's code as a base.]
Last edited on
closed account (Dy7SLyTq)
alright i wasnt aware of that. ill try to update then
closed account (Dy7SLyTq)
i tried compiling and got these errors (i installed all of the dependecies that the tutorial told me to and then sudo apt-get update'd.


/home/dtscode/Desktop/gcc-4.8.1/lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../i686-pc-linux-gnu/bin/ld: warning: libGLEW.so.1.7, needed by ../SFML-2.1/lib/libsfml-graphics.so, not found (try using -rpath or -rpath-link)
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewUniform1fARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__GLEW_ARB_shader_objects'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewGetObjectParameterivARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__GLEW_ARB_vertex_shader'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewFramebufferTexture2DEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewUniformMatrix4fvARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewGenFramebuffersEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewUniform3fARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewRenderbufferStorageEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__GLEW_ARB_fragment_shader'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewActiveTextureARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewShaderSourceARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `glewInit'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewBindFramebufferEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewFramebufferRenderbufferEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewLinkProgramARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewUseProgramObjectARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewBlendFuncSeparateEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewGenRenderbuffersEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewUniform2fARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewUniform4fARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewCreateProgramObjectARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `glewGetErrorString'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewCompileShaderARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewDeleteRenderbuffersEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewDeleteObjectARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewGetUniformLocationARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewGetInfoLogARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__GLEW_EXT_blend_func_separate'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewDeleteFramebuffersEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewCheckFramebufferStatusEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewUniform1iARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__GLEW_ARB_texture_non_power_of_two'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__GLEW_ARB_shading_language_100'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewCreateShaderObjectARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewBindRenderbufferEXT'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__GLEW_EXT_framebuffer_object'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewAttachObjectARB'
../SFML-2.1/lib/libsfml-graphics.so: undefined reference to `__glewGetHandleARB'
collect2: error: ld returned 1 exit status

The error tells you that you need libGLEW-1.7
warning: libGLEW.so.1.7, needed by ../SFML-2.1/lib/libsfml-graphics.so, not found


Could've stopped there. The rest is a result of that.

The following may be of interest:
http://en.sfml-dev.org/forums/index.php?topic=12504.msg87350#msg87350

closed account (Dy7SLyTq)
from this am i to understand i need to compile sfml 2.1 from source?
That's one possible thing you could do to resolve your situation. The other would be to obtain the version of libglew that your precompiled SFML package requires.

Personally, I always compile from source when possible.
closed account (Dy7SLyTq)
i dont usually have much luck with that, but ill try. thanks for all of the help!
closed account (Dy7SLyTq)
ok now im getting this error: ./uploader: error while loading shared libraries: libsfml-graphics.so.2: cannot open shared object file: No such file or directory but i ran sudo make install to install to usr/local/include and usr/local/lib. im compiling with ../gcc*/bin/g++ -std=c++11 main.cpp -o uploader -lsfml-graphics -lsfml-window -lsfml-system
Did you run ldconfig?
closed account (Dy7SLyTq)
no i didnt know i needed to do that
closed account (Dy7SLyTq)
thanks that worked.
Topic archived. No new replies allowed.