-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnstars.c
285 lines (256 loc) · 6.71 KB
/
nstars.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright 2016 Philip J Freeman <[email protected]>
*
* This file is part of stars.
*
* stars is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* stars is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with stars. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "starslib.h"
#define MAX_Z 384
#define INPUT_PROMPT ">"
#define PROMPT_LEN 1
#define MAX_CMD_BUF_LEN 16
#define MAX_CMD_HIST_LEN 4
#define DEFAULT_STAR '.'
#define XSTR(s) #s
#define STR(s) XSTR(s)
#define HELP "\
Usage: %s [OPTION]...\n\
Starfield with ncurses frontend\n\
\n\
-s star character (default " STR(DEFAULT_STAR) ")\n\
-c color mode: mono or 256 (default \"mono\")\n\
-h display this help and exit\n\
"
enum COLORMODE {
CM_MONO,
CM_256
};
#define STARCOLOR(opacity) (232+(256-232-1)*opacity/OPACITY_MAX)
void init256color()
{
static bool inited = false;
if (inited)
return;
if (has_colors() == FALSE) {
endwin();
fprintf(stderr, "terminal does not support colors");
exit(EXIT_FAILURE);
}
start_color();
for (int i=0; i<=OPACITY_MAX; i++)
init_pair(STARCOLOR(i), STARCOLOR(i), COLOR_BLACK);
inited = true;
}
int main(int argc, char *argv[])
{
WINDOW *win_stars, *win_status, *win_input;
int c, x;
int quit = 0;
char cmd_buf[MAX_CMD_BUF_LEN+1] = "";
char *cmd_buf_p = (char *) &cmd_buf;
int buf_len = 0;
int cur_loc = 0;
char *cmd_hist[MAX_CMD_HIST_LEN];
cmd_hist[0] = NULL;
int cmd_hist_loc = -1;
struct universe* u;
int frames = 0;
int sleep_time = 3840;
char star_char = DEFAULT_STAR;
enum COLORMODE colormode = CM_MONO;
// parse command-line options
int opt;
while ((opt=getopt(argc, argv, "s:c:h")) != -1) {
switch (opt) {
case 's':
star_char = *optarg;
break;
case 'c':
if (strcmp(optarg, "mono") == 0) {
colormode = CM_MONO;
} else if (strcmp(optarg, "256") == 0) {
colormode = CM_256;
}
break;
case 'h':
fprintf(stderr, HELP, argv[0]);
exit(EXIT_SUCCESS);
default:
fprintf(stderr, HELP, argv[0]);
exit(EXIT_FAILURE);
}
}
initscr(); /* start curses mode */
raw(); /* disable line buffering */
keypad(stdscr, TRUE); /* enable arrows an function keys */
noecho(); /* don't echo any input */
curs_set(0); /* set invisible cursor */
nodelay(stdscr, TRUE);
if (colormode == CM_256)
init256color();
new_universe(&u, COLS, LINES-2, MAX_Z);
refresh();
win_stars = newwin(LINES-2, COLS, 0, 0);
win_status = newwin(1, COLS, LINES-2, 0);
win_input = newwin(1, COLS, LINES-1, 0);
while (quit == 0) {
// draw stars
wclear(win_stars);
for (;;) {
struct return_point rp;
int return_code = process_point(u, &rp);
if (return_code == 0) break;
if (return_code == 1) {
if (colormode == CM_256)
wattron(win_stars, COLOR_PAIR(STARCOLOR(rp.opacity)));
mvwprintw(win_stars,rp.y+((LINES-2)/2),rp.x+(COLS/2),"%c",star_char);
if (colormode == CM_256)
wattroff(win_stars, COLOR_PAIR(STARCOLOR(rp.opacity)));
}
}
frames++;
wrefresh(win_stars);
for (c=0; c < rand()%2; c++)
new_point(u);
usleep(sleep_time);
// update status line
mvwprintw(win_status, 0, 0, "[Console] f:%06d s:%06d", frames, sleep_time);
wrefresh(win_status);
// redraw input line
buf_len = strlen(cmd_buf_p);
mvwprintw(win_input, 0, 0, INPUT_PROMPT);
mvwprintw(win_input, 0, PROMPT_LEN, "%s", &cmd_buf);
for (x=PROMPT_LEN+buf_len; x <= COLS; x++)
mvwaddch(win_input, 0, x, ' ');
mvwchgat(win_input, 0, PROMPT_LEN+cur_loc, 1, A_REVERSE, 1, NULL);
wrefresh(win_input);
c = getch();
if (c == -1) continue;
switch (c) {
/* ALT ESC? Doesn't work...
case 27:
c = getch();
if (c == -1)
quit = 1;
break;
*/
case KEY_DOWN:
if (cmd_hist_loc > -1){
cmd_hist_loc--;
if (cmd_hist_loc == -1) {
cmd_buf[0] = '\0';
cur_loc=0;
} else {
strncpy(cmd_buf_p, cmd_hist[cmd_hist_loc], MAX_CMD_BUF_LEN);
cur_loc=strlen(cmd_buf_p);
}
} else {
beep();
}
break;
case KEY_UP:
if (
(cmd_hist_loc == -1 && cmd_hist[0] != NULL)
||
(
cmd_hist_loc >=0 &&
cmd_hist_loc < (MAX_CMD_HIST_LEN-1) &&
cmd_hist[cmd_hist_loc+1] != NULL
)
) {
cmd_hist_loc++;
strncpy(cmd_buf_p, cmd_hist[cmd_hist_loc], MAX_CMD_BUF_LEN);
cur_loc=strlen(cmd_buf_p);
} else {
beep();
}
break;
case KEY_LEFT:
if (cur_loc > 0)
cur_loc--;
else
beep();
break;
case KEY_RIGHT:
if (cur_loc < buf_len)
cur_loc++;
else
beep();
break;
case 8: /* Ctrl-H */
case KEY_BACKSPACE:
if (cur_loc > 0) {
for (x=cur_loc; x <= buf_len; x++)
cmd_buf[x-1] = cmd_buf[x];
cur_loc--;
} else {
beep();
}
break;
case '\n': /* Line Feed (0x0A) */
case '\r': /* Carriage Return (0x0D) */
case KEY_ENTER:
if (strncmp(cmd_buf_p, "/quit", MAX_CMD_BUF_LEN) == 0) {
quit = 1;
} else if (strncmp(cmd_buf_p, "/sleep ", 7) == 0) {
sscanf(cmd_buf_p, "/sleep %d", &sleep_time);
} else if ( strlen(cmd_buf_p) == 7 &&
strncmp(cmd_buf_p, "/char ", 6) == 0
) {
sscanf(cmd_buf_p, "/char %c", &star_char);
} else if (strncmp(cmd_buf_p, "/colormode ", 11) == 0) {
if (strcmp(cmd_buf_p+11, "mono") == 0) {
colormode = CM_MONO;
} else if (strcmp(cmd_buf_p+11, "256") == 0) {
init256color();
colormode = CM_256;
}
}
/* save command to history */
if ( cmd_hist[0] == NULL ||
strncmp(cmd_buf_p, cmd_hist[0], MAX_CMD_BUF_LEN) != 0
) {
for (x=0; cmd_hist[x] != NULL && x < (MAX_CMD_HIST_LEN-1); x++);
if (x == (MAX_CMD_HIST_LEN-1))
free(cmd_hist[x]);
else
cmd_hist[x+1] = NULL;
for (;x > 0; x--)
cmd_hist[x] = cmd_hist[x-1];
cmd_hist[0] = strndup(cmd_buf_p, MAX_CMD_BUF_LEN);
}
/* reset command buffer */
cmd_buf[0] = '\0';
cur_loc = 0;
cmd_hist_loc = -1;
break;
default: /* Everything Else! */
if (buf_len == MAX_CMD_BUF_LEN) {
beep();
break;
}
cmd_buf[buf_len] = c;
cmd_buf[buf_len+1] = '\0';
cur_loc++;
}
}
delwin(win_status);
endwin(); /* end curses mode */
}