source: libcaca/trunk/cucul/cucul.c @ 819

Last change on this file since 819 was 819, checked in by Sam Hocevar, 15 years ago
  • More function mass-renaming: caca_attach -> caca_create_display, etc.
  • Property svn:keywords set to Id
File size: 9.8 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: cucul.c 819 2006-04-19 10:10:58Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains the main functions used by \e libcucul applications
16 *  to initialise a drawing context.
17 */
18
19#include "config.h"
20
21#if !defined(__KERNEL__)
22#   include <stdio.h>
23#   include <stdlib.h>
24#   include <string.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30/** \brief Initialise a \e libcucul canvas.
31 *
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.
36 *
37 *  If one of the desired canvas coordinates is zero, a default canvas size
38 *  of 80x32 is used instead.
39 *
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 */
44cucul_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.
79 *
80 *  \param data The memory area to be loaded into a canvas.
81 *  \param size The length of the memory area.
82 *  \return A libcucul canvas, or NULL in case of error.
83 */
84cucul_canvas_t *cucul_load_canvas(void *data, unsigned int size)
85{
86    cucul_canvas_t *cv;
87    uint8_t *buf = (uint8_t *)data;
88    unsigned int width, height, n;
89
90    if(size < 16)
91        return NULL;
92
93    if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
94        return NULL;
95
96    if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
97        return NULL;
98
99    width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
100           | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
101    height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
102            | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
103
104    if(!width || !height)
105        return NULL;
106
107    if(size != 16 + width * height * 8)
108        return NULL;
109
110    cv = cucul_create_canvas(width, height);
111
112    if(!cv)
113        return NULL;
114
115    for(n = height * width; n--; )
116    {
117        cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
118                     | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
119                     | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
120                     | (uint32_t)buf[16 + 3 + 8 * n];
121        cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
122                    | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
123                    | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
124                    | (uint32_t)buf[16 + 7 + 8 * n];
125    }
126
127    return cv;
128}
129
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 new
135 *  canvas size. Newly allocated character cells at the right and/or at
136 *  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 has
139 *  been attached to the canvas using caca_create_display(). You need to
140 *  remove the output driver using caca_free_display() before you can change
141 *  the  canvas size again. However, the caca output driver can cause a
142 *  canvas resize through user interaction. See the caca_event() documentation
143 *  for more about this.
144 *
145 *  \param cv A libcucul canvas
146 *  \param width The desired canvas width
147 *  \param height The desired canvas height
148 */
149void 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 canvas
163 *  \return The canvas width.
164 */
165unsigned 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 canvas
175 *  \return The canvas height.
176 */
177unsigned 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-readable
185 *  description string of the associated colour.
186 *
187 *  \param color The colour value.
188 *  \return A static string containing the colour's name.
189 */
190char 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(). After
221 *  cucul_free_canvas() has been called, no other \e libcucul functions may be
222 *  used unless a new call to cucul_create_canvas() is done.
223 *
224 *  \param cv A libcucul canvas
225 */
226void 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 - 1
244 *  (inclusive).
245 */
246int 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
255void _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 to
284         * copy lines starting from the bottom of the screen otherwise
285         * 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    else
302    {
303        /* New width is smaller. Copy as many lines as possible. Ignore
304         * 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 TracBrowser for help on using the repository browser.