Changeset 246 for libcaca/trunk/src


Ignore:
Timestamp:
Dec 11, 2003, 5:31:49 PM (19 years ago)
Author:
Sam Hocevar
Message:
  • src/graphics.c: + Internally cache screen width and height.
  • src/bitmap.c: + Added alpha support to caca_draw_bitmap(). For now, we only treat 0%

alpha as fully transparent, and any other value as fully opaque.

+ Slightly tuned colour weights in the renderer.
+ caca_set_bitmap_palette() takes unsigned ints.

  • examples/demo.c: + Added a crap render demo.
  • examples/aafire.c: + Set a 20ms delay. + Added alpha values to the palette.
  • examples/view.c: + Draw a gray checkered grid below the image so that transparent images

look a lot nicer.

+ 'f' toggles "fullscreen" mode.

Location:
libcaca/trunk/src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/src/bitmap.c

    r240 r246  
    5151static void mask2shift(unsigned int, int *, int *);
    5252
    53 static void get_rgb_default(struct caca_bitmap *, uint8_t *, int, int,
    54                             unsigned int *, unsigned int *, unsigned int *);
     53static void get_rgba_default(const struct caca_bitmap *, uint8_t *, int, int,
     54                             unsigned int *, unsigned int *, unsigned int *,
     55                             unsigned int *);
    5556static void rgb2hsv_default(int, int, int, int *, int *, int *);
    5657
     
    128129    int bpp, palette;
    129130    int w, h, pitch;
    130     int rmask, gmask, bmask;
    131     int rright, gright, bright;
    132     int rleft, gleft, bleft;
     131    int rmask, gmask, bmask, amask;
     132    int rright, gright, bright, aright;
     133    int rleft, gleft, bleft, aleft;
    133134    void (*get_hsv)(struct caca_bitmap *, char *, int, int);
    134     int red[256], green[256], blue[256];
     135    int red[256], green[256], blue[256], alpha[256];
    135136};
    136137
     
    161162
    162163struct caca_bitmap *caca_create_bitmap(int bpp, int w, int h, int pitch,
    163                                        int rmask, int gmask, int bmask)
     164                                       int rmask, int gmask, int bmask,
     165                                       int amask)
    164166{
    165167    struct caca_bitmap *bitmap;
     
    183185    bitmap->gmask = gmask;
    184186    bitmap->bmask = bmask;
     187    bitmap->amask = amask;
    185188
    186189    /* Load bitmasks */
    187     if(rmask || gmask || bmask)
     190    if(rmask || gmask || bmask || amask)
    188191    {
    189192        mask2shift(rmask, &bitmap->rright, &bitmap->rleft);
    190193        mask2shift(gmask, &bitmap->gright, &bitmap->gleft);
    191194        mask2shift(bmask, &bitmap->bright, &bitmap->bleft);
     195        mask2shift(amask, &bitmap->aright, &bitmap->aleft);
    192196    }
    193197
     
    199203        for(i = 0; i < 256; i++)
    200204        {
    201             bitmap->red[i] = i * 0x10;
    202             bitmap->green[i] = i * 0x10;
    203             bitmap->blue[i] = i * 0x10;
     205            bitmap->red[i] = i * 0xfff / 256;
     206            bitmap->green[i] = i * 0xfff / 256;
     207            bitmap->blue[i] = i * 0xfff / 256;
     208            bitmap->alpha[i] = 0xfff;
    204209        }
    205210    }
     
    209214
    210215void caca_set_bitmap_palette(struct caca_bitmap *bitmap,
    211                              int red[], int green[], int blue[])
     216                             unsigned int red[], unsigned int green[],
     217                             unsigned int blue[], unsigned int alpha[])
    212218{
    213219    int i;
     
    220226        if(red[i] >= 0 && red[i] < 0x1000 &&
    221227           green[i] >= 0 && green[i] < 0x1000 &&
    222            blue[i] >= 0 && blue[i] < 0x1000)
     228           blue[i] >= 0 && blue[i] < 0x1000 &&
     229           alpha[i] >= 0 && alpha[i] < 0x1000)
    223230        {
    224231            bitmap->red[i] = red[i];
    225232            bitmap->green[i] = green[i];
    226233            bitmap->blue[i] = blue[i];
     234            bitmap->alpha[i] = alpha[i];
    227235        }
    228236    }
     
    237245}
    238246
    239 static void get_rgb_default(struct caca_bitmap *bitmap, uint8_t *pixels,
    240                             int x, int y, unsigned int *r,
    241                             unsigned int *g, unsigned int *b)
     247static void get_rgba_default(const struct caca_bitmap *bitmap, uint8_t *pixels,
     248                             int x, int y, unsigned int *r, unsigned int *g,
     249                             unsigned int *b, unsigned int *a)
    242250{
    243251    uint32_t bits;
     
    255263            if(__BYTE_ORDER == __BIG_ENDIAN)
    256264#else
    257             static const uint32_t rmask = 0x12345678;
    258             if(*(uint8_t *)&rmask == 0x12)
     265            /* This is compile-time optimised with at least -O1 or -Os */
     266            const uint32_t rmask = 0x12345678;
     267            if(*(const uint8_t *)&rmask == 0x12)
    259268#endif
    260269                bits = ((uint32_t)pixels[0] << 16) |
     
    281290        *g = bitmap->green[bits];
    282291        *b = bitmap->blue[bits];
     292        *a = bitmap->alpha[bits];
    283293    }
    284294    else
     
    287297        *g = ((bits & bitmap->gmask) >> bitmap->gright) << bitmap->gleft;
    288298        *b = ((bits & bitmap->bmask) >> bitmap->bright) << bitmap->bleft;
     299        *a = ((bits & bitmap->amask) >> bitmap->aright) << bitmap->aleft;
    289300    }
    290301}
     
    321332
    322333void caca_draw_bitmap(int x1, int y1, int x2, int y2,
    323                       struct caca_bitmap *bitmap, char *pixels)
     334                      const struct caca_bitmap *bitmap, char *pixels)
    324335{
    325336#if !NEW_RENDERER
     
    359370    static const char density_chars[] =
    360371        "    "
    361         "    "
    362         ".`  "
    363         ",`.'"
    364         "i:-^"
    365         "|=+;"
    366         "ox/\\"
    367         "<>x%"
    368         "&$zw"
    369         "WXKM"
    370         "#8##"
    371         "8@8#"
    372         "@8@8"
     372        ".   "
     373        "..  "
     374        "...."
     375        "::::"
     376        ";=;="
     377        "tftf"
     378        "%$%$"
     379        "&KSZ"
     380        "WXGM"
     381        "@@@@"
     382        "8888"
     383        "####"
    373384        "????";
    374385
     
    392403    }
    393404
    394     for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)caca_get_height(); y++)
     405    for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)_caca_height; y++)
    395406        for(x = x1 > 0 ? x1 : 0, _init_dither(y);
    396             x <= x2 && x <= (int)caca_get_width();
     407            x <= x2 && x <= (int)_caca_width;
    397408            x++)
    398409    {
    399410        int ch;
    400         unsigned int r, g, b, R, G, B;
     411        unsigned int r, g, b, a, R, G, B;
    401412        int hue, sat, val;
    402413        int fromx = w * (x - x1) / (x2 - x1 + 1);
     
    414425        /* First get RGB */
    415426        R = 0, G = 0, B = 0;
    416         get_rgb_default(bitmap, pixels, fromx, fromy, &r, &g, &b);
     427        get_rgba_default(bitmap, pixels, fromx, fromy, &r, &g, &b, &a);
     428        if(a == 0)
     429            continue;
    417430#if 1
    418431        R += r; G += g; B += b;
    419432#else
    420433        R += r; G += g; B += b;
    421         get_rgb_default(bitmap, pixels, fromx - 1, fromy, &r, &g, &b);
     434        get_rgba_default(bitmap, pixels, fromx - 1, fromy, &r, &g, &b, &a);
    422435        R += r; G += g; B += b;
    423         get_rgb_default(bitmap, pixels, fromx, fromy - 1, &r, &g, &b);
     436        get_rgba_default(bitmap, pixels, fromx, fromy - 1, &r, &g, &b, &a);
    424437        R += r; G += g; B += b;
    425         get_rgb_default(bitmap, pixels, fromx + 1, fromy, &r, &g, &b);
     438        get_rgba_default(bitmap, pixels, fromx + 1, fromy, &r, &g, &b, &a);
    426439        R += r; G += g; B += b;
    427         get_rgb_default(bitmap, pixels, fromx, fromy + 1, &r, &g, &b);
     440        get_rgba_default(bitmap, pixels, fromx, fromy + 1, &r, &g, &b, &a);
    428441        R += r; G += g; B += b;
    429442        R /= 5; G /= 5; B /= 5;
     
    444457        distbg = XRATIO * val * val;
    445458        distbg += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
     459        distbg = distbg * 2 / 4;
    446460        outbg = CACA_COLOR_BLACK;
    447461
     
    510524             + HRATIO * (hue - hue2) * (hue - hue2);
    511525        dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
    512         dist = dist * 3 / 4;
     526//        dist = dist * 3 / 4;
    513527        if(dist <= distbg)
    514528        {
     
    529543             + HRATIO * (hue - hue2) * (hue - hue2);
    530544        dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
    531         dist = dist / 2;
     545        //dist = dist * 3 / 4;
     546        //dist = dist / 2;
    532547        if(dist <= distbg)
    533548        {
  • libcaca/trunk/src/box.c

    r205 r246  
    5959    }
    6060
    61     xmax = caca_get_width() - 1;
    62     ymax = caca_get_height() - 1;
     61    xmax = _caca_width - 1;
     62    ymax = _caca_height - 1;
    6363
    6464    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
     
    112112    }
    113113
    114     xmax = caca_get_width() - 1;
    115     ymax = caca_get_height() - 1;
     114    xmax = _caca_width - 1;
     115    ymax = _caca_height - 1;
    116116
    117117    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  • libcaca/trunk/src/caca.c

    r238 r246  
    3939#   include <dos.h>
    4040#   include <conio.h>
    41 #   if defined(SCREENUPDATE_IN_PC_H)
    42 #       include <pc.h>
    43 #   endif
    4441#else
    4542#   error "no graphics library detected"
     
    6360
    6461#if defined(USE_CONIO)
    65 static struct text_info ti;
    6662char *_caca_screen;
    6763#endif
     
    133129unsigned int caca_get_width(void)
    134130{
    135 #if defined(USE_SLANG)
    136     return SLtt_Screen_Cols;
    137 #elif defined(USE_NCURSES)
    138     return COLS;
    139 #elif defined(USE_CONIO)
    140     return ti.screenwidth;
    141 #endif
     131    return _caca_width;
    142132}
    143133
    144134unsigned int caca_get_height(void)
    145135{
    146 #if defined(USE_SLANG)
    147     return SLtt_Screen_Rows;
    148 #elif defined(USE_NCURSES)
    149     return LINES;
    150 #else
    151     return ti.screenheight;
    152 #endif
     136    return _caca_height;
    153137}
    154138
     
    213197    textcolor((enum COLORS)WHITE);
    214198    textbackground((enum COLORS)BLACK);
    215     gotoxy(caca_get_width(), caca_get_height());
     199    gotoxy(_caca_width, _caca_height);
    216200    cputs("\r\n");
    217201    _setcursortype(_NORMALCURSOR);
  • libcaca/trunk/src/caca.h

    r238 r246  
    201201struct caca_sprite;
    202202struct caca_sprite * caca_load_sprite(const char *);
    203 int caca_get_sprite_frames(struct caca_sprite *);
    204 int caca_get_sprite_width(struct caca_sprite *, int);
    205 int caca_get_sprite_height(struct caca_sprite *, int);
    206 int caca_get_sprite_dx(struct caca_sprite *, int);
    207 int caca_get_sprite_dy(struct caca_sprite *, int);
    208 void caca_draw_sprite(int, int, struct caca_sprite *, int);
     203int caca_get_sprite_frames(const struct caca_sprite *);
     204int caca_get_sprite_width(const struct caca_sprite *, int);
     205int caca_get_sprite_height(const struct caca_sprite *, int);
     206int caca_get_sprite_dx(const struct caca_sprite *, int);
     207int caca_get_sprite_dy(const struct caca_sprite *, int);
     208void caca_draw_sprite(int, int, const struct caca_sprite *, int);
    209209void caca_free_sprite(struct caca_sprite *);
    210210
     
    213213 */
    214214struct caca_bitmap;
    215 struct caca_bitmap *caca_create_bitmap(int, int, int, int, int, int, int);
    216 void caca_set_bitmap_palette(struct caca_bitmap *, int[], int[], int[]);
    217 void caca_draw_bitmap(int, int, int, int, struct caca_bitmap *, char *);
     215struct caca_bitmap *caca_create_bitmap(int, int, int, int, int, int, int, int);
     216void caca_set_bitmap_palette(struct caca_bitmap *, unsigned int[],
     217                             unsigned int[], unsigned int[], unsigned int[]);
     218void caca_draw_bitmap(int, int, int, int, const struct caca_bitmap *, char *);
    218219void caca_free_bitmap(struct caca_bitmap *);
    219220
  • libcaca/trunk/src/caca_internals.h

    r227 r246  
    3838
    3939#if defined(USE_CONIO)
     40extern struct text_info _ti;
    4041extern char *_caca_screen;
    4142#endif
     43
     44extern unsigned int _caca_width;
     45extern unsigned int _caca_height;
    4246
    4347extern char *_caca_empty_line;
  • libcaca/trunk/src/conic.c

    r205 r246  
    192192    uint8_t b = 0;
    193193
    194     if(xo + x >= 0 && xo + x < (int)caca_get_width())
     194    if(xo + x >= 0 && xo + x < (int)_caca_width)
    195195        b |= 0x1;
    196     if(xo - x >= 0 && xo - x < (int)caca_get_width())
     196    if(xo - x >= 0 && xo - x < (int)_caca_width)
    197197        b |= 0x2;
    198     if(yo + y >= 0 && yo + y < (int)caca_get_height())
     198    if(yo + y >= 0 && yo + y < (int)_caca_height)
    199199        b |= 0x4;
    200     if(yo - y >= 0 && yo - y < (int)caca_get_height())
     200    if(yo - y >= 0 && yo - y < (int)_caca_height)
    201201        b |= 0x8;
    202202
  • libcaca/trunk/src/graphics.c

    r231 r246  
    3636#elif defined(USE_CONIO)
    3737#   include <conio.h>
     38#   if defined(SCREENUPDATE_IN_PC_H)
     39#       include <pc.h>
     40#   endif
    3841#else
    3942#   error "no graphics library detected"
     
    5558#include "caca_internals.h"
    5659
     60#ifdef USE_CONIO
     61static struct text_info ti;
     62#endif
     63
     64unsigned int _caca_width;
     65unsigned int _caca_height;
     66
    5767static unsigned int _caca_delay;
    5868static unsigned int _caca_rendertime;
     
    93103    char *data;
    94104#endif
    95     if(x < 0 || x >= (int)caca_get_width() ||
    96        y < 0 || y >= (int)caca_get_height())
     105    if(x < 0 || x >= (int)_caca_width ||
     106       y < 0 || y >= (int)_caca_height)
    97107        return;
    98108
     
    104114    addch(c);
    105115#elif defined(USE_CONIO)
    106     data = _caca_screen + 2 * (x + y * caca_get_width());
     116    data = _caca_screen + 2 * (x + y * _caca_width);
    107117    data[0] = c;
    108118    data[1] = (_caca_bgcolor << 4) | _caca_fgcolor;
     
    116126    unsigned int len;
    117127
    118     if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width())
     128    if(y < 0 || y >= (int)_caca_height || x >= (int)_caca_width)
    119129        return;
    120130
     
    130140    }
    131141
    132     if(x + len >= caca_get_width())
    133     {
    134         memcpy(_caca_scratch_line, s, caca_get_width() - x);
    135         _caca_scratch_line[caca_get_width() - x] = '\0';
     142    if(x + len >= _caca_width)
     143    {
     144        memcpy(_caca_scratch_line, s, _caca_width - x);
     145        _caca_scratch_line[_caca_width - x] = '\0';
    136146        s = _caca_scratch_line;
    137147    }
     
    144154    addstr(s);
    145155#elif defined(USE_CONIO)
    146     char *buf = _caca_screen + 2 * (x + y * caca_get_width());
     156    char *buf = _caca_screen + 2 * (x + y * _caca_width);
    147157    while(*s)
    148158    {
     
    161171    va_list args;
    162172
    163     if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width())
     173    if(y < 0 || y >= (int)_caca_height || x >= (int)_caca_width)
    164174        return;
    165175
    166     if(caca_get_width() - x + 1 > BUFSIZ)
    167         buf = malloc(caca_get_width() - x + 1);
     176    if(_caca_width - x + 1 > BUFSIZ)
     177        buf = malloc(_caca_width - x + 1);
    168178
    169179    va_start(args, format);
    170180#if defined(HAVE_VSNPRINTF)
    171     vsnprintf(buf, caca_get_width() - x + 1, format, args);
     181    vsnprintf(buf, _caca_width - x + 1, format, args);
    172182#else
    173183    vsprintf(buf, format, args);
    174184#endif
    175     buf[caca_get_width() - x] = '\0';
     185    buf[_caca_width - x] = '\0';
    176186    va_end(args);
    177187
     
    186196    enum caca_color oldfg = caca_get_fg_color();
    187197    enum caca_color oldbg = caca_get_bg_color();
    188     int y = caca_get_height();
     198    int y = _caca_height;
    189199
    190200    caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
     
    235245    SLtt_Has_Alt_Charset = 0;
    236246
     247    _caca_width = SLtt_Screen_Cols;
     248    _caca_height = SLtt_Screen_Rows;
     249
    237250#elif defined(USE_NCURSES)
    238251    static int curses_colors[] =
     
    293306        }
    294307
     308    _caca_width = COLS;
     309    _caca_height = LINES;
     310
    295311#elif defined(USE_CONIO)
    296312    gettextinfo(&ti);
     
    303319    /* FIXME */
    304320#   endif
    305 
    306 #endif
    307     _caca_empty_line = malloc(caca_get_width() + 1);
    308     memset(_caca_empty_line, ' ', caca_get_width());
    309     _caca_empty_line[caca_get_width()] = '\0';
    310 
    311     _caca_scratch_line = malloc(caca_get_width() + 1);
     321    _caca_width = ti.screenwidth;
     322    _caca_height = ti.screenheight;
     323
     324#endif
     325    _caca_empty_line = malloc(_caca_width + 1);
     326    memset(_caca_empty_line, ' ', _caca_width);
     327    _caca_empty_line[_caca_width] = '\0';
     328
     329    _caca_scratch_line = malloc(_caca_width + 1);
    312330
    313331    _caca_delay = 0;
  • libcaca/trunk/src/line.c

    r205 r246  
    172172    else if(bits1 & (1<<1))
    173173    {
    174         int xmax = caca_get_width() - 1;
     174        int xmax = _caca_width - 1;
    175175        s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
    176176        s->x1 = xmax;
     
    183183    else if(bits1 & (1<<3))
    184184    {
    185         int ymax = caca_get_height() - 1;
     185        int ymax = _caca_height - 1;
    186186        s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
    187187        s->y1 = ymax;
     
    204204    if(x < 0)
    205205        b |= (1<<0);
    206     else if(x >= (int)caca_get_width())
     206    else if(x >= (int)_caca_width)
    207207        b |= (1<<1);
    208208
    209209    if(y < 0)
    210210        b |= (1<<2);
    211     else if(y >= (int)caca_get_height())
     211    else if(y >= (int)_caca_height)
    212212        b |= (1<<3);
    213213
  • libcaca/trunk/src/sprite.c

    r226 r246  
    166166}
    167167
    168 int caca_get_sprite_frames(struct caca_sprite *sprite)
     168int caca_get_sprite_frames(const struct caca_sprite *sprite)
    169169{
    170170    if(sprite == NULL)
     
    174174}
    175175
    176 int caca_get_sprite_width(struct caca_sprite *sprite, int f)
     176int caca_get_sprite_width(const struct caca_sprite *sprite, int f)
    177177{
    178178    if(sprite == NULL)
     
    185185}
    186186
    187 int caca_get_sprite_height(struct caca_sprite *sprite, int f)
     187int caca_get_sprite_height(const struct caca_sprite *sprite, int f)
    188188{
    189189    if(sprite == NULL)
     
    196196}
    197197
    198 int caca_get_sprite_dx(struct caca_sprite *sprite, int f)
     198int caca_get_sprite_dx(const struct caca_sprite *sprite, int f)
    199199{
    200200    if(sprite == NULL)
     
    207207}
    208208
    209 int caca_get_sprite_dy(struct caca_sprite *sprite, int f)
     209int caca_get_sprite_dy(const struct caca_sprite *sprite, int f)
    210210{
    211211    if(sprite == NULL)
     
    218218}
    219219
    220 void caca_draw_sprite(int x, int y, struct caca_sprite *sprite, int f)
     220void caca_draw_sprite(int x, int y, const struct caca_sprite *sprite, int f)
    221221{
    222222    int i, j;
  • libcaca/trunk/src/triangle.c

    r205 r246  
    7171    x3 *= 4;
    7272
    73     xmax = caca_get_width() - 1;
    74     ymax = caca_get_height() - 1;
     73    xmax = _caca_width - 1;
     74    ymax = _caca_height - 1;
    7575
    7676    /* Rasterize our triangle */
Note: See TracChangeset for help on using the changeset viewer.