Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ncurses Window not Displaying Anything

Tags:

c++

linux

ncurses

I am using the ncurses library to write a console application. I have a function that is suppose to write to a window buffer and then refresh the window. In my test function I then call the window function and something happens, since the program moves to the next line (getch() which just waits for a character from me), but nothing is displayed.

Here is my function:

void UI::drawAudience(int audience1, int audience2)
{

    string bar1 = "", bar2 = "";

    for (int i; i < BAR_SIZE; i++)
    {

        bar1 += (i <= audience1) ? ' ' : '+';

        if (i <= audience2)
            bar2 += '+';
    }

    string audienceName = "Audience Name";

    //mvwprintw(audience, 0, 11 - audienceName.size() / 2, "%s", audienceName.c_str());
    //mvwprintw(audience, 1, 0, "%s|%s", bar1.c_str(), bar2.c_str());
    wprintw(audience, "Test");

    wrefresh(audience);
}

Here is my test code:

#include "Tests.h"
#include <string>

using namespace std;

void test()
{
    int y = 1;

    UI testUI;

    initscr();
    cbreak();

    WINDOW* windowTest = newwin(1, 23, 3, 0);

    wprintw(windowTest, "This is a new window");
    wrefresh(windowTest);

    getch();

    clear();

    delwin(windowTest);

    testUI.drawAudience(4,5);

    getch();

    endwin();

}
like image 348
Ironlenny Avatar asked Nov 19 '12 16:11

Ironlenny


1 Answers

EDIT: The cause of you problem is the getch() line. By calling getch() your program sticks the stdscr back on top. A solution is to use wgetch() as described in (https://stackoverflow.com/a/3808913/2372604).

Another solution I found is the following, unfortunately this may not work depending on your implementation of the UI class. Try the following code with both refresh() lines commented out and then try running again with either (or both) lines uncommented. If you don't refresh the screen before refreshing the window, you never get to see your window.

#include <ncurses.h>

int main(int argc, char** argv)
{
    initscr();
    cbreak();
    refresh();      // Important to refresh screen before refresh window

    WINDOW* windowTest = newwin(1, 23, 3, 0);
    wprintw(windowTest, "Hello World");

    //refresh();      // Refresh here works as well
    wrefresh(windowTest);

    getch();

    delwin(windowTest);
    endwin();

    return 0;
}
like image 87
ilent2 Avatar answered Sep 20 '22 00:09

ilent2