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.

преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. static int _ee_delay;
  43. #if defined(USE_CONIO)
  44. static struct text_info ti;
  45. char *_screen_buffer;
  46. #endif
  47. int ee_init(void)
  48. {
  49. #if defined(USE_SLANG)
  50. static char * colors[] = { "black", "green", "yellow", "white",
  51. "red", "gray", "lightgray", "blue", "cyan", "magenta", NULL };
  52. int i;
  53. /* Initialize slang library */
  54. SLsig_block_signals();
  55. SLtt_get_terminfo();
  56. if(SLkp_init() == -1)
  57. {
  58. SLsig_unblock_signals();
  59. return -1;
  60. }
  61. SLang_init_tty(-1, 0, 1);
  62. if(SLsmg_init_smg() == -1)
  63. {
  64. SLsig_unblock_signals();
  65. return -1;
  66. }
  67. SLsig_unblock_signals();
  68. SLsmg_cls();
  69. SLtt_set_cursor_visibility(0);
  70. SLsmg_refresh();
  71. for(i = 0; colors[i]; i++)
  72. {
  73. SLtt_set_color(i + 1, NULL, colors[i], "black");
  74. }
  75. #elif defined(USE_NCURSES)
  76. /* Initialize ncurses library */
  77. initscr();
  78. keypad(stdscr, TRUE);
  79. nonl();
  80. cbreak();
  81. noecho();
  82. nodelay(stdscr, TRUE);
  83. curs_set(0);
  84. start_color();
  85. init_pair(EE_BLACK, COLOR_BLACK, COLOR_BLACK);
  86. init_pair(EE_GREEN, COLOR_GREEN, COLOR_BLACK);
  87. init_pair(EE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
  88. init_pair(EE_WHITE, COLOR_WHITE, COLOR_BLACK);
  89. init_pair(EE_RED, COLOR_RED, COLOR_BLACK);
  90. init_pair(EE_GRAY, COLOR_WHITE, COLOR_BLACK); // XXX
  91. init_pair(EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK); // XXX
  92. init_pair(EE_BLUE, COLOR_BLUE, COLOR_BLACK);
  93. init_pair(EE_CYAN, COLOR_CYAN, COLOR_BLACK);
  94. init_pair(EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
  95. #elif defined(USE_CONIO)
  96. _wscroll = 0;
  97. _setcursortype(_NOCURSOR);
  98. clrscr();
  99. gettextinfo(&ti);
  100. _screen_buffer = malloc(ee_get_width() * ee_get_height() * 2);
  101. ScreenRetrieve(_screen_buffer);
  102. #endif
  103. _ee_delay = 0;
  104. return 0;
  105. }
  106. void ee_set_delay(int usec)
  107. {
  108. _ee_delay = usec;
  109. }
  110. int ee_get_width(void)
  111. {
  112. #if defined(USE_SLANG)
  113. return SLtt_Screen_Cols;
  114. #elif defined(USE_NCURSES)
  115. return COLS;
  116. #elif defined(USE_CONIO)
  117. return ti.screenwidth;
  118. #endif
  119. }
  120. int ee_get_height(void)
  121. {
  122. #if defined(USE_SLANG)
  123. return SLtt_Screen_Rows;
  124. #elif defined(USE_NCURSES)
  125. return LINES;
  126. #else
  127. return ti.screenheight;
  128. #endif
  129. }
  130. #if !defined(USE_CONIO)
  131. static int64_t local_time(void)
  132. {
  133. struct timeval tv;
  134. int64_t now;
  135. gettimeofday(&tv, NULL);
  136. now = tv.tv_sec;
  137. now *= 1000000;
  138. now += tv.tv_usec;
  139. return now;
  140. }
  141. #endif
  142. void ee_refresh(void)
  143. {
  144. #if !defined(USE_CONIO)
  145. static int64_t local_clock = 0;
  146. int64_t now;
  147. if(!local_clock)
  148. {
  149. /* Initialize local_clock */
  150. local_clock = local_time();
  151. }
  152. if(local_time() > local_clock + 10000)
  153. {
  154. /* If we are late, we shouldn't display anything */
  155. }
  156. #endif
  157. #if defined(USE_SLANG)
  158. SLsmg_refresh();
  159. #elif defined(USE_NCURSES)
  160. refresh();
  161. #elif defined(USE_CONIO)
  162. ScreenUpdate(_screen_buffer);
  163. #endif
  164. #if !defined(USE_CONIO)
  165. now = local_time();
  166. if(now < local_clock + _ee_delay - 10000)
  167. {
  168. usleep(local_clock + _ee_delay - 10000 - now);
  169. }
  170. local_clock += _ee_delay;
  171. #else
  172. delay(5);
  173. #endif
  174. }
  175. void ee_end(void)
  176. {
  177. #if defined(USE_SLANG)
  178. SLtt_set_cursor_visibility(1);
  179. SLang_reset_tty();
  180. SLsmg_reset_smg();
  181. #elif defined(USE_NCURSES)
  182. curs_set(1);
  183. endwin();
  184. #elif defined(USE_CONIO)
  185. ScreenUpdate(_screen_buffer);
  186. _wscroll = 1;
  187. textcolor((enum COLORS)WHITE);
  188. textbackground((enum COLORS)BLACK);
  189. gotoxy(ee_get_width(), ee_get_height());
  190. cputs("\r\n");
  191. _setcursortype(_NORMALCURSOR);
  192. #endif
  193. }