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

Last change on this file since 76 was 76, checked in by Sam Hocevar, 17 years ago
  • libee/ee.c: + First file in libee, from src/graphics.c. + Disable cursor upon initialisation. + Added delay code for constant framerate.
  • src/common.h: + Minor compilation fix for latest ncurses.
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1/*
2 *   ttyvaders     Textmode shoot'em up
3 *   Copyright (c) 2002 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: ee.c 76 2003-11-09 11:26:08Z 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#include <stdlib.h>
26#include <unistd.h>
27#include <sys/time.h>
28#include <time.h>
29
30#include "common.h"
31
32int init_graphics( void )
33{
34#ifdef USE_SLANG
35    /* Initialize slang library */
36    SLsig_block_signals();
37    SLtt_get_terminfo();
38
39    if( SLkp_init() == -1 )
40    {
41        SLsig_unblock_signals();
42        return 1;
43    }
44
45    SLang_init_tty (-1, 0, 1);
46
47    if( SLsmg_init_smg() == -1 )
48    {
49        SLsig_unblock_signals();
50        return 1;
51    }
52
53    SLsig_unblock_signals();
54
55    SLsmg_cls();
56    SLtt_set_cursor_visibility( 0 );
57    SLsmg_refresh();
58#elif USE_NCURSES
59    /* Initialize ncurses library */
60    initscr();
61    keypad(stdscr, TRUE);
62    nonl();
63    cbreak();
64    noecho();
65    nodelay(stdscr, TRUE);
66    curs_set( 0 );
67#else
68    /* Dummy driver */
69#endif
70
71    return 0;
72}
73
74void init_game( game *g )
75{
76#ifdef USE_SLANG
77    static char * const colors[] =
78    {
79        "black", "green", "yellow", "white",
80        "red", "gray", "lightgray", "blue", "cyan", "magenta", NULL
81    };
82
83    int i;
84
85    for( i = 0; colors[i] ; i++ )
86    {
87        SLtt_set_color( i+1, NULL, colors[i], "black" );
88    }
89
90    g->w = SLtt_Screen_Cols;
91    g->h = SLtt_Screen_Rows;
92#elif USE_NCURSES
93    start_color();
94
95    init_pair( BLACK, COLOR_BLACK, COLOR_BLACK );
96    init_pair( GREEN, COLOR_GREEN, COLOR_BLACK );
97    init_pair( YELLOW, COLOR_YELLOW, COLOR_BLACK );
98    init_pair( WHITE, COLOR_WHITE, COLOR_BLACK );
99    init_pair( RED, COLOR_RED, COLOR_BLACK );
100    init_pair( GRAY, COLOR_WHITE, COLOR_BLACK ); // XXX
101    init_pair( LIGHTGRAY, COLOR_WHITE, COLOR_BLACK ); // XXX
102    init_pair( BLUE, COLOR_BLUE, COLOR_BLACK );
103    init_pair( CYAN, COLOR_CYAN, COLOR_BLACK );
104    init_pair( MAGENTA, COLOR_MAGENTA, COLOR_BLACK );
105
106    g->w = COLS;
107    g->h = LINES;
108#else
109    /* Use dummy driver */
110    g->w = 80;
111    g->h = 25;
112#endif
113}
114
115char get_key( void )
116{
117#ifdef USE_SLANG
118    if( SLang_input_pending (0) )
119    {
120        return SLang_getkey();
121    }
122#elif USE_NCURSES
123    char key;
124
125    if( ( key = getch() ) != ERR )
126    {
127        return key;
128    }
129#else
130    /* Use dummy driver */
131    char key = GET_RAND(0,256);
132
133    if( key != 'q' && key != 'p' && key != '\t' )
134    {
135        return key;
136    }
137#endif
138
139    return 0;
140}
141
142void clear_graphics( game *g )
143{
144#ifdef USE_SLANG
145    //SLsmg_cls();
146    int y;
147    for( y = 0; y < g->h; y++ )
148    {
149        gfx_goto( 0, y );
150        gfx_putstr( "                                                                                " );
151    }
152#elif USE_NCURSES
153    //clear();
154    int y;
155    for( y = 0; y < g->h; y++ )
156    {
157        gfx_goto( 0, y );
158        gfx_putstr( "                                                                                " );
159    }
160#else
161    /* Use dummy driver */
162#endif
163}
164
165static int64_t local_time(void)
166{
167    struct timeval tv;
168    int64_t now;
169
170    gettimeofday(&tv, NULL);
171    now = tv.tv_sec;
172    now *= 1000000;
173    now += tv.tv_usec;
174    return now;
175}
176
177#define DELAY 40000
178
179void refresh_graphics( void )
180{
181    static int64_t local_clock = 0;
182    int64_t now;
183
184    gfx_goto( 0, 0 );
185
186    if( !local_clock )
187    {
188        /* Initialize local_clock */
189        local_clock = local_time();
190    }
191
192    if( local_time() > local_clock + 10000 )
193    {
194        /* If we are late, we shouldn't display anything */
195    }
196
197#ifdef USE_SLANG
198    SLsmg_refresh();
199#elif USE_NCURSES
200    refresh();
201#else
202    /* Use dummy driver */
203#endif
204
205    now = local_time();
206
207    if( now < local_clock + DELAY - 10000 )
208    {
209        usleep( local_clock + DELAY - 10000 - now );
210    }
211
212    local_clock += DELAY;
213}
214
215void end_graphics( void )
216{
217#ifdef USE_SLANG
218    SLtt_set_cursor_visibility( 1 );
219    SLang_reset_tty();
220    SLsmg_reset_smg();
221#elif USE_NCURSES
222    curs_set( 1 );
223    endwin();
224#else
225    /* Use dummy driver */
226#endif
227}
228
Note: See TracBrowser for help on using the repository browser.