source: ttyvaders/trunk/libee/ee.c @ 168

Last change on this file since 168 was 168, checked in by Sam Hocevar, 17 years ago
  • libee/ee.c: + Error checking in ee_init(). + Pre-generate the empty line for ee_clear().
  • libee/sprite.c: + Better error checking in ee_sprite_load().
  • Property svn:keywords set to Id
File size: 6.2 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 168 2003-11-13 16:45:25Z 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 */
49char *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 int _ee_delay;
70char *_ee_empty_line;
71
72#if defined(USE_NCURSES)
73int _ee_attr[16];
74#endif
75
76#if defined(USE_CONIO)
77static struct text_info ti;
78char *_ee_screen;
79#endif
80
81int ee_init(void)
82{
83#if defined(USE_SLANG)
84    static char *slang_colors[16] =
85    {
86        "black",
87        "blue",
88        "green",
89        "cyan",
90        "red",
91        "magenta",
92        "brown",
93        "lightgray",
94        "gray",
95        "brightblue",
96        "brightgreen",
97        "brightcyan",
98        "brightred",
99        "brightmagenta",
100        "yellow",
101        "white",
102    };
103
104    int i;
105
106    /* Initialize slang library */
107    SLsig_block_signals();
108    SLtt_get_terminfo();
109
110    if(SLkp_init() == -1)
111    {
112        SLsig_unblock_signals();
113        return -1;
114    }
115
116    SLang_init_tty(-1, 0, 1);
117
118    if(SLsmg_init_smg() == -1)
119    {
120        SLsig_unblock_signals();
121        return -1;
122    }
123
124    SLsig_unblock_signals();
125
126    SLsmg_cls();
127    SLtt_set_cursor_visibility(0);
128    SLsmg_refresh();
129
130    for(i = 0; i < 16; i++)
131        SLtt_set_color(i + 1, NULL, slang_colors[i], "black");
132
133#elif defined(USE_NCURSES)
134    int i;
135
136    initscr();
137    keypad(stdscr, TRUE);
138    nonl();
139    cbreak();
140    noecho();
141    nodelay(stdscr, TRUE);
142    curs_set(0);
143
144    start_color();
145
146    init_pair(1 + EE_BLACK,        COLOR_BLACK,   COLOR_BLACK);
147    init_pair(1 + EE_BLUE,         COLOR_BLUE,    COLOR_BLACK);
148    init_pair(1 + EE_GREEN,        COLOR_GREEN,   COLOR_BLACK);
149    init_pair(1 + EE_CYAN,         COLOR_CYAN,    COLOR_BLACK);
150    init_pair(1 + EE_RED,          COLOR_RED,     COLOR_BLACK);
151    init_pair(1 + EE_MAGENTA,      COLOR_MAGENTA, COLOR_BLACK);
152    init_pair(1 + EE_BROWN,        COLOR_YELLOW,  COLOR_BLACK);
153    init_pair(1 + EE_LIGHTGRAY,    COLOR_WHITE,   COLOR_BLACK);
154    init_pair(1 + EE_DARKGRAY,     COLOR_BLACK,   COLOR_BLACK);
155    init_pair(1 + EE_LIGHTBLUE,    COLOR_BLUE,    COLOR_BLACK);
156    init_pair(1 + EE_LIGHTGREEN,   COLOR_GREEN,   COLOR_BLACK);
157    init_pair(1 + EE_LIGHTCYAN,    COLOR_CYAN,    COLOR_BLACK);
158    init_pair(1 + EE_LIGHTRED,     COLOR_RED,     COLOR_BLACK);
159    init_pair(1 + EE_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK);
160    init_pair(1 + EE_YELLOW,       COLOR_YELLOW,  COLOR_BLACK);
161    init_pair(1 + EE_WHITE,        COLOR_WHITE,   COLOR_BLACK);
162
163    for(i = 0; i < 8; i++)
164    {
165        _ee_attr[i] = COLOR_PAIR(1 + i);
166        _ee_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i);
167    }
168
169#elif defined(USE_CONIO)
170    gettextinfo(&ti);
171    _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight);
172    if(_ee_screen == NULL)
173        return -1;
174    _wscroll = 0;
175    _setcursortype(_NOCURSOR);
176    clrscr();
177#   if defined(SCREENUPDATE_IN_PC_H)
178    ScreenRetrieve(_ee_screen);
179#   else
180    /* FIXME */
181#   endif
182
183#endif
184    _ee_empty_line = malloc(ee_get_width() + 1);
185    memset(_ee_empty_line, ' ', ee_get_width());
186    _ee_empty_line[ee_get_width()] = '\0';
187
188    _ee_delay = 0;
189
190    return 0;
191}
192
193void ee_set_delay(int usec)
194{
195    _ee_delay = usec;
196}
197
198int 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
209int 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
220#if !defined(USE_CONIO)
221static int64_t local_time(void)
222{
223    struct timeval tv;
224    int64_t now;
225
226    gettimeofday(&tv, NULL);
227    now = tv.tv_sec;
228    now *= 1000000;
229    now += tv.tv_usec;
230    return now;
231}
232#endif
233
234void ee_refresh(void)
235{
236#if !defined(USE_CONIO)
237    static int64_t local_clock = 0;
238    int64_t now;
239
240    if(!local_clock)
241    {
242        /* Initialize local_clock */
243        local_clock = local_time();
244    }
245
246    if(local_time() > local_clock + 10000)
247    {
248        /* If we are late, we shouldn't display anything */
249    }
250#endif
251
252#if defined(USE_SLANG)
253    SLsmg_refresh();
254#elif defined(USE_NCURSES)
255    refresh();
256#elif defined(USE_CONIO)
257#   if defined(SCREENUPDATE_IN_PC_H)
258    ScreenUpdate(_ee_screen);
259#   else
260    /* FIXME */
261#   endif
262#endif
263
264#if !defined(USE_CONIO)
265    now = local_time();
266
267    if(now < local_clock + _ee_delay - 10000)
268    {
269        usleep(local_clock + _ee_delay - 10000 - now);
270    }
271
272    local_clock += _ee_delay;
273#else
274    delay(5);
275#endif
276}
277
278void ee_end(void)
279{
280#if defined(USE_SLANG)
281    SLtt_set_cursor_visibility(1);
282    SLang_reset_tty();
283    SLsmg_reset_smg();
284#elif defined(USE_NCURSES)
285    curs_set(1);
286    endwin();
287#elif defined(USE_CONIO)
288    _wscroll = 1;
289    textcolor((enum COLORS)WHITE);
290    textbackground((enum COLORS)BLACK);
291    gotoxy(ee_get_width(), ee_get_height());
292    cputs("\r\n");
293    _setcursortype(_NORMALCURSOR);
294#endif
295}
296
Note: See TracBrowser for help on using the repository browser.