source: libcaca/trunk/libee/ee.c @ 179

Last change on this file since 179 was 179, checked in by Sam Hocevar, 19 years ago
  • libee/graphics.c: + Correct clipping in ee_putstr() for long strings.
  • libee/ee.c: + New ee_get_rendertime() call to provide framerate information.
  • libee/ee.h: + Added const keywords where it was meaningful, despite Slang's blatant

omission of such keywords in its prototypes.

  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1/*
2 *   libee         ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: ee.c 179 2003-11-15 09:58:20Z sam $
7 *
8 *   This program is free software; you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation; either version 2 of the License, or
11 *   (at your option) any later version.
12 *
13 *   This program 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
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include "config.h"
24
25#if defined(USE_SLANG)
26#   include <slang.h>
27#elif defined(USE_NCURSES)
28#   include <curses.h>
29#elif defined(USE_CONIO)
30#   include <dos.h>
31#   include <conio.h>
32#   if defined(SCREENUPDATE_IN_PC_H)
33#       include <pc.h>
34#   endif
35#else
36#   error "no graphics library detected"
37#endif
38
39#include <stdlib.h>
40#include <unistd.h>
41#include <string.h>
42#include <sys/time.h>
43#include <time.h>
44
45#include "ee.h"
46#include "ee_internals.h"
47
48/* Global array with color names */
49const char *ee_color_names[16] =
50{
51    "black",
52    "blue",
53    "green",
54    "cyan",
55    "red",
56    "magenta",
57    "brown",
58    "lightgray",
59    "darkgray",
60    "lightblue",
61    "lightgreen",
62    "lightcyan",
63    "lightred",
64    "lightmagenta",
65    "yellow",
66    "white",
67};
68
69static unsigned int _ee_delay;
70static unsigned int _ee_rendertime;
71char *_ee_empty_line;
72char *_ee_scratch_line;
73
74#if defined(USE_NCURSES)
75int _ee_attr[16];
76#endif
77
78#if defined(USE_CONIO)
79static struct text_info ti;
80char *_ee_screen;
81#endif
82
83int ee_init(void)
84{
85#if defined(USE_SLANG)
86    static char *slang_colors[16] =
87    {
88        "black",
89        "blue",
90        "green",
91        "cyan",
92        "red",
93        "magenta",
94        "brown",
95        "lightgray",
96        "gray",
97        "brightblue",
98        "brightgreen",
99        "brightcyan",
100        "brightred",
101        "brightmagenta",
102        "yellow",
103        "white",
104    };
105
106    int i;
107
108    /* Initialize slang library */
109    SLsig_block_signals();
110    SLtt_get_terminfo();
111
112    if(SLkp_init() == -1)
113    {
114        SLsig_unblock_signals();
115        return -1;
116    }
117
118    SLang_init_tty(-1, 0, 1);
119
120    if(SLsmg_init_smg() == -1)
121    {
122        SLsig_unblock_signals();
123        return -1;
124    }
125
126    SLsig_unblock_signals();
127
128    SLsmg_cls();
129    SLtt_set_cursor_visibility(0);
130    SLsmg_refresh();
131
132    for(i = 0; i < 16; i++)
133        SLtt_set_color(i + 1, NULL, slang_colors[i], "black");
134
135#elif defined(USE_NCURSES)
136    int i;
137
138    initscr();
139    keypad(stdscr, TRUE);
140    nonl();
141    cbreak();
142    noecho();
143    nodelay(stdscr, TRUE);
144    curs_set(0);
145
146    start_color();
147
148    init_pair(1 + EE_BLACK,        COLOR_BLACK,   COLOR_BLACK);
149    init_pair(1 + EE_BLUE,         COLOR_BLUE,    COLOR_BLACK);
150    init_pair(1 + EE_GREEN,        COLOR_GREEN,   COLOR_BLACK);
151    init_pair(1 + EE_CYAN,         COLOR_CYAN,    COLOR_BLACK);
152    init_pair(1 + EE_RED,          COLOR_RED,     COLOR_BLACK);
153    init_pair(1 + EE_MAGENTA,      COLOR_MAGENTA, COLOR_BLACK);
154    init_pair(1 + EE_BROWN,        COLOR_YELLOW,  COLOR_BLACK);
155    init_pair(1 + EE_LIGHTGRAY,    COLOR_WHITE,   COLOR_BLACK);
156    init_pair(1 + EE_DARKGRAY,     COLOR_BLACK,   COLOR_BLACK);
157    init_pair(1 + EE_LIGHTBLUE,    COLOR_BLUE,    COLOR_BLACK);
158    init_pair(1 + EE_LIGHTGREEN,   COLOR_GREEN,   COLOR_BLACK);
159    init_pair(1 + EE_LIGHTCYAN,    COLOR_CYAN,    COLOR_BLACK);
160    init_pair(1 + EE_LIGHTRED,     COLOR_RED,     COLOR_BLACK);
161    init_pair(1 + EE_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK);
162    init_pair(1 + EE_YELLOW,       COLOR_YELLOW,  COLOR_BLACK);
163    init_pair(1 + EE_WHITE,        COLOR_WHITE,   COLOR_BLACK);
164
165    for(i = 0; i < 8; i++)
166    {
167        _ee_attr[i] = COLOR_PAIR(1 + i);
168        _ee_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i);
169    }
170
171#elif defined(USE_CONIO)
172    gettextinfo(&ti);
173    _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight);
174    if(_ee_screen == NULL)
175        return -1;
176    _wscroll = 0;
177    _setcursortype(_NOCURSOR);
178    clrscr();
179#   if defined(SCREENUPDATE_IN_PC_H)
180    ScreenRetrieve(_ee_screen);
181#   else
182    /* FIXME */
183#   endif
184
185#endif
186    _ee_empty_line = malloc(ee_get_width() + 1);
187    memset(_ee_empty_line, ' ', ee_get_width());
188    _ee_empty_line[ee_get_width()] = '\0';
189
190    _ee_scratch_line = malloc(ee_get_width() + 1);
191
192    _ee_delay = 0;
193    _ee_rendertime = 0;
194
195    return 0;
196}
197
198unsigned int ee_get_width(void)
199{
200#if defined(USE_SLANG)
201    return SLtt_Screen_Cols;
202#elif defined(USE_NCURSES)
203    return COLS;
204#elif defined(USE_CONIO)
205    return ti.screenwidth;
206#endif
207}
208
209unsigned int ee_get_height(void)
210{
211#if defined(USE_SLANG)
212    return SLtt_Screen_Rows;
213#elif defined(USE_NCURSES)
214    return LINES;
215#else
216    return ti.screenheight;
217#endif
218}
219
220void ee_set_delay(unsigned int usec)
221{
222    _ee_delay = usec;
223}
224
225unsigned int ee_get_rendertime(void)
226{
227    return _ee_rendertime;
228}
229
230static unsigned int _ee_getticks(void)
231{
232    static unsigned int last_sec = 0, last_usec = 0;
233
234    struct timeval tv;
235    unsigned int ticks = 0;
236
237    gettimeofday(&tv, NULL);
238
239    if(last_sec != 0)
240    {
241        ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec);
242    }
243
244    last_sec = tv.tv_sec;
245    last_usec = tv.tv_usec;
246
247    return ticks;
248}
249
250void ee_refresh(void)
251{
252    static unsigned int lastticks = 0;
253    unsigned int ticks = lastticks + _ee_getticks();
254
255#if defined(USE_SLANG)
256    SLsmg_refresh();
257#elif defined(USE_NCURSES)
258    refresh();
259#elif defined(USE_CONIO)
260#   if defined(SCREENUPDATE_IN_PC_H)
261    ScreenUpdate(_ee_screen);
262#   else
263    /* FIXME */
264#   endif
265#endif
266
267    /* Wait until _ee_delay + time of last call */
268    for(ticks += _ee_getticks(); ticks < _ee_delay; ticks += _ee_getticks())
269        usleep(10000);
270
271    /* Update the sliding mean of the render time */
272    _ee_rendertime = (7 * _ee_rendertime + ticks) / 8;
273
274    lastticks = ticks - _ee_delay;
275
276    /* If we drifted too much, it's bad, bad, bad. */
277    if(lastticks > _ee_delay)
278        lastticks = 0;
279}
280
281void ee_end(void)
282{
283#if defined(USE_SLANG)
284    SLtt_set_cursor_visibility(1);
285    SLang_reset_tty();
286    SLsmg_reset_smg();
287#elif defined(USE_NCURSES)
288    curs_set(1);
289    endwin();
290#elif defined(USE_CONIO)
291    _wscroll = 1;
292    textcolor((enum COLORS)WHITE);
293    textbackground((enum COLORS)BLACK);
294    gotoxy(ee_get_width(), ee_get_height());
295    cputs("\r\n");
296    _setcursortype(_NORMALCURSOR);
297#endif
298}
299
Note: See TracBrowser for help on using the repository browser.