Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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)
5
 ******************************************************************************/
6
 
7
#include <cstdio>
8
#include <cmath>
9
#include <unistd.h>
10
using namespace std;
11
 
12
#include "draw.h"
13
 
14
void wait_for_newline ()
15
{
16
    char c;
17
 
18
    while ((c = getchar()) != '\n')
19
        usleep (10000);
20
}
21
 
22
int main ()
23
{
24
    int i;
25
    int sinpos;
26
    const int strsiz = 100;
27
    char *tempstr;
28
 
29
    /* Initialize the draw library */
30
    draw_init ();
31
    tempstr = (char*) malloc (strsiz * sizeof(char));
32
 
33
    /* Testcase #1, a few boxes, a line, and a pixel */
34
    draw_clearscreen ();
35
    draw_putpixel (20, 20, RED);
36
    draw_box (50, 50, 100, 100, GREEN);
37
    draw_line (30, 20, 300, 180, BLUE);
38
    draw_box (100, 80, 100, 100, WHITE);
39
 
40
    wait_for_newline ();
41
 
42
    /* Testcase #2, all of the possible colors */
43
    draw_clearscreen ();
44
 
45
    for (i=0; i<16; i++)
46
    {
47
        draw_box (10, (i+1)*22, 20, 20, i);
48
 
49
        snprintf (tempstr, strsiz, "color: #%d", i);
50
        draw_puts (36, (i+1)*22+8, tempstr, WHITE);
51
    }
52
 
53
    wait_for_newline ();
54
 
55
    /* Testcase #3, draw the sin() function, labeling it as we go */
56
    draw_clearscreen ();
57
 
58
    for (i=0; i<800; i++)
59
    {
60
        /* sin itself */
61
        sinpos = (int)(400+(50.0 * sin (i / 50.0)));
62
        draw_putpixel (i, sinpos, GREEN);
63
 
64
        /* labels */
65
        if (i % 100 == 0)
66
        {
67
            snprintf (tempstr, strsiz, "%d sec", i / 100);
68
            draw_puts (i, sinpos + 10, tempstr, WHITE);
69
            draw_line (i, sinpos, i, sinpos+8, WHITE);
70
        }
71
 
72
        /* slow it down */
73
        usleep (10000);
74
    }
75
 
76
    wait_for_newline ();
77
 
78
    /* Testcase #4, text output */
79
    draw_clearscreen ();
80
 
81
    draw_puts (0, 80, "Hello world!", WHITE);
82
    draw_puts (20, 100, "Some more test text", RED);
83
 
84
    wait_for_newline ();
85
 
86
    /* Cleanup */
87
    draw_close ();
88
    free (tempstr);
89
 
90
    return (0);
91
}
92