Ignore:
Timestamp:
Sep 9, 2006, 6:53:13 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Made features (such as dithering mode) and caca_sqrt work in the legacy 0.x glue code.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/caca0.c

    r1013 r1015  
    2121
    2222#if !defined(__KERNEL__)
    23 #   include <stdio.h>
     23#   include <stdlib.h>
    2424#endif
    2525
     
    3030cucul_canvas_t *__caca0_cv = NULL;
    3131caca_display_t *__caca0_dp = NULL;
    32 unsigned char __caca0_fg;
    33 unsigned char __caca0_bg;
     32unsigned char __caca0_fg = CUCUL_COLOR_LIGHTGRAY;
     33unsigned char __caca0_bg = CUCUL_COLOR_BLACK;
    3434char __caca0_utf8[2] = " ";
    3535
     
    3838void __caca0_end(void);
    3939unsigned int __caca0_get_event(unsigned int, int);
     40unsigned int __caca0_sqrt(unsigned int);
    4041int __caca0_get_feature(int);
    4142void __caca0_set_feature(int);
    4243char const *__caca0_get_feature_name(int);
    4344cucul_canvas_t *__caca0_load_sprite(char const *);
     45cucul_dither_t *__caca0_create_bitmap(unsigned int, unsigned int,
     46          unsigned int, unsigned int, unsigned long int, unsigned long int,
     47          unsigned long int, unsigned long int);
     48void __caca0_free_bitmap(cucul_dither_t *);
    4449
    4550/* Emulation functions */
     
    100105}
    101106
    102 enum caca_feature
    103 {
    104     CACA_BACKGROUND       = 0x10,
    105     CACA_BACKGROUND_BLACK = 0x11,
    106     CACA_BACKGROUND_SOLID = 0x12,
    107 #define CACA_BACKGROUND_MIN 0x11
    108 #define CACA_BACKGROUND_MAX 0x12
    109     CACA_ANTIALIASING           = 0x20,
    110     CACA_ANTIALIASING_NONE      = 0x21,
    111     CACA_ANTIALIASING_PREFILTER = 0x22,
    112 #define CACA_ANTIALIASING_MIN     0x21
    113 #define CACA_ANTIALIASING_MAX     0x22
    114     CACA_DITHERING          = 0x30,
    115     CACA_DITHERING_NONE     = 0x31,
    116     CACA_DITHERING_ORDERED2 = 0x32,
    117     CACA_DITHERING_ORDERED4 = 0x33,
    118     CACA_DITHERING_ORDERED8 = 0x34,
    119     CACA_DITHERING_RANDOM   = 0x35,
    120 #define CACA_DITHERING_MIN    0x31
    121 #define CACA_DITHERING_MAX    0x35
    122     CACA_FEATURE_UNKNOWN = 0xffff
     107unsigned int __caca0_sqrt(unsigned int a)
     108{
     109    if(a == 0)
     110        return 0;
     111
     112    if(a < 1000000000)
     113    {
     114        unsigned int x = a < 10 ? 1
     115                       : a < 1000 ? 10
     116                       : a < 100000 ? 100
     117                       : a < 10000000 ? 1000
     118                       : 10000;
     119
     120        /* Newton's method. Three iterations would be more than enough. */
     121        x = (x * x + a) / x / 2;
     122        x = (x * x + a) / x / 2;
     123        x = (x * x + a) / x / 2;
     124        x = (x * x + a) / x / 2;
     125
     126        return x;
     127    }
     128
     129    return 2 * __caca0_sqrt(a / 4);
     130}
     131
     132static char const *features[] =
     133{
     134    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
     135    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
     136
     137    NULL, "16", "full16", NULL, NULL, NULL, NULL, NULL,
     138    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
     139
     140    NULL, "none", "prefilter", NULL, NULL, NULL, NULL, NULL,
     141    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
     142
     143    NULL, "none", "ordered2", "ordered4", "ordered8", "random"
    123144};
    124145
     146static cucul_dither_t **bitmaps = NULL;
     147static unsigned int nbitmaps = 0;
     148
     149static int background = 0x12;
     150static int antialiasing = 0x22;
     151static int dithering = 0x33;
     152
    125153int __caca0_get_feature(int feature)
    126154{
    127     return feature;
     155    if(feature == 0x10)
     156        return background;
     157    if(feature == 0x20)
     158        return antialiasing;
     159    if(feature == 0x30)
     160        return dithering;
     161    return 0xffff; /* CACA_FEATURE_UNKNOWN */
    128162}
    129163
    130164void __caca0_set_feature(int feature)
    131165{
     166    unsigned int i;
     167
    132168    switch(feature)
    133169    {
    134         case CACA_BACKGROUND:
    135             feature = CACA_BACKGROUND_SOLID;
    136         case CACA_BACKGROUND_BLACK:
    137         case CACA_BACKGROUND_SOLID:
    138             //_caca_background = feature;
    139             break;
    140 
    141         case CACA_ANTIALIASING:
    142             feature = CACA_ANTIALIASING_PREFILTER;
    143         case CACA_ANTIALIASING_NONE:
    144         case CACA_ANTIALIASING_PREFILTER:
    145             //_caca_antialiasing = feature;
    146             break;
    147 
    148         case CACA_DITHERING:
    149             feature = CACA_DITHERING_ORDERED4;
    150         case CACA_DITHERING_NONE:
    151         case CACA_DITHERING_ORDERED2:
    152         case CACA_DITHERING_ORDERED4:
    153         case CACA_DITHERING_ORDERED8:
    154         case CACA_DITHERING_RANDOM:
    155             //_caca_dithering = feature;
     170        case 0x10: feature = 0x12; /* CACA_BACKGROUND_SOLID */
     171        case 0x11: case 0x12:
     172            background = feature;
     173            for(i = 0; i < nbitmaps; i++)
     174                cucul_set_dither_color(bitmaps[i], features[feature]);
     175            break;
     176
     177        case 0x20: feature = 0x22; /* CACA_ANTIALIASING_PREFILTER */
     178        case 0x21: case 0x22:
     179            antialiasing = feature;
     180            for(i = 0; i < nbitmaps; i++)
     181                cucul_set_dither_antialias(bitmaps[i], features[feature]);
     182            break;
     183
     184        case 0x30: feature = 0x33; /* CACA_DITHERING_ORDERED4 */
     185        case 0x31: case 0x32: case 0x33: case 0x34: case 0x35:
     186            dithering = feature;
     187            for(i = 0; i < nbitmaps; i++)
     188                cucul_set_dither_mode(bitmaps[i], features[feature]);
    156189            break;
    157190    }
     
    162195    switch(feature)
    163196    {
    164         case CACA_BACKGROUND_BLACK: return "black background";
    165         case CACA_BACKGROUND_SOLID: return "solid background";
    166 
    167         case CACA_ANTIALIASING_NONE:      return "no antialiasing";
    168         case CACA_ANTIALIASING_PREFILTER: return "prefilter antialiasing";
    169 
    170         case CACA_DITHERING_NONE:    return "no dithering";
    171         case CACA_DITHERING_ORDERED2: return "2x2 ordered dithering";
    172         case CACA_DITHERING_ORDERED4: return "4x4 ordered dithering";
    173         case CACA_DITHERING_ORDERED8: return "8x8 ordered dithering";
    174         case CACA_DITHERING_RANDOM:  return "random dithering";
     197        case 0x11: return "black background";
     198        case 0x12: return "solid background";
     199
     200        case 0x21: return "no antialiasing";
     201        case 0x22: return "prefilter antialiasing";
     202
     203        case 0x31: return "no dithering";
     204        case 0x32: return "2x2 ordered dithering";
     205        case 0x33: return "4x4 ordered dithering";
     206        case 0x34: return "8x8 ordered dithering";
     207        case 0x35: return "random dithering";
    175208
    176209        default: return "unknown";
     
    194227}
    195228
     229cucul_dither_t *__caca0_create_bitmap(unsigned int bpp, unsigned int w,
     230                                      unsigned int h, unsigned int pitch,
     231                                      unsigned long int r, unsigned long int g,
     232                                      unsigned long int b, unsigned long int a)
     233{
     234    cucul_dither_t *d;
     235
     236    d = cucul_create_dither(bpp, w, h, pitch, r, g, b, a);
     237    if(!d)
     238        return NULL;
     239
     240    cucul_set_dither_color(d, features[background]);
     241    cucul_set_dither_antialias(d, features[antialiasing]);
     242    cucul_set_dither_mode(d, features[dithering]);
     243
     244    /* Store bitmap in our list */
     245    nbitmaps++;
     246    bitmaps = realloc(bitmaps, nbitmaps * (sizeof(cucul_dither_t *)));
     247    bitmaps[nbitmaps - 1] = d;
     248
     249    return d;
     250}
     251
     252void __caca0_free_bitmap(cucul_dither_t *d)
     253{
     254    unsigned int i, found = 0;
     255
     256    cucul_free_dither(d);
     257
     258    /* Remove bitmap from our list */
     259    for(i = 0; i + 1 < nbitmaps; i++)
     260    {
     261        if(bitmaps[i] == d)
     262            found = 1;
     263        if(found)
     264            bitmaps[i] = bitmaps[i + 1];
     265    }
     266
     267    nbitmaps--;
     268}
     269
Note: See TracChangeset for help on using the changeset viewer.