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

Last change on this file since 246 was 246, checked in by Sam Hocevar, 19 years ago
  • src/graphics.c: + Internally cache screen width and height.
  • src/bitmap.c: + Added alpha support to caca_draw_bitmap(). For now, we only treat 0%

alpha as fully transparent, and any other value as fully opaque.

+ Slightly tuned colour weights in the renderer.
+ caca_set_bitmap_palette() takes unsigned ints.

  • examples/demo.c: + Added a crap render demo.
  • examples/aafire.c: + Set a 20ms delay. + Added alpha values to the palette.
  • examples/view.c: + Draw a gray checkered grid below the image so that transparent images

look a lot nicer.

+ 'f' toggles "fullscreen" mode.

  • Property svn:keywords set to Id
File size: 5.3 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 246 2003-12-11 16:31:49Z 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_terminal(void);
52
53char *_caca_empty_line;
54char *_caca_scratch_line;
55
56#if defined(USE_NCURSES)
57static mmask_t oldmask;
58int _caca_attr[16*16];
59#endif
60
61#if defined(USE_CONIO)
62char *_caca_screen;
63#endif
64
65int caca_init(void)
66{
67#if defined(USE_NCURSES)
68    mmask_t newmask;
69#endif
70
71    caca_init_terminal();
72
73#if defined(USE_SLANG)
74    /* Initialize slang library */
75    SLsig_block_signals();
76    SLtt_get_terminfo();
77
78    if(SLkp_init() == -1)
79    {
80        SLsig_unblock_signals();
81        return -1;
82    }
83
84    SLang_init_tty(-1, 0, 1);
85
86    if(SLsmg_init_smg() == -1)
87    {
88        SLsig_unblock_signals();
89        return -1;
90    }
91
92    SLsig_unblock_signals();
93
94    SLsmg_cls();
95    SLtt_set_cursor_visibility(0);
96    SLkp_define_keysym("\e[M", 1001);
97    SLtt_set_mouse_mode(1, 0);
98    SLsmg_refresh();
99
100    /* Disable scrolling so that hashmap scrolling optimization code
101     * does not cause ugly refreshes due to slow terminals */
102    SLtt_Term_Cannot_Scroll = 1;
103
104#elif defined(USE_NCURSES)
105    initscr();
106    keypad(stdscr, TRUE);
107    nonl();
108    cbreak();
109    noecho();
110    nodelay(stdscr, TRUE);
111    curs_set(0);
112
113    /* Activate mouse */
114    newmask = ALL_MOUSE_EVENTS;
115    mousemask(newmask, &oldmask);
116
117#elif defined(USE_CONIO)
118    _wscroll = 0;
119    _setcursortype(_NOCURSOR);
120    clrscr();
121
122#endif
123    if(_caca_init_graphics())
124        return -1;
125
126    return 0;
127}
128
129unsigned int caca_get_width(void)
130{
131    return _caca_width;
132}
133
134unsigned int caca_get_height(void)
135{
136    return _caca_height;
137}
138
139const char *caca_get_color_name(enum caca_color color)
140{
141    static const char *color_names[] =
142    {
143        "black",
144        "blue",
145        "green",
146        "cyan",
147        "red",
148        "magenta",
149        "brown",
150        "light gray",
151        "dark gray",
152        "light blue",
153        "light green",
154        "light cyan",
155        "light red",
156        "light magenta",
157        "yellow",
158        "white",
159    };
160
161    if(color < 0 || color > 15)
162        return "unknown";
163
164    return color_names[color];
165}
166
167const char *caca_get_dithering_name(enum caca_dithering dithering)
168{
169    static const char *dithering_names[] =
170    {
171        "no",
172        "2x2 ordered",
173        "4x4 ordered",
174        "8x8 ordered",
175        "random"
176    };
177
178    if(dithering < 0 || dithering > 4)
179        return "unknown";
180
181    return dithering_names[dithering];
182}
183
184void caca_end(void)
185{
186#if defined(USE_SLANG)
187    SLtt_set_mouse_mode(0, 0);
188    SLtt_set_cursor_visibility(1);
189    SLang_reset_tty();
190    SLsmg_reset_smg();
191#elif defined(USE_NCURSES)
192    mousemask(oldmask, NULL);
193    curs_set(1);
194    endwin();
195#elif defined(USE_CONIO)
196    _wscroll = 1;
197    textcolor((enum COLORS)WHITE);
198    textbackground((enum COLORS)BLACK);
199    gotoxy(_caca_width, _caca_height);
200    cputs("\r\n");
201    _setcursortype(_NORMALCURSOR);
202#endif
203}
204
205static void caca_init_terminal(void)
206{
207#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
208    char *term, *colorterm, *other;
209
210    term = getenv("TERM");
211    colorterm = getenv("COLORTERM");
212
213    if(term && !strcmp(term, "xterm"))
214    {
215        /* If we are using gnome-terminal, it's really a 16 colour terminal */
216        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
217        {
218#if defined(USE_NCURSES)
219            SCREEN *screen;
220            screen = newterm("xterm-16color", stdout, stdin);
221            if(screen == NULL)
222                return;
223            endwin();
224#endif
225            (void)putenv("TERM=xterm-16color");
226            return;
227        }
228
229        /* Ditto if we are using Konsole */
230        other = getenv("KONSOLE_DCOP_SESSION");
231        if(other)
232        {
233#if defined(USE_NCURSES)
234            SCREEN *screen;
235            screen = newterm("xterm-16color", stdout, stdin);
236            if(screen == NULL)
237                return;
238            endwin();
239#endif
240            (void)putenv("TERM=xterm-16color");
241            return;
242        }
243    }
244#endif
245}
246
Note: See TracBrowser for help on using the repository browser.