source: libcaca/trunk/src/aafire.c @ 1452

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