From 5ef8b56c5ee746dfec0fc220711f9a87cfa0d8da Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 5 Nov 2004 14:44:41 +0000 Subject: [PATCH] * examples/: + Use Space to pause demos, Esc to quit. --- examples/aafire.c | 17 ++++++++++++++++- examples/cacaball.c | 16 ++++++++++++++-- examples/cacamoir.c | 17 +++++++++++++++-- examples/cacaplas.c | 23 ++++++++++++----------- 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/examples/aafire.c b/examples/aafire.c index db14d56..ae8bd9c 100644 --- a/examples/aafire.c +++ b/examples/aafire.c @@ -40,6 +40,7 @@ static int XSIZ, YSIZ; static struct caca_bitmap *caca_bitmap; static char *bitmap; +static int pause = 0; #else static aa_context *context; static aa_renderparams *params; @@ -193,6 +194,9 @@ drawfire (void) register unsigned char *p; #ifndef LIBCACA char *bitmap = aa_image (context); +#else + if(pause) + goto paused; #endif height++; @@ -216,6 +220,7 @@ drawfire (void) i = 0; firemain (); #ifdef LIBCACA +paused: caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1, caca_bitmap, bitmap); caca_refresh(); @@ -230,14 +235,24 @@ drawfire (void) static void game (void) { +#ifndef LIBCACA int event; +#endif gentable (); #ifdef LIBCACA - while (!(event = caca_get_event(CACA_EVENT_KEY_PRESS))) + for(;;) #else while (!(event = aa_getevent (context, 0)) || event == AA_RESIZE) #endif { +#ifdef LIBCACA + switch (caca_get_event(CACA_EVENT_KEY_PRESS)) + { + case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: return; + case CACA_EVENT_KEY_PRESS | ' ': pause = !pause; + } + +#endif drawfire (); } } diff --git a/examples/cacaball.c b/examples/cacaball.c index 98014be..9f91d86 100644 --- a/examples/cacaball.c +++ b/examples/cacaball.c @@ -57,7 +57,7 @@ int main(int argc, char **argv) unsigned int x[METABALLS], y[METABALLS]; struct caca_bitmap *caca_bitmap; float i = 10.0, j = 17.0, k = 11.0; - int p, frame = 0; + int p, frame = 0, pause = 0; if(caca_init()) return 1; @@ -86,8 +86,17 @@ int main(int argc, char **argv) } /* Go ! */ - while(!caca_get_event(CACA_EVENT_KEY_PRESS)) + for(;;) { + switch(caca_get_event(CACA_EVENT_KEY_PRESS)) + { + case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end; + case CACA_EVENT_KEY_PRESS | ' ': pause = !pause; + } + + if(pause) + goto paused; + frame++; /* Crop the palette */ @@ -134,6 +143,7 @@ int main(int argc, char **argv) for(p = 0; p < METABALLS; p++) draw_ball(x[p], y[p]); +paused: /* Draw our virtual buffer to screen, letting libcaca resize it */ caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1, caca_bitmap, pixels + (METASIZE / 2) * (1 + XSIZ)); @@ -141,6 +151,8 @@ int main(int argc, char **argv) } /* End, bye folks */ +end: + caca_free_bitmap(caca_bitmap); caca_end(); return 0; diff --git a/examples/cacamoir.c b/examples/cacamoir.c index 67192c1..39df1ca 100644 --- a/examples/cacamoir.c +++ b/examples/cacamoir.c @@ -46,7 +46,7 @@ int main (int argc, char **argv) { int red[256], green[256], blue[256], alpha[256]; struct caca_bitmap *bitmap; - int i, x, y, frame; + int i, x, y, frame = 0, pause = 0; if(caca_init() < 0) return 1; @@ -68,8 +68,17 @@ int main (int argc, char **argv) bitmap = caca_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0); /* Main loop */ - for(frame = 0; !caca_get_event(CACA_EVENT_KEY_PRESS); frame++) + for(;;) { + switch(caca_get_event(CACA_EVENT_KEY_PRESS)) + { + case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end; + case CACA_EVENT_KEY_PRESS | ' ': pause = !pause; + } + + if(pause) + goto paused; + memset(screen, 0, XSIZ * YSIZ); /* Set the palette */ @@ -92,11 +101,15 @@ int main (int argc, char **argv) y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2); put_disc(x, y); + frame++; + +paused: caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1, bitmap, screen); caca_refresh(); } +end: caca_free_bitmap(bitmap); caca_end(); diff --git a/examples/cacaplas.c b/examples/cacaplas.c index 12f9827..67dec7f 100644 --- a/examples/cacaplas.c +++ b/examples/cacaplas.c @@ -89,6 +89,9 @@ int main (int argc, char **argv) case CACA_EVENT_KEY_PRESS | ' ': pause = !pause; } + if(pause) + goto paused; + for(i = 0 ; i < 256; i++) { double z = ((double)i) / 256 * 6 * M_PI; @@ -101,18 +104,16 @@ int main (int argc, char **argv) /* Set the palette */ caca_set_bitmap_palette(bitmap, red, green, blue, alpha); - if(!pause) - { - 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); - frame++; - } + 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); + frame++; +paused: caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1, bitmap, screen); caca_refresh();