int main(void) { int event; int ch; struct caca_sprite *carita=NULL; if (caca_init()) return 1; caca_set_feature(CACA_ANTIALIASING_PREFILTER); caca_set_delay(40000); caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_RED); caca_draw_thin_box(0,0,10,2); caca_refresh(); carita=caca_load_sprite("carita.txt"); while(1) { while((event = caca_get_event(CACA_EVENT_ANY))) { caca_set_color(CACA_COLOR_BLACK, CACA_COLOR_WHITE); caca_printf(1,1, "%x\n", event); if(event & CACA_EVENT_KEY_PRESS) { ch=event & 0xffff; switch(ch) { case 'q': case 'Q': goto fin; case '0'...'9': /* extension gcc */ caca_set_color(caca_rand(2,14), CACA_COLOR_BLACK); caca_draw_thin_line(0,3,caca_get_width(),3+(ch-'0')*10); default: if (carita) caca_draw_sprite(caca_rand(4,caca_get_width()), caca_rand(4,caca_get_height()), carita, 0); } } caca_refresh(); } }
void langton(enum action action, caca_canvas_t *cv) { static char gradient[] = { ' ', ' ', '.', '.', ':', ':', 'x', 'x', 'X', 'X', '&', '&', 'W', 'W', '@', '@', }; static int steps[][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; static uint8_t *screen; static int width, height; static int ax[ANTS], ay[ANTS], dir[ANTS]; int i, a, x, y; switch(action) { case PREPARE: width = caca_get_canvas_width(cv); height = caca_get_canvas_height(cv); for(i = 0; i < ANTS; i++) { ax[i] = caca_rand(0, width); ay[i] = caca_rand(0, height); dir[i] = caca_rand(0, 4); } break; case INIT: screen = malloc(width * height); memset(screen, 0, width * height); break; case UPDATE: for(i = 0; i < ITER; i++) { for(x = 0; x < width * height; x++) { uint8_t p = screen[x]; if((p & 0x0f) > 1) screen[x] = p - 1; } for(a = 0; a < ANTS; a++) { uint8_t p = screen[ax[a] + width * ay[a]]; if(p & 0x0f) { dir[a] = (dir[a] + 1) % 4; screen[ax[a] + width * ay[a]] = a << 4; } else { dir[a] = (dir[a] + 3) % 4; screen[ax[a] + width * ay[a]] = (a << 4) | 0x0f; } ax[a] = (width + ax[a] + steps[dir[a]][0]) % width; ay[a] = (height + ay[a] + steps[dir[a]][1]) % height; } } break; case RENDER: for(y = 0; y < height; y++) { for(x = 0; x < width; x++) { uint8_t p = screen[x + width * y]; if(p & 0x0f) caca_set_color_ansi(cv, CACA_WHITE, p >> 4); else caca_set_color_ansi(cv, CACA_BLACK, CACA_BLACK); caca_put_char(cv, x, y, gradient[p & 0x0f]); } } break; case FREE: free(screen); break; }
int main(int argc, char **argv) { static caca_display_t *dp; static caca_canvas_t *frontcv, *backcv, *mask; int demo, next = -1, paused = 0, next_transition = DEMO_FRAMES; unsigned int i; int tmode = caca_rand(0, TRANSITION_COUNT); /* Set up two canvases, a mask, and attach a display to the front one */ frontcv = caca_create_canvas(0, 0); backcv = caca_create_canvas(0, 0); mask = caca_create_canvas(0, 0); dp = caca_create_display(frontcv); if(!dp) return 1; caca_set_canvas_size(backcv, caca_get_canvas_width(frontcv), caca_get_canvas_height(frontcv)); caca_set_canvas_size(mask, caca_get_canvas_width(frontcv), caca_get_canvas_height(frontcv)); caca_set_display_time(dp, 20000); /* Initialise all demos' lookup tables */ for(i = 0; i < DEMOS; i++) fn[i](PREPARE, frontcv); /* Choose a demo at random */ demo = caca_rand(0, DEMOS); fn[demo](INIT, frontcv); for(;;) { /* Handle events */ caca_event_t ev; while(caca_get_event(dp, CACA_EVENT_KEY_PRESS | CACA_EVENT_QUIT, &ev, 0)) { if(caca_get_event_type(&ev) == CACA_EVENT_QUIT) goto end; switch(caca_get_event_key_ch(&ev)) { case CACA_KEY_ESCAPE: case CACA_KEY_CTRL_C: case CACA_KEY_CTRL_Z: goto end; case ' ': paused = !paused; break; case '\r': if(next == -1) next_transition = frame; break; } } /* Resize the spare canvas, just in case the main one changed */ caca_set_canvas_size(backcv, caca_get_canvas_width(frontcv), caca_get_canvas_height(frontcv)); caca_set_canvas_size(mask, caca_get_canvas_width(frontcv), caca_get_canvas_height(frontcv)); if(paused) goto _paused; /* Update demo's data */ fn[demo](UPDATE, frontcv); /* Handle transitions */ if(frame == next_transition) { next = caca_rand(0, DEMOS); if(next == demo) next = (next + 1) % DEMOS; fn[next](INIT, backcv); } else if(frame == next_transition + TRANSITION_FRAMES) { fn[demo](FREE, frontcv); demo = next; next = -1; next_transition = frame + DEMO_FRAMES; tmode = caca_rand(0, TRANSITION_COUNT); } if(next != -1) fn[next](UPDATE, backcv); frame++; _paused: /* Render main demo's canvas */ fn[demo](RENDER, frontcv); /* If a transition is on its way, render it */ if(next != -1) { fn[next](RENDER, backcv); caca_set_color_ansi(mask, CACA_LIGHTGRAY, CACA_BLACK); caca_clear_canvas(mask); caca_set_color_ansi(mask, CACA_WHITE, CACA_WHITE); transition(mask, tmode, 100 * (frame - next_transition) / TRANSITION_FRAMES); caca_blit(frontcv, 0, 0, backcv, mask); } caca_set_color_ansi(frontcv, CACA_WHITE, CACA_BLUE); if(frame < 100) caca_put_str(frontcv, caca_get_canvas_width(frontcv) - 30, caca_get_canvas_height(frontcv) - 2, " -=[ Powered by libcaca ]=- "); caca_refresh_display(dp); } end: if(next != -1) fn[next](FREE, frontcv); fn[demo](FREE, frontcv); caca_free_display(dp); caca_free_canvas(mask); caca_free_canvas(backcv); caca_free_canvas(frontcv); return 0; }
void moire(enum action action, caca_canvas_t *cv) { static caca_dither_t *dither; static uint8_t *screen; static float d[6]; static uint32_t red[256], green[256], blue[256], alpha[256]; int i, x, y; switch(action) { case PREPARE: /* Fill various tables */ for(i = 0 ; i < 256; i++) red[i] = green[i] = blue[i] = alpha[i] = 0; for(i = 0; i < 6; i++) d[i] = ((float)caca_rand(50, 70)) / 1000.0; red[0] = green[0] = blue[0] = 0x777; red[1] = green[1] = blue[1] = 0xfff; /* Fill the circle */ for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS) { int t, dx, dy; for(t = 0, dx = 0, dy = i; dx <= dy; dx++) { draw_line(dx / 3, dy / 3, (i / DISCTHICKNESS) % 2); draw_line(dy / 3, dx / 3, (i / DISCTHICKNESS) % 2); t += t > 0 ? dx - dy-- : dx; } } break; case INIT: screen = malloc(XSIZ * YSIZ * sizeof(uint8_t)); dither = caca_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0); break; case UPDATE: memset(screen, 0, XSIZ * YSIZ); /* Set the palette */ red[0] = 0.5 * (1 + sin(d[0] * (frame + 1000))) * 0xfff; green[0] = 0.5 * (1 + cos(d[1] * frame)) * 0xfff; blue[0] = 0.5 * (1 + cos(d[2] * (frame + 3000))) * 0xfff; red[1] = 0.5 * (1 + sin(d[3] * (frame + 2000))) * 0xfff; green[1] = 0.5 * (1 + cos(d[4] * frame + 5.0)) * 0xfff; blue[1] = 0.5 * (1 + cos(d[5] * (frame + 4000))) * 0xfff; caca_set_dither_palette(dither, red, green, blue, alpha); /* Draw circles */ x = cos(d[0] * (frame + 1000)) * 128.0 + (XSIZ / 2); y = sin(0.11 * frame) * 128.0 + (YSIZ / 2); put_disc(screen, x, y); x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2); y = sin(d[1] * (frame + 2000)) * 64.0 + (YSIZ / 2); put_disc(screen, x, y); break; case RENDER: caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv), caca_get_canvas_height(cv), dither, screen); break; case FREE: free(screen); caca_free_dither(dither); break; } }
void metaballs(enum action action, caca_canvas_t *cv) { static caca_dither_t *caca_dither; static uint8_t *screen; static uint32_t r[256], g[256], b[256], a[256]; static float dd[METABALLS], di[METABALLS], dj[METABALLS], dk[METABALLS]; static unsigned int x[METABALLS], y[METABALLS]; static float i = 10.0, j = 17.0, k = 11.0; static double offset[360 + 80]; static unsigned int angleoff; int n, angle; switch(action) { case PREPARE: /* Make the palette eatable by libcaca */ for(n = 0; n < 256; n++) r[n] = g[n] = b[n] = a[n] = 0x0; r[255] = g[255] = b[255] = 0xfff; /* Generate ball sprite */ create_ball(); for(n = 0; n < METABALLS; n++) { dd[n] = caca_rand(0, 100); di[n] = (float)caca_rand(500, 4000) / 6000.0; dj[n] = (float)caca_rand(500, 4000) / 6000.0; dk[n] = (float)caca_rand(500, 4000) / 6000.0; } angleoff = caca_rand(0, 360); for(n = 0; n < 360 + 80; n++) offset[n] = 1.0 + sin((double)(n * M_PI / 60)); break; case INIT: screen = malloc(XSIZ * YSIZ * sizeof(uint8_t)); /* Create a libcaca dither smaller than our pixel buffer, so that we * display only the interesting part of it */ caca_dither = caca_create_dither(8, XSIZ - METASIZE, YSIZ - METASIZE, XSIZ, 0, 0, 0, 0); break; case UPDATE: angle = (frame + angleoff) % 360; /* Crop the palette */ for(n = CROPBALL; n < 255; n++) { int t1, t2, t3; double c1 = offset[angle]; double c2 = offset[angle + 40]; double c3 = offset[angle + 80]; t1 = n < 0x40 ? 0 : n < 0xc0 ? (n - 0x40) * 0x20 : 0xfff; t2 = n < 0xe0 ? 0 : (n - 0xe0) * 0x80; t3 = n < 0x40 ? n * 0x40 : 0xfff; r[n] = (c1 * t1 + c2 * t2 + c3 * t3) / 4; g[n] = (c1 * t2 + c2 * t3 + c3 * t1) / 4; b[n] = (c1 * t3 + c2 * t1 + c3 * t2) / 4; } /* Set the palette */ caca_set_dither_palette(caca_dither, r, g, b, a); /* Silly paths for our balls */ for(n = 0; n < METABALLS; n++) { float u = di[n] * i + dj[n] * j + dk[n] * sin(di[n] * k); float v = dd[n] + di[n] * j + dj[n] * k + dk[n] * sin(dk[n] * i); u = sin(i + u * 2.1) * (1.0 + sin(u)); v = sin(j + v * 1.9) * (1.0 + sin(v)); x[n] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4; y[n] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4; } i += 0.011; j += 0.017; k += 0.019; memset(screen, 0, XSIZ * YSIZ); for(n = 0; n < METABALLS; n++) draw_ball(screen, x[n], y[n]); break; case RENDER: caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv), caca_get_canvas_height(cv), caca_dither, screen + (METASIZE / 2) * (1 + XSIZ)); break; case FREE: free(screen); caca_free_dither(caca_dither); break; } }
void plasma(enum action action, caca_canvas_t *cv) { static caca_dither_t *dither; static uint8_t *screen; static uint32_t red[256], green[256], blue[256], alpha[256]; static double r[3], R[6]; int i, x, y; switch(action) { case PREPARE: /* Fill various tables */ for(i = 0 ; i < 256; i++) red[i] = green[i] = blue[i] = alpha[i] = 0; for(i = 0; i < 3; i++) r[i] = (double)(caca_rand(1, 1000)) / 60000 * M_PI; for(i = 0; i < 6; i++) R[i] = (double)(caca_rand(1, 1000)) / 10000; for(y = 0 ; y < TABLEY ; y++) for(x = 0 ; x < TABLEX ; x++) { double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2)) + (y - (TABLEX / 2)) * (y - (TABLEX / 2)))) * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY))); table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6; } break; case INIT: screen = malloc(XSIZ * YSIZ * sizeof(uint8_t)); dither = caca_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0); break; case UPDATE: for(i = 0 ; i < 256; i++) { double z = ((double)i) / 256 * 6 * M_PI; red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff; blue[i] = (1.0 + cos(z + r[0] * (frame + 100))) / 2 * 0xfff; green[i] = (1.0 + cos(z + r[2] * (frame + 200))) / 2 * 0xfff; } /* Set the palette */ caca_set_dither_palette(dither, red, green, blue, alpha); do_plasma(screen, (1.0 + sin(((double)frame) * R[0])) / 2, (1.0 + sin(((double)frame) * R[1])) / 2, (1.0 + sin(((double)frame) * R[2])) / 2, (1.0 + sin(((double)frame) * R[3])) / 2, (1.0 + sin(((double)frame) * R[4])) / 2, (1.0 + sin(((double)frame) * R[5])) / 2); break; case RENDER: caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv), caca_get_canvas_height(cv), dither, screen); break; case FREE: free(screen); caca_free_dither(dither); break; } }
int main(int argc, char **argv){ SDL_AudioSpec requested, obtained; int quit = 0; int xo, yo; int i, j, k; int meter[4]; static char chars[10] = { '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W' }; caca_display_t *dp; caca_canvas_t *cv; caca_canvas_t *pineapple; if(SDL_Init( SDL_INIT_AUDIO ) < 0){ err(1, "Couldnt initialize SDL\n"); exit(1); } cv = caca_create_canvas(80, 24); pineapple = caca_create_canvas(0, 0); if((cv == NULL) || (pineapple == NULL)){ printf("failed to create canvas\n"); return 1; } dp = caca_create_display(cv); caca_set_display_time(dp, 20000); if(dp == NULL){ printf("Failed to create display\n"); return 1; } caca_import_file(pineapple, "./pineapple", ""); atexit(SDL_Quit); requested.freq = 16000; requested.format = AUDIO_U8; requested.samples = 256; requested.callback = audiocb; requested.channels = 1; if(SDL_OpenAudio(&requested, &obtained) == -1){ err(1, "SDL_OpenAudio"); } initchip(); loadfile(argv[1]); SDL_PauseAudio(0); silence(); startplaysong(0); while(!quit) { caca_event_t ev; caca_set_color_ansi(cv, CACA_DEFAULT, CACA_DEFAULT); caca_clear_canvas(cv); xo = caca_get_canvas_width(cv); yo = caca_get_canvas_height(cv); //caca_blit(cv, 0, 0, pineapple, NULL); caca_blit(cv, 55, 0, pineapple, NULL); caca_set_color_ansi(cv, caca_rand(0, 16), caca_rand(0, 16)); caca_put_str(cv, (xo - strlen("pineapple player")) / 2, (yo / 2) - 5, "pineapple player"); caca_set_color_ansi(cv, caca_rand(0, 16), caca_rand(0, 16)); caca_printf(cv, (xo - strlen("song pos -> ")) / 2, (yo / 2) - 3, "song pos -> %x", songpos); for(i = 0; i < 4; i ++) meter[i] = (osc[i].volume*20)/255; /* note visualizer */ i = 0; for(j = 0; j < 25; j=j+6){ for(k = 0; k < 4; k++){ caca_draw_line(cv, (((xo/2)+10)-j)-k, yo, (((xo/2)+10)-j)-k, yo - meter[i], chars[caca_rand(0, 9)]); } i++; } for(i = 0; i < 4; i ++) caca_printf(cv, 0, i, "%0x", osc[i].volume); while(caca_get_event(dp, CACA_EVENT_ANY, &ev, 0)) { if(caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS) { switch(caca_get_event_key_ch(&ev)) { case 'q': case 'Q': case CACA_KEY_ESCAPE: quit = 1; break; } } } caca_refresh_display(dp); } silence(); caca_free_display(dp); caca_free_canvas(cv); return 0; }