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

Last change on this file since 193 was 193, checked in by Sam Hocevar, 18 years ago
  • src/blit.c: + Added caca_set_dithering() (unused yet). + Added random dithering.
  • src/caca.h: + Renamed legacy EE_* enums to CACA_*.
  • examples/demo.c examples/caca.txt: + Added a default sprite for libcaca.
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/*
2 *   libcaca       ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: caca.c 193 2003-11-16 15:20:17Z sam $
7 *
8 *   This library is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU Lesser General Public
10 *   License as published by the Free Software Foundation; either
11 *   version 2 of the License, or (at your option) any later version.
12 *
13 *   This library is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 *   Lesser General Public License for more details.
17 *
18 *   You should have received a copy of the GNU Lesser General Public
19 *   License along with this library; if not, write to the Free Software
20 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 *   02111-1307  USA
22 */
23
24#include "config.h"
25
26#if defined(USE_SLANG)
27#   include <slang.h>
28#elif defined(USE_NCURSES)
29#   include <curses.h>
30#elif defined(USE_CONIO)
31#   include <dos.h>
32#   include <conio.h>
33#   if defined(SCREENUPDATE_IN_PC_H)
34#       include <pc.h>
35#   endif
36#else
37#   error "no graphics library detected"
38#endif
39
40#include <stdlib.h>
41#include <unistd.h>
42#include <string.h>
43#include <sys/time.h>
44#include <time.h>
45
46#include "caca.h"
47#include "caca_internals.h"
48
49static unsigned int _caca_delay;
50static unsigned int _caca_rendertime;
51char *_caca_empty_line;
52char *_caca_scratch_line;
53
54#if defined(USE_NCURSES)
55int _caca_attr[16];
56#endif
57
58#if defined(USE_CONIO)
59static struct text_info ti;
60char *_caca_screen;
61#endif
62
63int caca_init(void)
64{
65#if defined(USE_SLANG)
66    static char *slang_colors[16] =
67    {
68        "black",
69        "blue",
70        "green",
71        "cyan",
72        "red",
73        "magenta",
74        "brown",
75        "lightgray",
76        "gray",
77        "brightblue",
78        "brightgreen",
79        "brightcyan",
80        "brightred",
81        "brightmagenta",
82        "yellow",
83        "white",
84    };
85
86    int i;
87
88    /* Initialize slang library */
89    SLsig_block_signals();
90    SLtt_get_terminfo();
91
92    if(SLkp_init() == -1)
93    {
94        SLsig_unblock_signals();
95        return -1;
96    }
97
98    SLang_init_tty(-1, 0, 1);
99
100    if(SLsmg_init_smg() == -1)
101    {
102        SLsig_unblock_signals();
103        return -1;
104    }
105
106    SLsig_unblock_signals();
107
108    SLsmg_cls();
109    SLtt_set_cursor_visibility(0);
110    SLsmg_refresh();
111
112    for(i = 0; i < 16; i++)
113        SLtt_set_color(i + 1, NULL, slang_colors[i], "black");
114
115#elif defined(USE_NCURSES)
116    int i;
117
118    initscr();
119    keypad(stdscr, TRUE);
120    nonl();
121    cbreak();
122    noecho();
123    nodelay(stdscr, TRUE);
124    curs_set(0);
125
126    start_color();
127
128    init_pair(1 + CACA_COLOR_BLACK,        COLOR_BLACK,   COLOR_BLACK);
129    init_pair(1 + CACA_COLOR_BLUE,         COLOR_BLUE,    COLOR_BLACK);
130    init_pair(1 + CACA_COLOR_GREEN,        COLOR_GREEN,   COLOR_BLACK);
131    init_pair(1 + CACA_COLOR_CYAN,         COLOR_CYAN,    COLOR_BLACK);
132    init_pair(1 + CACA_COLOR_RED,          COLOR_RED,     COLOR_BLACK);
133    init_pair(1 + CACA_COLOR_MAGENTA,      COLOR_MAGENTA, COLOR_BLACK);
134    init_pair(1 + CACA_COLOR_BROWN,        COLOR_YELLOW,  COLOR_BLACK);
135    init_pair(1 + CACA_COLOR_LIGHTGRAY,    COLOR_WHITE,   COLOR_BLACK);
136    init_pair(1 + CACA_COLOR_DARKGRAY,     COLOR_BLACK,   COLOR_BLACK);
137    init_pair(1 + CACA_COLOR_LIGHTBLUE,    COLOR_BLUE,    COLOR_BLACK);
138    init_pair(1 + CACA_COLOR_LIGHTGREEN,   COLOR_GREEN,   COLOR_BLACK);
139    init_pair(1 + CACA_COLOR_LIGHTCYAN,    COLOR_CYAN,    COLOR_BLACK);
140    init_pair(1 + CACA_COLOR_LIGHTRED,     COLOR_RED,     COLOR_BLACK);
141    init_pair(1 + CACA_COLOR_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK);
142    init_pair(1 + CACA_COLOR_YELLOW,       COLOR_YELLOW,  COLOR_BLACK);
143    init_pair(1 + CACA_COLOR_WHITE,        COLOR_WHITE,   COLOR_BLACK);
144
145    for(i = 0; i < 8; i++)
146    {
147        _caca_attr[i] = COLOR_PAIR(1 + i);
148        _caca_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i);
149    }
150
151#elif defined(USE_CONIO)
152    gettextinfo(&ti);
153    _caca_screen = malloc(2 * ti.screenwidth * ti.screenheight);
154    if(_caca_screen == NULL)
155        return -1;
156    _wscroll = 0;
157    _setcursortype(_NOCURSOR);
158    clrscr();
159#   if defined(SCREENUPDATE_IN_PC_H)
160    ScreenRetrieve(_caca_screen);
161#   else
162    /* FIXME */
163#   endif
164
165#endif
166    _caca_empty_line = malloc(caca_get_width() + 1);
167    memset(_caca_empty_line, ' ', caca_get_width());
168    _caca_empty_line[caca_get_width()] = '\0';
169
170    _caca_scratch_line = malloc(caca_get_width() + 1);
171
172    _caca_delay = 0;
173    _caca_rendertime = 0;
174
175    return 0;
176}
177
178unsigned int caca_get_width(void)
179{
180#if defined(USE_SLANG)
181    return SLtt_Screen_Cols;
182#elif defined(USE_NCURSES)
183    return COLS;
184#elif defined(USE_CONIO)
185    return ti.screenwidth;
186#endif
187}
188
189unsigned int caca_get_height(void)
190{
191#if defined(USE_SLANG)
192    return SLtt_Screen_Rows;
193#elif defined(USE_NCURSES)
194    return LINES;
195#else
196    return ti.screenheight;
197#endif
198}
199
200void caca_set_delay(unsigned int usec)
201{
202    _caca_delay = usec;
203}
204
205unsigned int caca_get_rendertime(void)
206{
207    return _caca_rendertime;
208}
209
210const char *caca_get_color_name(unsigned int color)
211{
212    static const char *color_names[16] =
213    {
214        "black",
215        "blue",
216        "green",
217        "cyan",
218        "red",
219        "magenta",
220        "brown",
221        "light gray",
222        "dark gray",
223        "light blue",
224        "light green",
225        "light cyan",
226        "light red",
227        "light magenta",
228        "yellow",
229        "white",
230    };
231
232    if(color < 0 || color > 15)
233        return "unknown color";
234
235    return color_names[color];
236}
237
238static unsigned int _caca_getticks(void)
239{
240    static unsigned int last_sec = 0, last_usec = 0;
241
242    struct timeval tv;
243    unsigned int ticks = 0;
244
245    gettimeofday(&tv, NULL);
246
247    if(last_sec != 0)
248    {
249        ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec);
250    }
251
252    last_sec = tv.tv_sec;
253    last_usec = tv.tv_usec;
254
255    return ticks;
256}
257
258void caca_refresh(void)
259{
260#define IDLE_USEC 10000
261    static int lastticks = 0;
262    int ticks = lastticks + _caca_getticks();
263
264#if defined(USE_SLANG)
265    SLsmg_refresh();
266#elif defined(USE_NCURSES)
267    refresh();
268#elif defined(USE_CONIO)
269#   if defined(SCREENUPDATE_IN_PC_H)
270    ScreenUpdate(_caca_screen);
271#   else
272    /* FIXME */
273#   endif
274#endif
275
276    /* Wait until _caca_delay + time of last call */
277    ticks += _caca_getticks();
278    for(; ticks + IDLE_USEC < (int)_caca_delay; ticks += _caca_getticks())
279        usleep(IDLE_USEC);
280
281    /* Update the sliding mean of the render time */
282    _caca_rendertime = (7 * _caca_rendertime + ticks) / 8;
283
284    lastticks = ticks - _caca_delay;
285
286    /* If we drifted too much, it's bad, bad, bad. */
287    if(lastticks > (int)_caca_delay)
288        lastticks = 0;
289}
290
291void caca_end(void)
292{
293#if defined(USE_SLANG)
294    SLtt_set_cursor_visibility(1);
295    SLang_reset_tty();
296    SLsmg_reset_smg();
297#elif defined(USE_NCURSES)
298    curs_set(1);
299    endwin();
300#elif defined(USE_CONIO)
301    _wscroll = 1;
302    textcolor((enum COLORS)WHITE);
303    textbackground((enum COLORS)BLACK);
304    gotoxy(caca_get_width(), caca_get_height());
305    cputs("\r\n");
306    _setcursortype(_NORMALCURSOR);
307#endif
308}
309
Note: See TracBrowser for help on using the repository browser.