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 187 2003-11-16 11:26:54Z 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 | |
---|
42 | static int _caca_color = 0; |
---|
43 | |
---|
44 | void 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 | |
---|
59 | int caca_get_color(void) |
---|
60 | { |
---|
61 | return _caca_color; |
---|
62 | } |
---|
63 | |
---|
64 | void caca_putchar(int x, int y, char c) |
---|
65 | { |
---|
66 | if(x < 0 || x >= (int)caca_get_width() || |
---|
67 | y < 0 || y >= (int)caca_get_height()) |
---|
68 | return; |
---|
69 | |
---|
70 | #if defined(USE_SLANG) |
---|
71 | SLsmg_gotorc(y, x); |
---|
72 | SLsmg_write_char(c); |
---|
73 | #elif defined(USE_NCURSES) |
---|
74 | move(y, x); |
---|
75 | addch(c); |
---|
76 | #elif defined(USE_CONIO) |
---|
77 | _caca_screen[2 * (x + y * caca_get_width())] = c; |
---|
78 | _caca_screen[2 * (x + y * caca_get_width()) + 1] = _caca_color; |
---|
79 | // gotoxy(x + 1, y + 1); |
---|
80 | // putch(c); |
---|
81 | #endif |
---|
82 | } |
---|
83 | |
---|
84 | void caca_putstr(int x, int y, const char *s) |
---|
85 | { |
---|
86 | unsigned int len; |
---|
87 | |
---|
88 | if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width()) |
---|
89 | return; |
---|
90 | |
---|
91 | len = strlen(s); |
---|
92 | |
---|
93 | if(x < 0) |
---|
94 | { |
---|
95 | len -= -x; |
---|
96 | if(len < 0) |
---|
97 | return; |
---|
98 | s += -x; |
---|
99 | x = 0; |
---|
100 | } |
---|
101 | |
---|
102 | if(x + len >= caca_get_width()) |
---|
103 | { |
---|
104 | memcpy(_caca_scratch_line, s, caca_get_width() - x); |
---|
105 | _caca_scratch_line[caca_get_width() - x] = '\0'; |
---|
106 | s = _caca_scratch_line; |
---|
107 | } |
---|
108 | |
---|
109 | #if defined(USE_SLANG) |
---|
110 | SLsmg_gotorc(y, x); |
---|
111 | SLsmg_write_string(s); |
---|
112 | #elif defined(USE_NCURSES) |
---|
113 | move(y, x); |
---|
114 | addstr(s); |
---|
115 | #elif defined(USE_CONIO) |
---|
116 | char *buf = _caca_screen + 2 * (x + y * caca_get_width()); |
---|
117 | while(*s) |
---|
118 | { |
---|
119 | *buf++ = *s++; |
---|
120 | *buf++ = _caca_color; |
---|
121 | } |
---|
122 | // gotoxy(x + 1, y + 1); |
---|
123 | // cputs(s); |
---|
124 | #endif |
---|
125 | } |
---|
126 | |
---|
127 | void caca_printf(int x, int y, const char *format, ...) |
---|
128 | { |
---|
129 | char tmp[BUFSIZ]; |
---|
130 | char *buf = tmp; |
---|
131 | va_list args; |
---|
132 | |
---|
133 | if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width()) |
---|
134 | return; |
---|
135 | |
---|
136 | if(caca_get_width() - x + 1 > BUFSIZ) |
---|
137 | buf = malloc(caca_get_width() - x + 1); |
---|
138 | |
---|
139 | va_start(args, format); |
---|
140 | vsnprintf(buf, caca_get_width() - x + 1, format, args); |
---|
141 | buf[caca_get_width() - x] = '\0'; |
---|
142 | va_end(args); |
---|
143 | |
---|
144 | caca_putstr(x, y, buf); |
---|
145 | |
---|
146 | if(buf != tmp) |
---|
147 | free(buf); |
---|
148 | } |
---|
149 | |
---|
150 | void caca_clear(void) |
---|
151 | { |
---|
152 | /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */ |
---|
153 | int y = caca_get_height(); |
---|
154 | |
---|
155 | while(y--) |
---|
156 | caca_putstr(0, y, _caca_empty_line); |
---|
157 | } |
---|
158 | |
---|