1

I'm trying to get into the ncurses library, but it appears that no matter what I code, I get this in ncurses: screenshot

With this code:

#include <stdio.h>
#include <ncurses.h>

void main()
{
    WINDOW *vin;
    initscr();
    start_color();
    init_pair(1,COLOR_YELLOW,COLOR_BLUE);
    init_pair(2,COLOR_BLUE,COLOR_YELLOW);
    init_pair(3,COLOR_BLUE,COLOR_WHITE); 
    vin=newwin(12,40,13,0);
    wmove(vin,0,5);
    wprintw(vin,"Hello, World.");
    wbkgd(vin,COLOR_PAIR(1));
    wrefresh(vin);
    getch();
    delwin(vin);
    endwin();
}

But, again, this seems to be an issue no matter what code I write whenever I compile with this:

gcc main.c -lncurses

Is this a bug with the Ubuntu release of ncurses? Or am I forgetting a library?

Tombert
  • 530
  • 5
  • 13
  • 1
    [`void main` is wrong](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c). – melpomene Sep 13 '15 at 22:25
  • 1
    @melpomene: True, but to be clear that's not the cause of the problem. – Keith Thompson Sep 13 '15 at 22:53
  • Strongly suggest: just have one window, the default window, no colors, try the wmove() and wprintw() When you have that working, then add the internal window 'vin' and get that working. Then add the color. By developing step by step, you should easily find where your code contains a logic error – user3629249 Sep 15 '15 at 07:39

1 Answers1

4

You need to call wgetch(vin) instead of getch(). (And, since wgetch will call wrefresh automatically, you can remove the preceding wrefresh(vin).)

ncurses doesn't let you use overlapping windows. Or, perhaps better said, you can use overlapping windows, but you are responsible for displaying them in the right order.

All of the non-w functions -- including getch -- work on the main window (returned by initscr), which covers the entire screen. If you refresh the main window, you'll overwrite any other windows. So you need to ensure that other windows are refreshed after any refresh of the main window. If you use multiple windows, it is generally best to avoid using the main window; just refresh it once at the beginning and thereafter leave it alone.

Because (w)getch will automatically refresh whatever window it applies to, the call to getch() implicitly called refresh(); since that came after the call to wrefresh(vin), your subwindow was overwritten.

rici
  • 234,347
  • 28
  • 237
  • 341