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.

graphics.c 3.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library 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 GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "config.h"
  24. #if defined(USE_SLANG)
  25. # include <slang.h>
  26. #elif defined(USE_NCURSES)
  27. # include <curses.h>
  28. #elif defined(USE_CONIO)
  29. # include <conio.h>
  30. #else
  31. # error "no graphics library detected"
  32. #endif
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <stdarg.h>
  36. #include "caca.h"
  37. #include "caca_internals.h"
  38. static enum caca_color _caca_color = CACA_COLOR_WHITE;
  39. void caca_set_color(enum caca_color color)
  40. {
  41. if(color < 0 || color > 15)
  42. return;
  43. _caca_color = color;
  44. #if defined(USE_SLANG)
  45. SLsmg_set_color(color + 1);
  46. #elif defined(USE_NCURSES)
  47. attrset(_caca_attr[color]);
  48. #elif defined(USE_CONIO)
  49. textcolor(color);
  50. #endif
  51. }
  52. enum caca_color caca_get_color(void)
  53. {
  54. return _caca_color;
  55. }
  56. void caca_putchar(int x, int y, char c)
  57. {
  58. if(x < 0 || x >= (int)caca_get_width() ||
  59. y < 0 || y >= (int)caca_get_height())
  60. return;
  61. #if defined(USE_SLANG)
  62. SLsmg_gotorc(y, x);
  63. SLsmg_write_char(c);
  64. #elif defined(USE_NCURSES)
  65. move(y, x);
  66. addch(c);
  67. #elif defined(USE_CONIO)
  68. _caca_screen[2 * (x + y * caca_get_width())] = c;
  69. _caca_screen[2 * (x + y * caca_get_width()) + 1] = _caca_color;
  70. // gotoxy(x + 1, y + 1);
  71. // putch(c);
  72. #endif
  73. }
  74. void caca_putstr(int x, int y, const char *s)
  75. {
  76. unsigned int len;
  77. if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width())
  78. return;
  79. len = strlen(s);
  80. if(x < 0)
  81. {
  82. len -= -x;
  83. if(len < 0)
  84. return;
  85. s += -x;
  86. x = 0;
  87. }
  88. if(x + len >= caca_get_width())
  89. {
  90. memcpy(_caca_scratch_line, s, caca_get_width() - x);
  91. _caca_scratch_line[caca_get_width() - x] = '\0';
  92. s = _caca_scratch_line;
  93. }
  94. #if defined(USE_SLANG)
  95. SLsmg_gotorc(y, x);
  96. SLsmg_write_string(s);
  97. #elif defined(USE_NCURSES)
  98. move(y, x);
  99. addstr(s);
  100. #elif defined(USE_CONIO)
  101. char *buf = _caca_screen + 2 * (x + y * caca_get_width());
  102. while(*s)
  103. {
  104. *buf++ = *s++;
  105. *buf++ = _caca_color;
  106. }
  107. // gotoxy(x + 1, y + 1);
  108. // cputs(s);
  109. #endif
  110. }
  111. void caca_printf(int x, int y, const char *format, ...)
  112. {
  113. char tmp[BUFSIZ];
  114. char *buf = tmp;
  115. va_list args;
  116. if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width())
  117. return;
  118. if(caca_get_width() - x + 1 > BUFSIZ)
  119. buf = malloc(caca_get_width() - x + 1);
  120. va_start(args, format);
  121. vsnprintf(buf, caca_get_width() - x + 1, format, args);
  122. buf[caca_get_width() - x] = '\0';
  123. va_end(args);
  124. caca_putstr(x, y, buf);
  125. if(buf != tmp)
  126. free(buf);
  127. }
  128. void caca_clear(void)
  129. {
  130. /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
  131. int y = caca_get_height();
  132. while(y--)
  133. caca_putstr(0, y, _caca_empty_line);
  134. }