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

Last change on this file since 177 was 177, 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
RevLine 
[35]1/*
[79]2 *   libee         ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
[35]4 *                 All Rights Reserved
5 *
[75]6 *   $Id: ee.c 177 2003-11-14 19:00:07Z sam $
[35]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 */
[17]22
[63]23#include "config.h"
24
[157]25#if defined(USE_SLANG)
[147]26#   include <slang.h>
[157]27#elif defined(USE_NCURSES)
[147]28#   include <curses.h>
[157]29#elif defined(USE_CONIO)
30#   include <dos.h>
[155]31#   include <conio.h>
[157]32#   if defined(SCREENUPDATE_IN_PC_H)
33#       include <pc.h>
34#   endif
[155]35#else
36#   error "no graphics library detected"
[147]37#endif
38
[17]39#include <stdlib.h>
[63]40#include <unistd.h>
[83]41#include <string.h>
[75]42#include <sys/time.h>
43#include <time.h>
[17]44
[79]45#include "ee.h"
[159]46#include "ee_internals.h"
[17]47
[159]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
[93]69static int _ee_delay;
[167]70char *_ee_empty_line;
[159]71
72#if defined(USE_NCURSES)
73int _ee_attr[16];
74#endif
75
[157]76#if defined(USE_CONIO)
[155]77static struct text_info ti;
[159]78char *_ee_screen;
[155]79#endif
[93]80
[79]81int ee_init(void)
[17]82{
[157]83#if defined(USE_SLANG)
[159]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
[79]104    int i;
105
[17]106    /* Initialize slang library */
107    SLsig_block_signals();
108    SLtt_get_terminfo();
109
[79]110    if(SLkp_init() == -1)
[17]111    {
112        SLsig_unblock_signals();
[79]113        return -1;
[17]114    }
115
[79]116    SLang_init_tty(-1, 0, 1);
[17]117
[79]118    if(SLsmg_init_smg() == -1)
[17]119    {
120        SLsig_unblock_signals();
[79]121        return -1;
[17]122    }
123
124    SLsig_unblock_signals();
125
126    SLsmg_cls();
[79]127    SLtt_set_cursor_visibility(0);
[17]128    SLsmg_refresh();
[79]129
[159]130    for(i = 0; i < 16; i++)
131        SLtt_set_color(i + 1, NULL, slang_colors[i], "black");
[79]132
[157]133#elif defined(USE_NCURSES)
[159]134    int i;
135
[17]136    initscr();
137    keypad(stdscr, TRUE);
138    nonl();
139    cbreak();
140    noecho();
141    nodelay(stdscr, TRUE);
[79]142    curs_set(0);
143
144    start_color();
145
[159]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);
[79]162
[159]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
[157]169#elif defined(USE_CONIO)
[167]170    gettextinfo(&ti);
171    _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight);
172    if(_ee_screen == NULL)
173        return -1;
[155]174    _wscroll = 0;
175    _setcursortype(_NOCURSOR);
176    clrscr();
[159]177#   if defined(SCREENUPDATE_IN_PC_H)
178    ScreenRetrieve(_ee_screen);
179#   else
180    /* FIXME */
181#   endif
[79]182
[17]183#endif
[167]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
[93]188    _ee_delay = 0;
[17]189
190    return 0;
191}
192
[79]193int ee_get_width(void)
[17]194{
[157]195#if defined(USE_SLANG)
[79]196    return SLtt_Screen_Cols;
[157]197#elif defined(USE_NCURSES)
[79]198    return COLS;
[157]199#elif defined(USE_CONIO)
[155]200    return ti.screenwidth;
[17]201#endif
202}
203
[79]204int ee_get_height(void)
[17]205{
[157]206#if defined(USE_SLANG)
[79]207    return SLtt_Screen_Rows;
[157]208#elif defined(USE_NCURSES)
[79]209    return LINES;
[51]210#else
[155]211    return ti.screenheight;
[17]212#endif
213}
214
[177]215void ee_set_delay(int usec)
[17]216{
[177]217    _ee_delay = usec;
218}
219
220static unsigned int _ee_getticks(void)
221{
222    static unsigned int last_sec = 0, last_usec = 0;
223
[75]224    struct timeval tv;
[177]225    unsigned int ticks = 0;
[75]226
227    gettimeofday(&tv, NULL);
[177]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;
[17]238}
239
[79]240void ee_refresh(void)
[17]241{
[177]242    static int lastticks = 0;
243    unsigned int ticks = lastticks + _ee_getticks();
[75]244
[157]245#if defined(USE_SLANG)
[17]246    SLsmg_refresh();
[157]247#elif defined(USE_NCURSES)
[51]248    refresh();
[157]249#elif defined(USE_CONIO)
[159]250#   if defined(SCREENUPDATE_IN_PC_H)
251    ScreenUpdate(_ee_screen);
252#   else
253    /* FIXME */
254#   endif
[17]255#endif
[75]256
[177]257    /* Wait until _ee_delay + time of last call */
258    for(ticks += _ee_getticks(); ticks < _ee_delay; ticks += _ee_getticks())
259        usleep(10000);
[75]260
[177]261    lastticks = ticks - _ee_delay;
[75]262
[177]263    /* If we drifted too much, it's bad, bad, bad. */
264    if(lastticks > _ee_delay)
265        lastticks = 0;
[17]266}
267
[79]268void ee_end(void)
[17]269{
[157]270#if defined(USE_SLANG)
[79]271    SLtt_set_cursor_visibility(1);
[17]272    SLang_reset_tty();
273    SLsmg_reset_smg();
[157]274#elif defined(USE_NCURSES)
[79]275    curs_set(1);
[51]276    endwin();
[157]277#elif defined(USE_CONIO)
[155]278    _wscroll = 1;
[157]279    textcolor((enum COLORS)WHITE);
280    textbackground((enum COLORS)BLACK);
281    gotoxy(ee_get_width(), ee_get_height());
282    cputs("\r\n");
[155]283    _setcursortype(_NORMALCURSOR);
[17]284#endif
285}
286
Note: See TracBrowser for help on using the repository browser.