Subversion Repositories programming

Rev

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

#include <stdio.h>
#include <ggi/ggi.h>


#define BLACK 0
#define RED   1
#define GREEN 2
#define BLUE  3
#define WHITE 4


static ggi_visual_t vis;
static ggi_color palette[5];
static ggi_mode tm; //FIXME

/* Initialize the palette's colors */
void init_palette ()
{
    palette[0].r = 0;
    palette[0].g = 0;
    palette[0].b = 0;

    palette[1].r = 0xffff;
    palette[1].g = 0;
    palette[1].b = 0;

    palette[2].r = 0;
    palette[2].g = 0xffff;
    palette[2].b = 0;

    palette[3].r = 0;
    palette[3].g = 0;
    palette[3].b = 0xffff;

    palette[4].r = 0xffff;
    palette[4].g = 0xffff;
    palette[4].b = 0xffff;
}

/* Initialize GGI for drawing */
void draw_init ()
{

    init_palette ();

    if (ggiInit () != 0)
    {
        printf ("Error initialising GGI!\n");
        exit (1);
    }

    if ((vis = ggiOpen (NULL)) == NULL)
    {
        printf ("Error opening visual!\n");
        ggiExit ();
        exit (1);
    }

    if (ggiSetSimpleMode (vis, GGI_AUTO, GGI_AUTO, 0, GT_AUTO))
    {
        printf ("Set the GGI_DISPLAY variable to the palemu!\n");
        exit (1);
    }

    ggiSetPalette (vis, 0, 5, palette);
}

void draw_flush ()
{
    ggiFlush (vis);
}

/* Close GGI when we're done drawing */
void draw_close ()
{
    ggiClose (vis);
    ggiExit ();
}

void draw_clrscreen ()
{
    ggiSetGCForeground (vis, BLACK);
    ggiFillscreen (vis);
    draw_flush ();
}

void draw_putpixel (int x, int y, int color)
{
    ggiSetGCForeground (vis, color);
    ggiDrawPixel (vis, x, y);
    draw_flush ();
}

void draw_box (int x, int y, int width, int height, int color)
{
    ggiSetGCForeground (vis, color);
    ggiDrawBox (vis, x, y, width, height);
    draw_flush ();
}

void draw_line (int x1, int y1, int x2, int y2, int color)
{
    ggiSetGCForeground (vis, color);
    ggiDrawLine (vis, x1, y1, x2, y2);
    draw_flush ();
}

void draw_printmode ()
{
    fprintf (stderr, "virt.x: %d\n", tm.virt.x);
    fprintf (stderr, "virt.y: %d\n", tm.virt.y);

    fprintf (stderr, "size.x: %d\n", tm.size.x);
    fprintf (stderr, "size.y: %d\n", tm.size.y);

    fprintf (stderr, "dpp:  %d\n", tm.dpp);
}

int main ()
{
    int i;

    /* Initialize */
    draw_init ();

    /* Use */
    draw_clrscreen ();
    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);

    /* Sleep until the keyboard is pressed */
    ggiGetc (vis);

    draw_clrscreen ();
    draw_putpixel (40, 40, RED);
    draw_putpixel (20, 20, GREEN);
    draw_putpixel (30, 30, BLUE);
    
    ggiGetc (vis);

    /* Slow draw */
    draw_clrscreen ();

    for (i=0; i<800; i++)
    {
        draw_putpixel (i, i, RED);
        usleep (10000);
    }

    ggiGetMode (vis, &tm);

    /* Quit */
    draw_close ();

    draw_printmode ();

    return (0);
}