ncurses: window color don't work - c

I found the solution myself: refresh win after stdscr.
I would like to have a white window in a blue screen, and I tried it with the following code:
initscr();
WINDOW *win = newwin(10, 10, 10, 10);
start_color();
init_pair(1, COLOR_BLACK, COLOR_BLUE);
init_pair(2, COLOR_BLACK, COLOR_WHITE);
wbkgd(stdscr, COLOR_PAIR(1));
wbkgd(win, COLOR_PAIR(2));
wrefresh(win);
refresh();
But I only get a completly blue screen. What is wrong?
Thanks for all help!

Solution: Refresh the window after stdscr:
refresh();
wrefresh(win);
instead of:
wrefresh(win);
refresh();

Related

Box passed window in ncurses.h

I'm having a hard time understanding the problem with a window in ncurses. I created a layout of several windows and want to have a function that will border a window. Look like this:
int main() {
use_env(TRUE);
initscr();
start_color();
cbreak();
noecho();
curs_set(0);
WINDOW *win_corner = newwin(WCLINES, WCCOLS, WCX, WCY);
choose_lvl(win_corner);
...
}
int choose_lvl(WINDOW *win) {
box(win, 0, 0);
wrefresh(win);
refresh();
int c, curr_option = 0;
init_pair(1, COLOR_CYAN, COLOR_BLACK);
attron(COLOR_PAIR(1));
mvwprintw(win, 1, 1, "Choose your level");
...
}
It seems like box() doesnt have any effect at all - what could it be? My current theory is to suppose something wrong with the coordinates of the window.
refresh() refreshes the updates on stdscr. newwin() creates an independant window. A refresh on stdscr does not update the alternate window. The same without refresh() in choose_lvl() function would work fine as you would force update only on the alternate window.
You can also create a sub-window of stdscr with subwin() to make refresh() update the whole hierarchy:
#include <unistd.h>
#include <curses.h>
#define WCLINES 10
#define WCCOLS 80
#define WCX 4
#define WCY 4
int choose_lvl(WINDOW *win) {
box(win, 0, 0);
//wrefresh(win);
//refresh();
int c, curr_option = 0;
init_pair(1, COLOR_CYAN, COLOR_BLACK);
attron(COLOR_PAIR(1));
mvwprintw(win, 1, 1, "Choose your level");
refresh();
return 0;
}
int main() {
use_env(TRUE);
initscr();
start_color();
cbreak();
noecho();
curs_set(0);
//WINDOW *win_corner = newwin(WCLINES, WCCOLS, WCX, WCY);
WINDOW *win_corner = subwin(stdscr, WCLINES, WCCOLS, WCX, WCY);
choose_lvl(win_corner);
// quick&dirty pause to suspend program execution
// right after the windows display
pause();
return 0;
}
Execution:
$ gcc win.c -lncurses
$ ./a.out
The result is:

How do I use a software SDL_Renderer correctly while resizing the window?

I'm not sure how to use a software SDL_Renderer to render to a window while resizing that window. For example, I expect the following code to constantly display a fully black window even as the uses resizes the window. Instead, when the user resizes the window, it's not filled with black, you get kind of "streaks" of black against a transparent background.
I'm sure I could cobble something together with catching resize events, maybe using SDL_RenderFilleRect() instead of SDL_RenderClear(), but fundamentally I just don't know what I should be doing at all. Should I be freeing the SDL_Surface and getting an entirely new surface and renderer every time the window resizes? Should I be calling SDL_RenderSetViewport()?
#include <SDL.h>
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow(
"Main Window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
600,
600,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
);
SDL_Surface *window_surface = SDL_GetWindowSurface(window);
SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(window_surface);
SDL_Event event;
bool quit = false;
while (!quit)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(window);
SDL_Delay(1000 / 60);
}
SDL_Quit();
return 0;
}

ncurses print in new color without changing the previous printed text

