source: libcaca/trunk/examples/aafire.c @ 298

Last change on this file since 298 was 295, checked in by Sam Hocevar, 17 years ago
  • examples/aafire.c: + Only quit upon keypress, not mouse motion.
  • Property svn:keywords set to Id
File size: 7.9 KB
Line 
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: aafire.c 295 2003-12-31 13:26:47Z sam $
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
28#ifdef LIBCACA
29#include "config.h"
30#include <stdio.h>
31#include <stdlib.h>
32#include "caca.h"
33
34#define XSIZ (caca_get_width() * 2)
35#define YSIZ (caca_get_height() * 2 - 4)
36#else
37#include <stdio.h>
38#include "aalib.h"
39
40#define XSIZ aa_imgwidth(context)
41#define YSIZ (aa_imgheight(context)-4)
42#endif
43#define MAXTABLE (256*5)
44#ifdef LIBCACA
45static struct caca_bitmap *caca_bitmap;
46static char *bitmap;
47#else
48static aa_context *context;
49static aa_renderparams *params;
50static aa_palette palette;
51#endif
52static unsigned int table[MAXTABLE];
53#ifdef LIBCACA
54const static int 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,
602, 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
92
93static void
94initialize (void)
95{
96  int i;
97#ifdef LIBCACA
98  int r[256], g[256], b[256], a[256];
99#endif
100
101#ifdef LIBCACA
102  if (caca_init())
103    {
104      printf ("Failed to initialize libcaca\n");
105      exit (1);
106    }
107  caca_set_delay(0);
108#else
109  context = aa_autoinit (&aa_defparams);
110  if (context == NULL)
111    {
112      printf ("Failed to initialize aalib\n");
113      exit (1);
114    }
115  aa_autoinitkbd (context, 0);
116  params = aa_getrenderparams ();
117#endif
118
119  for (i = 0; i < 256; i++)
120#ifdef LIBCACA
121  {
122    r[i] = pal[i * 3] * 64;
123    g[i] = pal[i * 3 + 1] * 64;
124    b[i] = pal[i * 3 + 2] * 64;
125    a[i] = 0xfff;
126  }
127#else
128    aa_setpalette (palette, i, pal[i * 3] * 4,
129                   pal[i * 3 + 1] * 4, pal[i * 3 + 2] * 4);
130#endif
131
132#ifdef LIBCACA
133  caca_bitmap = caca_create_bitmap(8, XSIZ, YSIZ - 2, XSIZ, 0, 0, 0, 0);
134  caca_set_bitmap_palette(caca_bitmap, r, g, b, a);
135  bitmap = malloc(4 * caca_get_width() * caca_get_height() * sizeof(char));
136#else
137  aa_hidecursor (context);
138#endif
139}
140static void
141uninitialize (void)
142{
143#ifdef LIBCACA
144  caca_end();
145#else
146  aa_close (context);
147#endif
148  exit (0);
149}
150static void
151gentable (void)
152{
153  unsigned int i, p2;
154  unsigned int minus = 800 / YSIZ;
155  if (minus == 0)
156    minus = 1;
157  for (i = 0; i < MAXTABLE; i++)
158    {
159      if (i > minus)
160        {
161          p2 = (i - minus) / 5;
162          table[i] = p2;
163        }
164      else
165        table[i] = 0;
166    }
167}
168
169#define MA 5
170static void
171firemain (void)
172{
173  register unsigned int i;
174  unsigned char *p;
175#ifndef LIBCACA
176  char *bitmap = aa_image (context);
177#endif
178  i = 0;
179#define END (bitmap + XSIZ * YSIZ)
180  for (p = bitmap; p <= (unsigned char *) (END); p += 1)
181    {
182      *p = table[(*(p + XSIZ - 1) + *(p + XSIZ + 1) + *(p + XSIZ)) +
183                 (*(p + 2 * XSIZ - 1) + *(p + 2 * XSIZ + 1))];
184    }
185}
186
187#define min(x,y) ((x)<(y)?(x):(y))
188static void
189drawfire (void)
190{
191  unsigned int i, last1, i1, i2;
192  static int loop = 0, sloop = 0;
193  static unsigned int height = 0;
194  register unsigned char *p;
195#ifndef LIBCACA
196  char *bitmap = aa_image (context);
197#endif
198
199  height++;
200  loop--;
201  if (loop < 0)
202    loop = rand () % 3, sloop++;;
203  i1 = 1;
204  i2 = 4 * XSIZ + 1;
205  for (p = (char *) bitmap + XSIZ * (YSIZ + 0);
206       p < ((unsigned char *) bitmap + XSIZ * (YSIZ + 1));
207       p++, i1 += 4, i2 -= 4)
208    {
209      last1 = rand () % min (i1, min (i2, height));
210      i = rand () % 6;
211      for (; p < (unsigned char *) bitmap + XSIZ * (YSIZ + 1) && i != 0;
212           p++, i--, i1 += 4, i2 -= 4)
213        *p = last1, last1 += rand () % 6 - 2, *(p + XSIZ) = last1, last1 +=
214          rand () % 6 - 2;
215      *(p + 2 * XSIZ) = last1, last1 += rand () % 6 - 2;
216    }
217  i = 0;
218  firemain ();
219#ifdef LIBCACA
220  caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
221                   caca_bitmap, bitmap);
222  caca_refresh();
223#else
224  aa_renderpalette (context, palette, params, 0, 0, aa_scrwidth (context),
225                    aa_scrheight (context));
226  aa_flush (context);
227#endif
228}
229static void
230game (void)
231{
232  int event;
233  gentable ();
234#ifdef LIBCACA
235  while (!(event = caca_get_event() & CACA_EVENT_KEY_PRESS))
236#else
237  while (!(event = aa_getevent (context, 0)) || event == AA_RESIZE)
238#endif
239    {
240      drawfire ();
241    }
242}
243int
244main (int argc, char **argv)
245{
246#ifndef LIBCACA
247  if (!aa_parseoptions (NULL, NULL, &argc, argv) || argc != 1)
248    {
249      printf ("%s", aa_help);
250      exit (1);
251    }
252#endif
253  initialize ();
254#ifndef LIBCACA
255  aa_resizehandler (context, (void *) aa_resize);
256#endif
257  game ();
258  uninitialize ();
259  return 1;
260}
Note: See TracBrowser for help on using the repository browser.