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

Last change on this file since 158 was 158, checked in by Sam Hocevar, 17 years ago
  • README: + Added a note about dos cross-compilation.
  • configure.ac: + Added a check for ScreenUpdate? in <pc.h>.
  • libee/graphics.c libee/ee.c: + Improved the conio port thanks to ScreenUpdate?().
  • Property svn:keywords set to Id
File size: 4.8 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 158 2003-11-12 18:41:02Z 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
47static int _ee_delay;
48#if defined(USE_CONIO)
49static struct text_info ti;
50char *_screen_buffer;
51#endif
52
53int ee_init(void)
54{
55#if defined(USE_SLANG)
56    static char * colors[] = { "black", "green", "yellow", "white",
57        "red", "gray", "lightgray", "blue", "cyan", "magenta", NULL };
58    int i;
59
60    /* Initialize slang library */
61    SLsig_block_signals();
62    SLtt_get_terminfo();
63
64    if(SLkp_init() == -1)
65    {
66        SLsig_unblock_signals();
67        return -1;
68    }
69
70    SLang_init_tty(-1, 0, 1);
71
72    if(SLsmg_init_smg() == -1)
73    {
74        SLsig_unblock_signals();
75        return -1;
76    }
77
78    SLsig_unblock_signals();
79
80    SLsmg_cls();
81    SLtt_set_cursor_visibility(0);
82    SLsmg_refresh();
83
84    for(i = 0; colors[i]; i++)
85    {
86        SLtt_set_color(i + 1, NULL, colors[i], "black");
87    }
88
89#elif defined(USE_NCURSES)
90    /* Initialize ncurses library */
91    initscr();
92    keypad(stdscr, TRUE);
93    nonl();
94    cbreak();
95    noecho();
96    nodelay(stdscr, TRUE);
97    curs_set(0);
98
99    start_color();
100
101    init_pair(EE_BLACK, COLOR_BLACK, COLOR_BLACK);
102    init_pair(EE_GREEN, COLOR_GREEN, COLOR_BLACK);
103    init_pair(EE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
104    init_pair(EE_WHITE, COLOR_WHITE, COLOR_BLACK);
105    init_pair(EE_RED, COLOR_RED, COLOR_BLACK);
106    init_pair(EE_GRAY, COLOR_WHITE, COLOR_BLACK); // XXX
107    init_pair(EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK); // XXX
108    init_pair(EE_BLUE, COLOR_BLUE, COLOR_BLACK);
109    init_pair(EE_CYAN, COLOR_CYAN, COLOR_BLACK);
110    init_pair(EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
111
112#elif defined(USE_CONIO)
113    _wscroll = 0;
114    _setcursortype(_NOCURSOR);
115    clrscr();
116    gettextinfo(&ti);
117    _screen_buffer = malloc(ee_get_width() * ee_get_height() * 2);
118    ScreenRetrieve(_screen_buffer);
119
120#endif
121    _ee_delay = 0;
122
123    return 0;
124}
125
126void ee_set_delay(int usec)
127{
128    _ee_delay = usec;
129}
130
131int ee_get_width(void)
132{
133#if defined(USE_SLANG)
134    return SLtt_Screen_Cols;
135#elif defined(USE_NCURSES)
136    return COLS;
137#elif defined(USE_CONIO)
138    return ti.screenwidth;
139#endif
140}
141
142int ee_get_height(void)
143{
144#if defined(USE_SLANG)
145    return SLtt_Screen_Rows;
146#elif defined(USE_NCURSES)
147    return LINES;
148#else
149    return ti.screenheight;
150#endif
151}
152
153#if !defined(USE_CONIO)
154static int64_t local_time(void)
155{
156    struct timeval tv;
157    int64_t now;
158
159    gettimeofday(&tv, NULL);
160    now = tv.tv_sec;
161    now *= 1000000;
162    now += tv.tv_usec;
163    return now;
164}
165#endif
166
167void ee_refresh(void)
168{
169#if !defined(USE_CONIO)
170    static int64_t local_clock = 0;
171    int64_t now;
172
173    if(!local_clock)
174    {
175        /* Initialize local_clock */
176        local_clock = local_time();
177    }
178
179    if(local_time() > local_clock + 10000)
180    {
181        /* If we are late, we shouldn't display anything */
182    }
183#endif
184
185#if defined(USE_SLANG)
186    SLsmg_refresh();
187#elif defined(USE_NCURSES)
188    refresh();
189#elif defined(USE_CONIO)
190    ScreenUpdate(_screen_buffer);
191#endif
192
193#if !defined(USE_CONIO)
194    now = local_time();
195
196    if(now < local_clock + _ee_delay - 10000)
197    {
198        usleep(local_clock + _ee_delay - 10000 - now);
199    }
200
201    local_clock += _ee_delay;
202#else
203    delay(5);
204#endif
205}
206
207void ee_end(void)
208{
209#if defined(USE_SLANG)
210    SLtt_set_cursor_visibility(1);
211    SLang_reset_tty();
212    SLsmg_reset_smg();
213#elif defined(USE_NCURSES)
214    curs_set(1);
215    endwin();
216#elif defined(USE_CONIO)
217    ScreenUpdate(_screen_buffer);
218    _wscroll = 1;
219    textcolor((enum COLORS)WHITE);
220    textbackground((enum COLORS)BLACK);
221    gotoxy(ee_get_width(), ee_get_height());
222    cputs("\r\n");
223    _setcursortype(_NORMALCURSOR);
224#endif
225}
226
Note: See TracBrowser for help on using the repository browser.