Search code examples
c++animationncurses

C++ how to move Text with ncurses.h?


C++ how to move Text from off screen to inside the terminal (animation 20/50 millisec.) and then stop the animation. with ncurses.h ? pls with examples

with perhaps a loop that increases the X coordinate from (x = 0-number characters text-1) to (x = 2 or 1), for each Line of text containing the color.

let me explain: the previous sentence no longer moves (it remains stationary on X2 coordinates), the sentence of now makes the entry animation, as soon as the animation ends, it becomes the still sentence together with the other preceding sentences. moves the next one, and continues the loop until it is finished.


I didn't get the result I wanted: (this is the code)

#include <iostream> 
#include <windows.h>
#include <ncurses.h>

using namespace std;

void tdnVoid(short colore)
{
    HANDLE a = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(a, colore);
}

int main()
    {
    initscr();
    int b = 0;

    while (b != 256)
    {
    b++;
    refresh();
    move(0, 0);
    tdnVoid(b);
    cout << b << ". ■ ";
    cout << "\n";
        if (b % 16 == 0)
        {
        cout << "\n";
        }
    Sleep(100);
}

return 0;
}

☑️the result I wanted (GIF): Gif Hello World example - GIF

❌the result I got: Gif C++ Program execution, pls help me solve it

NEW EDIT: according to the documentation, you can use this: mvprintw(y,x,"your string",); refresh(); but i use and try for like 2 hours, having errors;

pls if someone can help me, you will really help me!

this program may seem useless, but I am using it to learn C ++, once I have learned this, I can use the same function and complicate and do more interesting things. hope you understand me (there are those who created Nokia's Snake game with ncurses, there are those who 3D things that rotate in space) thank pls help me to understand :)


Solution

  • stackoverflow guys. i started stackoverflow bad. sorry for my wrong attitude (that's why I didn't get an answer)

    but luckily I managed to get the result I wanted! I can be of help for you! in the documentation it won't say, but through a while () loop I managed to do it!

    int main()
    {
    initscr();
    
    int x = -1;
    
    while (x < 22)
    {
        x++;
        refresh();
        mvprintw(0, x, " C++ colori", 10);
        Sleep(300);
    }
    //other my code
    }
    

    before putting refresh (); which is important to update, then mvprintw() which is like a cout << /*from <iostream> library*/ but with mv (mv is move()) ... which is what I wanted !! the first attribute is the Y which are the vertical, and the second attribute is the X that is to move in the same horizontal line. all separated by commas, attention: I put the x as a variable and not as a number because I made it move through the while loop.

    after put the string of characters, remember: put a space before if not you have a result like this

    ❌output: CCCCCCCCCCCCCC++ colori

    with a space before, it appears to be invisible, so it works.

    ☑️output:                                    C++ colori

    after I put Sleep(/*milliseconds*/); remember to put the <windows.h> library

    10 is the number of characters, I know you could have done better by automating it with char [] =" your string " and make the program count, but it was was a beta.

    ===========

    remember: if you don't put endwin (); at the end of the loop what you saw in the GIF will happen. on all your next normal code. here my first Bug