Search code examples
c++ncurses

Ncurses - Blank Window That Wait for Button Press Before Initilizing my Window


I am currently using ncurses at an attempt to make a game. I am attempting to force the user to press ENTER on the keypad to start, but seem to be running into some problems. When I create a switch case for the choices, it won't show my splash screen window until I press a button. Without implementing any sort of user input, it shows the splash screen the instant the program is run.

I have included my main and my choices function. Functions such as totalInformation and gameTitle are just graphics and are not important.

My main:

int main(void)
{
initscr();
WINDOW * opening_screen;
keypad(stdscr, TRUE);
noecho();
cbreak();
raw();
start_color();

int height, width, start_y, start_x;
width = 80;
height = 40;
start_y = 0;
start_x = 0;
init_pair(1, COLOR_CYAN, COLOR_CYAN);

opening_screen = newwin(width, height, start_y, start_x);
refresh();

keypad(opening_screen, true);
choices(opening_screen);
wrefresh(opening_screen);

totalInformation();
wrefresh(opening_screen);

border('|', '|', '-', '-', '+', '+', '+', '+');
wrefresh(opening_screen);

attron(COLOR_PAIR(1));
gameTitle();
wrefresh(opening_screen);
attroff(COLOR_PAIR(1));

attron(A_BLINK);
mvaddstr(37, 24, "Press the ENTER key to continue");
attroff(A_BLINK);
wrefresh(opening_screen);

getch();
endwin();
}

My choice function:

void choices(WINDOW * screen)
{
int c = wgetch(screen);
switch(c)
{
    case 10:
        mvaddstr(3,3,"YOUVE PRESSED ENTER");
        break;
    default:
        break;
}
}

Right now, when this runs, a black screen starts off the program. After I press ENTER, my full screen pops up as well as "YOUVE PRESSED ENTER".


Solution

  • When curses starts up, the screen is initially blank, but the program has to refresh (repaint) the screen to get to that point. The wgetch call repaints (and clears, on the terminal) the opening_screen, and then the mvaddstr call updates the main window stdscr. Finally, the getch call refreshes (i.e., repaints) the main window stdscr, and obscures the other windows.