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();
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With