Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

300 lignes
6.6 KiB

  1. /*
  2. * libee ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  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. #include "config.h"
  23. #if defined(USE_SLANG)
  24. # include <slang.h>
  25. #elif defined(USE_NCURSES)
  26. # include <curses.h>
  27. #elif defined(USE_CONIO)
  28. # include <dos.h>
  29. # include <conio.h>
  30. # if defined(SCREENUPDATE_IN_PC_H)
  31. # include <pc.h>
  32. # endif
  33. #else
  34. # error "no graphics library detected"
  35. #endif
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39. #include <sys/time.h>
  40. #include <time.h>
  41. #include "ee.h"
  42. #include "ee_internals.h"
  43. /* Global array with color names */
  44. const char *ee_color_names[16] =
  45. {
  46. "black",
  47. "blue",
  48. "green",
  49. "cyan",
  50. "red",
  51. "magenta",
  52. "brown",
  53. "lightgray",
  54. "darkgray",
  55. "lightblue",
  56. "lightgreen",
  57. "lightcyan",
  58. "lightred",
  59. "lightmagenta",
  60. "yellow",
  61. "white",
  62. };
  63. static unsigned int _ee_delay;
  64. static unsigned int _ee_rendertime;
  65. char *_ee_empty_line;
  66. char *_ee_scratch_line;
  67. #if defined(USE_NCURSES)
  68. int _ee_attr[16];
  69. #endif
  70. #if defined(USE_CONIO)
  71. static struct text_info ti;
  72. char *_ee_screen;
  73. #endif
  74. int ee_init(void)
  75. {
  76. #if defined(USE_SLANG)
  77. static char *slang_colors[16] =
  78. {
  79. "black",
  80. "blue",
  81. "green",
  82. "cyan",
  83. "red",
  84. "magenta",
  85. "brown",
  86. "lightgray",
  87. "gray",
  88. "brightblue",
  89. "brightgreen",
  90. "brightcyan",
  91. "brightred",
  92. "brightmagenta",
  93. "yellow",
  94. "white",
  95. };
  96. int i;
  97. /* Initialize slang library */
  98. SLsig_block_signals();
  99. SLtt_get_terminfo();
  100. if(SLkp_init() == -1)
  101. {
  102. SLsig_unblock_signals();
  103. return -1;
  104. }
  105. SLang_init_tty(-1, 0, 1);
  106. if(SLsmg_init_smg() == -1)
  107. {
  108. SLsig_unblock_signals();
  109. return -1;
  110. }
  111. SLsig_unblock_signals();
  112. SLsmg_cls();
  113. SLtt_set_cursor_visibility(0);
  114. SLsmg_refresh();
  115. for(i = 0; i < 16; i++)
  116. SLtt_set_color(i + 1, NULL, slang_colors[i], "black");
  117. #elif defined(USE_NCURSES)
  118. int i;
  119. initscr();
  120. keypad(stdscr, TRUE);
  121. nonl();
  122. cbreak();
  123. noecho();
  124. nodelay(stdscr, TRUE);
  125. curs_set(0);
  126. start_color();
  127. init_pair(1 + EE_BLACK, COLOR_BLACK, COLOR_BLACK);
  128. init_pair(1 + EE_BLUE, COLOR_BLUE, COLOR_BLACK);
  129. init_pair(1 + EE_GREEN, COLOR_GREEN, COLOR_BLACK);
  130. init_pair(1 + EE_CYAN, COLOR_CYAN, COLOR_BLACK);
  131. init_pair(1 + EE_RED, COLOR_RED, COLOR_BLACK);
  132. init_pair(1 + EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
  133. init_pair(1 + EE_BROWN, COLOR_YELLOW, COLOR_BLACK);
  134. init_pair(1 + EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK);
  135. init_pair(1 + EE_DARKGRAY, COLOR_BLACK, COLOR_BLACK);
  136. init_pair(1 + EE_LIGHTBLUE, COLOR_BLUE, COLOR_BLACK);
  137. init_pair(1 + EE_LIGHTGREEN, COLOR_GREEN, COLOR_BLACK);
  138. init_pair(1 + EE_LIGHTCYAN, COLOR_CYAN, COLOR_BLACK);
  139. init_pair(1 + EE_LIGHTRED, COLOR_RED, COLOR_BLACK);
  140. init_pair(1 + EE_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK);
  141. init_pair(1 + EE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
  142. init_pair(1 + EE_WHITE, COLOR_WHITE, COLOR_BLACK);
  143. for(i = 0; i < 8; i++)
  144. {
  145. _ee_attr[i] = COLOR_PAIR(1 + i);
  146. _ee_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i);
  147. }
  148. #elif defined(USE_CONIO)
  149. gettextinfo(&ti);
  150. _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight);
  151. if(_ee_screen == NULL)
  152. return -1;
  153. _wscroll = 0;
  154. _setcursortype(_NOCURSOR);
  155. clrscr();
  156. # if defined(SCREENUPDATE_IN_PC_H)
  157. ScreenRetrieve(_ee_screen);
  158. # else
  159. /* FIXME */
  160. # endif
  161. #endif
  162. _ee_empty_line = malloc(ee_get_width() + 1);
  163. memset(_ee_empty_line, ' ', ee_get_width());
  164. _ee_empty_line[ee_get_width()] = '\0';
  165. _ee_scratch_line = malloc(ee_get_width() + 1);
  166. _ee_delay = 0;
  167. _ee_rendertime = 0;
  168. return 0;
  169. }
  170. unsigned int ee_get_width(void)
  171. {
  172. #if defined(USE_SLANG)
  173. return SLtt_Screen_Cols;
  174. #elif defined(USE_NCURSES)
  175. return COLS;
  176. #elif defined(USE_CONIO)
  177. return ti.screenwidth;
  178. #endif
  179. }
  180. unsigned int ee_get_height(void)
  181. {
  182. #if defined(USE_SLANG)
  183. return SLtt_Screen_Rows;
  184. #elif defined(USE_NCURSES)
  185. return LINES;
  186. #else
  187. return ti.screenheight;
  188. #endif
  189. }
  190. void ee_set_delay(unsigned int usec)
  191. {
  192. _ee_delay = usec;
  193. }
  194. unsigned int ee_get_rendertime(void)
  195. {
  196. return _ee_rendertime;
  197. }
  198. static unsigned int _ee_getticks(void)
  199. {
  200. static unsigned int last_sec = 0, last_usec = 0;
  201. struct timeval tv;
  202. unsigned int ticks = 0;
  203. gettimeofday(&tv, NULL);
  204. if(last_sec != 0)
  205. {
  206. ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec);
  207. }
  208. last_sec = tv.tv_sec;
  209. last_usec = tv.tv_usec;
  210. return ticks;
  211. }
  212. void ee_refresh(void)
  213. {
  214. static unsigned int lastticks = 0;
  215. unsigned int ticks = lastticks + _ee_getticks();
  216. #if defined(USE_SLANG)
  217. SLsmg_refresh();
  218. #elif defined(USE_NCURSES)
  219. refresh();
  220. #elif defined(USE_CONIO)
  221. # if defined(SCREENUPDATE_IN_PC_H)
  222. ScreenUpdate(_ee_screen);
  223. # else
  224. /* FIXME */
  225. # endif
  226. #endif
  227. /* Wait until _ee_delay + time of last call */
  228. for(ticks += _ee_getticks(); ticks < _ee_delay; ticks += _ee_getticks())
  229. usleep(10000);
  230. /* Update the sliding mean of the render time */
  231. _ee_rendertime = (7 * _ee_rendertime + ticks) / 8;
  232. lastticks = ticks - _ee_delay;
  233. /* If we drifted too much, it's bad, bad, bad. */
  234. if(lastticks > _ee_delay)
  235. lastticks = 0;
  236. }
  237. void ee_end(void)
  238. {
  239. #if defined(USE_SLANG)
  240. SLtt_set_cursor_visibility(1);
  241. SLang_reset_tty();
  242. SLsmg_reset_smg();
  243. #elif defined(USE_NCURSES)
  244. curs_set(1);
  245. endwin();
  246. #elif defined(USE_CONIO)
  247. _wscroll = 1;
  248. textcolor((enum COLORS)WHITE);
  249. textbackground((enum COLORS)BLACK);
  250. gotoxy(ee_get_width(), ee_get_height());
  251. cputs("\r\n");
  252. _setcursortype(_NORMALCURSOR);
  253. #endif
  254. }