You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

365 lines
8.5 KiB

  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. /** \file graphics.c
  22. * \version \$Id$
  23. * \author Sam Hocevar <sam@zoy.org>
  24. * \brief Character drawing functions
  25. *
  26. * This file contains character and string drawing functions.
  27. */
  28. #include "config.h"
  29. #if defined(USE_SLANG)
  30. # include <slang.h>
  31. #elif defined(USE_NCURSES)
  32. # include <curses.h>
  33. #elif defined(USE_CONIO)
  34. # include <conio.h>
  35. #else
  36. # error "no graphics library detected"
  37. #endif
  38. #include <stdio.h> /* BUFSIZ */
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <stdarg.h>
  43. #include <sys/time.h>
  44. #include <time.h>
  45. #include "caca.h"
  46. #include "caca_internals.h"
  47. static unsigned int _caca_delay;
  48. static unsigned int _caca_rendertime;
  49. static enum caca_color _caca_fgcolor = CACA_COLOR_LIGHTGRAY;
  50. static enum caca_color _caca_bgcolor = CACA_COLOR_BLACK;
  51. void caca_set_color(enum caca_color fgcolor, enum caca_color bgcolor)
  52. {
  53. if(fgcolor < 0 || fgcolor > 15 || bgcolor < 0 || bgcolor > 15)
  54. return;
  55. _caca_fgcolor = fgcolor;
  56. _caca_bgcolor = bgcolor;
  57. #if defined(USE_SLANG)
  58. SLsmg_set_color(fgcolor + 16 * bgcolor);
  59. #elif defined(USE_NCURSES)
  60. attrset(_caca_attr[fgcolor + 16 * bgcolor]);
  61. #elif defined(USE_CONIO)
  62. textbackground(bgcolor);
  63. textcolor(fgcolor);
  64. #endif
  65. }
  66. enum caca_color caca_get_fg_color(void)
  67. {
  68. return _caca_fgcolor;
  69. }
  70. enum caca_color caca_get_bg_color(void)
  71. {
  72. return _caca_bgcolor;
  73. }
  74. void caca_putchar(int x, int y, char c)
  75. {
  76. #if defined(USE_CONIO)
  77. char *data;
  78. #endif
  79. if(x < 0 || x >= (int)caca_get_width() ||
  80. y < 0 || y >= (int)caca_get_height())
  81. return;
  82. #if defined(USE_SLANG)
  83. SLsmg_gotorc(y, x);
  84. SLsmg_write_char(c);
  85. #elif defined(USE_NCURSES)
  86. move(y, x);
  87. addch(c);
  88. #elif defined(USE_CONIO)
  89. data = _caca_screen + 2 * (x + y * caca_get_width());
  90. data[0] = c;
  91. data[1] = (_caca_bgcolor << 4) | _caca_fgcolor;
  92. // gotoxy(x + 1, y + 1);
  93. // putch(c);
  94. #endif
  95. }
  96. void caca_putstr(int x, int y, const char *s)
  97. {
  98. unsigned int len;
  99. if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width())
  100. return;
  101. len = strlen(s);
  102. if(x < 0)
  103. {
  104. len -= -x;
  105. if(len < 0)
  106. return;
  107. s += -x;
  108. x = 0;
  109. }
  110. if(x + len >= caca_get_width())
  111. {
  112. memcpy(_caca_scratch_line, s, caca_get_width() - x);
  113. _caca_scratch_line[caca_get_width() - x] = '\0';
  114. s = _caca_scratch_line;
  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_bgcolor << 4) | _caca_fgcolor;
  128. }
  129. // gotoxy(x + 1, y + 1);
  130. // cputs(s);
  131. #endif
  132. }
  133. void caca_printf(int x, int y, const char *format, ...)
  134. {
  135. char tmp[BUFSIZ];
  136. char *buf = tmp;
  137. va_list args;
  138. if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width())
  139. return;
  140. if(caca_get_width() - x + 1 > BUFSIZ)
  141. buf = malloc(caca_get_width() - x + 1);
  142. va_start(args, format);
  143. #if defined(HAVE_VSNPRINTF)
  144. vsnprintf(buf, caca_get_width() - x + 1, format, args);
  145. #else
  146. vsprintf(buf, format, args);
  147. #endif
  148. buf[caca_get_width() - x] = '\0';
  149. va_end(args);
  150. caca_putstr(x, y, buf);
  151. if(buf != tmp)
  152. free(buf);
  153. }
  154. void caca_clear(void)
  155. {
  156. enum caca_color oldfg = caca_get_fg_color();
  157. enum caca_color oldbg = caca_get_bg_color();
  158. int y = caca_get_height();
  159. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  160. /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
  161. while(y--)
  162. caca_putstr(0, y, _caca_empty_line);
  163. caca_set_color(oldfg, oldbg);
  164. }
  165. int _caca_init_graphics(void)
  166. {
  167. #if defined(USE_SLANG)
  168. /* See SLang ref., 5.4.4. */
  169. static char *slang_colors[16] =
  170. {
  171. "black",
  172. "blue",
  173. "green",
  174. "cyan",
  175. "red",
  176. "magenta",
  177. "brown",
  178. "lightgray",
  179. "gray",
  180. "brightblue",
  181. "brightgreen",
  182. "brightcyan",
  183. "brightred",
  184. "brightmagenta",
  185. "yellow",
  186. "white",
  187. };
  188. int fg, bg;
  189. for(bg = 0; bg < 16; bg++)
  190. for(fg = 0; fg < 16; fg++)
  191. {
  192. int i = fg + 16 * bg;
  193. SLtt_set_color(i, NULL, slang_colors[fg], slang_colors[bg]);
  194. }
  195. #elif defined(USE_NCURSES)
  196. static int curses_colors[] =
  197. {
  198. COLOR_BLACK,
  199. COLOR_BLUE,
  200. COLOR_GREEN,
  201. COLOR_CYAN,
  202. COLOR_RED,
  203. COLOR_MAGENTA,
  204. COLOR_YELLOW,
  205. COLOR_WHITE,
  206. /* Extra values for xterm-16color */
  207. COLOR_BLACK + 8,
  208. COLOR_BLUE + 8,
  209. COLOR_GREEN + 8,
  210. COLOR_CYAN + 8,
  211. COLOR_RED + 8,
  212. COLOR_MAGENTA + 8,
  213. COLOR_YELLOW + 8,
  214. COLOR_WHITE + 8
  215. };
  216. int fg, bg, max;
  217. /* Activate colour */
  218. start_color();
  219. max = COLORS >= 16 ? 16 : 8;
  220. for(bg = 0; bg < max; bg++)
  221. for(fg = 0; fg < max; fg++)
  222. {
  223. /* Use ((max + 7 - fg) % max) instead of fg so that colour 0
  224. * is light gray on black, since some terminals don't like
  225. * this colour pair to be redefined. */
  226. int col = ((max + 7 - fg) % max) + max * bg;
  227. init_pair(col, curses_colors[fg], curses_colors[bg]);
  228. _caca_attr[fg + 16 * bg] = COLOR_PAIR(col);
  229. if(max == 8)
  230. {
  231. /* Bright fg on simple bg */
  232. _caca_attr[fg + 8 + 16 * bg] = A_BOLD | COLOR_PAIR(col);
  233. /* Simple fg on bright bg */
  234. _caca_attr[fg + 16 * (bg + 8)] = A_BLINK | COLOR_PAIR(col);
  235. /* Bright fg on bright bg */
  236. _caca_attr[fg + 8 + 16 * (bg + 8)] = A_BLINK | A_BOLD | COLOR_PAIR(col);
  237. }
  238. }
  239. #elif defined(USE_CONIO)
  240. gettextinfo(&ti);
  241. _caca_screen = malloc(2 * ti.screenwidth * ti.screenheight);
  242. if(_caca_screen == NULL)
  243. return -1;
  244. # if defined(SCREENUPDATE_IN_PC_H)
  245. ScreenRetrieve(_caca_screen);
  246. # else
  247. /* FIXME */
  248. # endif
  249. #endif
  250. _caca_empty_line = malloc(caca_get_width() + 1);
  251. memset(_caca_empty_line, ' ', caca_get_width());
  252. _caca_empty_line[caca_get_width()] = '\0';
  253. _caca_scratch_line = malloc(caca_get_width() + 1);
  254. _caca_delay = 0;
  255. _caca_rendertime = 0;
  256. return 0;
  257. }
  258. void caca_set_delay(unsigned int usec)
  259. {
  260. _caca_delay = usec;
  261. }
  262. unsigned int caca_get_rendertime(void)
  263. {
  264. return _caca_rendertime;
  265. }
  266. static unsigned int _caca_getticks(void)
  267. {
  268. static unsigned int last_sec = 0, last_usec = 0;
  269. struct timeval tv;
  270. unsigned int ticks = 0;
  271. gettimeofday(&tv, NULL);
  272. if(last_sec != 0)
  273. {
  274. ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec);
  275. }
  276. last_sec = tv.tv_sec;
  277. last_usec = tv.tv_usec;
  278. return ticks;
  279. }
  280. void caca_refresh(void)
  281. {
  282. #define IDLE_USEC 10000
  283. static int lastticks = 0;
  284. int ticks = lastticks + _caca_getticks();
  285. #if defined(USE_SLANG)
  286. SLsmg_refresh();
  287. #elif defined(USE_NCURSES)
  288. refresh();
  289. #elif defined(USE_CONIO)
  290. # if defined(SCREENUPDATE_IN_PC_H)
  291. ScreenUpdate(_caca_screen);
  292. # else
  293. /* FIXME */
  294. # endif
  295. #endif
  296. /* Wait until _caca_delay + time of last call */
  297. ticks += _caca_getticks();
  298. for(; ticks + IDLE_USEC < (int)_caca_delay; ticks += _caca_getticks())
  299. usleep(IDLE_USEC);
  300. /* Update the sliding mean of the render time */
  301. _caca_rendertime = (7 * _caca_rendertime + ticks) / 8;
  302. lastticks = ticks - _caca_delay;
  303. /* If we drifted too much, it's bad, bad, bad. */
  304. if(lastticks > (int)_caca_delay)
  305. lastticks = 0;
  306. }