source: libcaca/trunk/src/caca.c @ 249

Last change on this file since 249 was 249, checked in by Sam Hocevar, 19 years ago
  • src/bitmap.c: + Added antialiasing support. + Dithering, antialiasing and background mode can now be selected at

runtime.

  • src/caca.c src/caca.h: + Renamed caca_dithering into caca_feature and extended the type to

express background colour and antialiasing mode.

  • examples/demo.c: + Fixed a bug that disabled the sprite demo. + Draw solid flares.
  • examples/view.c: + Select antialiasing and background mode at runtime.
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1/*
2 *   libcaca       ASCII-Art library
3 *   Copyright (c) 2002, 2003 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 GNU Lesser General Public
8 *   License as published by the Free Software Foundation; either
9 *   version 2 of the License, or (at your option) any later version.
10 *
11 *   This library is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 *   Lesser General Public License for more details.
15 *
16 *   You should have received a copy of the GNU Lesser General Public
17 *   License along with this library; if not, write to the Free Software
18 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 *   02111-1307  USA
20 */
21
22/**  \file caca.c
23 *   \version \$Id: caca.c 249 2003-12-12 14:49:41Z sam $
24 *   \author Sam Hocevar <sam@zoy.org>
25 *   \brief Main \e libcaca functions
26 *
27 *   This file contains the main functions used by \e libcaca applications to
28 *   initialise the library, get the screen properties, set the framerate and
29 *   so on.
30 */
31
32#include "config.h"
33
34#if defined(USE_SLANG)
35#   include <slang.h>
36#elif defined(USE_NCURSES)
37#   include <curses.h>
38#elif defined(USE_CONIO)
39#   include <dos.h>
40#   include <conio.h>
41#else
42#   error "no graphics library detected"
43#endif
44
45#include <stdlib.h>
46#include <string.h>
47
48#include "caca.h"
49#include "caca_internals.h"
50
51static void caca_init_features(void);
52static void caca_init_terminal(void);
53
54#if defined(USE_NCURSES)
55static mmask_t oldmask;
56#endif
57
58int caca_init(void)
59{
60#if defined(USE_NCURSES)
61    mmask_t newmask;
62#endif
63
64    caca_init_features();
65    caca_init_terminal();
66
67#if defined(USE_SLANG)
68    /* Initialize slang library */
69    SLsig_block_signals();
70    SLtt_get_terminfo();
71
72    if(SLkp_init() == -1)
73    {
74        SLsig_unblock_signals();
75        return -1;
76    }
77
78    SLang_init_tty(-1, 0, 1);
79
80    if(SLsmg_init_smg() == -1)
81    {
82        SLsig_unblock_signals();
83        return -1;
84    }
85
86    SLsig_unblock_signals();
87
88    SLsmg_cls();
89    SLtt_set_cursor_visibility(0);
90    SLkp_define_keysym("\e[M", 1001);
91    SLtt_set_mouse_mode(1, 0);
92    SLsmg_refresh();
93
94    /* Disable scrolling so that hashmap scrolling optimization code
95     * does not cause ugly refreshes due to slow terminals */
96    SLtt_Term_Cannot_Scroll = 1;
97
98#elif defined(USE_NCURSES)
99    initscr();
100    keypad(stdscr, TRUE);
101    nonl();
102    cbreak();
103    noecho();
104    nodelay(stdscr, TRUE);
105    curs_set(0);
106
107    /* Activate mouse */
108    newmask = ALL_MOUSE_EVENTS;
109    mousemask(newmask, &oldmask);
110
111#elif defined(USE_CONIO)
112    _wscroll = 0;
113    _setcursortype(_NOCURSOR);
114    clrscr();
115
116#endif
117    if(_caca_init_graphics())
118        return -1;
119
120    return 0;
121}
122
123unsigned int caca_get_width(void)
124{
125    return _caca_width;
126}
127
128unsigned int caca_get_height(void)
129{
130    return _caca_height;
131}
132
133const char *caca_get_color_name(enum caca_color color)
134{
135    static const char *color_names[] =
136    {
137        "black",
138        "blue",
139        "green",
140        "cyan",
141        "red",
142        "magenta",
143        "brown",
144        "light gray",
145        "dark gray",
146        "light blue",
147        "light green",
148        "light cyan",
149        "light red",
150        "light magenta",
151        "yellow",
152        "white",
153    };
154
155    if(color < 0 || color > 15)
156        return "unknown";
157
158    return color_names[color];
159}
160
161enum caca_feature caca_get_feature(enum caca_feature feature)
162{
163    switch(feature)
164    {
165        case CACA_BACKGROUND:
166            return _caca_background;
167        case CACA_ANTIALIASING:
168            return _caca_antialiasing;
169        case CACA_DITHERING:
170            return _caca_dithering;
171
172        default:
173            return CACA_UNKNOWN_FEATURE;
174    }
175}
176
177void caca_set_feature(enum caca_feature feature)
178{
179    switch(feature)
180    {
181        case CACA_BACKGROUND:
182            feature = CACA_BACKGROUND_SOLID;
183        case CACA_BACKGROUND_BLACK:
184        case CACA_BACKGROUND_SOLID:
185            _caca_background = feature;
186            break;
187
188        case CACA_ANTIALIASING:
189            feature = CACA_ANTIALIASING_PREFILTER;
190        case CACA_ANTIALIASING_NONE:
191        case CACA_ANTIALIASING_PREFILTER:
192            _caca_antialiasing = feature;
193            break;
194
195        case CACA_DITHERING:
196            feature = CACA_DITHERING_ORDERED4;
197        case CACA_DITHERING_NONE:
198        case CACA_DITHERING_ORDERED2:
199        case CACA_DITHERING_ORDERED4:
200        case CACA_DITHERING_ORDERED8:
201        case CACA_DITHERING_RANDOM:
202            _caca_dithering = feature;
203            break;
204
205        case CACA_UNKNOWN_FEATURE:
206            break;
207    }
208}
209
210const char *caca_get_feature_name(enum caca_feature feature)
211{
212    switch(feature)
213    {
214        case CACA_BACKGROUND_BLACK: return "black background";
215        case CACA_BACKGROUND_SOLID: return "solid background";
216
217        case CACA_ANTIALIASING_NONE:      return "no antialiasing";
218        case CACA_ANTIALIASING_PREFILTER: return "prefilter antialiasing";
219
220        case CACA_DITHERING_NONE:     return "no dithering";
221        case CACA_DITHERING_ORDERED2: return "2x2 ordered dithering";
222        case CACA_DITHERING_ORDERED4: return "4x4 ordered dithering";
223        case CACA_DITHERING_ORDERED8: return "8x8 ordered dithering";
224        case CACA_DITHERING_RANDOM:   return "random dithering";
225
226        default: return "unknown";
227    }
228}
229
230void caca_end(void)
231{
232#if defined(USE_SLANG)
233    SLtt_set_mouse_mode(0, 0);
234    SLtt_set_cursor_visibility(1);
235    SLang_reset_tty();
236    SLsmg_reset_smg();
237#elif defined(USE_NCURSES)
238    mousemask(oldmask, NULL);
239    curs_set(1);
240    endwin();
241#elif defined(USE_CONIO)
242    _wscroll = 1;
243    textcolor((enum COLORS)WHITE);
244    textbackground((enum COLORS)BLACK);
245    gotoxy(_caca_width, _caca_height);
246    cputs("\r\n");
247    _setcursortype(_NORMALCURSOR);
248#endif
249}
250
251static void caca_init_features(void)
252{
253    caca_set_feature(CACA_BACKGROUND);
254    caca_set_feature(CACA_ANTIALIASING);
255    caca_set_feature(CACA_DITHERING);
256}
257
258static void caca_init_terminal(void)
259{
260#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
261    char *term, *colorterm, *other;
262
263    term = getenv("TERM");
264    colorterm = getenv("COLORTERM");
265
266    if(term && !strcmp(term, "xterm"))
267    {
268        /* If we are using gnome-terminal, it's really a 16 colour terminal */
269        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
270        {
271#if defined(USE_NCURSES)
272            SCREEN *screen;
273            screen = newterm("xterm-16color", stdout, stdin);
274            if(screen == NULL)
275                return;
276            endwin();
277#endif
278            (void)putenv("TERM=xterm-16color");
279            return;
280        }
281
282        /* Ditto if we are using Konsole */
283        other = getenv("KONSOLE_DCOP_SESSION");
284        if(other)
285        {
286#if defined(USE_NCURSES)
287            SCREEN *screen;
288            screen = newterm("xterm-16color", stdout, stdin);
289            if(screen == NULL)
290                return;
291            endwin();
292#endif
293            (void)putenv("TERM=xterm-16color");
294            return;
295        }
296    }
297#endif
298}
299
Note: See TracBrowser for help on using the repository browser.