how can i blink the text?

how can i blink the text?
on my console class i did:
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
static void BlinkTexttherad(string text, int x=0, int y=0, int delayMilliSecs=500)
    {

        string textspace=text;
        COORD ord={x,y};
        for(int i=0; i<=text.size();i++)
        {
            textspace[i]=' ';
        }
        for(;;)
        {
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);
            std::cout << textspace;
            ::Sleep(300);
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);
            std::cout << text;
            ::Sleep(delayMilliSecs);
        }
    }

    struct arg_struct
    {
        string text;
        int x;
        int y;
        int delay;

    };


    static void *multithreadproc(void *pThisArg)
    {
        arg_struct *args = static_cast<arg_struct*>(pThisArg);
        BlinkTexttherad(args->text,args->x,args->y,args->delay);
        return nullptr;//terminates the thread
    }



    void BlinkText(string text, int x=0, int y=0, int delayMilliSecs=500)
    {
        struct arg_struct args{text,x,y,delayMilliSecs};
        pthread_t some_thread;
        pthread_create(&some_thread, NULL, &multithreadproc,(void*)&args);
    }

but sometimes i get a memory leak :(
what i'm doing wrong?
Last edited on
@Cambalinho

Here's how would set up text to flash on the console..

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
// Blink Text.cpp : main project file.

#include <iostream>
#include <string>
#include <Windows.h>

using std::string;
using std::cout;
using std::cin;
using std::endl;

void gotoXY(int x, int y, string text);

int main()
{
	string words;
	int x;
	getline(cin,words);
	x = words.length();
	string rub(x, ' ');
	for (int x = 0; x < 30; x++)
	{
		gotoXY(10, 12, words);
		Sleep(300);
		gotoXY(10, 12, rub);
		Sleep(300);
	}
	cin >> words;// Just to prevent screen from closing after finishing
    return 0;
}

void gotoXY(int x, int y, string text)
{

	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD CursorPosition;
	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console, CursorPosition);
	cout << text;
}
sometimes i get a memory leak :(
i don't understand why :(
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
static void BlinkTexttherad(string text, int x=0, int y=0, int delayMilliSecs=500)
    {
        COORD ord;

        ord.X = x;

        ord.Y = y;
        int textlength = text.length();
        string rub(textlength, ' ');
        for(;;)

        {
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);

            std::cout << rub.c_str();

            ::Sleep(300);

            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);

            std::cout << text.c_str();

            ::Sleep(delayMilliSecs);
        }
    }

    struct arg_struct
    {
        string text;
        int x;
        int y;
        int delay;

    };

    pthread_t some_thread;
    static void *multithreadproc(void *pThisArg)
    {
        arg_struct *args = static_cast<arg_struct*>(pThisArg);
        BlinkTexttherad(args->text,args->x,args->y,args->delay);
        pthread_exit(NULL);
        return nullptr;//terminates the thread
    }



    void BlinkText(string text, int x=0, int y=0, int delayMilliSecs=500)
    {
        struct arg_struct args{text,x,y,delayMilliSecs};

        pthread_create(&some_thread, NULL, &multithreadproc,(void*)&args);
    }
i found the error. is the string. so how can i resolve it?
i tryied:
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
struct arg_struct
    {
        const char *chrtext;
        int x;
        int y;
        int delay;

    };


    static void *multithreadproc(void *pThisArg)
    {
        arg_struct *pThis = static_cast<arg_struct*>(pThisArg);
        int x=pThis->x;
        int y=pThis->y;
        int delay=pThis->delay;
        string text=pThis->chrtext;
        BlinkTexttherad(text,x,y,delay);
        return nullptr;//terminates the thread
    }



    void BlinkText(string text, int x=0, int y=0, int delayMilliSecs=500)
    {
        const char *crtext=text.c_str();
        arg_struct args{crtext,x,y,delayMilliSecs};
        pthread_t some_thread;
        pthread_create(&some_thread, nullptr, &console::multithreadproc,static_cast<void*>(&args));
    }

