0

I'm building a menu for a game using ncurses.h. In this part, I want a blinking text to be on screen and, only exit that loop, if the user press any key. In conio.h, I used this (was not perfect):

void start()
{
    int i;

    for (i = 0; i < 10; i++)
    {
        gotoxy(32, 18 - i);
        printf("1024");
        Sleep(200);
        clrscr();
    }

    gotoxy(32, 8);
    printf("1024");
    gotoxy (35, 18);

    while (!kbhit())            

    {
        textcolor(15);          
        gotoxy (15, 15);
        printf("Press any key to continue...");

        if (kbhit() != 0)                                   

        Sleep(1000);
        textcolor(0);           
        gotoxy (15, 15);
        printf("Aperte qualquer tecla para continuar...");

        if (kbhit() != 0)
            break;

        Sleep(500);

        if (kbhit() != 0)
            break;

    }

    textcolor(15);
    fflush(stdin);
    clrscr();

}

In ncurses.h, I'm trying to do the same thing, but the getch() seems to wait for the user imput, therefore "pausing" the execution.

void start()
{
    int i, ch = 0;

    for (i = 0; i < 10; i++)
    {
        attron(COLOR_PAIR(2));
        move(18 - i, 32);
        printw("1024");
        refresh();
        usleep(200 * 1000);
        clear();
        refresh();
    }

    move(18, 35);

    while (1)
    {
        if (getch() != 0)
            break;

        attron(COLOR_PAIR(2));
        move(15, 15);

        printw("Aperte qualquer tecla para continuar...");
        refresh();

        usleep(1000 * 1000);
        clear();
        refresh();
        move(15, 15);
        printw("Aperte qualquer tecla para continuar...");

        usleep(500 * 1000);

    }

    attroff(COLOR_PAIR(2));
    system("clear");

}

Solutions? Thanks!

NeoFahrenheit
  • 347
  • 5
  • 16

1 Answers1

0

With ncurses (any curses implementation), you can get single-character-input by calling cbreak during initialization (see the manual page section on Initialization).

Also, I'd use napms rather than usleep.

Since your program is using getch, expecting to just poll, you might want to use timeout or nodelay and ignore cases where getch returns ERR. For example

cbreak();    /* control/C will kill the program */
timeout(10); /* 10 milliseconds is a good compromise */
while (1)
{
    if (getch() == ERR)
        continue;  /* ignore, timeout expired with nothing read */
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Hmm, sorry, but I don't know how to use timeout or nodelay for my specific problem. Is that to exit that loop if the user doens't press anything in a x amount of time? If, so I still want to try to be in that loop indefinitely until the user press something... Thank you for your answer! – NeoFahrenheit Jul 23 '17 at 15:05
  • Here's what I got untik now: `while(1) { wattron(win, COLOR_PAIR(CONTINUE_START_ON)); mvwprintw(win, 18, 25, "Press any key to continue"); wrefresh(win); napms(1000); wattron(win, COLOR_PAIR(CONTINUE_START_OFF)); mvwprintw(win, 18, 25, "Press any key to continue"); wrefresh(win); napms(1000); timeout(10); if(getch() == ERR) break; }` – NeoFahrenheit Jul 25 '17 at 21:00
  • breaking on timeout won't be helpful, since the loop will exit after 1 second. – Thomas Dickey Jul 25 '17 at 22:39