Curses library - begginer's question

Hello.
I wanted to ask few things about Curses library, I decided that my topic will fit here best.

I've recently started working on Curses library. I want to learn how to use it.

I'm using tutorial( http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/index.html ).
Ignoring the fact that I couldn't figure out how to use mouse, as tutorial on site won't work here, I've decided to create a simple program.

My program has to create a window(Fixed size), and allow user to move it with arrows. After each move program outputs in the window that moved coordinates of its 4 corners.
If user press enter, program exits.

I've created this code(I'm really sorry for it, I'm bad at making it readable):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//My first program in Curses
//Create a program, which allows you to move window using arrows
//After each movement window shows coordinates of its 4 corners in itself.

#include <curses.h>

WINDOW* create_newwin(int height, int width, int starty, int startx);
void destroy_window(WINDOW* targetwin);

int main() {
    WINDOW* okno;
    int input;
    int y,x;
    int hei, wid;
    int winx, winy;
    bool doloop = true;

    initscr();
    noecho();
    cbreak();
    keypad(stdscr, TRUE);

    hei = 7;
    wid = 45;
    y = (LINES - hei)/2;
    x = (COLS - wid)/2;
    refresh();
    while(doloop) {
        refresh();
        okno = create_newwin(hei, wid, y, x);
        getbegyx(okno, winy, winx);
        attron(A_REVERSE);
        mvwprintw(okno, 1, 2, "My top left corner coordinates: %d, %d!", winy, winx);
        mvwprintw(okno, 2, 2, "My top right corner coordinates: %d, %d!", winy, winx+wid);
        mvwprintw(okno, 3, 2, "My bottom left corner coordinates: %d, %d!", winy+hei, winx);
        mvwprintw(okno, 4, 2, "My bottom right corner coordinates: %d, %d!", winy+hei, winx+wid);
        attroff(A_REVERSE);
        input = getch();
        switch(input) {
            case KEY_LEFT:
                x-=1;
                break;
            case KEY_RIGHT:
                x+=1;
                break;
            case KEY_UP:
                y-=1;
                break;
            case KEY_DOWN:
                y+=1;
                break;
            case 10:
                doloop = false;
                break;
        }
        destroy_window(okno);
    }
    endwin();
    return 0;
}

WINDOW* create_newwin(int height, int width, int starty,int startx) {
    WINDOW* local_win;
    local_win = newwin(height, width, starty, startx);
    box(local_win, 0, 0);
    wrefresh(local_win);
    return local_win;
}

void destroy_window(WINDOW* targetwin){
   wborder(targetwin, ' ', ' ', ' ',' ',' ',' ',' ',' ');//clear space after window
   wrefresh(targetwin);//refresh it
   delwin(targetwin);
}

Actually both functions allowing me to move window are not efficient(constantly creating and destroying window), but the other solution presented of site(which looked rather like a trick, that with creating window-like structure was used to setup borders of 'window', but no real window was created; I decided to stick to window using).
Now I've got two problems:
1)I can't see what I wrote. If windows leaves border of console(so it disappears) I can see what is written there. But not before(maybe sometimes, for half a second). Theoretically it looks like window was covering the strings, but first - window is created before strings are wrote, second - in order to do so it had to create black space in its middle. So I don't know what to do with that. Tried changing colour, but didn't work.
2)I don't really know how to use refresh(); . It's rather question than problem :)
When I write some of the easy programs, it doesn't matter if I use refresh or not. In other it counts. When do I use refresh?

Thanks for your help guys.:)
I think you should put wrefresh(okno); between line 37-38. I don't think you need refresh() on line 29 because you don't update the stdscr window (default window).
Topic archived. No new replies allowed.