/*
            __
           / /_______________  ____  ___
          / __/ ___/ ___/ __ \/ __ \/ _ \
         / /_(__  ) /__/ /_/ / /_/ /  __/
         \__/____/\___/\____/ .___/\___/
                           /_/
    
    graphics modes and basic screen operations

    By Michael Stevens

    See license.html for copyright information
*/

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

// -----------------------------------------------------------------------
// explicit init & exit functions

// open the screen
int ts_init()
{
    return _screen_init();
}


// close tscope with an error message
void ts_fatal(const char *format, ...)
{
    if (!_tscope_flag)
        _tscope_init();
    set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);

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

    fprintf(stderr, "\n\tFatal error - closing Tscope.\n\t%s\n\n", buf);
    allegro_message("Fatal error - closing Tscope.\n%s\n", buf);
    exit(-1);
}

// -----------------------------------------------------------------------
// routines to set the screen parameters 

// read screen parameters from a configuration file
void ts_scrcfg(char *file)
{
    // helper variables for reading the config file
    int count;
    char **data;
    char msg[100];

    _setcfg(file);

    // debug mode
    data = get_config_argv("screen", "ts_debug", &count);
    if (count == 1) {
        sprintf(msg, "ts_debug: %s\n", data[0]);
        int val = _readcfg(_ndebug, _sdebug, data[0]);
        if (val != -1)
            ts_debug(_vdebug[val]);
        else
            ts_fatal("ts_scrcfg: ts_debug entry not valid.");
    } else
        sprintf(msg, "ts_debug not set\n");
    if (_debug_flag > DEBUG0)
        fprintf(stderr, "%s", msg);

    // refresh rate
    data = get_config_argv("screen", "ts_refreshrate", &count);
    if (count == 1) {
        int tmp = atoi(data[0]);
        sprintf(msg, "ts_refreshrate: %d\n", tmp);
        ts_refreshrate(tmp);
    } else
        sprintf(msg, "ts_refreshrate not set\n");
    if (_debug_flag > DEBUG0)
        fprintf(stderr, "%s", msg);

    // color depth
    data = get_config_argv("screen", "ts_colordepth", &count);
    if (count == 1) {
        int tmp = atoi(data[0]);
        sprintf(msg, "ts_colordepth: %d\n", tmp);
        ts_colordepth(tmp);
    } else
        sprintf(msg, "ts_colordepth not set\n");
    if (_debug_flag > DEBUG0)
        fprintf(stderr, "%s", msg);

    // screen size
    data = get_config_argv("screen", "ts_scrsize", &count);
    if (count == 1) {
        sprintf(msg, "ts_scrsize: %s\n", data[0]);
        int val = _readcfg(_nscreensize, _sscreensize, data[0]);
        if (val != -1)
            ts_scrsize(_vscreensize[val]);
        else
            ts_fatal("ts_scrcfg: ts_scrsize entry not valid.");
    } else
        sprintf(msg, "ts_scrsize not set\n");
    if (_debug_flag > DEBUG0)
        fprintf(stderr, "%s", msg);

    // xy screen size
    data = get_config_argv("screen", "ts_scrxy", &count);
    if (count == 2) {
        int x = atoi(data[0]);
        int y = atoi(data[1]);
        sprintf(msg, "ts_scrxy: %d %d\n", x, y);
        ts_scrxy(x, y);
    } else
        sprintf(msg, "ts_scrxy not set\n");
    if (_debug_flag > DEBUG0)
        fprintf(stderr, "%s", msg);


    // screen mode
    data = get_config_argv("screen", "ts_scrmode", &count);
    if (count == 1) {
        sprintf(msg, "ts_scrmode: %s\n", data[0]);
        int val = _readcfg(_nscreenmode, _sscreenmode, data[0]);
        if (val != -1)
            ts_scrmode(_vscreenmode[val]);
        else
            ts_fatal("ts_scrcfg: ts_scrmode entry not valid.");
    } else
        sprintf(msg, "ts_scrmode not set\n");
    if (_debug_flag > DEBUG0)
        fprintf(stderr, "%s", msg);
}

// set the amount of debugging information
int ts_debug(int mode)
{
    int oldval;
    oldval = _debug_flag;

    if (mode >= DEBUG0 || mode <= DEBUG5)
        _debug_flag = mode;
    else
        ts_fatal("ts_debug: requested verbosity level not supported.");

    return oldval;
}

// request refreshrate
int ts_refreshrate(int rate)
{
    if (!_tscope_flag)
        _tscope_init();
    if (rate < 50 || rate > 150)
        ts_fatal("ts_refreshrate: requested refreshrate not sensible.");

    int oldval;
    oldval = _refreshrate;
    _refreshrate = rate;
    return oldval;
}

