Changeset 463


Ignore:
Timestamp:
01/13/05 23:19:01 (8 years ago)
Author:
sam
Message:
  • src/graphics.c: + Implemented caca_get_screen().
Location:
libcaca/trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/src/caca.h

    r375 r463  
    279279void caca_putstr(int, int, char const *); 
    280280void caca_printf(int, int, char const *, ...); 
     281void caca_get_screen(char *); 
    281282void caca_clear(void); 
    282283/*  @} */ 
  • libcaca/trunk/src/graphics.c

    r369 r463  
    9797 * Local variables 
    9898 */ 
     99#if !defined(_DOXYGEN_SKIP_ME) 
     100static uint8_t *cache_char, *cache_attr; 
     101#endif 
     102 
    99103#if defined(USE_NCURSES) 
    100104static int ncurses_attr[16*16]; 
     
    175179int x11_font_width, x11_font_height; 
    176180unsigned int x11_new_width, x11_new_height; 
    177 static uint8_t *x11_char, *x11_attr; 
    178181static int x11_colors[16]; 
    179182static Font x11_font; 
     
    186189 
    187190#if defined(USE_WIN32) 
    188 static uint8_t *win32_char, *win32_attr; 
    189191HANDLE win32_hin, win32_hout; 
    190192static HANDLE win32_front, win32_back; 
     
    377379        return; 
    378380 
     381    cache_char[x + y * _caca_width] = c; 
     382    cache_attr[x + y * _caca_width] = (_caca_bgcolor << 4) | _caca_fgcolor; 
     383 
    379384    switch(_caca_driver) 
    380385    { 
     
    405410#if defined(USE_X11) 
    406411    case CACA_DRIVER_X11: 
    407         x11_char[x + y * _caca_width] = c; 
    408         x11_attr[x + y * _caca_width] = (_caca_bgcolor << 4) | _caca_fgcolor; 
    409412        break; 
    410413#endif 
    411414#if defined(USE_WIN32) 
    412415    case CACA_DRIVER_WIN32: 
    413         win32_char[x + y * _caca_width] = c; 
    414         win32_attr[x + y * _caca_width] = (_caca_bgcolor << 4) | _caca_fgcolor; 
    415416        break; 
    416417#endif 
     
    439440    char *attrbuf; 
    440441#endif 
     442    char const *t; 
    441443    unsigned int len; 
    442444 
     
    461463        _caca_scratch_line[len] = '\0'; 
    462464        s = _caca_scratch_line; 
     465    } 
     466 
     467    charbuf = cache_char + x + y * _caca_width; 
     468    attrbuf = cache_attr + x + y * _caca_width; 
     469    t = s; 
     470    while(*t) 
     471    { 
     472        *charbuf++ = *t++; 
     473        *attrbuf++ = (_caca_bgcolor << 4) | _caca_fgcolor; 
    463474    } 
    464475 
     
    498509#if defined(USE_X11) 
    499510    case CACA_DRIVER_X11: 
    500         charbuf = x11_char + x + y * _caca_width; 
    501         attrbuf = x11_attr + x + y * _caca_width; 
    502         while(*s) 
    503         { 
    504             *charbuf++ = *s++; 
    505             *attrbuf++ = (_caca_bgcolor << 4) | _caca_fgcolor; 
    506         } 
    507511        break; 
    508512#endif 
    509513#if defined(USE_WIN32) 
    510514    case CACA_DRIVER_WIN32: 
    511         charbuf = win32_char + x + y * _caca_width; 
    512         attrbuf = win32_attr + x + y * _caca_width; 
    513         while(*s) 
    514         { 
    515             *charbuf++ = *s++; 
    516             *attrbuf++ = (_caca_bgcolor << 4) | _caca_fgcolor; 
    517         } 
    518515        break; 
    519516#endif 
     
    561558    if(buf != tmp) 
    562559        free(buf); 
     560} 
     561 
     562/** \brief Get the screen. 
     563 * 
     564 *  This function fills a byte array with the character values. 
     565 */ 
     566void caca_get_screen(char *buffer) 
     567{ 
     568    unsigned int x, y; 
     569 
     570    for(y = 0; y < _caca_height; y++) 
     571    { 
     572        for(x = 0; x < _caca_width; x++) 
     573        { 
     574            *buffer++ = cache_attr[x + y * _caca_width]; 
     575            *buffer++ = cache_char[x + y * _caca_width]; 
     576        } 
     577    } 
    563578} 
    564579 
     
    727742            _caca_height = 32; 
    728743 
    729         x11_char = malloc(_caca_width * _caca_height * sizeof(int)); 
    730         if(x11_char == NULL) 
    731             return -1; 
    732  
    733         x11_attr = malloc(_caca_width * _caca_height * sizeof(int)); 
    734         if(x11_attr == NULL) 
    735         { 
    736             free(x11_char); 
    737             return -1; 
    738         } 
    739  
    740         memset(x11_char, 0, _caca_width * _caca_height * sizeof(int)); 
    741         memset(x11_attr, 0, _caca_width * _caca_height * sizeof(int)); 
    742  
    743744        x11_dpy = XOpenDisplay(NULL); 
    744745        if(x11_dpy == NULL) 
    745         { 
    746             free(x11_char); 
    747             free(x11_attr); 
    748746            return -1; 
    749         } 
    750747 
    751748        if(getenv("CACA_FONT") && *(getenv("CACA_FONT"))) 
     
    759756        { 
    760757            XCloseDisplay(x11_dpy); 
    761             free(x11_char); 
    762             free(x11_attr); 
    763758            return -1; 
    764759        } 
     
    769764            XUnloadFont(x11_dpy, x11_font); 
    770765            XCloseDisplay(x11_dpy); 
    771             free(x11_char); 
    772             free(x11_attr); 
    773766            return -1; 
    774767        } 
     
    886879        SetConsoleActiveScreenBuffer(win32_front); 
    887880 
    888         win32_char = malloc(_caca_width * _caca_height * sizeof(int)); 
    889         if(win32_char == NULL) 
    890             return -1; 
    891  
    892         win32_attr = malloc(_caca_width * _caca_height * sizeof(int)); 
    893         if(win32_attr == NULL) 
    894         { 
    895             free(win32_char); 
    896             return -1; 
    897         } 
    898  
    899881        win32_buffer = malloc(_caca_width * _caca_height * sizeof(CHAR_INFO)); 
    900882        if(win32_buffer == NULL) 
    901         { 
    902             free(win32_attr); 
    903             free(win32_char); 
    904883            return -1; 
    905         } 
    906  
    907         memset(win32_char, 0, _caca_width * _caca_height * sizeof(int)); 
    908         memset(win32_attr, 0, _caca_width * _caca_height * sizeof(int)); 
    909884    } 
    910885    else 
     
    913888        /* Dummy */ 
    914889    } 
     890 
     891    cache_char = malloc(_caca_width * _caca_height * sizeof(uint8_t)); 
     892    if(cache_char == NULL) 
     893        return -1; 
     894 
     895    cache_attr = malloc(_caca_width * _caca_height * sizeof(uint8_t)); 
     896    if(cache_attr == NULL) 
     897    { 
     898        free(cache_char); 
     899        return -1; 
     900    } 
     901 
     902    memset(cache_char, 0, _caca_width * _caca_height * sizeof(uint8_t)); 
     903    memset(cache_attr, 0, _caca_width * _caca_height * sizeof(uint8_t)); 
    915904 
    916905    _caca_empty_line = malloc(_caca_width + 1); 
     
    928917int _caca_end_graphics(void) 
    929918{ 
     919    free(cache_char); 
     920    free(cache_attr); 
     921 
    930922#if defined(USE_SLANG) 
    931923    /* Nothing to do */ 
     
    955947        XDestroyWindow(x11_dpy, x11_window); 
    956948        XCloseDisplay(x11_dpy); 
    957         free(x11_char); 
    958         free(x11_attr); 
    959949    } 
    960950    else 
     
    966956        CloseHandle(win32_back); 
    967957        CloseHandle(win32_front); 
    968         free(win32_char); 
    969         free(win32_attr); 
    970958    } 
    971959    else 
     
    11671155            for(x = 0; x < _caca_width; x += len) 
    11681156            { 
    1169                 unsigned char *attr = x11_attr + x + y * _caca_width; 
     1157                uint8_t *attr = cache_attr + x + y * _caca_width; 
    11701158 
    11711159                len = 1; 
     
    11861174            for(x = 0; x < _caca_width; x += len) 
    11871175            { 
    1188                 unsigned char *attr = x11_attr + x + y * _caca_width; 
     1176                uint8_t *attr = cache_attr + x + y * _caca_width; 
    11891177 
    11901178                len = 1; 
    11911179 
    11921180                /* Skip spaces */ 
    1193                 if(x11_char[x + y * _caca_width] == ' ') 
     1181                if(cache_char[x + y * _caca_width] == ' ') 
    11941182                    continue; 
    11951183 
     
    12011189                XDrawString(x11_dpy, x11_pixmap, x11_gc, x * x11_font_width, 
    12021190                            (y + 1) * x11_font_height - x11_font_offset, 
    1203                             x11_char + x + y * _caca_width, len); 
     1191                            cache_char + x + y * _caca_width, len); 
    12041192            } 
    12051193        } 
     
    12221210        for(i = 0; i < _caca_width * _caca_height; i++) 
    12231211        { 
    1224             win32_buffer[i].Char.AsciiChar = win32_char[i]; 
    1225             win32_buffer[i].Attributes = win32_fg_palette[win32_attr[i] & 0xf] 
    1226                                        | win32_bg_palette[win32_attr[i] >> 4]; 
     1212            win32_buffer[i].Char.AsciiChar = cache_char[i]; 
     1213            win32_buffer[i].Attributes = win32_fg_palette[cache_attr[i] & 0xf] 
     1214                                       | win32_bg_palette[cache_attr[i] >> 4]; 
    12271215        } 
    12281216 
     
    13191307        _caca_width = x11_new_width; 
    13201308        _caca_height = x11_new_height; 
    1321  
    1322         free(x11_char); 
    1323         free(x11_attr); 
    13241309 
    13251310        new_pixmap = XCreatePixmap(x11_dpy, x11_window, 
     
    13331318        XFreePixmap(x11_dpy, x11_pixmap); 
    13341319        x11_pixmap = new_pixmap; 
    1335  
    1336         x11_char = malloc(_caca_width * _caca_height * sizeof(int)); 
    1337         memset(x11_char, 0, _caca_width * _caca_height * sizeof(int)); 
    1338         x11_attr = malloc(_caca_width * _caca_height * sizeof(int)); 
    1339         memset(x11_attr, 0, _caca_width * _caca_height * sizeof(int)); 
    13401320    } 
    13411321    else 
     
    13491329    { 
    13501330        /* Dummy */ 
     1331    } 
     1332 
     1333    if(_caca_width != old_width || _caca_height != old_height) 
     1334    { 
     1335        free(cache_char); 
     1336        free(cache_attr); 
     1337 
     1338        cache_char = malloc(_caca_width * _caca_height * sizeof(uint8_t)); 
     1339        memset(cache_char, 0, _caca_width * _caca_height * sizeof(uint8_t)); 
     1340        cache_attr = malloc(_caca_width * _caca_height * sizeof(uint8_t)); 
     1341        memset(cache_attr, 0, _caca_width * _caca_height * sizeof(uint8_t)); 
    13511342    } 
    13521343 
Note: See TracChangeset for help on using the changeset viewer.