Advertisement
Guest User

Ncurses Simple moving box

a guest
Jan 30th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. #include <ncurses.h>
  2.  
  3. typedef struct _win_border_struct {
  4.     chtype  ls, rs, ts, bs,
  5.         tl, tr, bl, br;
  6. }WIN_BORDER;
  7.  
  8. typedef struct _WIN_struct {
  9.  
  10.     int startx, starty;
  11.     int height, width;
  12.     WIN_BORDER border;
  13. }WIN;
  14.  
  15. void init_win_params(WIN *p_win);
  16. void print_win_params(WIN *p_win);
  17. void create_box(WIN *win, bool flag);
  18.  
  19. int main(int argc, char *argv[])
  20. {   WIN win;
  21.     int ch;
  22.  
  23.     initscr();          /* Start curses mode        */
  24.     start_color();          /* Start the color functionality */
  25.     cbreak();           /* Line buffering disabled, Pass on
  26.                      * everty thing to me       */
  27.     keypad(stdscr, TRUE);       /* I need that nifty F1     */
  28.     noecho();
  29.     curs_set(FALSE);
  30.     init_pair(1, COLOR_CYAN, COLOR_BLACK);
  31.  
  32.     /* Initialize the window parameters */
  33.     init_win_params(&win);
  34.     print_win_params(&win);
  35.  
  36.     attron(COLOR_PAIR(1));
  37.     refresh();
  38.     attroff(COLOR_PAIR(1));
  39.    
  40.     create_box(&win, TRUE);
  41.     while((ch = getch()) != KEY_F(1))
  42.     {   switch(ch)
  43.         {   case KEY_LEFT:
  44.             if(win.startx>1){
  45.                 create_box(&win, FALSE);
  46.                 --win.startx;
  47.                 create_box(&win, TRUE);
  48.             }
  49.                 break;
  50.             case KEY_RIGHT:
  51.             if(win.startx<122){
  52.                 create_box(&win, FALSE);
  53.                 ++win.startx;
  54.                 create_box(&win, TRUE);
  55.             }
  56.                 break;
  57.             case KEY_UP:
  58.             if(win.starty>1){
  59.                 create_box(&win, FALSE);
  60.                 --win.starty;
  61.                 create_box(&win, TRUE);
  62.             }
  63.                 break;
  64.             case KEY_DOWN:
  65.             if(win.starty<44){
  66.                 create_box(&win, FALSE);
  67.                 ++win.starty;
  68.                 create_box(&win, TRUE);
  69.             }
  70.                 break; 
  71.         }
  72.     }
  73.     endwin();           /* End curses mode        */
  74.     return 0;
  75. }
  76. void init_win_params(WIN *p_win)
  77. {
  78.     p_win->height = 2;
  79.     p_win->width = 2;
  80.     p_win->starty = (LINES - p_win->height)/2; 
  81.     p_win->startx = (COLS - p_win->width)/2;
  82.  
  83.     p_win->border.ls = '|';
  84.     p_win->border.rs = '|';
  85.     p_win->border.ts = '-';
  86.     p_win->border.bs = '-';
  87.     p_win->border.tl = '+';
  88.     p_win->border.tr = '+';
  89.     p_win->border.bl = '+';
  90.     p_win->border.br = '+';
  91.  
  92. }
  93. void print_win_params(WIN *p_win)
  94. {
  95. #ifdef _DEBUG
  96.     mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty,
  97.                 p_win->width, p_win->height);
  98.     refresh();
  99. #endif
  100. }
  101. void create_box(WIN *p_win, bool flag)
  102. {   int i, j;
  103.     int x, y, w, h;
  104.  
  105.     x = p_win->startx;
  106.     y = p_win->starty;
  107.     w = p_win->width;
  108.     h = p_win->height;
  109.     mvprintw(y+1,x+1,"0");
  110.     if(flag == TRUE)
  111.     {   mvaddch(y, x, p_win->border.tl);
  112.         mvaddch(y, x + w, p_win->border.tr);
  113.         mvaddch(y + h, x, p_win->border.bl);
  114.         mvaddch(y + h, x + w, p_win->border.br);
  115.         mvhline(y, x + 1, p_win->border.ts, w - 1);
  116.         mvhline(y + h, x + 1, p_win->border.bs, w - 1);
  117.         mvvline(y + 1, x, p_win->border.ls, h - 1);
  118.         mvvline(y + 1, x + w, p_win->border.rs, h - 1);
  119.  
  120.     }
  121.     else
  122.         for(j = y; j <= y + h; ++j)
  123.             for(i = x; i <= x + w; ++i)
  124.                 mvaddch(j, i, ' ');
  125.                
  126.     refresh();
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement