]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_curses.c
477c4a14ef116b60854324901ce83b86c725d7be
[ncurses.git] / test / dots_curses.c
1 /****************************************************************************
2  * Copyright (c) 2014,2017 Free Software Foundation, Inc.                   *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /*
30  * Author: Thomas E. Dickey
31  *
32  * $Id: dots_curses.c,v 1.7 2017/10/11 08:16:33 tom Exp $
33  *
34  * A simple demo of the curses interface used for comparison with termcap.
35  */
36 #include <test.priv.h>
37
38 #if !defined(__MINGW32__)
39 #include <sys/time.h>
40 #endif
41
42 #include <time.h>
43
44 static bool interrupted = FALSE;
45 static long total_chars = 0;
46 static time_t started;
47
48 static void
49 cleanup(void)
50 {
51     endwin();
52
53     printf("\n\n%ld total cells, rate %.2f/sec\n",
54            total_chars,
55            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
56 }
57
58 static void
59 onsig(int n GCC_UNUSED)
60 {
61     interrupted = TRUE;
62 }
63
64 static double
65 ranf(void)
66 {
67     long r = (rand() & 077777);
68     return ((double) r / 32768.);
69 }
70
71 static int
72 mypair(int fg, int bg)
73 {
74     int pair = (fg * COLORS) + bg;
75     return (pair >= COLOR_PAIRS) ? -1 : pair;
76 }
77
78 static void
79 set_colors(int fg, int bg)
80 {
81     int pair = mypair(fg, bg);
82     if (pair > 0) {
83         attron(COLOR_PAIR(mypair(fg, bg)));
84     }
85 }
86
87 int
88 main(int argc GCC_UNUSED,
89      char *argv[]GCC_UNUSED)
90 {
91     int x, y, z, p;
92     int fg, bg;
93     double r;
94     double c;
95
96     srand((unsigned) time(0));
97
98     InitAndCatch(initscr(), onsig);
99     if (has_colors()) {
100         start_color();
101         for (fg = 0; fg < COLORS; fg++) {
102             for (bg = 0; bg < COLORS; bg++) {
103                 int pair = mypair(fg, bg);
104                 if (pair > 0)
105                     init_pair((short) pair, (short) fg, (short) bg);
106             }
107         }
108     }
109
110     r = (double) (LINES - 4);
111     c = (double) (COLS - 4);
112     started = time((time_t *) 0);
113
114     fg = COLOR_WHITE;
115     bg = COLOR_BLACK;
116     while (!interrupted) {
117         x = (int) (c * ranf()) + 2;
118         y = (int) (r * ranf()) + 2;
119         p = (ranf() > 0.9) ? '*' : ' ';
120
121         move(y, x);
122         if (has_colors()) {
123             z = (int) (ranf() * COLORS);
124             if (ranf() > 0.01) {
125                 set_colors(fg = z, bg);
126                 attron(COLOR_PAIR(mypair(fg, bg)));
127             } else {
128                 set_colors(fg, bg = z);
129                 napms(1);
130             }
131         } else {
132             if (ranf() <= 0.01) {
133                 if (ranf() > 0.6) {
134                     attron(A_REVERSE);
135                 } else {
136                     attroff(A_REVERSE);
137                 }
138                 napms(1);
139             }
140         }
141         AddCh(p);
142         refresh();
143         ++total_chars;
144     }
145     cleanup();
146     ExitProgram(EXIT_SUCCESS);
147 }