/*
            __
           / /_______________  ____  ___
          / __/ ___/ ___/ __ \/ __ \/ _ \
         / /_(__  ) /__/ /_/ / /_/ /  __/
         \__/____/\___/\____/ .___/\___/
                           /_/
    
    graphics parameters

    By Michael Stevens

    See license.html for copyright information
*/

#include "../include/tscope.h"
#include "../include/tscope/internal.h"

#define MAXPATH     500

// set the background color
int ts_bgcolor(int color)
{
    if (!_graphics_flag)
        _graphics_init();
    int oldval;
    oldval = _bgcolor;
    _bgcolor = color;
    if (_dbuff)
        ts_clrbuff();
    ts_clrscr();
    return oldval;
}

// set the foreground color
int ts_fgcolor(int color)
{
    if (!_graphics_flag)
        _graphics_init();
    int oldval = _fgcolor;
    _fgcolor = color;
    return oldval;
}

// set text background
int ts_textmode(int color)
{
    if (!_graphics_flag)
        _graphics_init();
    int oldval = _textbgcolor;
    _textbgcolor = color;
#if (ALLEGRO_SUB_VERSION==0)
    text_mode(color);
#endif
    return oldval;
}

// make a color based on rgb values
int ts_makecolor(int r, int g, int b)
{
    if (!_graphics_flag)
        _graphics_init();
    if (r < 0 || g < 0 || b < 0 || r > 255 || g > 255 || b > 255)
        ts_fatal("ts_makecolor: color values must be between 0 and 255.");
    return makecol(r, g, b);
}

int ts_makehsvcolor(float h, float s, float v)
{
    if (!_graphics_flag)
        _graphics_init();
    if (h < 0 || h > 360 || s < 0 || s > 1 || v < 0 || v > 1)
        ts_fatal
            ("ts_makergbcolor: hue must be between 0 and 360, saturation and value between 0 and 1.");

    int r, g, b;
    hsv_to_rgb(h, s, v, &r, &g, &b);
    return makecol(r, g, b);
}


// set fillmode
int ts_fill(int mode)
{
    if (!_graphics_flag)
        _graphics_init();
    int oldval = _fill;
    if (mode)
        _fill = ON;
    else
        _fill = OFF;
    return oldval;
}

