source: libcaca/trunk/src/cucul.c @ 525

Last change on this file since 525 was 525, checked in by Sam Hocevar, 16 years ago
  • src/cucul.c: s/width/height/.
  • Property svn:keywords set to Id
File size: 8.9 KB
Line 
1/*
2 *  libcucul      Unicode canvas library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file cucul.c
13 *  \version \$Id: cucul.c 525 2006-03-06 08:22:09Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Main \e libcucul functions
16 *
17 *  This file contains the main functions used by \e libcucul applications
18 *  to initialise a drawing context.
19 */
20
21#include "config.h"
22
23#if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME)
24#   include <inttypes.h>
25#else
26typedef unsigned char uint8_t;
27#endif
28
29#include <stdlib.h>
30#include <string.h>
31
32#include "cucul.h"
33#include "cucul_internals.h"
34
35static void cucul_init_features(cucul_t *qq);
36
37/** \brief Initialise \e libcucul.
38 *
39 *  This function initialises internal \e libcucul structures and the backend
40 *  that will be used for subsequent graphical operations. It must be the
41 *  first \e libcucul function to be called in a function. cucul_end() should
42 *  be called at the end of the program to free all allocated resources.
43 *
44 *  \return 0 upon success, a non-zero value if an error occurs.
45 */
46cucul_t * cucul_init(void)
47{
48    cucul_t *qq = malloc(sizeof(cucul_t));
49
50    cucul_init_features(qq);
51
52    qq->fgcolor = CUCUL_COLOR_LIGHTGRAY;
53    qq->bgcolor = CUCUL_COLOR_BLACK;
54#if defined(OPTIMISE_SLANG_PALETTE)
55    qq->fgisbg = 0;
56#endif
57
58    /* Initialise to a default size. If a graphic driver attaches to
59     * us before cucul_set_size is called, we'll adapt. */
60    qq->width = 80;
61    qq->height = 32;
62    qq->size_set = 0;
63
64    qq->chars = malloc(qq->width * qq->height * sizeof(uint8_t));
65    qq->attr = malloc(qq->width * qq->height * sizeof(uint8_t));
66
67    memset(qq->chars, 0, qq->width * qq->height * sizeof(uint8_t));
68    memset(qq->attr, 0, qq->width * qq->height * sizeof(uint8_t));
69
70    qq->empty_line = malloc(qq->width + 1);
71    memset(qq->empty_line, ' ', qq->width);
72    qq->empty_line[qq->width] = '\0';
73
74    qq->scratch_line = malloc(qq->width + 1);
75
76    if(_cucul_init_bitmap())
77    {
78        free(qq);
79        return NULL;
80    }
81
82    return qq;
83}
84
85/** \brief Set the screen size.
86 *
87 *  This function sets the screen width and height, in character cells.
88 *
89 *  \param width The desired screen width
90 *  \param height The desired screen height
91 */
92void cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
93{
94    qq->width = width;
95    qq->height = height;
96    qq->size_set = 1;
97}
98
99/** \brief Get the screen width.
100 *
101 *  This function returns the current screen width, in character cells.
102 *
103 *  \return The screen width.
104 */
105unsigned int cucul_get_width(cucul_t *qq)
106{
107    return qq->width;
108}
109
110/** \brief Get the screen height.
111 *
112 *  This function returns the current screen height, in character cells.
113 *
114 *  \return The screen height.
115 */
116unsigned int cucul_get_height(cucul_t *qq)
117{
118    return qq->height;
119}
120
121/** \brief Translate a colour index into the colour's name.
122 *
123 *  This function translates a caca_color enum into a human-readable
124 *  description string of the associated colour.
125 *
126 *  \param color The colour value.
127 *  \return A static string containing the colour's name.
128 */
129char const *cucul_get_color_name(enum cucul_color color)
130{
131    static char const *color_names[] =
132    {
133        "black",
134        "blue",
135        "green",
136        "cyan",
137        "red",
138        "magenta",
139        "brown",
140        "light gray",
141        "dark gray",
142        "light blue",
143        "light green",
144        "light cyan",
145        "light red",
146        "light magenta",
147        "yellow",
148        "white",
149    };
150
151    if(color < 0 || color > 15)
152        return "unknown";
153
154    return color_names[color];
155}
156
157/** \brief Get the current value of a feature.
158 *
159 *  This function retrieves the value of an internal \e libcaca feature. A
160 *  generic feature value is expected, such as CUCUL_ANTIALIASING.
161 *
162 *  \param feature The requested feature.
163 *  \return The current value of the feature or CUCUL_FEATURE_UNKNOWN if an
164 *          error occurred..
165 */
166enum cucul_feature cucul_get_feature(cucul_t *qq, enum cucul_feature feature)
167{
168    switch(feature)
169    {
170        case CUCUL_BACKGROUND:
171            return qq->background;
172        case CUCUL_ANTIALIASING:
173            return qq->antialiasing;
174        case CUCUL_DITHERING:
175            return qq->dithering;
176
177        default:
178            return CUCUL_FEATURE_UNKNOWN;
179    }
180}
181
182/** \brief Set a feature.
183 *
184 *  This function sets an internal \e libcaca feature such as the antialiasing
185 *  or dithering modes. If a specific feature such as CUCUL_DITHERING_RANDOM,
186 *  cucul_set_feature() will set it immediately. If a generic feature is given
187 *  instead, such as CUCUL_DITHERING, the default value will be used instead.
188 *
189 *  \param feature The requested feature.
190 */
191void cucul_set_feature(cucul_t *qq, enum cucul_feature feature)
192{
193    switch(feature)
194    {
195        case CUCUL_BACKGROUND:
196            feature = CUCUL_BACKGROUND_SOLID;
197        case CUCUL_BACKGROUND_BLACK:
198        case CUCUL_BACKGROUND_SOLID:
199            qq->background = feature;
200            break;
201
202        case CUCUL_ANTIALIASING:
203            feature = CUCUL_ANTIALIASING_PREFILTER;
204        case CUCUL_ANTIALIASING_NONE:
205        case CUCUL_ANTIALIASING_PREFILTER:
206            qq->antialiasing = feature;
207            break;
208
209        case CUCUL_DITHERING:
210            feature = CUCUL_DITHERING_FSTEIN;
211        case CUCUL_DITHERING_NONE:
212        case CUCUL_DITHERING_ORDERED2:
213        case CUCUL_DITHERING_ORDERED4:
214        case CUCUL_DITHERING_ORDERED8:
215        case CUCUL_DITHERING_RANDOM:
216        case CUCUL_DITHERING_FSTEIN:
217            qq->dithering = feature;
218            break;
219
220        case CUCUL_FEATURE_UNKNOWN:
221            break;
222    }
223}
224
225/** \brief Translate a feature value into the feature's name.
226 *
227 *  This function translates a cucul_feature enum into a human-readable
228 *  description string of the associated feature.
229 *
230 *  \param feature The feature value.
231 *  \return A static string containing the feature's name.
232 */
233char const *cucul_get_feature_name(enum cucul_feature feature)
234{
235    switch(feature)
236    {
237        case CUCUL_BACKGROUND_BLACK: return "black background";
238        case CUCUL_BACKGROUND_SOLID: return "solid background";
239
240        case CUCUL_ANTIALIASING_NONE:      return "no antialiasing";
241        case CUCUL_ANTIALIASING_PREFILTER: return "prefilter antialiasing";
242
243        case CUCUL_DITHERING_NONE:     return "no dithering";
244        case CUCUL_DITHERING_ORDERED2: return "2x2 ordered dithering";
245        case CUCUL_DITHERING_ORDERED4: return "4x4 ordered dithering";
246        case CUCUL_DITHERING_ORDERED8: return "8x8 ordered dithering";
247        case CUCUL_DITHERING_RANDOM:   return "random dithering";
248        case CUCUL_DITHERING_FSTEIN:   return "Floyd-Steinberg dithering";
249
250        default: return "unknown";
251    }
252}
253
254/** \brief Uninitialise \e libcucul.
255 *
256 *  This function frees all resources allocated by cucul_init(). After
257 *  cucul_end() has been called, no other \e libcucul functions may be used
258 *  unless a new call to cucul_init() is done.
259 */
260void cucul_end(cucul_t *qq)
261{
262    _cucul_end_bitmap();
263
264    free(qq->empty_line);
265    free(qq->scratch_line);
266
267    free(qq->chars);
268    free(qq->attr);
269
270    free(qq);
271}
272
273/*
274 * XXX: The following functions are local.
275 */
276
277static void cucul_init_features(cucul_t * qq)
278{
279    /* FIXME: if strcasecmp isn't available, use strcmp */
280#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
281    char *var;
282#endif
283
284    cucul_set_feature(qq, CUCUL_BACKGROUND);
285    cucul_set_feature(qq, CUCUL_ANTIALIASING);
286    cucul_set_feature(qq, CUCUL_DITHERING);
287
288#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
289    if((var = getenv("CUCUL_BACKGROUND")) && *var)
290    {
291        if(!strcasecmp("black", var))
292            cucul_set_feature(qq, CUCUL_BACKGROUND_BLACK);
293        else if(!strcasecmp("solid", var))
294            cucul_set_feature(qq, CUCUL_BACKGROUND_SOLID);
295    }
296
297    if((var = getenv("CUCUL_ANTIALIASING")) && *var)
298    {
299        if(!strcasecmp("none", var))
300            cucul_set_feature(qq, CUCUL_ANTIALIASING_NONE);
301        else if(!strcasecmp("prefilter", var))
302            cucul_set_feature(qq, CUCUL_ANTIALIASING_PREFILTER);
303    }
304
305    if((var = getenv("CUCUL_DITHERING")) && *var)
306    {
307        if(!strcasecmp("none", var))
308            cucul_set_feature(qq, CUCUL_DITHERING_NONE);
309        else if(!strcasecmp("ordered2", var))
310            cucul_set_feature(qq, CUCUL_DITHERING_ORDERED2);
311        else if(!strcasecmp("ordered4", var))
312            cucul_set_feature(qq, CUCUL_DITHERING_ORDERED4);
313        else if(!strcasecmp("ordered8", var))
314            cucul_set_feature(qq, CUCUL_DITHERING_ORDERED8);
315        else if(!strcasecmp("random", var))
316            cucul_set_feature(qq, CUCUL_DITHERING_RANDOM);
317        else if(!strcasecmp("fstein", var))
318            cucul_set_feature(qq, CUCUL_DITHERING_FSTEIN);
319    }
320#endif
321}
322
Note: See TracBrowser for help on using the repository browser.