312 |
ira |
1 |
/*******************************************************************************
|
|
|
2 |
* draw_test.c - a simple test of the draw library
|
|
|
3 |
*
|
|
|
4 |
* Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
|
317 |
ira |
5 |
*
|
|
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
7 |
* of this software and associated documentation files (the "Software"), to
|
|
|
8 |
* deal in the Software without restriction, including without limitation the
|
|
|
9 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
10 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
11 |
* furnished to do so, subject to the following conditions:
|
|
|
12 |
*
|
|
|
13 |
* The above copyright notice and this permission notice shall be included in
|
|
|
14 |
* all copies or substantial portions of the Software.
|
|
|
15 |
*
|
|
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
22 |
* IN THE SOFTWARE.
|
|
|
23 |
*
|
312 |
ira |
24 |
******************************************************************************/
|
|
|
25 |
|
|
|
26 |
#include <cstdio>
|
|
|
27 |
#include <cmath>
|
|
|
28 |
#include <unistd.h>
|
|
|
29 |
using namespace std;
|
|
|
30 |
|
|
|
31 |
#include "draw.h"
|
|
|
32 |
|
|
|
33 |
void wait_for_newline ()
|
|
|
34 |
{
|
|
|
35 |
char c;
|
|
|
36 |
|
|
|
37 |
while ((c = getchar()) != '\n')
|
|
|
38 |
usleep (10000);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
int main ()
|
|
|
42 |
{
|
|
|
43 |
int i;
|
|
|
44 |
int sinpos;
|
|
|
45 |
const int strsiz = 100;
|
|
|
46 |
char *tempstr;
|
|
|
47 |
|
|
|
48 |
/* Initialize the draw library */
|
|
|
49 |
draw_init ();
|
|
|
50 |
tempstr = (char*) malloc (strsiz * sizeof(char));
|
|
|
51 |
|
|
|
52 |
/* Testcase #1, a few boxes, a line, and a pixel */
|
|
|
53 |
draw_clearscreen ();
|
|
|
54 |
draw_putpixel (20, 20, RED);
|
|
|
55 |
draw_box (50, 50, 100, 100, GREEN);
|
|
|
56 |
draw_line (30, 20, 300, 180, BLUE);
|
|
|
57 |
draw_box (100, 80, 100, 100, WHITE);
|
|
|
58 |
|
|
|
59 |
wait_for_newline ();
|
|
|
60 |
|
|
|
61 |
/* Testcase #2, all of the possible colors */
|
|
|
62 |
draw_clearscreen ();
|
|
|
63 |
|
|
|
64 |
for (i=0; i<16; i++)
|
|
|
65 |
{
|
|
|
66 |
draw_box (10, (i+1)*22, 20, 20, i);
|
|
|
67 |
|
|
|
68 |
snprintf (tempstr, strsiz, "color: #%d", i);
|
|
|
69 |
draw_puts (36, (i+1)*22+8, tempstr, WHITE);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
wait_for_newline ();
|
|
|
73 |
|
|
|
74 |
/* Testcase #3, draw the sin() function, labeling it as we go */
|
|
|
75 |
draw_clearscreen ();
|
|
|
76 |
|
|
|
77 |
for (i=0; i<800; i++)
|
|
|
78 |
{
|
|
|
79 |
/* sin itself */
|
|
|
80 |
sinpos = (int)(400+(50.0 * sin (i / 50.0)));
|
|
|
81 |
draw_putpixel (i, sinpos, GREEN);
|
|
|
82 |
|
|
|
83 |
/* labels */
|
|
|
84 |
if (i % 100 == 0)
|
|
|
85 |
{
|
|
|
86 |
snprintf (tempstr, strsiz, "%d sec", i / 100);
|
|
|
87 |
draw_puts (i, sinpos + 10, tempstr, WHITE);
|
|
|
88 |
draw_line (i, sinpos, i, sinpos+8, WHITE);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/* slow it down */
|
|
|
92 |
usleep (10000);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
wait_for_newline ();
|
|
|
96 |
|
|
|
97 |
/* Testcase #4, text output */
|
|
|
98 |
draw_clearscreen ();
|
|
|
99 |
|
|
|
100 |
draw_puts (0, 80, "Hello world!", WHITE);
|
|
|
101 |
draw_puts (20, 100, "Some more test text", RED);
|
|
|
102 |
|
|
|
103 |
wait_for_newline ();
|
|
|
104 |
|
|
|
105 |
/* Cleanup */
|
|
|
106 |
draw_close ();
|
|
|
107 |
free (tempstr);
|
|
|
108 |
|
|
|
109 |
return (0);
|
|
|
110 |
}
|
|
|
111 |
|