libcaca  0.99.beta19
About: libcaca is a graphics library that outputs text instead of pixels, so that it can work on older video cards or text terminals (something like an advanced AAlib library).
  Fossies Dox: libcaca-0.99.beta19.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

aafire.c
Go to the documentation of this file.
1 /*
2  * cacafire fire demo for libcaca
3  * Copyright (c) 2003-2010 Sam Hocevar <sam@hocevar.net>
4  * 1998, 1999, 2001 Jan Hubicka <hubicka@freesoft.cz>
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  */
22 
23 #ifdef LIBCACA
24 #include "config.h"
25 #if !defined(__KERNEL__)
26 # include <stdio.h>
27 # include <stdlib.h>
28 # include <string.h>
29 #endif
30 #include "caca.h"
31 #else
32 #include <stdio.h>
33 #include "aalib.h"
34 
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 caca_canvas_t *cv;
41 static caca_display_t *dp;
42 static int XSIZ, YSIZ;
44 static char *bitmap;
45 static int paused = 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 
91 
92 static void
93 initialize (void)
94 {
95  int i;
96 #ifdef LIBCACA
97  uint32_t r[256], g[256], b[256], a[256];
98 #endif
99 
100 #ifdef LIBCACA
101  cv = caca_create_canvas(80, 32);
102  if (!cv)
103  {
104  printf ("Failed to initialize libcaca\n");
105  exit (1);
106  }
108  if (!dp)
109  {
110  printf ("Failed to initialize libcaca\n");
111  exit (1);
112  }
113  caca_set_display_time(dp, 10000);
115  YSIZ = caca_get_canvas_height(cv) * 2 - 4;
116 #else
117  context = aa_autoinit (&aa_defparams);
118  if (context == NULL)
119  {
120  printf ("Failed to initialize aalib\n");
121  exit (1);
122  }
123  aa_autoinitkbd (context, 0);
124  params = aa_getrenderparams ();
125 #endif
126 
127  for (i = 0; i < 256; i++)
128 #ifdef LIBCACA
129  {
130  r[i] = pal[i * 3] * 64;
131  g[i] = pal[i * 3 + 1] * 64;
132  b[i] = pal[i * 3 + 2] * 64;
133  a[i] = 0xfff;
134  }
135 #else
136  aa_setpalette (palette, i, pal[i * 3] * 4,
137  pal[i * 3 + 1] * 4, pal[i * 3 + 2] * 4);
138 #endif
139 
140 #ifdef LIBCACA
141  caca_dither = caca_create_dither(8, XSIZ, YSIZ - 2, XSIZ, 0, 0, 0, 0);
143  bitmap = malloc(4 * caca_get_canvas_width(cv)
145  memset(bitmap, 0, 4 * caca_get_canvas_width(cv)
147 #else
148  aa_hidecursor (context);
149 #endif
150 }
151 static void
153 {
154 #ifdef LIBCACA
157 #else
158  aa_close (context);
159 #endif
160  exit (0);
161 }
162 static void
163 gentable (void)
164 {
165  unsigned int i, p2;
166  unsigned int minus = 800 / YSIZ;
167  if (minus == 0)
168  minus = 1;
169  for (i = 0; i < MAXTABLE; i++)
170  {
171  if (i > minus)
172  {
173  p2 = (i - minus) / 5;
174  table[i] = p2;
175  }
176  else
177  table[i] = 0;
178  }
179 }
180 
181 #define MA 5
182 static void
183 firemain (void)
184 {
185  register unsigned int i;
186  unsigned char *p;
187 #ifndef LIBCACA
188  char *bitmap = aa_image (context);
189 #endif
190  i = 0;
191 #define END (bitmap + XSIZ * YSIZ)
192  for (p = (unsigned char*)bitmap;
193  (char*)p <= (( char *) (END));
194  p += 1)
195  {
196  *p = table[(*(p + XSIZ - 1) + *(p + XSIZ + 1) + *(p + XSIZ)) +
197  (*(p + 2 * XSIZ - 1) + *(p + 2 * XSIZ + 1))];
198  }
199 }
200 
201 #define min(x,y) ((x)<(y)?(x):(y))
202 static void
203 drawfire (void)
204 {
205  unsigned int i, last1, i1, i2;
206  static int loop = 0, sloop = 0;
207  static unsigned int height = 0;
208  register unsigned char *p;
209 #ifndef LIBCACA
210  char *bitmap = aa_image (context);
211 #else
212  if(paused)
213  goto _paused;
214 #endif
215 
216  height++;
217  loop--;
218  if (loop < 0)
219  loop = rand () % 3, sloop++;;
220  i1 = 1;
221  i2 = 4 * XSIZ + 1;
222  for (p = (unsigned char *) bitmap + XSIZ * (YSIZ + 0);
223  p < ((unsigned char *) bitmap + XSIZ * (YSIZ + 1));
224  p++, i1 += 4, i2 -= 4)
225  {
226  last1 = rand () % min (i1, min (i2, height));
227  i = rand () % 6;
228  for (; p < (unsigned char *) bitmap + XSIZ * (YSIZ + 1) && i != 0;
229  p++, i--, i1 += 4, i2 -= 4)
230  *p = last1, last1 += rand () % 6 - 2, *(p + XSIZ) = last1, last1 +=
231  rand () % 6 - 2;
232  *(p + 2 * XSIZ) = last1, last1 += rand () % 6 - 2;
233  }
234  i = 0;
235  firemain ();
236 #ifdef LIBCACA
237 _paused:
241  if (sloop < 100)
244  " -=[ Powered by libcaca ]=- ");
245 
247  /*XSIZ = caca_get_width() * 2;
248  YSIZ = caca_get_height() * 2 - 4;*/
249 #else
250  aa_renderpalette (context, palette, params, 0, 0, aa_scrwidth (context),
251  aa_scrheight (context));
252  aa_flush (context);
253 #endif
254 }
255 static void
256 game (void)
257 {
258 #ifndef LIBCACA
259  int event;
260 #endif
261  gentable ();
262 #ifdef LIBCACA
263  for(;;)
264 #else
265  while (!(event = aa_getevent (context, 0)) || event == AA_RESIZE)
266 #endif
267  {
268 #ifdef LIBCACA
269  caca_event_t ev;
271  {
273  return;
274  switch(caca_get_event_key_ch(&ev))
275  {
276  case CACA_KEY_CTRL_C:
277  case CACA_KEY_CTRL_Z:
278  case CACA_KEY_ESCAPE: return;
279  case ' ': paused = !paused;
280  }
281  }
282 #endif
283  drawfire ();
284  }
285 }
286 int
287 main (int argc, char **argv)
288 {
289 #ifndef LIBCACA
290  if (!aa_parseoptions (NULL, NULL, &argc, argv) || argc != 1)
291  {
292  printf ("%s", aa_help);
293  exit (1);
294  }
295 #endif
296  initialize ();
297 #ifndef LIBCACA
298  aa_resizehandler (context, (void *) aa_resize);
299 #endif
300  game ();
301  uninitialize ();
302  return 1;
303 }
caca_create_dither
caca_dither_t * caca_create_dither(int, int, int, int, uint32_t, uint32_t, uint32_t, uint32_t)
Create an internal dither object.
Definition: dither.c:261
caca_dither
Definition: dither.c:116
MAXTABLE
#define MAXTABLE
Definition: aafire.c:38
XSIZ
#define XSIZ
Definition: aafire.c:35
caca_create_display
caca_display_t * caca_create_display(caca_canvas_t *cv)
Attach a caca graphical context to a caca canvas.
Definition: caca.c:68
caca_set_dither_palette
int caca_set_dither_palette(caca_dither_t *, uint32_t r[], uint32_t g[], uint32_t b[], uint32_t a[])
Set the palette of an 8bpp dither object.
Definition: dither.c:372
context
static aa_context * context
Definition: aafire.c:47
printf
#define printf
Definition: caca_conio.h:144
palette
static aa_palette palette
Definition: aafire.c:49
gentable
static void gentable(void)
Definition: aafire.c:163
caca_set_display_time
int caca_set_display_time(caca_display_t *, int)
Set the refresh delay.
Definition: graphics.c:100
game
static void game(void)
Definition: aafire.c:256
g
int g
Definition: cacaview.c:60
caca_event
Handling of user events.
Definition: caca.h:129
table
static unsigned int table[(256 *5)]
Definition: aafire.c:51
caca_create_canvas
caca_canvas_t * caca_create_canvas(int, int)
Initialise a libcaca canvas.
Definition: canvas.c:54
caca_display
Definition: caca_internals.h:167
caca_free_canvas
int caca_free_canvas(caca_canvas_t *)
Free a libcaca canvas.
Definition: canvas.c:308
caca_get_event_type
enum caca_event_type caca_get_event_type(caca_event_t const *)
Return an event's type.
Definition: event.c:202
caca_get_canvas_height
int caca_get_canvas_height(caca_canvas_t const *)
Get the canvas height.
Definition: canvas.c:253
CACA_EVENT_QUIT
Definition: caca.h:117
main
int main(int argc, char **argv)
Definition: aafire.c:287
YSIZ
#define YSIZ
Definition: aafire.c:36
caca_get_canvas_width
int caca_get_canvas_width(caca_canvas_t const *)
Get the canvas width.
Definition: canvas.c:239
uninitialize
static void uninitialize(void)
Definition: aafire.c:152
CACA_WHITE
Definition: caca.h:86
caca_put_str
int caca_put_str(caca_canvas_t *, int, int, char const *)
Print a string.
Definition: string.c:254
CACA_KEY_CTRL_Z
Definition: caca.h:191
CACA_KEY_ESCAPE
Definition: caca.h:192
cv
caca_canvas_t * cv
Definition: cacaview.c:45
initialize
static void initialize(void)
Definition: aafire.c:93
caca_refresh_display
int caca_refresh_display(caca_display_t *)
Flush pending changes and redraw the screen.
Definition: graphics.c:146
caca_free_display
int caca_free_display(caca_display_t *dp)
Detach a caca graphical context from a caca backend context.
Definition: caca.c:238
min
#define min(x, y)
Definition: aafire.c:201
pal
static __AA_CONST int pal[]
Definition: aafire.c:55
CACA_KEY_CTRL_C
Definition: caca.h:168
CACA_BLUE
Definition: caca.h:72
caca.common.rand
def rand(range_min, range_max)
Definition: common.py:230
caca_dither_bitmap
int caca_dither_bitmap(caca_canvas_t *, int, int, int, int, caca_dither_t const *, void const *)
Dither a bitmap on the canvas.
Definition: dither.c:940
caca_set_color_ansi
int caca_set_color_ansi(caca_canvas_t *cv, uint8_t fg, uint8_t bg)
Set the default colour pair for text (ANSI version).
Definition: attr.c:234
caca.h
The libcaca public header.
config.h
caca_get_event
#define caca_get_event(x)
Definition: caca0.h:136
firemain
static void firemain(void)
Definition: aafire.c:183
params
static aa_renderparams * params
Definition: aafire.c:48
CACA_EVENT_KEY_PRESS
Definition: caca.h:111
drawfire
static void drawfire(void)
Definition: aafire.c:203
END
#define END
dp
caca_display_t * dp
Definition: cacaview.c:45
caca_get_event_key_ch
int caca_get_event_key_ch(caca_event_t const *)
Return a key press or key release event's value.
Definition: event.c:219
caca_canvas
Definition: caca_internals.h:47