source: libcaca/trunk/src/graphics.c @ 192

Last change on this file since 192 was 192, checked in by Sam Hocevar, 19 years ago
  • Changed copyleft to LGPL.
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1/*
2 *   libcaca       ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: graphics.c 192 2003-11-16 12:28:29Z sam $
7 *
8 *   This library is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU Lesser General Public
10 *   License as published by the Free Software Foundation; either
11 *   version 2 of the License, or (at your option) any later version.
12 *
13 *   This library 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 GNU
16 *   Lesser General Public License for more details.
17 *
18 *   You should have received a copy of the GNU Lesser General Public
19 *   License along with this library; if not, write to the Free Software
20 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 *   02111-1307  USA
22 */
23
24#include "config.h"
25
26#if defined(USE_SLANG)
27#   include <slang.h>
28#elif defined(USE_NCURSES)
29#   include <curses.h>
30#elif defined(USE_CONIO)
31#   include <conio.h>
32#else
33#   error "no graphics library detected"
34#endif
35
36#include <string.h>
37#include <stdlib.h>
38#include <stdarg.h>
39
40#include "caca.h"
41#include "caca_internals.h"
42
43static int _caca_color = 0;
44
45void caca_set_color(int color)
46{
47    if(color < 0 || color > 15)
48        return;
49
50    _caca_color = color;
51#if defined(USE_SLANG)
52    SLsmg_set_color(color + 1);
53#elif defined(USE_NCURSES)
54    attrset(_caca_attr[color]);
55#elif defined(USE_CONIO)
56    textcolor(color);
57#endif
58}
59
60int caca_get_color(void)
61{
62    return _caca_color;
63}
64
65void caca_putchar(int x, int y, char c)
66{
67    if(x < 0 || x >= (int)caca_get_width() ||
68       y < 0 || y >= (int)caca_get_height())
69        return;
70
71#if defined(USE_SLANG)
72    SLsmg_gotorc(y, x);
73    SLsmg_write_char(c);
74#elif defined(USE_NCURSES)
75    move(y, x);
76    addch(c);
77#elif defined(USE_CONIO)
78    _caca_screen[2 * (x + y * caca_get_width())] = c;
79    _caca_screen[2 * (x + y * caca_get_width()) + 1] = _caca_color;
80//    gotoxy(x + 1, y + 1);
81//    putch(c);
82#endif
83}
84
85void caca_putstr(int x, int y, const char *s)
86{
87    unsigned int len;
88
89    if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width())
90        return;
91
92    len = strlen(s);
93
94    if(x < 0)
95    {
96        len -= -x;
97        if(len < 0)
98            return;
99        s += -x;
100        x = 0;
101    }
102
103    if(x + len >= caca_get_width())
104    {
105        memcpy(_caca_scratch_line, s, caca_get_width() - x);
106        _caca_scratch_line[caca_get_width() - x] = '\0';
107        s = _caca_scratch_line;
108    }
109
110#if defined(USE_SLANG)
111    SLsmg_gotorc(y, x);
112    SLsmg_write_string(s);
113#elif defined(USE_NCURSES)
114    move(y, x);
115    addstr(s);
116#elif defined(USE_CONIO)
117    char *buf = _caca_screen + 2 * (x + y * caca_get_width());
118    while(*s)
119    {
120        *buf++ = *s++;
121        *buf++ = _caca_color;
122    }
123//    gotoxy(x + 1, y + 1);
124//    cputs(s);
125#endif
126}
127
128void caca_printf(int x, int y, const char *format, ...)
129{
130    char tmp[BUFSIZ];
131    char *buf = tmp;
132    va_list args;
133
134    if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width())
135        return;
136
137    if(caca_get_width() - x + 1 > BUFSIZ)
138        buf = malloc(caca_get_width() - x + 1);
139
140    va_start(args, format);
141    vsnprintf(buf, caca_get_width() - x + 1, format, args);
142    buf[caca_get_width() - x] = '\0';
143    va_end(args);
144
145    caca_putstr(x, y, buf);
146
147    if(buf != tmp)
148        free(buf);
149}
150
151void caca_clear(void)
152{
153    /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
154    int y = caca_get_height();
155
156    while(y--)
157        caca_putstr(0, y, _caca_empty_line);
158}
159
Note: See TracBrowser for help on using the repository browser.