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

Last change on this file since 160 was 160, checked in by Sam Hocevar, 16 years ago
  • libee/ee.h: + Added ee_color_names[] containing our 16 color names.
  • libee/ee.c: + Extended our color set to the full 16 instead of 10. + Precalculate ncurses attributes.
  • libee/graphics.c: + Clip color value in ee_set_color(). + Clip characters in ee_putchar(). + Partially clip characters in ee_putstr(), overflows aren't checked yet.
  • libee/ee_internals.h: + New file to share extern variables within libee.
  • test/demo.c: + Added a simple demo_color() to output all colors. + Replaced four ee_draw_line() with ee_draw_thin_box(). + Replaced x1, y1, x2 etc. with xa, ya, xb etc. because <math.h> already

defines y1.

+ Randomized colours from 0 to 15 instead of 1 to 10.

  • src/Makefiles.am: + Added -lm to the ttyvaders linking flags because of the intro.
  • README BUGS TODO: + Updated.
  • Property svn:keywords set to Id
File size: 6.0 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 160 2003-11-12 21:18:50Z 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;
70
71#if defined(USE_NCURSES)
72int _ee_attr[16];
73#endif
74
75#if defined(USE_CONIO)
76static struct text_info ti;
77char *_ee_screen;
78#endif
79
80int ee_init(void)
81{
82#if defined(USE_SLANG)
83    static char *slang_colors[16] =
84    {
85        "black",
86        "blue",
87        "green",
88        "cyan",
89        "red",
90        "magenta",
91        "brown",
92        "lightgray",
93        "gray",
94        "brightblue",
95        "brightgreen",
96        "brightcyan",
97        "brightred",
98        "brightmagenta",
99        "yellow",
100        "white",
101    };
102
103    int i;
104
105    /* Initialize slang library */
106    SLsig_block_signals();
107    SLtt_get_terminfo();
108
109    if(SLkp_init() == -1)
110    {
111        SLsig_unblock_signals();
112        return -1;
113    }
114
115    SLang_init_tty(-1, 0, 1);
116
117    if(SLsmg_init_smg() == -1)
118    {
119        SLsig_unblock_signals();
120        return -1;
121    }
122
123    SLsig_unblock_signals();
124
125    SLsmg_cls();
126    SLtt_set_cursor_visibility(0);
127    SLsmg_refresh();
128
129    for(i = 0; i < 16; i++)
130        SLtt_set_color(i + 1, NULL, slang_colors[i], "black");
131
132#elif defined(USE_NCURSES)
133    int i;
134
135    initscr();
136    keypad(stdscr, TRUE);
137    nonl();
138    cbreak();
139    noecho();
140    nodelay(stdscr, TRUE);
141    curs_set(0);
142
143    start_color();
144
145    init_pair(1 + EE_BLACK,        COLOR_BLACK,   COLOR_BLACK);
146    init_pair(1 + EE_BLUE,         COLOR_BLUE,    COLOR_BLACK);
147    init_pair(1 + EE_GREEN,        COLOR_GREEN,   COLOR_BLACK);
148    init_pair(1 + EE_CYAN,         COLOR_CYAN,    COLOR_BLACK);
149    init_pair(1 + EE_RED,          COLOR_RED,     COLOR_BLACK);
150    init_pair(1 + EE_MAGENTA,      COLOR_MAGENTA, COLOR_BLACK);
151    init_pair(1 + EE_BROWN,        COLOR_YELLOW,  COLOR_BLACK);
152    init_pair(1 + EE_LIGHTGRAY,    COLOR_WHITE,   COLOR_BLACK);
153    init_pair(1 + EE_DARKGRAY,     COLOR_BLACK,   COLOR_BLACK);
154    init_pair(1 + EE_LIGHTBLUE,    COLOR_BLUE,    COLOR_BLACK);
155    init_pair(1 + EE_LIGHTGREEN,   COLOR_GREEN,   COLOR_BLACK);
156    init_pair(1 + EE_LIGHTCYAN,    COLOR_CYAN,    COLOR_BLACK);
157    init_pair(1 + EE_LIGHTRED,     COLOR_RED,     COLOR_BLACK);
158    init_pair(1 + EE_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK);
159    init_pair(1 + EE_YELLOW,       COLOR_YELLOW,  COLOR_BLACK);
160    init_pair(1 + EE_WHITE,        COLOR_WHITE,   COLOR_BLACK);
161
162    for(i = 0; i < 8; i++)
163    {
164        _ee_attr[i] = COLOR_PAIR(1 + i);
165        _ee_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i);
166    }
167
168#elif defined(USE_CONIO)
169    _wscroll = 0;
170    _setcursortype(_NOCURSOR);
171    clrscr();
172    gettextinfo(&ti);
173    _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight);
174#   if defined(SCREENUPDATE_IN_PC_H)
175    ScreenRetrieve(_ee_screen);
176#   else
177    /* FIXME */
178#   endif
179
180#endif
181    _ee_delay = 0;
182
183    return 0;
184}
185
186void ee_set_delay(int usec)
187{
188    _ee_delay = usec;
189}
190
191int ee_get_width(void)
192{
193#if defined(USE_SLANG)
194    return SLtt_Screen_Cols;
195#elif defined(USE_NCURSES)
196    return COLS;
197#elif defined(USE_CONIO)
198    return ti.screenwidth;
199#endif
200}
201
202int ee_get_height(void)
203{
204#if defined(USE_SLANG)
205    return SLtt_Screen_Rows;
206#elif defined(USE_NCURSES)
207    return LINES;
208#else
209    return ti.screenheight;
210#endif
211}
212
213#if !defined(USE_CONIO)
214static int64_t local_time(void)
215{
216    struct timeval tv;
217    int64_t now;
218
219    gettimeofday(&tv, NULL);
220    now = tv.tv_sec;
221    now *= 1000000;
222    now += tv.tv_usec;
223    return now;
224}
225#endif
226
227void ee_refresh(void)
228{
229#if !defined(USE_CONIO)
230    static int64_t local_clock = 0;
231    int64_t now;
232
233    if(!local_clock)
234    {
235        /* Initialize local_clock */
236        local_clock = local_time();
237    }
238
239    if(local_time() > local_clock + 10000)
240    {
241        /* If we are late, we shouldn't display anything */
242    }
243#endif
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#if !defined(USE_CONIO)
258    now = local_time();
259
260    if(now < local_clock + _ee_delay - 10000)
261    {
262        usleep(local_clock + _ee_delay - 10000 - now);
263    }
264
265    local_clock += _ee_delay;
266#else
267    delay(5);
268#endif
269}
270
271void ee_end(void)
272{
273#if defined(USE_SLANG)
274    SLtt_set_cursor_visibility(1);
275    SLang_reset_tty();
276    SLsmg_reset_smg();
277#elif defined(USE_NCURSES)
278    curs_set(1);
279    endwin();
280#elif defined(USE_CONIO)
281    _wscroll = 1;
282    textcolor((enum COLORS)WHITE);
283    textbackground((enum COLORS)BLACK);
284    gotoxy(ee_get_width(), ee_get_height());
285    cputs("\r\n");
286    _setcursortype(_NORMALCURSOR);
287#endif
288}
289
Note: See TracBrowser for help on using the repository browser.