source: libcaca/trunk/libee/graphics.c @ 181

Last change on this file since 181 was 181, checked in by Sam Hocevar, 19 years ago
  • libee/ee.c: + Replaced ee_color_names[] with ee_get_color_name(). + Don't oversleep in ee_refresh().
  • libee/graphics.c: + Implemented ee_printf().
  • test/demo.c: + If new keypresses are detected, don't wait for the next screen refresh. + Added an fps counter on demos. + Added controls for outlines and drawing boundaries.
  • Property svn:keywords set to Id
File size: 3.4 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: graphics.c 181 2003-11-15 12:42:38Z 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 <conio.h>
31#else
32#   error "no graphics library detected"
33#endif
34
35#include <string.h>
36#include <stdlib.h>
37#include <stdarg.h>
38
39#include "ee.h"
40#include "ee_internals.h"
41
42static int _ee_color = 0;
43
44void ee_set_color(int color)
45{
46    if(color < 0 || color > 15)
47        return;
48
49    _ee_color = color;
50#if defined(USE_SLANG)
51    SLsmg_set_color(color + 1);
52#elif defined(USE_NCURSES)
53    attrset(_ee_attr[color]);
54#elif defined(USE_CONIO)
55    textcolor(color);
56#endif
57}
58
59int ee_get_color(void)
60{
61    return _ee_color;
62}
63
64void ee_putchar(int x, int y, char c)
65{
66    if(x < 0 || x >= ee_get_width() || y < 0 || y >= ee_get_height())
67        return;
68
69#if defined(USE_SLANG)
70    SLsmg_gotorc(y, x);
71    SLsmg_write_char(c);
72#elif defined(USE_NCURSES)
73    move(y, x);
74    addch(c);
75#elif defined(USE_CONIO)
76    _ee_screen[2 * (x + y * ee_get_width())] = c;
77    _ee_screen[2 * (x + y * ee_get_width()) + 1] = _ee_color;
78//    gotoxy(x + 1, y + 1);
79//    putch(c);
80#endif
81}
82
83void ee_putstr(int x, int y, const char *s)
84{
85    int len;
86
87    if(y < 0 || y >= ee_get_height() || x >= ee_get_width())
88        return;
89
90    len = strlen(s);
91
92    if(x < 0)
93    {
94        len -= -x;
95        if(len < 0)
96            return;
97        s += -x;
98        x = 0;
99    }
100
101    if(x + len >= ee_get_width())
102    {
103        memcpy(_ee_scratch_line, s, ee_get_width() - x);
104        _ee_scratch_line[ee_get_width() - x] = '\0';
105        s = _ee_scratch_line;
106    }
107
108#if defined(USE_SLANG)
109    SLsmg_gotorc(y, x);
110    SLsmg_write_string(s);
111#elif defined(USE_NCURSES)
112    move(y, x);
113    addstr(s);
114#elif defined(USE_CONIO)
115    char *buf = _ee_screen + 2 * (x + y * ee_get_width());
116    while(*s)
117    {
118        *buf++ = *s++;
119        *buf++ = _ee_color;
120    }
121//    gotoxy(x + 1, y + 1);
122//    cputs(s);
123#endif
124}
125
126void ee_printf(int x, int y, const char *format, ...)
127{
128    char tmp[BUFSIZ];
129    char *buf = tmp;
130    va_list args;
131
132    if(y < 0 || y >= ee_get_height() || x >= ee_get_width())
133        return;
134
135    if(ee_get_width() - x + 1 > BUFSIZ)
136        buf = malloc(ee_get_width() - x + 1);
137
138    va_start(args, format);
139    vsnprintf(buf, ee_get_width() - x + 1, format, args);
140    buf[ee_get_width() - x] = '\0';
141    va_end(args);
142
143    ee_putstr(x, y, buf);
144
145    if(buf != tmp)
146        free(buf);
147}
148
149void ee_clear(void)
150{
151    /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
152    int y = ee_get_height();
153
154    while(y--)
155        ee_putstr(0, y, _ee_empty_line);
156}
157
Note: See TracBrowser for help on using the repository browser.