// request color depth
int ts_colordepth(int depth)
{
    if (!_tscope_flag)
        _tscope_init();

    if (depth != 8 && depth != 15 && depth != 16
        && depth != 24 && depth != 32) {
        ts_fatal("ts_colordepth: requested colordepth not supported.");
    }
    int oldval;
    oldval = _screendepth;
    _screendepth = depth;
    return oldval;
}

// request screen size
int ts_scrsize(int size)
{
    if (!_tscope_flag)
        _tscope_init();
    int oldval;
    oldval = _screensize;
    switch (size) {
    case QVGA:
        _screenx = QVGA_X;
        _screeny = QVGA_Y;
        break;
    case VGA:
        _screenx = VGA_X;
        _screeny = VGA_Y;
        break;
    case SVGA:
        _screenx = SVGA_X;
        _screeny = SVGA_Y;
        break;
    case XGA:
        _screenx = XGA_X;
        _screeny = XGA_Y;
        break;
    case WXGA:
        _screenx = WXGA_X;
        _screeny = WXGA_Y;
        break;
    case XGAplus:
        _screenx = XGAplus_X;
        _screeny = XGAplus_Y;
        break;
    case WXGAplus:
        _screenx = WXGAplus_X;
        _screeny = WXGAplus_Y;
        break;
    case SXGA:
        _screenx = SXGA_X;
        _screeny = SXGA_Y;
        break;
    case WSXGA:
        _screenx = WSXGA_X;
        _screeny = WSXGA_Y;
        break;
    case SXGAplus:
        _screenx = SXGAplus_X;
        _screeny = SXGAplus_Y;
        break;
    case WSXGAplus:
        _screenx = WSXGAplus_X;
        _screeny = WSXGAplus_Y;
        break;
    case UXGA:
        _screenx = UXGA_X;
        _screeny = UXGA_Y;
        break;
    case WUXGA:
        _screenx = WUXGA_X;
        _screeny = WUXGA_Y;
        break;
    default:
        ts_fatal("ts_scrsize: requested screen size not supported.");
    }
    _screensize = size;
    return oldval;
}

int ts_scrxy(int x, int y)
{
    if (!_tscope_flag)
        _tscope_init();

    int oldval;
    oldval = _screensize;
    _screensize = USERDEFINED;

    _screenx = x;
    _screeny = y;

    return oldval;
}

// request screen mode
int ts_scrmode(int mode)
{
    if (!_tscope_flag)
        _tscope_init();
    int oldval;
    oldval = _screenmode;
    if (mode != FULLSCREEN && mode != FULLSCREEN_ACCEL && mode != WINDOW)
        ts_fatal("ts_scrmode: requested screen mode not supported.");
    else
        _screenmode = mode;
    return oldval;
}

// request double buffer
int ts_doublebuff(int mode)
{
    if (!_tscope_flag)
        _tscope_init();
    int oldval;
    oldval = _dbuff;
    if (mode)
        _dbuff = ON;
    else
        _dbuff = OFF;
    return oldval;
}

// -----------------------------------------------------------------------
// routines for screen manipulation 

// clear the screen
void ts_clrscr()
{
    if (!_screen_flag)
        _screen_init();
    clear_to_color(screen, _bgcolor);
}

// make a screendump
void ts_scrdump()
{
    if (!_screen_flag)
        _screen_init();
    static int dumpno = 1;

    BITMAP *bmp;
    PALETTE pal;
    get_palette(pal);
    bmp = create_sub_bitmap(screen, 0, 0, SXMAX, SYMAX);

    char fname[20];
    sprintf(fname, "dump%d.bmp", dumpno);
    save_bitmap(fname, bmp, pal);

    destroy_bitmap(bmp);
    dumpno++;
}

// set the double buffer as target
void ts_tobuff()
{
    if (!_dbuff_flag)
        ts_fatal("ts_tobuff: double buffer not running.");
    _destmap = _dbuffmap;
}

// set the screen as target
void ts_toscr()
{
    if (!_screen_flag)
        _screen_init();
    _destmap = _screenmap;
}

// clear the double buffer
void ts_clrbuff()
{
    if (!_dbuff_flag)
        ts_fatal("ts_clrbuff:  double buffer not running.");
    clear_to_color(_dbuffmap->b, _bgcolor);
}

// blit the double buffer to the screen
void ts_blitbuff()
{
    if (!_dbuff_flag)
        ts_fatal("ts_blitbuff: double buffer not running.");
    scare_mouse();
    blit(_dbuffmap->b, screen, 0, 0, 0, 0, SXMAX, SYMAX);
    unscare_mouse();
}


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