/*
            __
           / /_______________  ____  ___
          / __/ ___/ ___/ __ \/ __ \/ _ \
         / /_(__  ) /__/ /_/ / /_/ /  __/
         \__/____/\___/\____/ .___/\___/
                           /_/
    
    bitmap operations

    By Michael Stevens

    See license.html for copyright information
*/

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

// allocate memory for a bitmap
map *ts_makebmp(int w, int h)
{
    if (!_screen_flag)
        _screen_init();
    map *tmp;
    tmp = malloc(sizeof(map));
    if (_coordinates == CARTESIAN) {
        tmp->b = create_bitmap(w * 2, h * 2);
        tmp->sw = w * 2;
        tmp->sh = h * 2;
        tmp->w = w;
        tmp->h = h;
    } else {
        tmp->b = create_bitmap(w, h);
        tmp->sw = w;
        tmp->sh = h;
        tmp->w = w / 2;
        tmp->h = h / 2;
    }

    if (!tmp)
        ts_fatal("ts_makebmp: error creating bitmap.");
    clear_to_color(tmp->b, _bgcolor);
    return tmp;
}

// read a bitmap from a file
map *ts_readbmp(char *file)
{
    if (!_screen_flag)
        _screen_init();
    map *tmp;
    PALETTE tmp2;
    char map_path[200];

    if (_debug_flag > DEBUG3)
        fprintf(stderr, "Reading bitmap %s\n", file);

#ifdef ALLEGRO_WINDOWS
    cygwin_conv_to_full_win32_path(file, map_path);
#else
    strcpy(map_path, file);
#endif

    tmp = malloc(sizeof(map));
    tmp->b = load_bitmap(map_path, tmp2);
    if (!tmp->b)
        ts_fatal("ts_readbmp: error reading bitmap.");

    tmp->w = tmp->b->w / 2;
    tmp->h = tmp->b->h / 2;
    tmp->sw = tmp->b->w;
    tmp->sh = tmp->b->h;
    return tmp;
}

// free memory used by bitmap
void ts_killbmp(map * what)
{
    if (!_screen_flag)
        _screen_init();
    if (!what)
        ts_fatal("ts_killbmp: error destroying bitmap");
    destroy_bitmap(what->b);
    free(what);
}

// set bitmap as drawing target
map *ts_tobmp(map * where)
{
    if (!_screen_flag)
        _screen_init();
    if (!where)
        ts_fatal("ts_tobmp: error writing to bitmap");
    map *oldmap;
    oldmap = _destmap;
    _destmap = where;
    return oldmap;
}

// clear a bitmap's contents
void ts_clrbmp(map * what, int color)
{
    if (!_screen_flag)
        _screen_init();
    if (!what)
        ts_fatal("ts_clrbmp: error clearing bitmap");

    if (color == TRANS)
        clear_to_color(what->b, bitmap_mask_color(screen));
    else if (color == NONE)
        clear_to_color(what->b, _bgcolor);
    else
        clear_to_color(what->b, color);

}

// blit a bitmap 
void ts_blit(map * what, int dest_x, int dest_y)
{
    if (!_screen_flag)
        _screen_init();
    if (!what)
        ts_fatal("ts_blit: error blitting bitmap");
    scare_mouse();
    if (_coordinates == CARTESIAN) {
        dest_x = sx(dest_x) - what->w;
        dest_y = sy(dest_y) - what->h;
    }
    masked_blit(what->b, _destmap->b, 0, 0, dest_x, dest_y, what->sw,
                what->sh);
    unscare_mouse();
}

// blit part of a bitmap
void ts_partblit(map * what, int src_x1, int src_y1, int src_x2,
                 int src_y2, int dest_x, int dest_y)
{
    if (!_screen_flag)
        _screen_init();
    if (!what)
        ts_fatal("ts_partblit: error blitting bitmap");

    int width = abs(src_x2 - src_x1);
    int height = abs(src_y2 - src_y1);

    int src_x, src_y;
    if (src_x1 < src_x2)
        src_x = src_x1;
    else
        src_x = src_x2;

    if (src_y1 < src_y2)
        src_y = src_y1;
    else
        src_y = src_y2;

    if (_coordinates == CARTESIAN) {
        dest_x = sx(dest_x / 2 - width / 2);
        dest_y = sy(dest_y / 2 + height / 2);
        src_x = src_x + what->w;
        src_y = src_y + what->h;
    }

    scare_mouse();
    masked_blit(what->b, _destmap->b, src_x, src_y, dest_x, dest_y,
                width, height);
    unscare_mouse();
}

// shrink/stretch & blit a bitmap
void ts_stretchblit(map * what, float ratio, int dest_x, int dest_y)
{
    if (!_screen_flag)
        _screen_init();
    if (!what)
        ts_fatal("ts_stretchblit: error blitting bitmap");

    int source_w, source_h, dest_w, dest_h;
    int source_x, source_y;

    source_w = what->sw;
    source_h = what->sh;
    source_x = 0;
    source_y = 0;
    dest_w = (int) (source_w * ratio);
    dest_h = (int) (source_h * ratio);

    if (_coordinates == CARTESIAN) {
        dest_x = sx(dest_x - dest_w / 2);
        dest_y = sy(dest_y + dest_h / 2);
    }

    scare_mouse();
    masked_stretch_blit(what->b, _destmap->b, source_x, source_y, source_w,
                        source_h, dest_x, dest_y, dest_w, dest_h);
    unscare_mouse();
}

// flip & blit a bitmap
void ts_flipblit(map * what, int flip, int dest_x, int dest_y)
{
    if (!_screen_flag)
        _screen_init();
    if (!what)
        ts_fatal("ts_flipblit: error blitting bitmap");

    if (_coordinates == CARTESIAN) {
        dest_x = sx(dest_x - what->w);
        dest_y = sy(dest_y + what->h);
    }

    scare_mouse();
    switch (flip) {
    case VFLIP:
        draw_sprite_v_flip(_destmap->b, what->b, dest_x, dest_y);
        break;
    case HFLIP:
        draw_sprite_h_flip(_destmap->b, what->b, dest_x, dest_y);
        break;
    case VHFLIP:
        draw_sprite_vh_flip(_destmap->b, what->b, dest_x, dest_y);
        break;
    default:
        break;
    }
    unscare_mouse();
}

// rotate & blit a bitmap
void ts_rotateblit(map * what, int cx, int cy, float angle, int dest_x,
                   int dest_y)
{
    if (!_screen_flag)
        _screen_init();
    if (!what)
        ts_fatal("ts_rotateblit: error blitting bitmap");

    if (_coordinates == CARTESIAN) {
        dest_x = sx(dest_x);
        dest_y = sy(dest_y);
        cx = what->w + cx;
        cy = what->h - cy;
    }

    while (angle >= 360)
        angle -= 360;

    while (angle < 0)
        angle += 360;

    fixed fixang;
    fixang = ftofix(angle);
    fixang = fixdiv(fixang, itofix(360));
    fixang = fixmul(fixang, itofix(-256));

    scare_mouse();
    pivot_sprite(_destmap->b, what->b, dest_x, dest_y, cx, cy, fixang);
    unscare_mouse();
}


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