]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - testpat/testpat.c
1 /* $NetBSD: testpat.c,v 1.2 2021/01/02 12:10:17 jmcneill Exp $ */
4 * Copyright (c) 2016 Nathanial Sloss <nathanialsloss@yahoo.com.au>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/types.h>
40 int main(int argc
, char *argv
[]) {
41 int i
, col
, colour
, line
, x_limit
, y_limit
, colourOK
, spacing
;
42 int xpos
, ypos
, spacing_residual
, spacing_start
, spacing_end
;
46 char title
[255] = "NetBSD";
49 int colour_list
[6] = {
58 float coord_x
, circle_int
;
62 printf("\nUnknown terminal type.");
69 errx(EINVAL
, "usage: testpat [title]");
72 if (argc
== 2 && strlen(argv
[1]) < (size_t)COLS
)
73 snprintf(title
, sizeof(title
), "%s", argv
[1]);
74 else if (argc
== 2 && (int)strlen(argv
[1]) > COLS
) {
76 errx(EINVAL
, "title string must be less than display columns");
79 colourOK
= has_colors();
81 if (COLS
< 13 || LINES
< 13) {
83 printf("\nTerminal size must be at least 72x25.");
91 init_pair( 0, COLOR_WHITE
, COLOR_BLACK
);
92 init_pair( 1, COLOR_WHITE
, COLOR_RED
);
93 init_pair( 2, COLOR_WHITE
, COLOR_GREEN
);
94 init_pair( 3, COLOR_WHITE
, COLOR_YELLOW
);
95 init_pair( 4, COLOR_WHITE
, COLOR_BLUE
);
96 init_pair( 5, COLOR_WHITE
, COLOR_MAGENTA
);
97 init_pair( 6, COLOR_WHITE
, COLOR_CYAN
);
98 init_pair( 7, COLOR_BLACK
, COLOR_WHITE
);
100 attrset(COLOR_PAIR(0));
103 x_limit
= (COLS
- 1) / 2;
104 x_limit
= x_limit
* 2;
105 y_limit
= (LINES
- 2) / 2;
106 y_limit
= y_limit
* 2;
107 spacing
= 2 * y_limit
/ numcolours
;
108 spacing_residual
= ((2 * y_limit
) % numcolours
) / 2;
109 a_axis
= y_limit
/ 2;
111 grid_unit
= b_axis
/ 13;
113 grid_x
= grid_unit
* 2;
115 int circle_pos
[y_limit
][2];
116 memset(circle_pos
, -1, sizeof(circle_pos
));
118 for (i
= 0; i
< y_limit
; i
++) {
119 /* Draw an elipse (looks more circular.) */
120 circle_int
= (i
- a_axis
) / a_axis
;
121 circle_int
= 1 - powf(circle_int
, 2);
122 circle_int
= circle_int
* powf(b_axis
, 2);
124 /* Draw a circle, commented out as elipse looks better.*/
125 circle_int
= powf(a_axis
, 2) - powf(i
- a_axis
, 2);
127 coord_x
= sqrtf(circle_int
);
128 circle_pos
[i
][0] = (-coord_x
+ ((float)x_limit
/ 2));
129 circle_pos
[i
][1] = (coord_x
+ ((float)x_limit
/ 2));
134 attron(A_ALTCHARSET
);
138 for (line
= 1; line
< y_limit
; line
+= grid_y
) {
139 for (col
= 1; col
< x_limit
; col
= col
+ grid_x
) {
141 while ((xpos
< col
+ grid_x
- 1) && (xpos
<
143 mvaddch(line
+ grid_y
- 1, xpos
, 113 |
148 mvaddch(line
+ grid_y
- 1, xpos
, 110 |
152 while (ypos
< line
+ grid_y
- 1) {
153 for (col
= grid_x
- 1; col
< x_limit
; col
+= grid_x
) {
154 mvaddch(ypos
, col
+ 1, 120 | A_ALTCHARSET
);
160 for (line
= 1; line
< y_limit
; line
+= grid_y
) {
161 mvaddch(line
+ grid_y
- 1, 0, 116 | A_ALTCHARSET
);
162 mvaddch(line
+ grid_y
- 1, x_limit
, 117 | A_ALTCHARSET
);
165 while (ypos
< line
+ grid_y
- 1) {
166 mvaddch(ypos
, 0, 120 | A_ALTCHARSET
);
167 mvaddch(ypos
, x_limit
, 120 | A_ALTCHARSET
);
172 for (col
= 1; col
< x_limit
; col
+= grid_x
) {
173 mvaddch(0, col
+ grid_x
- 1, 119 | A_ALTCHARSET
);
174 mvaddch(y_limit
, col
+ grid_x
- 1, 118 | A_ALTCHARSET
);
177 while ((xpos
< col
+ grid_x
- 1) && (xpos
< x_limit
)) {
178 mvaddch(0, xpos
, 113 | A_ALTCHARSET
);
179 mvaddch(y_limit
, xpos
, 113 | A_ALTCHARSET
);
184 mvaddch(0, 0, 108 | A_ALTCHARSET
);
185 mvaddch(0, x_limit
, 107 | A_ALTCHARSET
);
186 mvaddch(y_limit
, 0, 109 | A_ALTCHARSET
);
187 mvaddch(y_limit
, x_limit
, 106 | A_ALTCHARSET
);
189 /* Draw a white circle. */
190 for (i
= 1; i
< y_limit
; i
++) {
191 for (col
= circle_pos
[i
][0]; col
<= circle_pos
[i
][1]; col
++) {
192 mvaddch(i
, col
, 32 | A_REVERSE
);
196 /* Add title segment. */
197 for (i
= roundf(1 * grid_unit
); i
< roundf(2 * grid_unit
); i
++) {
199 attrset(COLOR_PAIR(COLOR_BLACK
));
201 for (col
= roundf((4 * grid_unit
* 2) +
202 circle_pos
[y_limit
/2][0]); col
<= roundf((9 * grid_unit
203 * 2) + circle_pos
[y_limit
/2][0]); col
++)
204 mvaddch(i
, col
, ' ');
207 i
= roundf(1.4 * grid_unit
);
209 col
= y_limit
- (strlen(title
) / 2) + circle_pos
[y_limit
/ 2][0];
210 mvprintw(i
, col
, "%s", title
);
212 /* Add black segments at top. */
213 for (line
= roundf(2 * grid_unit
); line
< 4 * grid_unit
; line
++) {
215 attrset(COLOR_PAIR(COLOR_BLACK
));
217 for (col
= 0; col
<= roundf((3.5 * grid_unit
* 2)); col
++) {
218 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
219 if (xpos
>= circle_pos
[line
][0] &&
220 xpos
<= circle_pos
[line
][1])
221 mvaddch(line
, xpos
, ' ');
224 for (col
= roundf((9.5 * grid_unit
* 2)); col
<
225 roundf((13 * grid_unit
* 2)); col
++) {
226 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
227 if (xpos
>= circle_pos
[line
][0] &&
228 xpos
<= circle_pos
[line
][1])
229 mvaddch(line
, xpos
, ' ');
233 /* Add black and white squares close to top. */
234 int gap
= (circle_pos
[(int)(5 * grid_unit
)][1] -
235 circle_pos
[(int)(5 * grid_unit
)][0]) / 13;
237 for (i
= roundf(3 * grid_unit
); i
< roundf(4 * grid_unit
); i
++) {
238 for (xpos
= 0; xpos
<= x_limit
; xpos
+= 2 * gap
) {
240 attrset(COLOR_PAIR(COLOR_BLACK
));
242 for (col
= xpos
; col
< xpos
+ gap
; col
++) {
243 if (col
>= circle_pos
[i
][0] &&
244 col
<= circle_pos
[i
][1])
245 mvaddch(i
, col
, ' ');
249 attrset(COLOR_PAIR(COLOR_WHITE
));
251 for (col
= xpos
+ gap
; col
< xpos
+ (2 * gap
);
253 if (col
>= circle_pos
[i
][0] &&
254 col
<= circle_pos
[i
][1])
255 mvaddch(i
, col
, ' ');
260 /* Add colour bars. */
261 for (i
= 0; i
< numcolours
; i
++) {
262 colour
= colour_list
[i
];
264 attrset(COLOR_PAIR(colour
));
269 spacing_start
= (spacing
* i
) + spacing_residual
;
271 if (i
== numcolours
- 1)
272 spacing_end
= circle_pos
[y_limit
/ 2][1];
274 spacing_end
= (spacing
* (i
+ 1)) + spacing_residual
;
276 for (line
= roundf(4 * grid_unit
); line
< (y_limit
/ 2);
278 for (col
= spacing_start
; col
< spacing_end
; col
++) {
279 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
280 if (xpos
>= circle_pos
[line
][0] &&
281 xpos
<= circle_pos
[line
][1])
282 mvprintw(line
, xpos
, " ");
287 /* Add black segment under centre line. */
288 for (line
= y_limit
/ 2; line
< (9.5 * grid_unit
); line
++) {
290 attrset(COLOR_PAIR(COLOR_BLACK
));
292 for (col
= circle_pos
[line
][0]; col
<= circle_pos
[line
][1];
294 mvaddch(line
, col
, ' ');
296 for (col
= roundf((1.5 * grid_unit
* 2)); col
<
297 roundf((4.3 * grid_unit
* 2)); col
++) {
298 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
299 if (xpos
>= circle_pos
[line
][0] &&
300 xpos
< circle_pos
[line
][1])
301 mvaddch(line
, xpos
, 120 | A_ALTCHARSET
);
304 for (col
= roundf((4.3 * grid_unit
* 2)); col
<
305 roundf((7.6 * grid_unit
* 2)); col
++) {
306 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
307 if (xpos
>= circle_pos
[line
][0] &&
308 xpos
< circle_pos
[line
][1])
309 mvaddch(line
, xpos
, '|');
312 for (col
= roundf((7.6 * grid_unit
* 2)); col
<
313 roundf((11.5 * grid_unit
* 2)); col
++) {
314 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
315 if (xpos
>= circle_pos
[line
][0] &&
316 xpos
< circle_pos
[line
][1])
317 mvaddch(line
, xpos
, 97 | A_ALTCHARSET
);
321 /* Add black segment close to bottom. */
322 for (line
= roundf(9.5 * grid_unit
); line
<= (10.5 * grid_unit
);
325 attrset(COLOR_PAIR(COLOR_BLACK
));
327 for (col
= roundf((0 * grid_unit
* 2)); col
<
328 roundf((4 * grid_unit
* 2)); col
++) {
329 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
330 if (xpos
>= circle_pos
[line
][0] &&
331 xpos
< circle_pos
[line
][1])
332 mvaddch(line
, xpos
, ' ');
335 for (col
= roundf((4 * grid_unit
* 2)); col
<
336 roundf((6.5 * grid_unit
* 2)); col
++) {
337 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
338 if (xpos
>= circle_pos
[line
][0] &&
339 xpos
< circle_pos
[line
][1])
340 mvaddch(line
, xpos
, 97 | A_ALTCHARSET
);
344 attrset(COLOR_PAIR(COLOR_WHITE
));
346 for (col
= roundf((6.5 * grid_unit
* 2)); col
<
347 roundf((9 * grid_unit
* 2)); col
++) {
348 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
349 if (xpos
>= circle_pos
[line
][0] &&
350 xpos
< circle_pos
[line
][1])
351 mvaddch(line
, xpos
, 97 | A_ALTCHARSET
);
354 for (col
= roundf((9 * grid_unit
* 2)); col
<
355 roundf((13 * grid_unit
* 2)); col
++) {
356 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
357 if (xpos
>= circle_pos
[line
][0] &&
358 xpos
< circle_pos
[line
][1])
359 mvaddch(line
, xpos
, ' ');
363 /* Add name segment close to bottom. */
364 for (line
= roundf(10.5 * grid_unit
); line
< (12 * grid_unit
);
367 attrset(COLOR_PAIR(COLOR_BLACK
));
369 for (col
= roundf(3.5 * grid_unit
* 2); col
<= roundf(9.5 *
370 grid_unit
* 2); col
++) {
371 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
372 if (xpos
>= circle_pos
[line
][0] &&
373 xpos
< circle_pos
[line
][1])
374 mvaddch(line
, xpos
, ' ');
378 attrset(COLOR_PAIR(COLOR_WHITE
));
380 for (col
= roundf(0 * grid_unit
* 2); col
<= roundf(3.5 *
381 grid_unit
* 2); col
++) {
382 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
383 if (xpos
>= circle_pos
[line
][0] &&
384 xpos
< circle_pos
[line
][1])
385 mvaddch(line
, xpos
, ' ');
388 for (col
= roundf(9.5 * grid_unit
* 2); col
<= roundf(13 *
389 grid_unit
* 2); col
++) {
390 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
391 if (xpos
>= circle_pos
[line
][0] &&
392 xpos
< circle_pos
[line
][1])
393 mvaddch(line
, xpos
, ' ');
397 /* Add yellow segment at bottom. */
398 for (line
= 12 * grid_unit
; line
< y_limit
; line
++) {
400 attrset(COLOR_PAIR(COLOR_YELLOW
));
402 for (col
= circle_pos
[line
][0]; col
<= circle_pos
[line
][1];
404 mvaddch(line
, col
, ' ');
407 attrset(COLOR_PAIR(COLOR_RED
));
409 for (col
= roundf((6 * grid_unit
* 2)); col
<
410 roundf((7 * grid_unit
* 2)); col
++) {
411 xpos
= col
+ circle_pos
[y_limit
/ 2][0];
412 if (xpos
>= circle_pos
[line
][0] &&
413 xpos
< circle_pos
[line
][1])
414 mvaddch(line
, xpos
, ' ');
419 attrset(COLOR_PAIR(COLOR_BLACK
));
421 for (line
= 6 * grid_unit
; line
<= (7 * grid_unit
) + 1; line
++) {
423 attrset(COLOR_PAIR(COLOR_BLACK
));
426 if (line
!= a_axis
) {
427 mvaddch(line
, col
- 1, ' ');
428 mvaddch(line
, col
, 120 | A_ALTCHARSET
);
429 mvaddch(line
, col
+ 1, ' ');
434 for (col
= 1; col
< x_limit
; col
= col
+ grid_x
) {
436 while (xpos
< col
+ grid_x
- 1) {
437 if (xpos
>= circle_pos
[line
][0] && xpos
< circle_pos
[line
][1])
438 mvaddch(line
, xpos
, 113 | A_ALTCHARSET
);
441 if (xpos
>= circle_pos
[line
][0] && xpos
< circle_pos
[line
][1])
442 mvaddch(line
, xpos
, 110 | A_ALTCHARSET
);
447 mvaddch(line
, col
, 110 | A_ALTCHARSET
);
448 mvaddch(line
, col
- 1, 113 | A_ALTCHARSET
);
449 mvaddch(line
, col
+ 1, 113 | A_ALTCHARSET
);