source: ttyvaders/trunk/libcaca/graphics.c @ 186

Last change on this file since 186 was 186, checked in by Sam Hocevar, 17 years ago
  • Renamed libee to libcaca. Far less collisions.
  • Property svn:keywords set to Id
File size: 3.5 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 186 2003-11-16 00:33:35Z 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 "caca.h"
40#include "caca_internals.h"
41
42static int _caca_color = 0;
43
44void caca_set_color(int color)
45{
46    if(color < 0 || color > 15)
47        return;
48
49    _caca_color = color;
50#if defined(USE_SLANG)
51    SLsmg_set_color(color + 1);
52#elif defined(USE_NCURSES)
53    attrset(_caca_attr[color]);
54#elif defined(USE_CONIO)
55    textcolor(color);
56#endif
57}
58
59int caca_get_color(void)
60{
61    return _caca_color;
62}
63
64void caca_putchar(int x, int y, char c)
65{
66    if(x < 0 || x >= caca_get_width() || y < 0 || y >= caca_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    _caca_screen[2 * (x + y * caca_get_width())] = c;
77    _caca_screen[2 * (x + y * caca_get_width()) + 1] = _caca_color;
78//    gotoxy(x + 1, y + 1);
79//    putch(c);
80#endif
81}
82
83void caca_putstr(int x, int y, const char *s)
84{
85    int len;
86
87    if(y < 0 || y >= caca_get_height() || x >= caca_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 >= caca_get_width())
102    {
103        memcpy(_caca_scratch_line, s, caca_get_width() - x);
104        _caca_scratch_line[caca_get_width() - x] = '\0';
105        s = _caca_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 = _caca_screen + 2 * (x + y * caca_get_width());
116    while(*s)
117    {
118        *buf++ = *s++;
119        *buf++ = _caca_color;
120    }
121//    gotoxy(x + 1, y + 1);
122//    cputs(s);
123#endif
124}
125
126void caca_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 >= caca_get_height() || x >= caca_get_width())
133        return;
134
135    if(caca_get_width() - x + 1 > BUFSIZ)
136        buf = malloc(caca_get_width() - x + 1);
137
138    va_start(args, format);
139    vsnprintf(buf, caca_get_width() - x + 1, format, args);
140    buf[caca_get_width() - x] = '\0';
141    va_end(args);
142
143    caca_putstr(x, y, buf);
144
145    if(buf != tmp)
146        free(buf);
147}
148
149void caca_clear(void)
150{
151    /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
152    int y = caca_get_height();
153
154    while(y--)
155        caca_putstr(0, y, _caca_empty_line);
156}
157
Note: See TracBrowser for help on using the repository browser.