[244] | 1 | /* |
---|
[268] | 2 | * cacafire fire demo for libcaca |
---|
| 3 | * Copyright (c) 2003 Sam Hocevar <sam@zoy.org> |
---|
[320] | 4 | * 1998, 1999, 2001 Jan Hubicka <hubicka@freesoft.cz> |
---|
[268] | 5 | * All Rights Reserved |
---|
[244] | 6 | * |
---|
[268] | 7 | * $Id: aafire.c 859 2006-04-24 20:35:59Z sam $ |
---|
[244] | 8 | * |
---|
[268] | 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. |
---|
[244] | 13 | * |
---|
[268] | 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. |
---|
[244] | 18 | * |
---|
[268] | 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 |
---|
[244] | 23 | */ |
---|
| 24 | |
---|
| 25 | #ifdef LIBCACA |
---|
| 26 | #include "config.h" |
---|
[859] | 27 | #include "common.h" |
---|
[568] | 28 | #if !defined(__KERNEL__) |
---|
| 29 | # include <stdio.h> |
---|
| 30 | # include <stdlib.h> |
---|
| 31 | # include <string.h> |
---|
| 32 | #endif |
---|
[244] | 33 | #include "caca.h" |
---|
| 34 | #else |
---|
| 35 | #include <stdio.h> |
---|
| 36 | #include "aalib.h" |
---|
| 37 | |
---|
| 38 | #define XSIZ aa_imgwidth(context) |
---|
| 39 | #define YSIZ (aa_imgheight(context)-4) |
---|
| 40 | #endif |
---|
| 41 | #define MAXTABLE (256*5) |
---|
| 42 | #ifdef LIBCACA |
---|
[811] | 43 | static cucul_canvas_t *cv; |
---|
| 44 | static caca_display_t *dp; |
---|
[304] | 45 | static int XSIZ, YSIZ; |
---|
[777] | 46 | static cucul_dither_t *cucul_dither; |
---|
[244] | 47 | static char *bitmap; |
---|
[377] | 48 | static int pause = 0; |
---|
[244] | 49 | #else |
---|
| 50 | static aa_context *context; |
---|
| 51 | static aa_renderparams *params; |
---|
| 52 | static aa_palette palette; |
---|
| 53 | #endif |
---|
| 54 | static unsigned int table[MAXTABLE]; |
---|
| 55 | #ifdef LIBCACA |
---|
[305] | 56 | static int const pal[] = |
---|
[244] | 57 | #else |
---|
| 58 | __AA_CONST static int pal[] = |
---|
| 59 | #endif |
---|
| 60 | { |
---|
| 61 | 0, 0, 0, 0, 0, 6, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 8, 0, 0, 9, 0, 0, 10, |
---|
| 62 | 2, 0, 10, 4, 0, 9, 6, 0, 9, 8, 0, 8, 10, 0, 7, 12, 0, 7, 14, 0, 6, 16, 0, 5, |
---|
| 63 | 18, 0, 5, 20, 0, 4, 22, 0, 4, 24, 0, 3, 26, 0, 2, 28, 0, 2, 30, 0, 1, 32, 0, 0, |
---|
| 64 | 32, 0, 0, 33, 0, 0, 34, 0, 0, 35, 0, 0, 36, 0, 0, 36, 0, 0, 37, 0, 0, 38, 0, 0, |
---|
| 65 | 39, 0, 0, 40, 0, 0, 40, 0, 0, 41, 0, 0, 42, 0, 0, 43, 0, 0, 44, 0, 0, 45, 0, 0, |
---|
| 66 | 46, 1, 0, 47, 1, 0, 48, 2, 0, 49, 2, 0, 50, 3, 0, 51, 3, 0, 52, 4, 0, 53, 4, 0, |
---|
| 67 | 54, 5, 0, 55, 5, 0, 56, 6, 0, 57, 6, 0, 58, 7, 0, 59, 7, 0, 60, 8, 0, 61, 8, 0, |
---|
| 68 | 63, 9, 0, 63, 9, 0, 63, 10, 0, 63, 10, 0, 63, 11, 0, 63, 11, 0, 63, 12, 0, 63, 12, 0, |
---|
| 69 | 63, 13, 0, 63, 13, 0, 63, 14, 0, 63, 14, 0, 63, 15, 0, 63, 15, 0, 63, 16, 0, 63, 16, 0, |
---|
| 70 | 63, 17, 0, 63, 17, 0, 63, 18, 0, 63, 18, 0, 63, 19, 0, 63, 19, 0, 63, 20, 0, 63, 20, 0, |
---|
| 71 | 63, 21, 0, 63, 21, 0, 63, 22, 0, 63, 22, 0, 63, 23, 0, 63, 24, 0, 63, 24, 0, 63, 25, 0, |
---|
| 72 | 63, 25, 0, 63, 26, 0, 63, 26, 0, 63, 27, 0, 63, 27, 0, 63, 28, 0, 63, 28, 0, 63, 29, 0, |
---|
| 73 | 63, 29, 0, 63, 30, 0, 63, 30, 0, 63, 31, 0, 63, 31, 0, 63, 32, 0, 63, 32, 0, 63, 33, 0, |
---|
| 74 | 63, 33, 0, 63, 34, 0, 63, 34, 0, 63, 35, 0, 63, 35, 0, 63, 36, 0, 63, 36, 0, 63, 37, 0, |
---|
| 75 | 63, 38, 0, 63, 38, 0, 63, 39, 0, 63, 39, 0, 63, 40, 0, 63, 40, 0, 63, 41, 0, 63, 41, 0, |
---|
| 76 | 63, 42, 0, 63, 42, 0, 63, 43, 0, 63, 43, 0, 63, 44, 0, 63, 44, 0, 63, 45, 0, 63, 45, 0, |
---|
| 77 | 63, 46, 0, 63, 46, 0, 63, 47, 0, 63, 47, 0, 63, 48, 0, 63, 48, 0, 63, 49, 0, 63, 49, 0, |
---|
| 78 | 63, 50, 0, 63, 50, 0, 63, 51, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, |
---|
| 79 | 63, 53, 0, 63, 53, 0, 63, 53, 0, 63, 53, 0, 63, 54, 0, 63, 54, 0, 63, 54, 0, 63, 54, 0, |
---|
| 80 | 63, 54, 0, 63, 55, 0, 63, 55, 0, 63, 55, 0, 63, 55, 0, 63, 56, 0, 63, 56, 0, 63, 56, 0, |
---|
| 81 | 63, 56, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 58, 0, 63, 58, 0, |
---|
| 82 | 63, 58, 0, 63, 58, 0, 63, 59, 0, 63, 59, 0, 63, 59, 0, 63, 59, 0, 63, 60, 0, 63, 60, 0, |
---|
| 83 | 63, 60, 0, 63, 60, 0, 63, 60, 0, 63, 61, 0, 63, 61, 0, 63, 61, 0, 63, 61, 0, 63, 62, 0, |
---|
| 84 | 63, 62, 0, 63, 62, 0, 63, 62, 0, 63, 63, 0, 63, 63, 1, 63, 63, 2, 63, 63, 3, 63, 63, 4, |
---|
| 85 | 63, 63, 5, 63, 63, 6, 63, 63, 7, 63, 63, 8, 63, 63, 9, 63, 63, 10, 63, 63, 10, 63, 63, 11, |
---|
| 86 | 63, 63, 12, 63, 63, 13, 63, 63, 14, 63, 63, 15, 63, 63, 16, 63, 63, 17, 63, 63, 18, 63, 63, 19, |
---|
| 87 | 63, 63, 20, 63, 63, 21, 63, 63, 21, 63, 63, 22, 63, 63, 23, 63, 63, 24, 63, 63, 25, 63, 63, 26, |
---|
| 88 | 63, 63, 27, 63, 63, 28, 63, 63, 29, 63, 63, 30, 63, 63, 31, 63, 63, 31, 63, 63, 32, 63, 63, 33, |
---|
| 89 | 63, 63, 34, 63, 63, 35, 63, 63, 36, 63, 63, 37, 63, 63, 38, 63, 63, 39, 63, 63, 40, 63, 63, 41, |
---|
| 90 | 63, 63, 42, 63, 63, 42, 63, 63, 43, 63, 63, 44, 63, 63, 45, 63, 63, 46, 63, 63, 47, 63, 63, 48, |
---|
| 91 | 63, 63, 49, 63, 63, 50, 63, 63, 51, 63, 63, 52, 63, 63, 52, 63, 63, 53, 63, 63, 54, 63, 63, 55, |
---|
| 92 | 63, 63, 56, 63, 63, 57, 63, 63, 58, 63, 63, 59, 63, 63, 60, 63, 63, 61, 63, 63, 62, 63, 63, 63}; |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | static void |
---|
| 96 | initialize (void) |
---|
| 97 | { |
---|
| 98 | int i; |
---|
| 99 | #ifdef LIBCACA |
---|
[514] | 100 | unsigned int r[256], g[256], b[256], a[256]; |
---|
[244] | 101 | #endif |
---|
| 102 | |
---|
| 103 | #ifdef LIBCACA |
---|
[813] | 104 | cv = cucul_create_canvas(80, 32); |
---|
[811] | 105 | if (!cv) |
---|
[244] | 106 | { |
---|
[524] | 107 | printf ("Failed to initialize libcucul\n"); |
---|
| 108 | exit (1); |
---|
| 109 | } |
---|
[819] | 110 | dp = caca_create_display(cv); |
---|
[811] | 111 | if (!dp) |
---|
[524] | 112 | { |
---|
[244] | 113 | printf ("Failed to initialize libcaca\n"); |
---|
| 114 | exit (1); |
---|
| 115 | } |
---|
[811] | 116 | caca_set_delay(dp, 10000); |
---|
[813] | 117 | XSIZ = cucul_get_canvas_width(cv) * 2; |
---|
| 118 | YSIZ = cucul_get_canvas_height(cv) * 2 - 4; |
---|
[244] | 119 | #else |
---|
| 120 | context = aa_autoinit (&aa_defparams); |
---|
| 121 | if (context == NULL) |
---|
| 122 | { |
---|
| 123 | printf ("Failed to initialize aalib\n"); |
---|
| 124 | exit (1); |
---|
| 125 | } |
---|
| 126 | aa_autoinitkbd (context, 0); |
---|
| 127 | params = aa_getrenderparams (); |
---|
| 128 | #endif |
---|
| 129 | |
---|
| 130 | for (i = 0; i < 256; i++) |
---|
| 131 | #ifdef LIBCACA |
---|
| 132 | { |
---|
| 133 | r[i] = pal[i * 3] * 64; |
---|
| 134 | g[i] = pal[i * 3 + 1] * 64; |
---|
| 135 | b[i] = pal[i * 3 + 2] * 64; |
---|
[246] | 136 | a[i] = 0xfff; |
---|
[244] | 137 | } |
---|
| 138 | #else |
---|
| 139 | aa_setpalette (palette, i, pal[i * 3] * 4, |
---|
| 140 | pal[i * 3 + 1] * 4, pal[i * 3 + 2] * 4); |
---|
| 141 | #endif |
---|
| 142 | |
---|
| 143 | #ifdef LIBCACA |
---|
[734] | 144 | cucul_dither = cucul_create_dither(8, XSIZ, YSIZ - 2, XSIZ, 0, 0, 0, 0); |
---|
| 145 | cucul_set_dither_palette(cucul_dither, r, g, b, a); |
---|
[813] | 146 | bitmap = malloc(4 * cucul_get_canvas_width(cv) |
---|
| 147 | * cucul_get_canvas_height(cv)); |
---|
| 148 | memset(bitmap, 0, 4 * cucul_get_canvas_width(cv) |
---|
| 149 | * cucul_get_canvas_height(cv)); |
---|
[244] | 150 | #else |
---|
| 151 | aa_hidecursor (context); |
---|
| 152 | #endif |
---|
| 153 | } |
---|
| 154 | static void |
---|
| 155 | uninitialize (void) |
---|
| 156 | { |
---|
| 157 | #ifdef LIBCACA |
---|
[819] | 158 | caca_free_display(dp); |
---|
[813] | 159 | cucul_free_canvas(cv); |
---|
[244] | 160 | #else |
---|
| 161 | aa_close (context); |
---|
| 162 | #endif |
---|
| 163 | exit (0); |
---|
| 164 | } |
---|
| 165 | static void |
---|
| 166 | gentable (void) |
---|
| 167 | { |
---|
| 168 | unsigned int i, p2; |
---|
| 169 | unsigned int minus = 800 / YSIZ; |
---|
| 170 | if (minus == 0) |
---|
| 171 | minus = 1; |
---|
| 172 | for (i = 0; i < MAXTABLE; i++) |
---|
| 173 | { |
---|
| 174 | if (i > minus) |
---|
| 175 | { |
---|
| 176 | p2 = (i - minus) / 5; |
---|
| 177 | table[i] = p2; |
---|
| 178 | } |
---|
| 179 | else |
---|
| 180 | table[i] = 0; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | #define MA 5 |
---|
| 185 | static void |
---|
| 186 | firemain (void) |
---|
| 187 | { |
---|
| 188 | register unsigned int i; |
---|
| 189 | unsigned char *p; |
---|
| 190 | #ifndef LIBCACA |
---|
| 191 | char *bitmap = aa_image (context); |
---|
| 192 | #endif |
---|
| 193 | i = 0; |
---|
| 194 | #define END (bitmap + XSIZ * YSIZ) |
---|
[514] | 195 | for (p = (unsigned char*)bitmap; |
---|
| 196 | (char*)p <= (( char *) (END)); |
---|
| 197 | p += 1) |
---|
[244] | 198 | { |
---|
| 199 | *p = table[(*(p + XSIZ - 1) + *(p + XSIZ + 1) + *(p + XSIZ)) + |
---|
| 200 | (*(p + 2 * XSIZ - 1) + *(p + 2 * XSIZ + 1))]; |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | #define min(x,y) ((x)<(y)?(x):(y)) |
---|
| 205 | static void |
---|
| 206 | drawfire (void) |
---|
| 207 | { |
---|
| 208 | unsigned int i, last1, i1, i2; |
---|
| 209 | static int loop = 0, sloop = 0; |
---|
| 210 | static unsigned int height = 0; |
---|
| 211 | register unsigned char *p; |
---|
| 212 | #ifndef LIBCACA |
---|
| 213 | char *bitmap = aa_image (context); |
---|
[377] | 214 | #else |
---|
| 215 | if(pause) |
---|
| 216 | goto paused; |
---|
[244] | 217 | #endif |
---|
| 218 | |
---|
| 219 | height++; |
---|
| 220 | loop--; |
---|
| 221 | if (loop < 0) |
---|
| 222 | loop = rand () % 3, sloop++;; |
---|
| 223 | i1 = 1; |
---|
| 224 | i2 = 4 * XSIZ + 1; |
---|
[514] | 225 | for (p = (unsigned char *) bitmap + XSIZ * (YSIZ + 0); |
---|
[244] | 226 | p < ((unsigned char *) bitmap + XSIZ * (YSIZ + 1)); |
---|
| 227 | p++, i1 += 4, i2 -= 4) |
---|
| 228 | { |
---|
| 229 | last1 = rand () % min (i1, min (i2, height)); |
---|
| 230 | i = rand () % 6; |
---|
| 231 | for (; p < (unsigned char *) bitmap + XSIZ * (YSIZ + 1) && i != 0; |
---|
| 232 | p++, i--, i1 += 4, i2 -= 4) |
---|
| 233 | *p = last1, last1 += rand () % 6 - 2, *(p + XSIZ) = last1, last1 += |
---|
| 234 | rand () % 6 - 2; |
---|
| 235 | *(p + 2 * XSIZ) = last1, last1 += rand () % 6 - 2; |
---|
| 236 | } |
---|
| 237 | i = 0; |
---|
| 238 | firemain (); |
---|
| 239 | #ifdef LIBCACA |
---|
[377] | 240 | paused: |
---|
[816] | 241 | cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv), |
---|
| 242 | cucul_get_canvas_height(cv), cucul_dither, bitmap); |
---|
[811] | 243 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
[813] | 244 | cucul_putstr(cv, cucul_get_canvas_width(cv) - 30, |
---|
| 245 | cucul_get_canvas_height(cv) - 2, " -=[ Powered by libcaca ]=- "); |
---|
[704] | 246 | |
---|
[819] | 247 | caca_refresh_display(dp); |
---|
[347] | 248 | /*XSIZ = caca_get_width() * 2; |
---|
| 249 | YSIZ = caca_get_height() * 2 - 4;*/ |
---|
[244] | 250 | #else |
---|
| 251 | aa_renderpalette (context, palette, params, 0, 0, aa_scrwidth (context), |
---|
| 252 | aa_scrheight (context)); |
---|
| 253 | aa_flush (context); |
---|
| 254 | #endif |
---|
| 255 | } |
---|
| 256 | static void |
---|
| 257 | game (void) |
---|
| 258 | { |
---|
[377] | 259 | #ifndef LIBCACA |
---|
[244] | 260 | int event; |
---|
[377] | 261 | #endif |
---|
[244] | 262 | gentable (); |
---|
| 263 | #ifdef LIBCACA |
---|
[377] | 264 | for(;;) |
---|
[244] | 265 | #else |
---|
| 266 | while (!(event = aa_getevent (context, 0)) || event == AA_RESIZE) |
---|
| 267 | #endif |
---|
| 268 | { |
---|
[377] | 269 | #ifdef LIBCACA |
---|
[777] | 270 | caca_event_t ev; |
---|
[811] | 271 | if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0)) |
---|
[377] | 272 | { |
---|
[810] | 273 | switch(ev.data.key.ch) |
---|
[681] | 274 | { |
---|
| 275 | case CACA_KEY_ESCAPE: return; |
---|
| 276 | case ' ': pause = !pause; |
---|
| 277 | } |
---|
[377] | 278 | } |
---|
| 279 | #endif |
---|
[244] | 280 | drawfire (); |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | int |
---|
| 284 | main (int argc, char **argv) |
---|
| 285 | { |
---|
| 286 | #ifndef LIBCACA |
---|
| 287 | if (!aa_parseoptions (NULL, NULL, &argc, argv) || argc != 1) |
---|
| 288 | { |
---|
| 289 | printf ("%s", aa_help); |
---|
| 290 | exit (1); |
---|
| 291 | } |
---|
| 292 | #endif |
---|
| 293 | initialize (); |
---|
| 294 | #ifndef LIBCACA |
---|
| 295 | aa_resizehandler (context, (void *) aa_resize); |
---|
| 296 | #endif |
---|
| 297 | game (); |
---|
| 298 | uninitialize (); |
---|
| 299 | return 1; |
---|
| 300 | } |
---|