-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
313 lines (242 loc) · 7.66 KB
/
main.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* vim:set ts=4 sw=4 tw=80 et ai si cindent cino=L0,b1,(1s,U1,m1,j1,J1,)50,*90 cinkeys=0{,0},0),0],\:,0#,!^F,o,O,e,0=break:
*/
/**********************************************************************
Squared 7 (curses)
Copyright (C) 2023 Krayon
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 ONLY, as published by the Free Software Foundation.
This program 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 this program, in the file COPYING or COPYING.txt; if
not, see http://www.gnu.org/licenses/ , or write to:
The Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**********************************************************************/
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <assert.h>
#include <time.h>
#include <sys/time.h>
#include <math.h>
#include <ncurses.h>
#include <panel.h>
#define SIGEXIT 0
#define SQR_WIDTH 5 //3
#define SQR_HEIGHT 3 //3
#define COLOR_GREY 8//15
#define TIMESTEP 2000
WINDOW *w_scr = NULL;
WINDOW *w_main = NULL;
PANEL *p_main = NULL;
int w_scr_w = -1;
int w_scr_h = -1;
int counter=0;
WINDOW *BOARD[7*7];
WINDOW *NEW[7];
int col_label = 1;
int col_sqr_empty = 2;
int oldcur = ERR;
int int_width(int number) {
if (number == 0) return 1;
return floor(log10(abs(number))) + 1 + (number < 0 ? 1 : 0);
}
void draw_square(int sq) {
switch (sq) {
case 7:
box(BOARD[sq], '7', '7');
break;
case 14:
wborder(BOARD[sq], '#', '#', '#', '#', '#', '#', '#', '#');
break;
case 25:
box(BOARD[sq], 'X', 'x');
break;
case 26:
box(BOARD[sq], 'O', 'o');
break;
default:
box(BOARD[sq], 0, 0);
}
wrefresh(BOARD[sq]);
}
int print_middle(WINDOW *win, int starty, int startx, int width, char*string, chtype color) {
int len, x, y;
float tmp;
if (win == NULL) win = stdscr;
getyx(win, y, x);
if (startx != 0) x = startx;
if (starty != 0) y = starty;
if (width == 0) width = 80;
len = strlen(string);
tmp = (width - len) / 2;
x = startx + (int)tmp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}
void board_create(int offy, int offx) {
int i;
int startx, starty;
for (starty = 0; starty < 7; ++starty) {
for (startx = 0; startx < 7; ++startx) {
fprintf(stderr, "Creating BOARD[%d,%d:%d] @ (%d, %d)\n", startx, starty, (starty * 7) + startx, SQR_WIDTH * startx, SQR_HEIGHT * starty);
BOARD[(starty * 7) + startx] = newwin(
SQR_HEIGHT
,SQR_WIDTH
,offy + (SQR_HEIGHT * starty)
,offx + (SQR_WIDTH * startx)
);
}
}
for (i = 0; i < 7*7; ++i) {
wattron(BOARD[i], COLOR_PAIR(col_sqr_empty));
draw_square(i);
wattroff(BOARD[i], COLOR_PAIR(col_sqr_empty));
//wrefresh(BOARD[i]);
}
}
// Timer
void init_timer(void) {
struct itimerval it;
// Clear itimerval struct members
timerclear(&it.it_interval);
timerclear(&it.it_value);
// Configure (and start) timer
it.it_interval.tv_usec = TIMESTEP;
it.it_value.tv_usec = TIMESTEP;
setitimer(ITIMER_REAL, &it, NULL);
}
void cb_timer(int signal) {
extern WINDOW * w_scr;
extern int w_scr_w, w_scr_h;
assert(signal == SIGALRM);
++counter;
//mvwprintw(w_scr, w_scr_h - 1, w_scr_w - int_width(counter) - 2, "%d", counter);
return;
}
void cb_sighandler(int signal) {
extern WINDOW * w_main;
extern int oldcur;
fprintf(stderr, "SIG: %d\n", signal);
switch (signal) {
case SIGEXIT:
case SIGTERM:
case SIGQUIT:
case SIGHUP:
case SIGINT:
// Clean up
fprintf(stderr, "Clean up...\n");
nocbreak();
echo();
clear();
if (p_main) del_panel(p_main); p_main = NULL;
if (w_main) delwin(w_main); w_main = NULL;
if (oldcur != ERR) curs_set(oldcur);
endwin();
refresh();
//free_other_resources();
exit(EXIT_SUCCESS);
}
}
void init_signals(void) {
struct sigaction sa;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
// Handle SIGINT, SIGHUP, SIGQUIT and SIGTERM via cb_sighandler
sa.sa_handler = cb_sighandler;
//sigaction(SIGEXIT, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
// Handle SIGALRM (timer) via cb_timer
sa.sa_handler = cb_timer;
sigaction(SIGALRM, &sa, NULL);
// Ignore SIGTSTP
sa.sa_handler = SIG_IGN;
sigaction(SIGTSTP, &sa, NULL);
}
int main(int argc, char* argv[]) {
int key;
int x, y;
extern int w_scr_w;
extern int w_scr_h;
// Seed RNG
srand((unsigned)time(NULL));
w_scr = initscr();
if (has_colors()) start_color();
init_signals();
init_timer();
// Hide cursor
oldcur = curs_set(0);
// No local echo
noecho();
// Disable line buffering?
cbreak();
clear();
refresh();
init_color(COLOR_GREY, 100, 100, 100);
init_pair(col_label, COLOR_RED, COLOR_BLACK);
init_pair(col_sqr_empty, COLOR_GREY, COLOR_BLACK);
getbegyx(w_scr, y, x);
getmaxyx(w_scr, w_scr_h, w_scr_w);
// Screen window
wattron(w_scr, COLOR_PAIR(col_label));
box(w_scr, 0, 0);
// Title bar border
mvwaddch(w_scr, 2, 0, ACS_LTEE);
mvwhline(w_scr, 2, 1, ACS_HLINE, w_scr_w - 2);
mvwaddch(w_scr, 2, w_scr_w - 1, ACS_RTEE);
wattroff(w_scr, COLOR_PAIR(col_label));
// Title bar
move(1, 1);
wattron(w_scr, COLOR_PAIR(col_label));
mvwhline(w_scr, 1, 1, ACS_CKBOARD, w_scr_w - 2);
wattroff(w_scr, COLOR_PAIR(col_label));
print_middle(w_scr, 1, 0, w_scr_w, " Squared 7 ", COLOR_PAIR(col_label));
wrefresh(w_scr);
// Main window
w_main = newwin(SQR_HEIGHT * 7, SQR_WIDTH * 7, 3, 1);
wrefresh(w_main);
p_main = new_panel(w_main);
update_panels();
// Create payfield
board_create(3, 1);
int last_counter = 0;
int cur_sqr = 0;
do {
key = getch();
switch (key) {
case ' ':
wattron(BOARD[cur_sqr], COLOR_PAIR(col_sqr_empty));
draw_square(cur_sqr);
wattroff(BOARD[cur_sqr], COLOR_PAIR(col_sqr_empty));
wrefresh(BOARD[cur_sqr]);
++cur_sqr; if (cur_sqr >= 7 * 7) cur_sqr = 0;
//wbkgd(BOARD[cur_sqr], COLOR_PAIR(col_label));
//wattron(BOARD[cur_sqr], A_BOLD | COLOR_PAIR(col_sqr_empty));
wattron(BOARD[cur_sqr], COLOR_PAIR(col_label));
draw_square(cur_sqr);
wattroff(BOARD[cur_sqr], COLOR_PAIR(col_label));
wrefresh(BOARD[cur_sqr]);
break;
}
if (last_counter < counter) {
//mvwprintw(w_scr, w_scr_h - 1, w_scr_w - int_width(counter) - 2, "%d", counter);
//mvwprintw(w_scr, w_scr_h - 1, w_scr_w - int_width(counter) - 2 - int_width(counter - last_counter) - 3, "%d (%d)", counter, counter - last_counter);
mvwprintw(w_scr, w_scr_h - 1, w_scr_w - 8 - 2 - 4 - 3, "%8d (%4d)", counter, counter - last_counter);
wrefresh(w_scr);
last_counter = counter;
}
} while ((key != 'q') && (key != 'Q'));
cb_sighandler(SIGEXIT);
return 0;
}