but i can lose data :(
Um... only the main thread should be touching the console.
Also, why on earth would you want blinking text?

If you just want something to blink a couple of times to get the user's attention, that's one thing. To make blinking text is blinking evil. Don't do it. Just say no!

There are two ways to do it:
- redraw as you are doing
- keep two buffers and switch between them

The update should be part of an update loop controlled by one of the wait functions. Use a Windows timer to signal every 500-600 ms for the blink. Don't forget to also wait on user input.
i fixed 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
static void BlinkTexttherad(string text, int x=0, int y=0, int delayMilliSecs=500)
    {
        COORD ord;

        ord.X = x;

        ord.Y = y;
        int textlength = text.length();
        string rub=text;
        //i know that theres a better solution, but i don't know it
        for(int i=0; i<textlength; i++)
        {
            rub[i]=' ';
        }
        const char *chrtext=text.c_str();
        const char *chrtext2=rub.c_str();
        for(;;)

        {
            COORD ActualCursorPosition;
            PCONSOLE_SCREEN_BUFFER_INFO consoleinfo;
            GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), consoleinfo);
            ActualCursorPosition=consoleinfo->dwCursorPosition;
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);

            std::cout << chrtext2;

            ::Sleep(300);

            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);

            std::cout << chrtext;

            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ActualCursorPosition);
            ::Sleep(delayMilliSecs);

        }
    }

but i see 2 problems:
1 - or i'm getting the ActualCursorPosition on wrong order or on wrong time, because i can't see the cursor :(
2 - i can only get 2 text's on bliking. i wil use an array for resolve it... or maybe is about the 1st problem.
please advice me on how get the actual cursor position.


whitenite1:
1
2
int textlength = text.length();
        string rub(textlength, ' ');

have a memory leak(tested when the code works)
its been a long time since i blinked text, but I always used ANSI escape sequences.

cout << "\x1b[1m blinking "\x1b[0m" << endl;
Jaybob66: it's a ASCCII code, but i can't use that code. i'm using Code Blocks with GCC.
heres my actual 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
#define delay(x) _sleep((long unsigned int)x)
struct arg_struct
    {
        console *consolepointer;
        WORD txtcolors=0;
        string chrtext;
        int x;
        int y;
        int delay;
    };

    //static for be used by threads:
    void BlinkTexttherad(string text, int x=0, int y=0, int delayMilliSecs=500, WORD textcolors=0)
    {
        //text position:
        COORD ord;
        ord.X = x;
        ord.Y = y;

        //getting the empty string of the text:
        int textlength = text.length();
        string rub=text;
        for(int i=0; i<textlength; i++)
        {
            rub[i]=' ';
        }

        //converting the string to char(i need avoid a memory leak):
        const char *chrtext=text.c_str();
        const char *chrtext2=rub.c_str();

        //infinite cycle for blink the text:
        CONSOLE_SCREEN_BUFFER_INFO txtcolor;
        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &txtcolor);
        LPDWORD lpNumberOfAttrsWritten=NULL;
        WORD lpAttribute[textlength];
        FillArray(lpAttribute, textlength, textcolors);
        static boolean blnonce=false;
        blnonce=!blnonce;
        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),lpAttribute,textlength,ord,lpNumberOfAttrsWritten);
        for(;;)
        {
            DWORD srect;
            WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), chrtext2 ,textlength,ord,&srect);
            if(blnonce==true)
                delay(280);
            else
                delay(300);
            WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),chrtext,textlength,ord,&srect);
            if(blnonce==true)
            {
                int dllay=delayMilliSecs-20;
                delay(dllay);
            }
            else
                delay(delayMilliSecs);
        }
    }

    static void *multithreadproc(void *pThisArg)
    {
        arg_struct *pThis = static_cast<arg_struct*>(pThisArg);
        int x=pThis->x;
        int y=pThis->y;
        int delay=pThis->delay;
        string text=pThis->chrtext;

        pThis->consolepointer->BlinkTexttherad(text,x,y,delay, pThis->txtcolors);
        return nullptr;//terminates the thread
    }

    pthread_t some_thread;
    arg_struct args;
void BlinkText(string text, int x=0, int y=0, int delayMilliSecs=500)
    {
        args.txtcolors=wrdtextbackcolor;
        args.chrtext=text;
        args.x=x;
        args.y=y;
        args.delay=delayMilliSecs;
        args.consolepointer=this;
        pthread_create(&some_thread, nullptr, &console::multithreadproc,static_cast<void*>(&args));
        delay(20);
    }

i hope these code helps others.
anotherthing: by some reason, someone tell me for avoiding the Sleep() API function, so i use the _sleep() function(on #define don't use ';' or you can get an error on code).
thanks to all
When someone tells you stuff but cannot offer a reason, it is often safe to disregard said advice.

Sleep() is the correct Windows API function for a non-alertable, blocking delay.
SleepEx() is the correct function for an alertable, blocking delay.

(An alertable function is one that can respond to alert conditions, such as more input ready to read, or a timer, etc. The wait functions are the usual examples of such.)

You have also disregarded my advice about keeping UI stuff in the main thread and using a timer. Don't be surprised if you get a corrupted display.
thanks for all
Topic archived. No new replies allowed.