25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

caca0.c 7.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains glue code for applications using the pre-1.0
  16. * libcaca API.
  17. */
  18. #include "config.h"
  19. #include "common.h"
  20. #if !defined(__KERNEL__)
  21. # include <stdlib.h>
  22. #endif
  23. #include "caca.h"
  24. #include "caca_internals.h"
  25. /* These variables are needed to emulate old non-thread safe behaviour */
  26. cucul_canvas_t *__caca0_cv = NULL;
  27. caca_display_t *__caca0_dp = NULL;
  28. unsigned char __caca0_fg = CUCUL_LIGHTGRAY;
  29. unsigned char __caca0_bg = CUCUL_BLACK;
  30. char __caca0_utf8[2] = " ";
  31. /* These functions are needed, too */
  32. int __caca0_init(void);
  33. void __caca0_end(void);
  34. unsigned int __caca0_get_event(unsigned int, int);
  35. unsigned int __caca0_sqrt(unsigned int);
  36. int __caca0_get_feature(int);
  37. void __caca0_set_feature(int);
  38. char const *__caca0_get_feature_name(int);
  39. cucul_canvas_t *__caca0_load_sprite(char const *);
  40. cucul_dither_t *__caca0_create_bitmap(unsigned int, unsigned int,
  41. unsigned int, unsigned int, unsigned long int, unsigned long int,
  42. unsigned long int, unsigned long int);
  43. void __caca0_free_bitmap(cucul_dither_t *);
  44. extern char const *__caca0_get_color_name(unsigned char);
  45. /* Emulation functions */
  46. int __caca0_init(void)
  47. {
  48. __caca0_cv = cucul_create_canvas(0, 0);
  49. if(!__caca0_cv)
  50. return -1;
  51. __caca0_dp = caca_create_display(__caca0_cv);
  52. if(!__caca0_dp)
  53. {
  54. cucul_free_canvas(__caca0_cv);
  55. __caca0_cv = NULL;
  56. return -1;
  57. }
  58. __caca0_fg = CUCUL_LIGHTGRAY;
  59. __caca0_bg = CUCUL_BLACK;
  60. return 0;
  61. }
  62. void __caca0_end(void)
  63. {
  64. caca_free_display(__caca0_dp);
  65. __caca0_dp = NULL;
  66. cucul_free_canvas(__caca0_cv);
  67. __caca0_cv = NULL;
  68. }
  69. unsigned int __caca0_get_event(unsigned int m, int t)
  70. {
  71. caca_event_t ev;
  72. int ret;
  73. ret = caca_get_event(__caca0_dp, (m >> 24) & 0x7f, &ev, t);
  74. if(!ret)
  75. return 0x00000000;
  76. switch(ev.type)
  77. {
  78. case CACA_EVENT_KEY_PRESS:
  79. return 0x01000000 | ev.data.key.ch;
  80. case CACA_EVENT_KEY_RELEASE:
  81. return 0x02000000 | ev.data.key.ch;
  82. case CACA_EVENT_MOUSE_PRESS:
  83. return 0x04000000 | ev.data.mouse.button;
  84. case CACA_EVENT_MOUSE_RELEASE:
  85. return 0x08000000 | ev.data.mouse.button;
  86. case CACA_EVENT_MOUSE_MOTION:
  87. return 0x10000000 | ((ev.data.mouse.x & 0xfff) << 12)
  88. | (ev.data.mouse.y & 0xfff);
  89. case CACA_EVENT_RESIZE:
  90. return 0x20000000;
  91. default:
  92. break;
  93. }
  94. return 0x00000000;
  95. }
  96. unsigned int __caca0_sqrt(unsigned int a)
  97. {
  98. if(a == 0)
  99. return 0;
  100. if(a < 1000000000)
  101. {
  102. unsigned int x = a < 10 ? 1
  103. : a < 1000 ? 10
  104. : a < 100000 ? 100
  105. : a < 10000000 ? 1000
  106. : 10000;
  107. /* Newton's method. Three iterations would be more than enough. */
  108. x = (x * x + a) / x / 2;
  109. x = (x * x + a) / x / 2;
  110. x = (x * x + a) / x / 2;
  111. x = (x * x + a) / x / 2;
  112. return x;
  113. }
  114. return 2 * __caca0_sqrt(a / 4);
  115. }
  116. static char const *features[] =
  117. {
  118. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  119. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  120. NULL, "16", "full16", NULL, NULL, NULL, NULL, NULL,
  121. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  122. NULL, "none", "prefilter", NULL, NULL, NULL, NULL, NULL,
  123. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  124. NULL, "none", "ordered2", "ordered4", "ordered8", "random"
  125. };
  126. static cucul_dither_t **bitmaps = NULL;
  127. static unsigned int nbitmaps = 0;
  128. static int background = 0x12;
  129. static int antialiasing = 0x22;
  130. static int dithering = 0x33;
  131. int __caca0_get_feature(int feature)
  132. {
  133. if(feature == 0x10)
  134. return background;
  135. if(feature == 0x20)
  136. return antialiasing;
  137. if(feature == 0x30)
  138. return dithering;
  139. return 0xffff; /* CACA_FEATURE_UNKNOWN */
  140. }
  141. void __caca0_set_feature(int feature)
  142. {
  143. unsigned int i;
  144. switch(feature)
  145. {
  146. case 0x10: feature = 0x12; /* CACA_BACKGROUND_SOLID */
  147. case 0x11: case 0x12:
  148. background = feature;
  149. for(i = 0; i < nbitmaps; i++)
  150. cucul_set_dither_color(bitmaps[i], features[feature]);
  151. break;
  152. case 0x20: feature = 0x22; /* CACA_ANTIALIASING_PREFILTER */
  153. case 0x21: case 0x22:
  154. antialiasing = feature;
  155. for(i = 0; i < nbitmaps; i++)
  156. cucul_set_dither_antialias(bitmaps[i], features[feature]);
  157. break;
  158. case 0x30: feature = 0x33; /* CACA_DITHERING_ORDERED4 */
  159. case 0x31: case 0x32: case 0x33: case 0x34: case 0x35:
  160. dithering = feature;
  161. for(i = 0; i < nbitmaps; i++)
  162. cucul_set_dither_mode(bitmaps[i], features[feature]);
  163. break;
  164. }
  165. }
  166. char const *__caca0_get_feature_name(int feature)
  167. {
  168. switch(feature)
  169. {
  170. case 0x11: return "black background";
  171. case 0x12: return "solid background";
  172. case 0x21: return "no antialiasing";
  173. case 0x22: return "prefilter antialiasing";
  174. case 0x31: return "no dithering";
  175. case 0x32: return "2x2 ordered dithering";
  176. case 0x33: return "4x4 ordered dithering";
  177. case 0x34: return "8x8 ordered dithering";
  178. case 0x35: return "random dithering";
  179. default: return "unknown";
  180. }
  181. }
  182. cucul_canvas_t *__caca0_load_sprite(char const *file)
  183. {
  184. cucul_canvas_t *cv;
  185. cv = cucul_create_canvas(0, 0);;
  186. if(cucul_import_file(cv, file, "") < 0)
  187. {
  188. cucul_free_canvas(cv);
  189. return NULL;
  190. }
  191. return cv;
  192. }
  193. cucul_dither_t *__caca0_create_bitmap(unsigned int bpp, unsigned int w,
  194. unsigned int h, unsigned int pitch,
  195. unsigned long int r, unsigned long int g,
  196. unsigned long int b, unsigned long int a)
  197. {
  198. cucul_dither_t *d;
  199. d = cucul_create_dither(bpp, w, h, pitch, r, g, b, a);
  200. if(!d)
  201. return NULL;
  202. cucul_set_dither_color(d, features[background]);
  203. cucul_set_dither_antialias(d, features[antialiasing]);
  204. cucul_set_dither_mode(d, features[dithering]);
  205. /* Store bitmap in our list */
  206. nbitmaps++;
  207. bitmaps = realloc(bitmaps, nbitmaps * (sizeof(cucul_dither_t *)));
  208. bitmaps[nbitmaps - 1] = d;
  209. return d;
  210. }
  211. void __caca0_free_bitmap(cucul_dither_t *d)
  212. {
  213. unsigned int i, found = 0;
  214. cucul_free_dither(d);
  215. /* Remove bitmap from our list */
  216. for(i = 0; i + 1 < nbitmaps; i++)
  217. {
  218. if(bitmaps[i] == d)
  219. found = 1;
  220. if(found)
  221. bitmaps[i] = bitmaps[i + 1];
  222. }
  223. nbitmaps--;
  224. }
  225. char const *__caca0_get_color_name(unsigned char color)
  226. {
  227. static char const *color_names[] =
  228. {
  229. "black",
  230. "blue",
  231. "green",
  232. "cyan",
  233. "red",
  234. "magenta",
  235. "brown",
  236. "light gray",
  237. "dark gray",
  238. "light blue",
  239. "light green",
  240. "light cyan",
  241. "light red",
  242. "light magenta",
  243. "yellow",
  244. "white",
  245. };
  246. if(color > 15)
  247. return "unknown";
  248. return color_names[(unsigned int)color];
  249. }