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.
 
 
 
 
 

158 lines
3.4 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 <conio.h>
  29. #else
  30. # error "no graphics library detected"
  31. #endif
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <stdarg.h>
  35. #include "ee.h"
  36. #include "ee_internals.h"
  37. static int _ee_color = 0;
  38. void ee_set_color(int color)
  39. {
  40. if(color < 0 || color > 15)
  41. return;
  42. _ee_color = color;
  43. #if defined(USE_SLANG)
  44. SLsmg_set_color(color + 1);
  45. #elif defined(USE_NCURSES)
  46. attrset(_ee_attr[color]);
  47. #elif defined(USE_CONIO)
  48. textcolor(color);
  49. #endif
  50. }
  51. int ee_get_color(void)
  52. {
  53. return _ee_color;
  54. }
  55. void ee_putchar(int x, int y, char c)
  56. {
  57. if(x < 0 || x >= ee_get_width() || y < 0 || y >= ee_get_height())
  58. return;
  59. #if defined(USE_SLANG)
  60. SLsmg_gotorc(y, x);
  61. SLsmg_write_char(c);
  62. #elif defined(USE_NCURSES)
  63. move(y, x);
  64. addch(c);
  65. #elif defined(USE_CONIO)
  66. _ee_screen[2 * (x + y * ee_get_width())] = c;
  67. _ee_screen[2 * (x + y * ee_get_width()) + 1] = _ee_color;
  68. // gotoxy(x + 1, y + 1);
  69. // putch(c);
  70. #endif
  71. }
  72. void ee_putstr(int x, int y, const char *s)
  73. {
  74. int len;
  75. if(y < 0 || y >= ee_get_height() || x >= ee_get_width())
  76. return;
  77. len = strlen(s);
  78. if(x < 0)
  79. {
  80. len -= -x;
  81. if(len < 0)
  82. return;
  83. s += -x;
  84. x = 0;
  85. }
  86. if(x + len >= ee_get_width())
  87. {
  88. memcpy(_ee_scratch_line, s, ee_get_width() - x);
  89. _ee_scratch_line[ee_get_width() - x] = '\0';
  90. s = _ee_scratch_line;
  91. }
  92. #if defined(USE_SLANG)
  93. SLsmg_gotorc(y, x);
  94. SLsmg_write_string(s);
  95. #elif defined(USE_NCURSES)
  96. move(y, x);
  97. addstr(s);
  98. #elif defined(USE_CONIO)
  99. char *buf = _ee_screen + 2 * (x + y * ee_get_width());
  100. while(*s)
  101. {
  102. *buf++ = *s++;
  103. *buf++ = _ee_color;
  104. }
  105. // gotoxy(x + 1, y + 1);
  106. // cputs(s);
  107. #endif
  108. }
  109. void ee_printf(int x, int y, const char *format, ...)
  110. {
  111. char tmp[BUFSIZ];
  112. char *buf = tmp;
  113. va_list args;
  114. if(y < 0 || y >= ee_get_height() || x >= ee_get_width())
  115. return;
  116. if(ee_get_width() - x + 1 > BUFSIZ)
  117. buf = malloc(ee_get_width() - x + 1);
  118. va_start(args, format);
  119. vsnprintf(buf, ee_get_width() - x + 1, format, args);
  120. buf[ee_get_width() - x] = '\0';
  121. va_end(args);
  122. ee_putstr(x, y, buf);
  123. if(buf != tmp)
  124. free(buf);
  125. }
  126. void ee_clear(void)
  127. {
  128. /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
  129. int y = ee_get_height();
  130. while(y--)
  131. ee_putstr(0, y, _ee_empty_line);
  132. }