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

Last change on this file since 178 was 178, checked in by Sam Hocevar, 17 years ago
  • libee/ee.c: + Implemented correct framerate wait loop. + No framedropping yet.
  • test/demo.c src/main.c: + Set an arbitrary framerate for demos.
  • 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 178 2003-11-14 19:00:07Z 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
193int ee_get_width(void)
194{
195#if defined(USE_SLANG)
196    return SLtt_Screen_Cols;
197#elif defined(USE_NCURSES)
198    return COLS;
199#elif defined(USE_CONIO)
200    return ti.screenwidth;
201#endif
202}
203
204int ee_get_height(void)
205{
206#if defined(USE_SLANG)
207    return SLtt_Screen_Rows;
208#elif defined(USE_NCURSES)
209    return LINES;
210#else
211    return ti.screenheight;
212#endif
213}
214
215void ee_set_delay(int usec)
216{
217    _ee_delay = usec;
218}
219
220static unsigned int _ee_getticks(void)
221{
222    static unsigned int last_sec = 0, last_usec = 0;
223
224    struct timeval tv;
225    unsigned int ticks = 0;
226
227    gettimeofday(&tv, NULL);
228
229    if(last_sec != 0)
230    {
231        ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec);
232    }
233
234    last_sec = tv.tv_sec;
235    last_usec = tv.tv_usec;
236
237    return ticks;
238}
239
240void ee_refresh(void)
241{
242    static int lastticks = 0;
243    unsigned int ticks = lastticks + _ee_getticks();
244
245#if defined(USE_SLANG)
246    SLsmg_refresh();
247#elif defined(USE_NCURSES)
248    refresh();
249#elif defined(USE_CONIO)
250#   if defined(SCREENUPDATE_IN_PC_H)
251    ScreenUpdate(_ee_screen);
252#   else
253    /* FIXME */
254#   endif
255#endif
256
257    /* Wait until _ee_delay + time of last call */
258    for(ticks += _ee_getticks(); ticks < _ee_delay; ticks += _ee_getticks())
259        usleep(10000);
260
261    lastticks = ticks - _ee_delay;
262
263    /* If we drifted too much, it's bad, bad, bad. */
264    if(lastticks > _ee_delay)
265        lastticks = 0;
266}
267
268void ee_end(void)
269{
270#if defined(USE_SLANG)
271    SLtt_set_cursor_visibility(1);
272    SLang_reset_tty();
273    SLsmg_reset_smg();
274#elif defined(USE_NCURSES)
275    curs_set(1);
276    endwin();
277#elif defined(USE_CONIO)
278    _wscroll = 1;
279    textcolor((enum COLORS)WHITE);
280    textbackground((enum COLORS)BLACK);
281    gotoxy(ee_get_width(), ee_get_height());
282    cputs("\r\n");
283    _setcursortype(_NORMALCURSOR);
284#endif
285}
286
Note: See TracBrowser for help on using the repository browser.