// set a font based on type, size & style
int ts_font(int type, int size, int style)
{
    if (!_graphics_flag)
        _graphics_init();

    // determine font number
    int fontno = 0;

    switch (type) {
    case ARIAL:
        fontno += 0;
        break;
    case COURIER:
        fontno += 64;
        break;
    case TIMES:
        fontno += 128;
        break;
    default:
        ts_fatal("ts_font: unknown font type requested.");
    }

    switch (style) {
    case REG:
        fontno += 0;
        break;
    case BOLD:
        fontno += 16;
        break;
    case IT:
        fontno += 32;
        break;
    case BOLDIT:
        fontno += 32;
        break;
    default:
        ts_fatal("ts_font: unknown font style requested.");
    }

    switch (size) {
    case 8:
        fontno += 0;
        break;
    case 9:
        fontno += 1;
        break;
    case 10:
        fontno += 2;
        break;
    case 11:
        fontno += 3;
        break;
    case 12:
        fontno += 4;
        break;
    case 14:
        fontno += 5;
        break;
    case 16:
        fontno += 6;
        break;
    case 18:
        fontno += 7;
        break;
    case 20:
        fontno += 8;
        break;
    case 22:
        fontno += 9;
        break;
    case 24:
        fontno += 10;
        break;
    case 26:
        fontno += 11;
        break;
    case 28:
        fontno += 12;
        break;
    case 36:
        fontno += 13;
        break;
    case 48:
        fontno += 14;
        break;
    case 72:
        fontno += 15;
        break;
    default:
        ts_fatal("ts_font: unknown font size requested.");
    }

    // open font file if necessary
    if (_fontfile[fontno] == NULL) {
        if (_debug_flag > DEBUG3)
            fprintf(stderr, "font %d is not loaded yet\n", fontno);

        // determine filename
        char typeS[20];
        if (type == ARIAL)
            sprintf(typeS, "arial");
        else if (type == COURIER)
            sprintf(typeS, "courier");
        else if (type == TIMES)
            sprintf(typeS, "times");
        else
            ts_fatal("ts_font: unknown font requested");

        char styleS[20];
        if (style == BOLD)
            sprintf(styleS, "b");
        else if (style == IT)
            sprintf(styleS, "i");
        else if (style == BOLDIT)
            sprintf(styleS, "bi");
        else if (style == REG)
            styleS[0] = '\0';
        else
            ts_fatal("ts_font: unknown font style requested.");

        char font_path1[MAXPATH];
        char font_path2[MAXPATH];
        char font_path3[MAXPATH];
        sprintf(font_path1, "%s%s%02d.dat", typeS, styleS, size);
        sprintf(font_path2, "fonts/%s", font_path1);
        sprintf(font_path3, "/usr/local/%s", font_path2);

        if (_debug_flag > DEBUG3)
            fprintf(stderr, "loading font %s\n", font_path1);

#ifdef ALLEGRO_WINDOWS
        char tmp_path[MAXPATH];
        cygwin_conv_to_full_win32_path(font_path1, tmp_path);
        strcpy(font_path1, tmp_path);
        cygwin_conv_to_full_win32_path(font_path2, tmp_path);
        strcpy(font_path2, tmp_path);
        cygwin_conv_to_full_win32_path(font_path3, tmp_path);
        strcpy(font_path3, tmp_path);
#endif
        _fontfile[fontno] = load_datafile(font_path1);
        if (!_fontfile[fontno])
            _fontfile[fontno] = load_datafile(font_path2);
        if (!_fontfile[fontno])
            _fontfile[fontno] = load_datafile(font_path3);
        if (!_fontfile[fontno])
            ts_fatal("ts_font: error opening font file.");

    }

    font = (FONT *) _fontfile[fontno][0].dat;
    int oldfont = _font;
    _font = fontno;
    return oldfont;
}

// set a font based on font number
int ts_setfont(int fontno)
{
    if (!_graphics_flag)
        _graphics_init();

    if (fontno) {
        if (_fontfile[fontno] == NULL)
            ts_fatal("ts_setfont: font not available.");
        font = (FONT *) _fontfile[fontno][0].dat;
    } else
        font = _basefont;

    int oldfont = _font;
    _font = fontno;
    return oldfont;
}

// load a custom font
int ts_loadfont(char *fontfile)
{
    if (!_graphics_flag)
        _graphics_init();

    int fontno = 192;
    while (_fontfile[fontno] != NULL) {
        fontno++;
        if (fontno > 199)
            ts_fatal("ts_loadfont: too many fonts loaded");
    }

    char font_path1[MAXPATH];
    char font_path2[MAXPATH];
    char font_path3[MAXPATH];

    sprintf(font_path1, "%s", fontfile);
    sprintf(font_path2, "fonts/%s", font_path1);
    sprintf(font_path3, "/usr/local/%s", font_path2);
    if (_debug_flag > DEBUG3)
        fprintf(stderr, "loading font %s\n", font_path1);

#ifdef ALLEGRO_WINDOWS
    char tmp_path[MAXPATH];
    cygwin_conv_to_full_win32_path(font_path1, tmp_path);
    strcpy(font_path1, tmp_path);
    cygwin_conv_to_full_win32_path(font_path2, tmp_path);
    strcpy(font_path2, tmp_path);
    cygwin_conv_to_full_win32_path(font_path3, tmp_path);
    strcpy(font_path3, tmp_path);
#endif
    _fontfile[fontno] = load_datafile(font_path1);
    if (!_fontfile[fontno])
        _fontfile[fontno] = load_datafile(font_path2);
    if (!_fontfile[fontno])
        _fontfile[fontno] = load_datafile(font_path3);
    if (!_fontfile[fontno])
        ts_fatal("ts_font: error opening font file.");

    font = (FONT *) _fontfile[fontno][0].dat;
    int oldfont = _font;
    _font = fontno;
    return oldfont;
}


top
Persoonlijke pagina Universiteit GentTscope
Allegro | Cygwin | Gcc
© See license.html for copyright information