Changeset 552


Ignore:
Timestamp:
Mar 8, 2006, 6:20:38 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • cucul_set_size() does nothing if a graphical backend is attached. Added documentation to explain this behaviour.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/cucul.c

    r540 r552  
    2727#include "cucul_internals.h"
    2828
    29 static void cucul_read_environment(cucul_t *qq);
     29static void cucul_read_environment(cucul_t *);
     30void _cucul_set_size(cucul_t *, unsigned int, unsigned int);
    3031
    3132/** \brief Initialise \e libcucul.
     
    7576}
    7677
    77 /** \brief Set the screen size.
    78  *
    79  *  This function sets the screen width and height, in character cells.
    80  *
    81  *  \param width The desired screen width
    82  *  \param height The desired screen height
     78/** \brief Set the canvas size.
     79 *
     80 *  This function sets the canvas width and height, in character cells.
     81 *
     82 *  It is an error to try to resize the canvas if an output driver has
     83 *  been attached to the canvas using caca_attach(). You need to remove
     84 *  the output driver using caca_detach() before you can change the
     85 *  canvas size again.
     86 *
     87 *  However, the caca output driver can cause a canvas resize through
     88 *  user interaction. See the caca_event() documentation for more about
     89 *  this.
     90 *
     91 *  \param width The desired canvas width
     92 *  \param height The desired canvas height
    8393 */
    8494void cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
    8595{
    86     unsigned int x, y, old_width, old_height, new_size, old_size;
    87 
    88     old_width = qq->width;
    89     old_height = qq->height;
    90     old_size = old_width * old_height;
    91 
    92     qq->width = width;
    93     qq->height = height;
    94     new_size = width * height;
    95 
    96     /* Step 1: if new area is bigger, resize the memory area now. */
    97     if(new_size > old_size)
    98     {
    99         qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
    100         qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
    101     }
    102 
    103     /* Step 2: move line data if necessary. */
    104     if(width == old_width)
    105     {
    106         /* Width did not change, which means we do not need to move data. */
    107         ;
    108     }
    109     else if(width > old_width)
    110     {
    111         /* New width is bigger than old width, which means we need to
    112          * copy lines starting from the bottom of the screen otherwise
    113          * we will overwrite information. */
    114         for(y = height < old_height ? height : old_height; y--; )
    115         {
    116             for(x = old_width; x--; )
    117             {
    118                 qq->chars[y * width + x] = qq->chars[y * old_width + x];
    119                 qq->attr[y * width + x] = qq->attr[y * old_width + x];
    120             }
    121 
    122             /* Zero the end of the line */
    123             for(x = width - old_width; x--; )
    124                 qq->chars[y * width + old_width + x] = (uint32_t)' ';
    125             memset(qq->attr + y * width + old_width, 0,
    126                    width - old_width);
    127         }
    128     }
    129     else
    130     {
    131         /* New width is smaller. Copy as many lines as possible. Ignore
    132          * the first line, it is already in place. */
    133         unsigned int lines = height < old_height ? height : old_height;
    134 
    135         for(y = 1; y < lines; y++)
    136         {
    137             for(x = 0; x < width; x++)
    138             {
    139                 qq->chars[y * width + x] = qq->chars[y * old_width + x];
    140                 qq->attr[y * width + x] = qq->attr[y * old_width + x];
    141             }
    142         }
    143     }
    144 
    145     /* Step 3: fill the bottom of the new screen if necessary. */
    146     if(height > old_height)
    147     {
    148         /* Zero the bottom of the screen */
    149         for(x = (height - old_height) * width; x--; )
    150             qq->chars[old_height * width + x] = (uint32_t)' ';
    151         memset(qq->attr + old_height * width, 0,
    152                (height - old_height) * width);
    153     }
    154 
    155     /* Step 4: if new area is smaller, resize memory area now. */
    156     if(new_size <= old_size)
    157     {
    158         qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
    159         qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
    160     }
    161 
    162     /* Recompute the scratch line and the empty line */
    163     if(width != old_width)
    164     {
    165         qq->empty_line = realloc(qq->empty_line, width + 1);
    166         memset(qq->empty_line, ' ', width);
    167         qq->empty_line[width] = '\0';
    168 
    169         qq->scratch_line = realloc(qq->scratch_line, width + 1);
    170     }
    171 }
    172 
    173 /** \brief Get the screen width.
    174  *
    175  *  This function returns the current screen width, in character cells.
    176  *
    177  *  \return The screen width.
     96    if(qq->refcount)
     97        return;
     98
     99    cucul_set_size_internal(qq, width, height);
     100}
     101
     102/** \brief Get the canvas width.
     103 *
     104 *  This function returns the current canvas width, in character cells.
     105 *
     106 *  \return The canvas width.
    178107 */
    179108unsigned int cucul_get_width(cucul_t *qq)
     
    182111}
    183112
    184 /** \brief Get the screen height.
    185  *
    186  *  This function returns the current screen height, in character cells.
    187  *
    188  *  \return The screen height.
     113/** \brief Get the canvas height.
     114 *
     115 *  This function returns the current canvas height, in character cells.
     116 *
     117 *  \return The canvas height.
    189118 */
    190119unsigned int cucul_get_height(cucul_t *qq)
     
    395324}
    396325
     326void _cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
     327{
     328    unsigned int x, y, old_width, old_height, new_size, old_size;
     329
     330    old_width = qq->width;
     331    old_height = qq->height;
     332    old_size = old_width * old_height;
     333
     334    qq->width = width;
     335    qq->height = height;
     336    new_size = width * height;
     337
     338    /* Step 1: if new area is bigger, resize the memory area now. */
     339    if(new_size > old_size)
     340    {
     341        qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
     342        qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
     343    }
     344
     345    /* Step 2: move line data if necessary. */
     346    if(width == old_width)
     347    {
     348        /* Width did not change, which means we do not need to move data. */
     349        ;
     350    }
     351    else if(width > old_width)
     352    {
     353        /* New width is bigger than old width, which means we need to
     354         * copy lines starting from the bottom of the screen otherwise
     355         * we will overwrite information. */
     356        for(y = height < old_height ? height : old_height; y--; )
     357        {
     358            for(x = old_width; x--; )
     359            {
     360                qq->chars[y * width + x] = qq->chars[y * old_width + x];
     361                qq->attr[y * width + x] = qq->attr[y * old_width + x];
     362            }
     363
     364            /* Zero the end of the line */
     365            for(x = width - old_width; x--; )
     366                qq->chars[y * width + old_width + x] = (uint32_t)' ';
     367            memset(qq->attr + y * width + old_width, 0,
     368                   width - old_width);
     369        }
     370    }
     371    else
     372    {
     373        /* New width is smaller. Copy as many lines as possible. Ignore
     374         * the first line, it is already in place. */
     375        unsigned int lines = height < old_height ? height : old_height;
     376
     377        for(y = 1; y < lines; y++)
     378        {
     379            for(x = 0; x < width; x++)
     380            {
     381                qq->chars[y * width + x] = qq->chars[y * old_width + x];
     382                qq->attr[y * width + x] = qq->attr[y * old_width + x];
     383            }
     384        }
     385    }
     386
     387    /* Step 3: fill the bottom of the new screen if necessary. */
     388    if(height > old_height)
     389    {
     390        /* Zero the bottom of the screen */
     391        for(x = (height - old_height) * width; x--; )
     392            qq->chars[old_height * width + x] = (uint32_t)' ';
     393        memset(qq->attr + old_height * width, 0,
     394               (height - old_height) * width);
     395    }
     396
     397    /* Step 4: if new area is smaller, resize memory area now. */
     398    if(new_size <= old_size)
     399    {
     400        qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
     401        qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
     402    }
     403
     404    /* Recompute the scratch line and the empty line */
     405    if(width != old_width)
     406    {
     407        qq->empty_line = realloc(qq->empty_line, width + 1);
     408        memset(qq->empty_line, ' ', width);
     409        qq->empty_line[width] = '\0';
     410
     411        qq->scratch_line = realloc(qq->scratch_line, width + 1);
     412    }
     413}
     414
Note: See TracChangeset for help on using the changeset viewer.