Subversion Repositories programming

Rev

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

/*******************************************************************************
 * draw.c - implementation for libggi-based drawing on the GNU/Linux
 *          framebuffer device.
 *
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 ******************************************************************************/

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

#ifdef __cplusplus
extern "C"
{
#endif

/* Initialize GGI for drawing */
void draw_init ()
{
    /* Initialize the palette */
    palette[AQUA].r = 0x0000;
    palette[AQUA].g = 0xffff;
    palette[AQUA].b = 0xffff;

    palette[BLACK].r = 0x0000;
    palette[BLACK].g = 0x0000;
    palette[BLACK].b = 0x0000;

    palette[BLUE].r = 0x0000;
    palette[BLUE].g = 0x0000;
    palette[BLUE].b = 0xffff;

    palette[FUCHSIA].r = 0xffff;
    palette[FUCHSIA].g = 0x0000;
    palette[FUCHSIA].b = 0xffff;

    palette[GRAY].r = 0x8080;
    palette[GRAY].g = 0x8080;
    palette[GRAY].b = 0x8080;

    palette[GREEN].r = 0x0000;
    palette[GREEN].g = 0x8080;
    palette[GREEN].b = 0x0000;

    palette[LIME].r = 0x0000;
    palette[LIME].g = 0xffff;
    palette[LIME].b = 0x0000;

    palette[MAROON].r = 0x8080;
    palette[MAROON].g = 0x0000;
    palette[MAROON].b = 0x0000;

    palette[NAVY].r = 0x0000;
    palette[NAVY].g = 0x0000;
    palette[NAVY].b = 0x8080;

    palette[OLIVE].r = 0x8080;
    palette[OLIVE].g = 0x8080;
    palette[OLIVE].b = 0x0000;

    palette[PURPLE].r = 0x8080;
    palette[PURPLE].g = 0x0000;
    palette[PURPLE].b = 0x8080;

    palette[RED].r = 0xffff;
    palette[RED].g = 0x0000;
    palette[RED].b = 0x0000;

    palette[SILVER].r = 0xc0c0;
    palette[SILVER].g = 0xc0c0;
    palette[SILVER].b = 0xc0c0;

    palette[TEAL].r = 0x0000;
    palette[TEAL].g = 0x8080;
    palette[TEAL].b = 0x8080;

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

    palette[YELLOW].r = 0xffff;
    palette[YELLOW].g = 0xffff;
    palette[YELLOW].b = 0x0000;

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

    /* Try to open the screen */
    /* if ((vis = ggiOpen (NULL)) == NULL) */
    if ((vis = ggiOpen ("display-fbdev:-noinput", NULL)) == NULL)
    {
        printf ("Error opening visual!\n");
        ggiExit ();
        exit (1);
    }

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

    /* load the palette to the screen */
    ggiSetPalette (vis, 0, NUM_COLORS, palette);

    /* Set default foreground and background */
    ggiSetGCForeground (vis, BLACK);
    ggiSetGCBackground (vis, BLACK);
}

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

void draw_clearscreen ()
{
    ggiSetGCForeground (vis, BLACK);
    ggiFillscreen (vis);
    ggiFlush (vis);
}

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

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

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

void draw_putc (int x, int y, char c, int color)
{
    ggiSetGCForeground (vis, color);
    ggiPutc (vis, x, y, c);
    ggiFlush (vis);
}

void draw_puts (int x, int y, const char *str, int color)
{
    ggiSetGCForeground (vis, color);
    ggiPuts (vis, x, y, str);
    ggiFlush (vis);
}

#ifdef __cplusplus
}
#endif