Subversion Repositories programming

Rev

Rev 312 | Details | Compare with Previous | Last modification | View Log | RSS feed

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