Question

I am getting a very strange "bug" I guess you could call it. I am using Xcode to develop a simple game and wanted to add some simple graphics.

To do this I decided to use the ncurses library.

In learning how to use ncurses, I began mucking around creating windows, one of the functions I will definitely be using later (possibly for health bars or something) is the box() function that surrounds a window in a box(). The simple test I did was to create a new window in the centre of the strscr window and then call box() on it. The code for this is below:

WINDOW *AboutWindow;
int width=60,height=12;
int OffsetX=0,OffsetY=0;
OffsetX = (getmaxx(stdscr) - width) / 2;
OffsetY = ((getmaxy(stdscr) - height) / 2);
AboutWindow = newwin(height, width, OffsetY, OffsetX);
getch();
box(AboutWindow, 0, 0);
//getch();
wrefresh(AboutWindow);

The odd "bug" is that the box will only get drawn if getch() is called before I call the box() function. If I comment out the getch() the box, does not get drawn.

I am completely flunked as to why this is happening.

Was it helpful?

Solution

Ok, I know the question is old, but for the future reference: There is a way to get it to work.

After reading a tutorial about ncurses here http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO I was wondering how is it that if you printw() and then getch() you don't have to run refresh(). I figured that it is invoked in getch() and it refreshes stdscr which overrides output from any other windows.

So in order to keep the window on screen and get keyboard input you have to use wgetch(WINDOW*).

Your code would look like that:

WINDOW *AboutWindow;
int width=60,height=12;
int OffsetX=0,OffsetY=0;
OffsetX = (getmaxx(stdscr) - width) / 2;
OffsetY = ((getmaxy(stdscr) - height) / 2);
AboutWindow = newwin(height, width, OffsetY, OffsetX);
box(AboutWindow, 0, 0);
wgetch(AboutWindow); //also invokes wrefresh()

OTHER TIPS

call refresh() before any windows related functions.

I don't know why, but it works.

Okay, after an hour of fiddling I've found that there isn't a lot you can do to get rid of the getch(), to my knowledge(which is not very extensive by any means!), however to get it so that you don't have to press a button every time to get past that line of code, you put a halfdelay() time on it, the following code works perfectly.

WINDOW *AboutWindow;
int width=60,height=12;
int OffsetX=0,OffsetY=0;
OffsetX = (getmaxx(stdscr) - width) / 2;
OffsetY = ((getmaxy(stdscr) - height) / 2);
AboutWindow = newwin(height, width, OffsetY, OffsetX);
halfdelay(1);
getch();
box(AboutWindow, 0, 0);
wrefresh(AboutWindow);
  • please note that to exit halfdelay mode call the cbreak() function.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top