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

Last change on this file since 205 was 205, checked in by Sam Hocevar, 19 years ago
  • src/: + Doxygenated a few things.
  • Property svn:keywords set to Id
File size: 7.4 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 205 2003-11-22 12:45:25Z 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#   if defined(SCREENUPDATE_IN_PC_H)
42#       include <pc.h>
43#   endif
44#else
45#   error "no graphics library detected"
46#endif
47
48#include <stdlib.h>
49#include <unistd.h>
50#include <string.h>
51#include <sys/time.h>
52#include <time.h>
53
54#include "caca.h"
55#include "caca_internals.h"
56
57static unsigned int _caca_delay;
58static unsigned int _caca_rendertime;
59char *_caca_empty_line;
60char *_caca_scratch_line;
61
62#if defined(USE_NCURSES)
63int _caca_attr[16];
64#endif
65
66#if defined(USE_CONIO)
67static struct text_info ti;
68char *_caca_screen;
69#endif
70
71int caca_init(void)
72{
73#if defined(USE_SLANG)
74    static char *slang_colors[16] =
75    {
76        "black",
77        "blue",
78        "green",
79        "cyan",
80        "red",
81        "magenta",
82        "brown",
83        "lightgray",
84        "gray",
85        "brightblue",
86        "brightgreen",
87        "brightcyan",
88        "brightred",
89        "brightmagenta",
90        "yellow",
91        "white",
92    };
93
94    int i;
95
96    /* Initialize slang library */
97    SLsig_block_signals();
98    SLtt_get_terminfo();
99
100    if(SLkp_init() == -1)
101    {
102        SLsig_unblock_signals();
103        return -1;
104    }
105
106    SLang_init_tty(-1, 0, 1);
107
108    if(SLsmg_init_smg() == -1)
109    {
110        SLsig_unblock_signals();
111        return -1;
112    }
113
114    SLsig_unblock_signals();
115
116    SLsmg_cls();
117    SLtt_set_cursor_visibility(0);
118    SLkp_define_keysym("\e[M", 1001);
119    SLtt_set_mouse_mode(1, 0);
120    SLsmg_refresh();
121
122    for(i = 0; i < 16; i++)
123        SLtt_set_color(i + 1, NULL, slang_colors[i], "black");
124
125#elif defined(USE_NCURSES)
126    int i;
127
128    initscr();
129    keypad(stdscr, TRUE);
130    nonl();
131    cbreak();
132    noecho();
133    nodelay(stdscr, TRUE);
134    curs_set(0);
135
136    start_color();
137
138    init_pair(1 + CACA_COLOR_BLACK,        COLOR_BLACK,   COLOR_BLACK);
139    init_pair(1 + CACA_COLOR_BLUE,         COLOR_BLUE,    COLOR_BLACK);
140    init_pair(1 + CACA_COLOR_GREEN,        COLOR_GREEN,   COLOR_BLACK);
141    init_pair(1 + CACA_COLOR_CYAN,         COLOR_CYAN,    COLOR_BLACK);
142    init_pair(1 + CACA_COLOR_RED,          COLOR_RED,     COLOR_BLACK);
143    init_pair(1 + CACA_COLOR_MAGENTA,      COLOR_MAGENTA, COLOR_BLACK);
144    init_pair(1 + CACA_COLOR_BROWN,        COLOR_YELLOW,  COLOR_BLACK);
145    init_pair(1 + CACA_COLOR_LIGHTGRAY,    COLOR_WHITE,   COLOR_BLACK);
146    init_pair(1 + CACA_COLOR_DARKGRAY,     COLOR_BLACK,   COLOR_BLACK);
147    init_pair(1 + CACA_COLOR_LIGHTBLUE,    COLOR_BLUE,    COLOR_BLACK);
148    init_pair(1 + CACA_COLOR_LIGHTGREEN,   COLOR_GREEN,   COLOR_BLACK);
149    init_pair(1 + CACA_COLOR_LIGHTCYAN,    COLOR_CYAN,    COLOR_BLACK);
150    init_pair(1 + CACA_COLOR_LIGHTRED,     COLOR_RED,     COLOR_BLACK);
151    init_pair(1 + CACA_COLOR_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK);
152    init_pair(1 + CACA_COLOR_YELLOW,       COLOR_YELLOW,  COLOR_BLACK);
153    init_pair(1 + CACA_COLOR_WHITE,        COLOR_WHITE,   COLOR_BLACK);
154
155    for(i = 0; i < 8; i++)
156    {
157        _caca_attr[i] = COLOR_PAIR(1 + i);
158        _caca_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i);
159    }
160
161#elif defined(USE_CONIO)
162    gettextinfo(&ti);
163    _caca_screen = malloc(2 * ti.screenwidth * ti.screenheight);
164    if(_caca_screen == NULL)
165        return -1;
166    _wscroll = 0;
167    _setcursortype(_NOCURSOR);
168    clrscr();
169#   if defined(SCREENUPDATE_IN_PC_H)
170    ScreenRetrieve(_caca_screen);
171#   else
172    /* FIXME */
173#   endif
174
175#endif
176    _caca_empty_line = malloc(caca_get_width() + 1);
177    memset(_caca_empty_line, ' ', caca_get_width());
178    _caca_empty_line[caca_get_width()] = '\0';
179
180    _caca_scratch_line = malloc(caca_get_width() + 1);
181
182    _caca_delay = 0;
183    _caca_rendertime = 0;
184
185    return 0;
186}
187
188unsigned int caca_get_width(void)
189{
190#if defined(USE_SLANG)
191    return SLtt_Screen_Cols;
192#elif defined(USE_NCURSES)
193    return COLS;
194#elif defined(USE_CONIO)
195    return ti.screenwidth;
196#endif
197}
198
199unsigned int caca_get_height(void)
200{
201#if defined(USE_SLANG)
202    return SLtt_Screen_Rows;
203#elif defined(USE_NCURSES)
204    return LINES;
205#else
206    return ti.screenheight;
207#endif
208}
209
210void caca_set_delay(unsigned int usec)
211{
212    _caca_delay = usec;
213}
214
215unsigned int caca_get_rendertime(void)
216{
217    return _caca_rendertime;
218}
219
220const char *caca_get_color_name(unsigned int color)
221{
222    static const char *color_names[16] =
223    {
224        "black",
225        "blue",
226        "green",
227        "cyan",
228        "red",
229        "magenta",
230        "brown",
231        "light gray",
232        "dark gray",
233        "light blue",
234        "light green",
235        "light cyan",
236        "light red",
237        "light magenta",
238        "yellow",
239        "white",
240    };
241
242    if(color < 0 || color > 15)
243        return "unknown color";
244
245    return color_names[color];
246}
247
248static unsigned int _caca_getticks(void)
249{
250    static unsigned int last_sec = 0, last_usec = 0;
251
252    struct timeval tv;
253    unsigned int ticks = 0;
254
255    gettimeofday(&tv, NULL);
256
257    if(last_sec != 0)
258    {
259        ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec);
260    }
261
262    last_sec = tv.tv_sec;
263    last_usec = tv.tv_usec;
264
265    return ticks;
266}
267
268void caca_refresh(void)
269{
270#define IDLE_USEC 10000
271    static int lastticks = 0;
272    int ticks = lastticks + _caca_getticks();
273
274#if defined(USE_SLANG)
275    SLsmg_refresh();
276#elif defined(USE_NCURSES)
277    refresh();
278#elif defined(USE_CONIO)
279#   if defined(SCREENUPDATE_IN_PC_H)
280    ScreenUpdate(_caca_screen);
281#   else
282    /* FIXME */
283#   endif
284#endif
285
286    /* Wait until _caca_delay + time of last call */
287    ticks += _caca_getticks();
288    for(; ticks + IDLE_USEC < (int)_caca_delay; ticks += _caca_getticks())
289        usleep(IDLE_USEC);
290
291    /* Update the sliding mean of the render time */
292    _caca_rendertime = (7 * _caca_rendertime + ticks) / 8;
293
294    lastticks = ticks - _caca_delay;
295
296    /* If we drifted too much, it's bad, bad, bad. */
297    if(lastticks > (int)_caca_delay)
298        lastticks = 0;
299}
300
301void caca_end(void)
302{
303#if defined(USE_SLANG)
304    SLtt_set_mouse_mode(0, 0);
305    SLtt_set_cursor_visibility(1);
306    SLang_reset_tty();
307    SLsmg_reset_smg();
308#elif defined(USE_NCURSES)
309    curs_set(1);
310    endwin();
311#elif defined(USE_CONIO)
312    _wscroll = 1;
313    textcolor((enum COLORS)WHITE);
314    textbackground((enum COLORS)BLACK);
315    gotoxy(caca_get_width(), caca_get_height());
316    cputs("\r\n");
317    _setcursortype(_NORMALCURSOR);
318#endif
319}
320
Note: See TracBrowser for help on using the repository browser.