forked from superjamie/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curses-window-resize.c
108 lines (74 loc) · 1.97 KB
/
curses-window-resize.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <signal.h>
/* ncurses with window resizing
*
* this handles dynamic resizing of graphical terminals when ncurses is
* drawing separate WINDOW*s instead of just using stdscr.
* documentation on actually implementing this properly is patchy. i
* don't know if this is "right" but hey it works!
*/
// move all this to a header file
int termx, termy, mapwinx, mapwiny, sidebarx, sidebary;
WINDOW *mapwin, *sidebar;
void terminal_start();
void terminal_stop();
void get_window_dimensions();
void draw_window(WINDOW*, int, int, char);
void resizehandler(int);
// start c file here
void terminal_start() {
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
refresh();
}
void terminal_stop() {
endwin();
}
void get_window_dimensions() {
getmaxyx(stdscr, termy, termx);
mapwinx = termx - 25;
mapwiny = sidebary = termy;
mapwin = newwin(mapwiny, mapwinx, 0, 0);
sidebar = newwin(sidebary, sidebarx, 0, mapwinx);
}
void draw_window(WINDOW* win, int height, int width, char c) {
int x, y;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
mvwaddch(win, y, x, c);
}
}
wborder(win, 0, 0, 0, 0, 0, 0, 0, 0);
wrefresh(win);
}
// i stole this idea of a resize handler which just calls start and stop from stone soup - http://crawl.develz.org/
void resizehandler(int sig) {
terminal_stop();
terminal_start();
get_window_dimensions();
draw_window(mapwin, mapwiny, mapwinx, 'm');
draw_window(sidebar, sidebary, sidebarx, 's');
}
int main(int argc, char *argv[]) {
terminal_start();
termx = 0; termy = 0; mapwinx = 0; mapwiny = 0; sidebarx = 25; sidebary = 0;
signal(SIGWINCH, resizehandler);
int c;
get_window_dimensions();
// game loop here
while (true) {
draw_window(mapwin, mapwiny, mapwinx, 'm');
draw_window(sidebar, sidebary, sidebarx, 's');
// handle input
c = 0;
c = getch();
if (c == 'q' || c == 27 /*ESC*/)
break;
}
terminal_stop();
return 0;
}