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.
 
 
 
 
 
 

305 line
9.0 KiB

  1. /*
  2. * cacafire fire demo for libcaca
  3. * Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
  4. * 1998, 1999, 2001 Jan Hubicka <hubicka@freesoft.cz>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22. * 02111-1307 USA
  23. */
  24. #ifdef LIBCACA
  25. #include "config.h"
  26. #include "common.h"
  27. #if !defined(__KERNEL__)
  28. # include <stdio.h>
  29. # include <stdlib.h>
  30. # include <string.h>
  31. #endif
  32. #include "caca.h"
  33. #else
  34. #include <stdio.h>
  35. #include "aalib.h"
  36. #define XSIZ aa_imgwidth(context)
  37. #define YSIZ (aa_imgheight(context)-4)
  38. #endif
  39. #define MAXTABLE (256*5)
  40. #ifdef LIBCACA
  41. static cucul_canvas_t *cv;
  42. static caca_display_t *dp;
  43. static int XSIZ, YSIZ;
  44. static cucul_dither_t *cucul_dither;
  45. static char *bitmap;
  46. static int pause = 0;
  47. #else
  48. static aa_context *context;
  49. static aa_renderparams *params;
  50. static aa_palette palette;
  51. #endif
  52. static unsigned int table[MAXTABLE];
  53. #ifdef LIBCACA
  54. static int const pal[] =
  55. #else
  56. __AA_CONST static int pal[] =
  57. #endif
  58. {
  59. 0, 0, 0, 0, 0, 6, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 8, 0, 0, 9, 0, 0, 10,
  60. 2, 0, 10, 4, 0, 9, 6, 0, 9, 8, 0, 8, 10, 0, 7, 12, 0, 7, 14, 0, 6, 16, 0, 5,
  61. 18, 0, 5, 20, 0, 4, 22, 0, 4, 24, 0, 3, 26, 0, 2, 28, 0, 2, 30, 0, 1, 32, 0, 0,
  62. 32, 0, 0, 33, 0, 0, 34, 0, 0, 35, 0, 0, 36, 0, 0, 36, 0, 0, 37, 0, 0, 38, 0, 0,
  63. 39, 0, 0, 40, 0, 0, 40, 0, 0, 41, 0, 0, 42, 0, 0, 43, 0, 0, 44, 0, 0, 45, 0, 0,
  64. 46, 1, 0, 47, 1, 0, 48, 2, 0, 49, 2, 0, 50, 3, 0, 51, 3, 0, 52, 4, 0, 53, 4, 0,
  65. 54, 5, 0, 55, 5, 0, 56, 6, 0, 57, 6, 0, 58, 7, 0, 59, 7, 0, 60, 8, 0, 61, 8, 0,
  66. 63, 9, 0, 63, 9, 0, 63, 10, 0, 63, 10, 0, 63, 11, 0, 63, 11, 0, 63, 12, 0, 63, 12, 0,
  67. 63, 13, 0, 63, 13, 0, 63, 14, 0, 63, 14, 0, 63, 15, 0, 63, 15, 0, 63, 16, 0, 63, 16, 0,
  68. 63, 17, 0, 63, 17, 0, 63, 18, 0, 63, 18, 0, 63, 19, 0, 63, 19, 0, 63, 20, 0, 63, 20, 0,
  69. 63, 21, 0, 63, 21, 0, 63, 22, 0, 63, 22, 0, 63, 23, 0, 63, 24, 0, 63, 24, 0, 63, 25, 0,
  70. 63, 25, 0, 63, 26, 0, 63, 26, 0, 63, 27, 0, 63, 27, 0, 63, 28, 0, 63, 28, 0, 63, 29, 0,
  71. 63, 29, 0, 63, 30, 0, 63, 30, 0, 63, 31, 0, 63, 31, 0, 63, 32, 0, 63, 32, 0, 63, 33, 0,
  72. 63, 33, 0, 63, 34, 0, 63, 34, 0, 63, 35, 0, 63, 35, 0, 63, 36, 0, 63, 36, 0, 63, 37, 0,
  73. 63, 38, 0, 63, 38, 0, 63, 39, 0, 63, 39, 0, 63, 40, 0, 63, 40, 0, 63, 41, 0, 63, 41, 0,
  74. 63, 42, 0, 63, 42, 0, 63, 43, 0, 63, 43, 0, 63, 44, 0, 63, 44, 0, 63, 45, 0, 63, 45, 0,
  75. 63, 46, 0, 63, 46, 0, 63, 47, 0, 63, 47, 0, 63, 48, 0, 63, 48, 0, 63, 49, 0, 63, 49, 0,
  76. 63, 50, 0, 63, 50, 0, 63, 51, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0,
  77. 63, 53, 0, 63, 53, 0, 63, 53, 0, 63, 53, 0, 63, 54, 0, 63, 54, 0, 63, 54, 0, 63, 54, 0,
  78. 63, 54, 0, 63, 55, 0, 63, 55, 0, 63, 55, 0, 63, 55, 0, 63, 56, 0, 63, 56, 0, 63, 56, 0,
  79. 63, 56, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 58, 0, 63, 58, 0,
  80. 63, 58, 0, 63, 58, 0, 63, 59, 0, 63, 59, 0, 63, 59, 0, 63, 59, 0, 63, 60, 0, 63, 60, 0,
  81. 63, 60, 0, 63, 60, 0, 63, 60, 0, 63, 61, 0, 63, 61, 0, 63, 61, 0, 63, 61, 0, 63, 62, 0,
  82. 63, 62, 0, 63, 62, 0, 63, 62, 0, 63, 63, 0, 63, 63, 1, 63, 63, 2, 63, 63, 3, 63, 63, 4,
  83. 63, 63, 5, 63, 63, 6, 63, 63, 7, 63, 63, 8, 63, 63, 9, 63, 63, 10, 63, 63, 10, 63, 63, 11,
  84. 63, 63, 12, 63, 63, 13, 63, 63, 14, 63, 63, 15, 63, 63, 16, 63, 63, 17, 63, 63, 18, 63, 63, 19,
  85. 63, 63, 20, 63, 63, 21, 63, 63, 21, 63, 63, 22, 63, 63, 23, 63, 63, 24, 63, 63, 25, 63, 63, 26,
  86. 63, 63, 27, 63, 63, 28, 63, 63, 29, 63, 63, 30, 63, 63, 31, 63, 63, 31, 63, 63, 32, 63, 63, 33,
  87. 63, 63, 34, 63, 63, 35, 63, 63, 36, 63, 63, 37, 63, 63, 38, 63, 63, 39, 63, 63, 40, 63, 63, 41,
  88. 63, 63, 42, 63, 63, 42, 63, 63, 43, 63, 63, 44, 63, 63, 45, 63, 63, 46, 63, 63, 47, 63, 63, 48,
  89. 63, 63, 49, 63, 63, 50, 63, 63, 51, 63, 63, 52, 63, 63, 52, 63, 63, 53, 63, 63, 54, 63, 63, 55,
  90. 63, 63, 56, 63, 63, 57, 63, 63, 58, 63, 63, 59, 63, 63, 60, 63, 63, 61, 63, 63, 62, 63, 63, 63};
  91. static void
  92. initialize (void)
  93. {
  94. int i;
  95. #ifdef LIBCACA
  96. unsigned int r[256], g[256], b[256], a[256];
  97. #endif
  98. #ifdef LIBCACA
  99. cv = cucul_create_canvas(80, 32);
  100. if (!cv)
  101. {
  102. printf ("Failed to initialize libcucul\n");
  103. exit (1);
  104. }
  105. dp = caca_create_display(cv);
  106. if (!dp)
  107. {
  108. printf ("Failed to initialize libcaca\n");
  109. exit (1);
  110. }
  111. caca_set_display_time(dp, 10000);
  112. XSIZ = cucul_get_canvas_width(cv) * 2;
  113. YSIZ = cucul_get_canvas_height(cv) * 2 - 4;
  114. #else
  115. context = aa_autoinit (&aa_defparams);
  116. if (context == NULL)
  117. {
  118. printf ("Failed to initialize aalib\n");
  119. exit (1);
  120. }
  121. aa_autoinitkbd (context, 0);
  122. params = aa_getrenderparams ();
  123. #endif
  124. for (i = 0; i < 256; i++)
  125. #ifdef LIBCACA
  126. {
  127. r[i] = pal[i * 3] * 64;
  128. g[i] = pal[i * 3 + 1] * 64;
  129. b[i] = pal[i * 3 + 2] * 64;
  130. a[i] = 0xfff;
  131. }
  132. #else
  133. aa_setpalette (palette, i, pal[i * 3] * 4,
  134. pal[i * 3 + 1] * 4, pal[i * 3 + 2] * 4);
  135. #endif
  136. #ifdef LIBCACA
  137. cucul_dither = cucul_create_dither(8, XSIZ, YSIZ - 2, XSIZ, 0, 0, 0, 0);
  138. cucul_set_dither_palette(cucul_dither, r, g, b, a);
  139. bitmap = malloc(4 * cucul_get_canvas_width(cv)
  140. * cucul_get_canvas_height(cv));
  141. memset(bitmap, 0, 4 * cucul_get_canvas_width(cv)
  142. * cucul_get_canvas_height(cv));
  143. #else
  144. aa_hidecursor (context);
  145. #endif
  146. }
  147. static void
  148. uninitialize (void)
  149. {
  150. #ifdef LIBCACA
  151. caca_free_display(dp);
  152. cucul_free_canvas(cv);
  153. #else
  154. aa_close (context);
  155. #endif
  156. exit (0);
  157. }
  158. static void
  159. gentable (void)
  160. {
  161. unsigned int i, p2;
  162. unsigned int minus = 800 / YSIZ;
  163. if (minus == 0)
  164. minus = 1;
  165. for (i = 0; i < MAXTABLE; i++)
  166. {
  167. if (i > minus)
  168. {
  169. p2 = (i - minus) / 5;
  170. table[i] = p2;
  171. }
  172. else
  173. table[i] = 0;
  174. }
  175. }
  176. #define MA 5
  177. static void
  178. firemain (void)
  179. {
  180. register unsigned int i;
  181. unsigned char *p;
  182. #ifndef LIBCACA
  183. char *bitmap = aa_image (context);
  184. #endif
  185. i = 0;
  186. #define END (bitmap + XSIZ * YSIZ)
  187. for (p = (unsigned char*)bitmap;
  188. (char*)p <= (( char *) (END));
  189. p += 1)
  190. {
  191. *p = table[(*(p + XSIZ - 1) + *(p + XSIZ + 1) + *(p + XSIZ)) +
  192. (*(p + 2 * XSIZ - 1) + *(p + 2 * XSIZ + 1))];
  193. }
  194. }
  195. #define min(x,y) ((x)<(y)?(x):(y))
  196. static void
  197. drawfire (void)
  198. {
  199. unsigned int i, last1, i1, i2;
  200. static int loop = 0, sloop = 0;
  201. static unsigned int height = 0;
  202. register unsigned char *p;
  203. #ifndef LIBCACA
  204. char *bitmap = aa_image (context);
  205. #else
  206. if(pause)
  207. goto paused;
  208. #endif
  209. height++;
  210. loop--;
  211. if (loop < 0)
  212. loop = rand () % 3, sloop++;;
  213. i1 = 1;
  214. i2 = 4 * XSIZ + 1;
  215. for (p = (unsigned char *) bitmap + XSIZ * (YSIZ + 0);
  216. p < ((unsigned char *) bitmap + XSIZ * (YSIZ + 1));
  217. p++, i1 += 4, i2 -= 4)
  218. {
  219. last1 = rand () % min (i1, min (i2, height));
  220. i = rand () % 6;
  221. for (; p < (unsigned char *) bitmap + XSIZ * (YSIZ + 1) && i != 0;
  222. p++, i--, i1 += 4, i2 -= 4)
  223. *p = last1, last1 += rand () % 6 - 2, *(p + XSIZ) = last1, last1 +=
  224. rand () % 6 - 2;
  225. *(p + 2 * XSIZ) = last1, last1 += rand () % 6 - 2;
  226. }
  227. i = 0;
  228. firemain ();
  229. #ifdef LIBCACA
  230. paused:
  231. cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv),
  232. cucul_get_canvas_height(cv), cucul_dither, bitmap);
  233. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  234. if (sloop < 100)
  235. cucul_put_str(cv, cucul_get_canvas_width(cv) - 30,
  236. cucul_get_canvas_height(cv) - 2,
  237. " -=[ Powered by libcaca ]=- ");
  238. caca_refresh_display(dp);
  239. /*XSIZ = caca_get_width() * 2;
  240. YSIZ = caca_get_height() * 2 - 4;*/
  241. #else
  242. aa_renderpalette (context, palette, params, 0, 0, aa_scrwidth (context),
  243. aa_scrheight (context));
  244. aa_flush (context);
  245. #endif
  246. }
  247. static void
  248. game (void)
  249. {
  250. #ifndef LIBCACA
  251. int event;
  252. #endif
  253. gentable ();
  254. #ifdef LIBCACA
  255. for(;;)
  256. #else
  257. while (!(event = aa_getevent (context, 0)) || event == AA_RESIZE)
  258. #endif
  259. {
  260. #ifdef LIBCACA
  261. caca_event_t ev;
  262. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0))
  263. {
  264. switch(ev.data.key.ch)
  265. {
  266. case CACA_KEY_CTRL_C:
  267. case CACA_KEY_CTRL_Z:
  268. case CACA_KEY_ESCAPE: return;
  269. case ' ': pause = !pause;
  270. }
  271. }
  272. #endif
  273. drawfire ();
  274. }
  275. }
  276. int
  277. main (int argc, char **argv)
  278. {
  279. #ifndef LIBCACA
  280. if (!aa_parseoptions (NULL, NULL, &argc, argv) || argc != 1)
  281. {
  282. printf ("%s", aa_help);
  283. exit (1);
  284. }
  285. #endif
  286. initialize ();
  287. #ifndef LIBCACA
  288. aa_resizehandler (context, (void *) aa_resize);
  289. #endif
  290. game ();
  291. uninitialize ();
  292. return 1;
  293. }