Subversion Repositories programming

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * draw_test.c - a simple test of the draw library
 *
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
 ******************************************************************************/

#include <cstdio>
#include <cmath>
#include <unistd.h>
using namespace std;

#include "draw.h"

void wait_for_newline ()
{
    char c;

    while ((c = getchar()) != '\n')
        usleep (10000);
}

int main ()
{
    int i;
    int sinpos;
    const int strsiz = 100;
    char *tempstr;

    /* Initialize the draw library */
    draw_init ();
    tempstr = (char*) malloc (strsiz * sizeof(char));

    /* Testcase #1, a few boxes, a line, and a pixel */
    draw_clearscreen ();
    draw_putpixel (20, 20, RED);
    draw_box (50, 50, 100, 100, GREEN);
    draw_line (30, 20, 300, 180, BLUE);
    draw_box (100, 80, 100, 100, WHITE);

    wait_for_newline ();

    /* Testcase #2, all of the possible colors */
    draw_clearscreen ();

    for (i=0; i<16; i++)
    {
        draw_box (10, (i+1)*22, 20, 20, i);

        snprintf (tempstr, strsiz, "color: #%d", i);
        draw_puts (36, (i+1)*22+8, tempstr, WHITE);
    }

    wait_for_newline ();

    /* Testcase #3, draw the sin() function, labeling it as we go */
    draw_clearscreen ();

    for (i=0; i<800; i++)
    {
        /* sin itself */
        sinpos = (int)(400+(50.0 * sin (i / 50.0)));
        draw_putpixel (i, sinpos, GREEN);

        /* labels */
        if (i % 100 == 0)
        {
            snprintf (tempstr, strsiz, "%d sec", i / 100);
            draw_puts (i, sinpos + 10, tempstr, WHITE);
            draw_line (i, sinpos, i, sinpos+8, WHITE);
        }

        /* slow it down */
        usleep (10000);
    }

    wait_for_newline ();

    /* Testcase #4, text output */
    draw_clearscreen ();

    draw_puts (0, 80, "Hello world!", WHITE);
    draw_puts (20, 100, "Some more test text", RED);

    wait_for_newline ();

    /* Cleanup */
    draw_close ();
    free (tempstr);

    return (0);
}