How can I use ncurses to print some text with a new color pair, but without changing the colors of previously printed text?
initscr();
start_color();
init_pair(1, COLOR_RED, COLOR_GREEN);
attron(COLOR_PAIR(1));
printw("Red text + Green back\n");
refresh();
attroff(COLOR_PAIR(1));
init_pair(1, COLOR_GREEN, COLOR_WHITE);
attron(COLOR_PAIR(1));
printw("Green text + white back\n"); //previously printed text also change to this new color
i know it's old, but hope to help someone.
initscr();
start_color();
init_pair(1, COLOR_RED, COLOR_GREEN);
attron(COLOR_PAIR(1));
printw("Red text + Green back\n");
attroff(COLOR_PAIR(1));
init_pair(2, COLOR_GREEN, COLOR_WHITE); // changed id of pair
attron(COLOR_PAIR(2));
printw("Green text + white back\n");
attroff(COLOR_PAIR(2));
refresh(); // move refresh to the end so both text will be printed

Cairo x11 doesn't display anything

I worked with cairo and X11 before, and had a piece of code working perfectly, and now I am developping a new project (supposed to be a karaoke), and I took a piece of the code that was used to display something on the screen, which worked on the old project, but which doesn't work anymore.
I have been looking for a mistake all day and I must be missing something, because nothing works.
The code is the following.
A first function used to display some text :
void display_line(cairo_surface_t *surface, lyrics_line l)
{
cairo_t *cr;
cr=cairo_create(surface);
cairo_set_source_rgb(cr, 0, 0, 0); // Should paint the window black
cairo_paint(cr);
cairo_set_source_rgb(cr, 1., 1., 1.);
cairo_select_font_face(cr, "Hacker", CAIRO_FONT_SLANT_NORMAL,CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 13);
cairo_move_to(cr, 620, 30);
char text[255];
strcpy(text, l.text);
cairo_show_text(cr, text); // Should print the text in white
cairo_destroy(cr);
printf("%s\n", text);
}
And the main function, that calls the previous one,
void display(song s)
{
// X11 display
Display *dpy;
Window rootwin;
Window win;
int scr;
// init the display
if(!(dpy=XOpenDisplay(NULL))) {
fprintf(stderr, "ERROR: Could not open display\n");
exit(1);
}
scr=DefaultScreen(dpy);
rootwin=RootWindow(dpy, scr);
win=XCreateSimpleWindow(dpy, rootwin, 1, 1, WINSIZEX, WINSIZEY, 0, BlackPixel(dpy, scr), BlackPixel(dpy, scr));
XStoreName(dpy, win, "Karaoke");
KeyPressMask|ButtonPressMask|ExposureMask);
XMapWindow(dpy, win);
// create cairo surface
cairo_surface_t *cs;
cs=cairo_xlib_surface_create(dpy, win, DefaultVisual(dpy, 0), WINSIZEX, WINSIZEY);
cairo_t *cr;
cr=cairo_create(cs);
cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
cairo_paint(cr); // Should fill the window in blue
cairo_destroy(cr);
int i;
printf("\n");
lyrics_line l = {"That's a test", 200};
display_line(cs, l);
usleep(s.text[0].length*10000);
for(i=0; i<s.length; i++)
{
display_line(cs, s.text[i]);
usleep((s.text[i+1].length - s.text[i].length)*10000);
}
cairo_surface_destroy(cs); // destroy cairo surface
XCloseDisplay(dpy); // close the display
}
I don't know if display_line causes a problem, because when I simply try to color the window in blue with displayit doesn't even work.
However, the lyrics do display in the console, so the algorithm is not the problem.
What could I be missing ?
Thanks in advance.

Draw simple box using curses

Just started to learn C and got a project using curses. I can't even get the simplest things to draw right now.
Want to do a box and have the following code and it doesn't work. The screens is just black.
What am I doing wrong?
#include <curses.h>
int main()
{
initscr();
noecho();
crmode();
WINDOW * win = newwin(10, 10, 1, 1);
wrefresh(win);
refresh();
getch();
endwin();
}
Try this.
#include <ncurses.h>
int main(int argc, char *argv[])
{
initscr();
WINDOW *win = newwin(10,10,1,1);
box(win, '*', '*');
touchwin(win);
wrefresh(win);
getchar();
endwin();
return 0;
}

Resources