/*
            __
           / /_______________  ____  ___
          / __/ ___/ ___/ __ \/ __ \/ _ \
         / /_(__  ) /__/ /_/ / /_/ /  __/
         \__/____/\___/\____/ .___/\___/
                           /_/
    
    formatted text input/output

    By Michael Stevens

    See license.html for copyright information
*/

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

// print some left-aligned text
int ts_printf(int x, int y, const char *format, ...)
{
    if (!_graphics_flag)
        _graphics_init();

    char buf[512];
    va_list ap;
    va_start(ap, format);
    uvszprintf(buf, sizeof(buf), format, ap);
    va_end(ap);

    if (_coordinates == CARTESIAN) {
        y = sy(y + ts_textheight() / 2);
        x = sx(x);
    }
#if (ALLEGRO_SUB_VERSION==0)
    textout(_destmap->b, font, buf, x, y, _fgcolor);
#else
    textout_ex(_destmap->b, font, buf, x, y, _fgcolor, _textbgcolor);
#endif
    return ts_textlength(buf);
}

// print some centred text 
int ts_printf_centre(int x, int y, const char *format, ...)
{
    if (!_graphics_flag)
        _graphics_init();

    char buf[512];
    va_list ap;
    va_start(ap, format);
    uvszprintf(buf, sizeof(buf), format, ap);
    va_end(ap);

    if (_coordinates == CARTESIAN) {
        y = sy(y + ts_textheight() / 2);
        x = sx(x);
    }
#if (ALLEGRO_SUB_VERSION==0)
    textout_centre(_destmap->b, font, buf, x, y, _fgcolor);
#else
    textout_centre_ex(_destmap->b, font, buf, x, y, _fgcolor,
                      _textbgcolor);
#endif
    return ts_textlength(buf);
}

// print some right-aligned text
int ts_printf_right(int x, int y, const char *format, ...)
{
    if (!_graphics_flag)
        _graphics_init();

    char buf[512];
    va_list ap;
    va_start(ap, format);
    uvszprintf(buf, sizeof(buf), format, ap);
    va_end(ap);

    if (_coordinates == CARTESIAN) {
        y = sy(y + ts_textheight() / 2);
        x = sx(x);
    }
#if (ALLEGRO_SUB_VERSION==0)
    textout_right(_destmap->b, font, buf, x, y, _fgcolor);
#else
    textout_right_ex(_destmap->b, font, buf, x, y, _fgcolor, _textbgcolor);
#endif

    return ts_textlength(buf);
}

// print some justified text
int ts_printf_justify(int x1, int x2, int y, const char *format, ...)
{
    if (!_graphics_flag)
        _graphics_init();

    char buf[512];
    va_list ap;
    va_start(ap, format);
    uvszprintf(buf, sizeof(buf), format, ap);
    va_end(ap);

    if (_coordinates == CARTESIAN) {
        y = sy(y + ts_textheight() / 2);
        x1 = sx(x1);
        x2 = sx(x2);
    }
#if (ALLEGRO_SUB_VERSION==0)
    textout_justify(_destmap->b, font, buf, x1, x2, y, (x2 - x1) / 4,
                    _fgcolor);
#else
    textout_justify_ex(_destmap->b, font, buf, x1, x2, y, (x2 - x1) / 4,
                       _fgcolor, _textbgcolor);
#endif

    int length = x2 - x1;
    if (length < 0)
        length = (-length);
    return length;
}

// compute height of font
int ts_textheight()
{
    if (!_graphics_flag)
        _graphics_init();
    return text_height(font);
}

// compute length of some text typed in the current font
int ts_textlength(char *txt)
{
    if (!_graphics_flag)
        _graphics_init();
    return text_length(font, txt);
}

// read formatted text from the keyboard
int ts_scanf(int x, int y, char *format, ...)
{
    if (!_graphics_flag)
        _graphics_init();
    if (!_keyboard_flag)
        _keyboard_init();

    char str[MAXREAD];

    int input, ascii, scancode;
    int done = 0, pos = 0;
    int oldcolor;

    clear_keybuf();
    do {
        input = readkey();
        scancode = input >> 8;
        ascii = input & 0xff;

        // overwrite old sting
        oldcolor = ts_fgcolor(_bgcolor);
        ts_printf(x, y, "%s", str);
        ts_fgcolor(oldcolor);

        if (scancode == KEY_SPACE) {
            str[pos] = ' ';
            pos++;
        } else if (scancode == KEY_BACKSPACE) {
            if (pos > 0)
                pos--;
        } else if (scancode == KEY_ENTER || scancode == KEY_ENTER_PAD)
            done = 1;
        else if (ascii >= 33 && ascii <= 255) {
            str[pos] = ascii;
            pos++;
        }
        if (pos > MAXREAD)
            pos = MAXREAD;
        str[pos] = '\0';

        // write new string
        ts_printf(x, y, "%s", str);
    } while (!done);

    int i;
    va_list args;
    va_start(args, format);
    i = vsscanf(str, format, args);
    va_end(args);
    return i;
}


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