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.
 
 
 
 
 
 

263 line
7.9 KiB

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