let INSTALL be distributed
[cmover.git] / src / main.c
blob55627c95dcf3056d4972cfd5974a9f9fd136619d
1 /* cmover: moves a character over the screen using curses */
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <curses.h>
6 static void quit (void);
7 static void update_screen (void);
8 static void update_mainview (void);
9 static void update_statusline (void);
10 static void check_size (void);
11 static void create_windows (void);
12 static int can_move (int, int);
13 static WINDOW * mainview = NULL;
14 static WINDOW * statusline = NULL;
15 static unsigned int lines, cols; /* size to work with */
16 static unsigned int x, y; /* coordinates */
17 static char c='X'; /* character to use */
19 int main (int argc, char ** argv)
21 int input;
22 int mv_x, mv_y;
24 atexit (quit);
26 initscr ();
27 noecho ();
28 clear ();
30 lines = LINES;
31 cols = COLS;
32 x = cols / 2;
33 y = lines / 2;
35 create_windows ();
36 update_screen ();
38 while (1) {
39 input = wgetch (mainview);
40 mv_x = mv_y = 0;
41 switch (input) {
42 case 'h':
43 case KEY_LEFT:
44 mv_x=-1;
45 break;
46 case 'j':
47 case KEY_DOWN:
48 mv_y=1;
49 break;
50 case 'k':
51 case KEY_UP:
52 mv_y=-1;
53 break;
54 case 'l':
55 case KEY_RIGHT:
56 mv_x=1;
57 break;
58 case 'y':
59 mv_x=-1;
60 mv_y=-1;
61 break;
62 case 'u':
63 mv_x=1;
64 mv_y=-1;
65 break;
66 case 'b':
67 mv_x=-1;
68 mv_y=1;
69 break;
70 case 'n':
71 mv_x=1;
72 mv_y=1;
73 break;
74 case 'q':
75 exit (0);
76 break;
78 check_size ();
79 if ((mv_x || mv_y) && can_move (mv_x, mv_y)) {
80 x += mv_x;
81 y += mv_y;
82 update_screen();
86 return 0;
89 static void quit (void)
91 endwin ();
92 printf ("exiting...\n");
95 static void check_size (void)
97 if (LINES != lines || COLS != cols) {
98 lines = LINES;
99 cols = COLS;
100 create_windows ();
101 clear ();
102 if (y >= lines-1) y = lines-2;
103 if (x >= cols) x = cols-1;
104 update_screen ();
108 /* (re)creates the two windows. this is needed after resizing the screen */
109 static void create_windows (void)
111 if (mainview) delwin (mainview);
112 if (statusline) delwin (statusline);
113 mainview = newwin (lines-1, 0, 0, 0);
114 statusline = newwin (0, 0, lines-1, 0);
115 keypad (mainview, TRUE); /* make arrow keys usable */
118 static void update_screen (void)
120 update_mainview ();
121 update_statusline ();
124 static void update_mainview (void)
126 waddch (mainview, ' ');
127 mvwaddch (mainview, y, x, c);
128 wmove (mainview, y, x);
129 wrefresh (mainview);
132 /* width and height are the size of the whole terminal, not just the mainview. */
133 static void update_statusline (void)
135 mvwprintw (statusline, 0, 0, "x: %i, y: %i, width: %i, height: %i",
136 x, y, cols, lines);
137 wclrtoeol (statusline);
138 wrefresh (statusline);
141 static int can_move (const int mv_x, const int mv_y)
143 return (mv_x+x<cols && mv_y+y<lines-1);