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.

aafire.c 8.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #if !defined(__KERNEL__)
  27. # include <stdio.h>
  28. # include <stdlib.h>
  29. # include <string.h>
  30. #endif
  31. #include "caca.h"
  32. #else
  33. #include <stdio.h>
  34. #include "aalib.h"
  35. #define XSIZ aa_imgwidth(context)
  36. #define YSIZ (aa_imgheight(context)-4)
  37. #endif
  38. #define MAXTABLE (256*5)
  39. #ifdef LIBCACA
  40. static cucul_t *qq;
  41. static caca_t *kk;
  42. static int XSIZ, YSIZ;
  43. static struct cucul_bitmap *cucul_bitmap;
  44. static char *bitmap;
  45. static int pause = 0;
  46. #else
  47. static aa_context *context;
  48. static aa_renderparams *params;
  49. static aa_palette palette;
  50. #endif
  51. static unsigned int table[MAXTABLE];
  52. #ifdef LIBCACA
  53. static int const pal[] =
  54. #else
  55. __AA_CONST static int pal[] =
  56. #endif
  57. {
  58. 0, 0, 0, 0, 0, 6, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 8, 0, 0, 9, 0, 0, 10,
  59. 2, 0, 10, 4, 0, 9, 6, 0, 9, 8, 0, 8, 10, 0, 7, 12, 0, 7, 14, 0, 6, 16, 0, 5,
  60. 18, 0, 5, 20, 0, 4, 22, 0, 4, 24, 0, 3, 26, 0, 2, 28, 0, 2, 30, 0, 1, 32, 0, 0,
  61. 32, 0, 0, 33, 0, 0, 34, 0, 0, 35, 0, 0, 36, 0, 0, 36, 0, 0, 37, 0, 0, 38, 0, 0,
  62. 39, 0, 0, 40, 0, 0, 40, 0, 0, 41, 0, 0, 42, 0, 0, 43, 0, 0, 44, 0, 0, 45, 0, 0,
  63. 46, 1, 0, 47, 1, 0, 48, 2, 0, 49, 2, 0, 50, 3, 0, 51, 3, 0, 52, 4, 0, 53, 4, 0,
  64. 54, 5, 0, 55, 5, 0, 56, 6, 0, 57, 6, 0, 58, 7, 0, 59, 7, 0, 60, 8, 0, 61, 8, 0,
  65. 63, 9, 0, 63, 9, 0, 63, 10, 0, 63, 10, 0, 63, 11, 0, 63, 11, 0, 63, 12, 0, 63, 12, 0,
  66. 63, 13, 0, 63, 13, 0, 63, 14, 0, 63, 14, 0, 63, 15, 0, 63, 15, 0, 63, 16, 0, 63, 16, 0,
  67. 63, 17, 0, 63, 17, 0, 63, 18, 0, 63, 18, 0, 63, 19, 0, 63, 19, 0, 63, 20, 0, 63, 20, 0,
  68. 63, 21, 0, 63, 21, 0, 63, 22, 0, 63, 22, 0, 63, 23, 0, 63, 24, 0, 63, 24, 0, 63, 25, 0,
  69. 63, 25, 0, 63, 26, 0, 63, 26, 0, 63, 27, 0, 63, 27, 0, 63, 28, 0, 63, 28, 0, 63, 29, 0,
  70. 63, 29, 0, 63, 30, 0, 63, 30, 0, 63, 31, 0, 63, 31, 0, 63, 32, 0, 63, 32, 0, 63, 33, 0,
  71. 63, 33, 0, 63, 34, 0, 63, 34, 0, 63, 35, 0, 63, 35, 0, 63, 36, 0, 63, 36, 0, 63, 37, 0,
  72. 63, 38, 0, 63, 38, 0, 63, 39, 0, 63, 39, 0, 63, 40, 0, 63, 40, 0, 63, 41, 0, 63, 41, 0,
  73. 63, 42, 0, 63, 42, 0, 63, 43, 0, 63, 43, 0, 63, 44, 0, 63, 44, 0, 63, 45, 0, 63, 45, 0,
  74. 63, 46, 0, 63, 46, 0, 63, 47, 0, 63, 47, 0, 63, 48, 0, 63, 48, 0, 63, 49, 0, 63, 49, 0,
  75. 63, 50, 0, 63, 50, 0, 63, 51, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0,
  76. 63, 53, 0, 63, 53, 0, 63, 53, 0, 63, 53, 0, 63, 54, 0, 63, 54, 0, 63, 54, 0, 63, 54, 0,
  77. 63, 54, 0, 63, 55, 0, 63, 55, 0, 63, 55, 0, 63, 55, 0, 63, 56, 0, 63, 56, 0, 63, 56, 0,
  78. 63, 56, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 58, 0, 63, 58, 0,
  79. 63, 58, 0, 63, 58, 0, 63, 59, 0, 63, 59, 0, 63, 59, 0, 63, 59, 0, 63, 60, 0, 63, 60, 0,
  80. 63, 60, 0, 63, 60, 0, 63, 60, 0, 63, 61, 0, 63, 61, 0, 63, 61, 0, 63, 61, 0, 63, 62, 0,
  81. 63, 62, 0, 63, 62, 0, 63, 62, 0, 63, 63, 0, 63, 63, 1, 63, 63, 2, 63, 63, 3, 63, 63, 4,
  82. 63, 63, 5, 63, 63, 6, 63, 63, 7, 63, 63, 8, 63, 63, 9, 63, 63, 10, 63, 63, 10, 63, 63, 11,
  83. 63, 63, 12, 63, 63, 13, 63, 63, 14, 63, 63, 15, 63, 63, 16, 63, 63, 17, 63, 63, 18, 63, 63, 19,
  84. 63, 63, 20, 63, 63, 21, 63, 63, 21, 63, 63, 22, 63, 63, 23, 63, 63, 24, 63, 63, 25, 63, 63, 26,
  85. 63, 63, 27, 63, 63, 28, 63, 63, 29, 63, 63, 30, 63, 63, 31, 63, 63, 31, 63, 63, 32, 63, 63, 33,
  86. 63, 63, 34, 63, 63, 35, 63, 63, 36, 63, 63, 37, 63, 63, 38, 63, 63, 39, 63, 63, 40, 63, 63, 41,
  87. 63, 63, 42, 63, 63, 42, 63, 63, 43, 63, 63, 44, 63, 63, 45, 63, 63, 46, 63, 63, 47, 63, 63, 48,
  88. 63, 63, 49, 63, 63, 50, 63, 63, 51, 63, 63, 52, 63, 63, 52, 63, 63, 53, 63, 63, 54, 63, 63, 55,
  89. 63, 63, 56, 63, 63, 57, 63, 63, 58, 63, 63, 59, 63, 63, 60, 63, 63, 61, 63, 63, 62, 63, 63, 63};
  90. static void
  91. initialize (void)
  92. {
  93. int i;
  94. #ifdef LIBCACA
  95. unsigned int r[256], g[256], b[256], a[256];
  96. #endif
  97. #ifdef LIBCACA
  98. qq = cucul_init();
  99. if (!qq)
  100. {
  101. printf ("Failed to initialize libcucul\n");
  102. exit (1);
  103. }
  104. kk = caca_attach(qq);
  105. if (!kk)
  106. {
  107. printf ("Failed to initialize libcaca\n");
  108. exit (1);
  109. }
  110. caca_set_delay(kk, 10000);
  111. XSIZ = cucul_get_width(qq) * 2;
  112. YSIZ = cucul_get_height(qq) * 2 - 4;
  113. #else
  114. context = aa_autoinit (&aa_defparams);
  115. if (context == NULL)
  116. {
  117. printf ("Failed to initialize aalib\n");
  118. exit (1);
  119. }
  120. aa_autoinitkbd (context, 0);
  121. params = aa_getrenderparams ();
  122. #endif
  123. for (i = 0; i < 256; i++)
  124. #ifdef LIBCACA
  125. {
  126. r[i] = pal[i * 3] * 64;
  127. g[i] = pal[i * 3 + 1] * 64;
  128. b[i] = pal[i * 3 + 2] * 64;
  129. a[i] = 0xfff;
  130. }
  131. #else
  132. aa_setpalette (palette, i, pal[i * 3] * 4,
  133. pal[i * 3 + 1] * 4, pal[i * 3 + 2] * 4);
  134. #endif
  135. #ifdef LIBCACA
  136. cucul_bitmap = cucul_create_bitmap(qq, 8, XSIZ, YSIZ - 2, XSIZ, 0, 0, 0, 0);
  137. cucul_set_bitmap_palette(qq, cucul_bitmap, r, g, b, a);
  138. bitmap = malloc(4 * cucul_get_width(qq) * cucul_get_height(qq) * sizeof(char));
  139. memset(bitmap, 0, 4 * cucul_get_width(qq) * cucul_get_height(qq));
  140. #else
  141. aa_hidecursor (context);
  142. #endif
  143. }
  144. static void
  145. uninitialize (void)
  146. {
  147. #ifdef LIBCACA
  148. caca_detach(kk);
  149. cucul_end(qq);
  150. #else
  151. aa_close (context);
  152. #endif
  153. exit (0);
  154. }
  155. static void
  156. gentable (void)
  157. {
  158. unsigned int i, p2;
  159. unsigned int minus = 800 / YSIZ;
  160. if (minus == 0)
  161. minus = 1;
  162. for (i = 0; i < MAXTABLE; i++)
  163. {
  164. if (i > minus)
  165. {
  166. p2 = (i - minus) / 5;
  167. table[i] = p2;
  168. }
  169. else
  170. table[i] = 0;
  171. }
  172. }
  173. #define MA 5
  174. static void
  175. firemain (void)
  176. {
  177. register unsigned int i;
  178. unsigned char *p;
  179. #ifndef LIBCACA
  180. char *bitmap = aa_image (context);
  181. #endif
  182. i = 0;
  183. #define END (bitmap + XSIZ * YSIZ)
  184. for (p = (unsigned char*)bitmap;
  185. (char*)p <= (( char *) (END));
  186. p += 1)
  187. {
  188. *p = table[(*(p + XSIZ - 1) + *(p + XSIZ + 1) + *(p + XSIZ)) +
  189. (*(p + 2 * XSIZ - 1) + *(p + 2 * XSIZ + 1))];
  190. }
  191. }
  192. #define min(x,y) ((x)<(y)?(x):(y))
  193. static void
  194. drawfire (void)
  195. {
  196. unsigned int i, last1, i1, i2;
  197. static int loop = 0, sloop = 0;
  198. static unsigned int height = 0;
  199. register unsigned char *p;
  200. #ifndef LIBCACA
  201. char *bitmap = aa_image (context);
  202. #else
  203. if(pause)
  204. goto paused;
  205. #endif
  206. height++;
  207. loop--;
  208. if (loop < 0)
  209. loop = rand () % 3, sloop++;;
  210. i1 = 1;
  211. i2 = 4 * XSIZ + 1;
  212. for (p = (unsigned char *) bitmap + XSIZ * (YSIZ + 0);
  213. p < ((unsigned char *) bitmap + XSIZ * (YSIZ + 1));
  214. p++, i1 += 4, i2 -= 4)
  215. {
  216. last1 = rand () % min (i1, min (i2, height));
  217. i = rand () % 6;
  218. for (; p < (unsigned char *) bitmap + XSIZ * (YSIZ + 1) && i != 0;
  219. p++, i--, i1 += 4, i2 -= 4)
  220. *p = last1, last1 += rand () % 6 - 2, *(p + XSIZ) = last1, last1 +=
  221. rand () % 6 - 2;
  222. *(p + 2 * XSIZ) = last1, last1 += rand () % 6 - 2;
  223. }
  224. i = 0;
  225. firemain ();
  226. #ifdef LIBCACA
  227. paused:
  228. cucul_draw_bitmap(qq, 0, 0,
  229. cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
  230. cucul_bitmap, bitmap);
  231. caca_display(kk);
  232. /*XSIZ = caca_get_width() * 2;
  233. YSIZ = caca_get_height() * 2 - 4;*/
  234. #else
  235. aa_renderpalette (context, palette, params, 0, 0, aa_scrwidth (context),
  236. aa_scrheight (context));
  237. aa_flush (context);
  238. #endif
  239. }
  240. static void
  241. game (void)
  242. {
  243. #ifndef LIBCACA
  244. int event;
  245. #endif
  246. gentable ();
  247. #ifdef LIBCACA
  248. for(;;)
  249. #else
  250. while (!(event = aa_getevent (context, 0)) || event == AA_RESIZE)
  251. #endif
  252. {
  253. #ifdef LIBCACA
  254. switch (caca_get_event(kk, CACA_EVENT_KEY_PRESS))
  255. {
  256. case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: return;
  257. case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
  258. }
  259. #endif
  260. drawfire ();
  261. }
  262. }
  263. int
  264. main (int argc, char **argv)
  265. {
  266. #ifndef LIBCACA
  267. if (!aa_parseoptions (NULL, NULL, &argc, argv) || argc != 1)
  268. {
  269. printf ("%s", aa_help);
  270. exit (1);
  271. }
  272. #endif
  273. initialize ();
  274. #ifndef LIBCACA
  275. aa_resizehandler (context, (void *) aa_resize);
  276. #endif
  277. game ();
  278. uninitialize ();
  279. return 1;
  280. }