Changeset 826 for libcaca/trunk/cucul/import.c
- Timestamp:
- Apr 21, 2006, 8:44:04 PM (16 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/cucul/import.c
r823 r826 13 13 14 14 /* 15 * This file contains the main functions used by \e libcucul applications 16 * to initialise a drawing context. 15 * This file contains various import functions. 17 16 */ 18 17 … … 28 27 #include "cucul_internals.h" 29 28 30 /** \brief Initialise a \e libcucul canvas. 29 static cucul_canvas_t *import_caca(void const *, unsigned int); 30 31 /** \brief Import a buffer into a canvas 31 32 * 32 * This function initialises internal \e libcucul structures and the backend 33 * that will be used for subsequent graphical operations. It must be the 34 * first \e libcucul function to be called in a function. cucul_free_canvas() 35 * should be called at the end of the program to free all allocated resources. 33 * This function imports a memory area into an internal libcucul canvas. 36 34 * 37 * If one of the desired canvas coordinates is zero, a default canvas size 38 * of 80x32 is used instead. 35 * Valid values for \c format are: 39 36 * 40 * \param width The desired canvas width 41 * \param height The desired canvas height 42 * \return A libcucul canvas handle upon success, NULL if an error occurred. 43 */ 44 cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height) 45 { 46 cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t)); 47 48 cv->refcount = 0; 49 50 cv->fgcolor = CUCUL_COLOR_LIGHTGRAY; 51 cv->bgcolor = CUCUL_COLOR_BLACK; 52 53 cv->width = cv->height = 0; 54 cv->chars = NULL; 55 cv->attr = NULL; 56 cv->empty_line = cv->scratch_line = NULL; 57 58 /* Initialise to a default size. 80x32 is arbitrary but matches AAlib's 59 * default X11 window. When a graphic driver attaches to us, it can set 60 * a different size. */ 61 if(width && height) 62 _cucul_set_canvas_size(cv, width, height); 63 else 64 _cucul_set_canvas_size(cv, 80, 32); 65 66 if(_cucul_init_dither()) 67 { 68 free(cv); 69 return NULL; 70 } 71 72 return cv; 73 } 74 75 /** \brief Load a memory area into a canvas. 76 * 77 * This function loads a memory area containing an exported canvas into 78 * a new \e libcucul canvas. 37 * \li \c "caca": import native libcaca files. 79 38 * 80 39 * \param data The memory area to be loaded into a canvas. 81 40 * \param size The length of the memory area. 41 * \param format A string describing the input format. 82 42 * \return A libcucul canvas, or NULL in case of error. 83 43 */ 84 cucul_canvas_t *cucul_load_canvas(void *data, unsigned int size) 44 cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size, 45 char const *format) 46 { 47 if(!strcasecmp("caca", format)) 48 return import_caca(data, size); 49 50 /* FIXME: Try to autodetect */ 51 if(!strcasecmp("", format)) 52 return import_caca(data, size); 53 54 return NULL; 55 } 56 57 /** \brief Get available import formats 58 * 59 * Return a list of available import formats. The list is a NULL-terminated 60 * array of strings, interleaving a string containing the internal value for 61 * the import format, to be used with cucul_import_canvas(), and a string 62 * containing the natural language description for that import format. 63 * 64 * \return An array of strings. 65 */ 66 char const * const * cucul_get_import_list(void) 67 { 68 static char const * const list[] = 69 { 70 "", "autodetect", 71 "caca", "native libcaca format", 72 NULL, NULL 73 }; 74 75 return list; 76 } 77 78 /* 79 * XXX: the following functions are local. 80 */ 81 82 static cucul_canvas_t *import_caca(void const *data, unsigned int size) 85 83 { 86 84 cucul_canvas_t *cv; … … 128 126 } 129 127 130 /** \brief Resize a canvas.131 *132 * This function sets the canvas width and height, in character cells.133 *134 * The contents of the canvas are preserved to the extent of the new135 * canvas size. Newly allocated character cells at the right and/or at136 * the bottom of the canvas are filled with spaces.137 *138 * It is an error to try to resize the canvas if an output driver has139 * been attached to the canvas using caca_create_display(). You need to140 * remove the output driver using caca_free_display() before you can change141 * the canvas size again. However, the caca output driver can cause a142 * canvas resize through user interaction. See the caca_event() documentation143 * for more about this.144 *145 * \param cv A libcucul canvas146 * \param width The desired canvas width147 * \param height The desired canvas height148 */149 void cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,150 unsigned int height)151 {152 if(cv->refcount)153 return;154 155 _cucul_set_canvas_size(cv, width, height);156 }157 158 /** \brief Get the canvas width.159 *160 * This function returns the current canvas width, in character cells.161 *162 * \param cv A libcucul canvas163 * \return The canvas width.164 */165 unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)166 {167 return cv->width;168 }169 170 /** \brief Get the canvas height.171 *172 * This function returns the current canvas height, in character cells.173 *174 * \param cv A libcucul canvas175 * \return The canvas height.176 */177 unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)178 {179 return cv->height;180 }181 182 /** \brief Translate a colour index into the colour's name.183 *184 * This function translates a cucul_color enum into a human-readable185 * description string of the associated colour.186 *187 * \param color The colour value.188 * \return A static string containing the colour's name.189 */190 char const *cucul_get_color_name(unsigned int color)191 {192 static char const *color_names[] =193 {194 "black",195 "blue",196 "green",197 "cyan",198 "red",199 "magenta",200 "brown",201 "light gray",202 "dark gray",203 "light blue",204 "light green",205 "light cyan",206 "light red",207 "light magenta",208 "yellow",209 "white",210 };211 212 if(color < 0 || color > 15)213 return "unknown";214 215 return color_names[color];216 }217 218 /** \brief Uninitialise \e libcucul.219 *220 * This function frees all resources allocated by cucul_create_canvas(). After221 * cucul_free_canvas() has been called, no other \e libcucul functions may be222 * used unless a new call to cucul_create_canvas() is done.223 *224 * \param cv A libcucul canvas225 */226 void cucul_free_canvas(cucul_canvas_t *cv)227 {228 _cucul_end_dither();229 230 free(cv->empty_line);231 free(cv->scratch_line);232 233 free(cv->chars);234 free(cv->attr);235 236 free(cv);237 }238 239 /** \brief Generate a random integer within a range.240 *241 * \param min The lower bound of the integer range.242 * \param max The upper bound of the integer range.243 * \return A random integer comprised between \p min and \p max - 1244 * (inclusive).245 */246 int cucul_rand(int min, int max)247 {248 return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));249 }250 251 /*252 * XXX: The following functions are local.253 */254 255 void _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,256 unsigned int height)257 {258 unsigned int x, y, old_width, old_height, new_size, old_size;259 260 old_width = cv->width;261 old_height = cv->height;262 old_size = old_width * old_height;263 264 cv->width = width;265 cv->height = height;266 new_size = width * height;267 268 /* Step 1: if new area is bigger, resize the memory area now. */269 if(new_size > old_size)270 {271 cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));272 cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));273 }274 275 /* Step 2: move line data if necessary. */276 if(width == old_width)277 {278 /* Width did not change, which means we do not need to move data. */279 ;280 }281 else if(width > old_width)282 {283 /* New width is bigger than old width, which means we need to284 * copy lines starting from the bottom of the screen otherwise285 * we will overwrite information. */286 for(y = height < old_height ? height : old_height; y--; )287 {288 for(x = old_width; x--; )289 {290 cv->chars[y * width + x] = cv->chars[y * old_width + x];291 cv->attr[y * width + x] = cv->attr[y * old_width + x];292 }293 294 /* Zero the end of the line */295 for(x = width - old_width; x--; )296 cv->chars[y * width + old_width + x] = (uint32_t)' ';297 memset(cv->attr + y * width + old_width, 0,298 (width - old_width) * 4);299 }300 }301 else302 {303 /* New width is smaller. Copy as many lines as possible. Ignore304 * the first line, it is already in place. */305 unsigned int lines = height < old_height ? height : old_height;306 307 for(y = 1; y < lines; y++)308 {309 for(x = 0; x < width; x++)310 {311 cv->chars[y * width + x] = cv->chars[y * old_width + x];312 cv->attr[y * width + x] = cv->attr[y * old_width + x];313 }314 }315 }316 317 /* Step 3: fill the bottom of the new screen if necessary. */318 if(height > old_height)319 {320 /* Zero the bottom of the screen */321 for(x = (height - old_height) * width; x--; )322 cv->chars[old_height * width + x] = (uint32_t)' ';323 memset(cv->attr + old_height * width, 0,324 (height - old_height) * width * 4);325 }326 327 /* Step 4: if new area is smaller, resize memory area now. */328 if(new_size <= old_size)329 {330 cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));331 cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));332 }333 334 /* Recompute the scratch line and the empty line */335 if(width != old_width)336 {337 cv->empty_line = realloc(cv->empty_line, width + 1);338 memset(cv->empty_line, ' ', width);339 cv->empty_line[width] = '\0';340 341 cv->scratch_line = realloc(cv->scratch_line, width + 1);342 }343 }344
Note: See TracChangeset
for help on using the changeset viewer.