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

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