[339] | 1 | /* |
---|
[340] | 2 | * cacamoir moiré circles effect for libcaca |
---|
[339] | 3 | * Copyright (c) 2004 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
| 5 | * |
---|
| 6 | * $Id: cacamoir.c 681 2006-03-23 18:36:59Z sam $ |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
[522] | 9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
| 10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
| 11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
[339] | 12 | */ |
---|
| 13 | |
---|
| 14 | #include "config.h" |
---|
| 15 | |
---|
[581] | 16 | #if !defined(__KERNEL__) |
---|
| 17 | # include <math.h> |
---|
| 18 | # include <string.h> |
---|
| 19 | #endif |
---|
[339] | 20 | |
---|
[524] | 21 | #include "cucul.h" |
---|
[339] | 22 | #include "caca.h" |
---|
| 23 | |
---|
| 24 | /* Virtual buffer size */ |
---|
| 25 | #define XSIZ 256 |
---|
| 26 | #define YSIZ 256 |
---|
| 27 | |
---|
| 28 | #define DISCSIZ 512 |
---|
[614] | 29 | #define DISCTHICKNESS 96 |
---|
[339] | 30 | |
---|
| 31 | static unsigned char screen[XSIZ * YSIZ]; |
---|
| 32 | static unsigned char disc[DISCSIZ * DISCSIZ]; |
---|
| 33 | |
---|
| 34 | static void put_disc(int, int); |
---|
| 35 | static void draw_disc(int, char); |
---|
| 36 | static void draw_line(int, int, char); |
---|
| 37 | |
---|
| 38 | int main (int argc, char **argv) |
---|
| 39 | { |
---|
[524] | 40 | cucul_t *qq; caca_t *kk; |
---|
[514] | 41 | unsigned int red[256], green[256], blue[256], alpha[256]; |
---|
[524] | 42 | struct cucul_bitmap *bitmap; |
---|
[377] | 43 | int i, x, y, frame = 0, pause = 0; |
---|
[339] | 44 | |
---|
[677] | 45 | qq = cucul_create(0, 0); |
---|
[524] | 46 | if(!qq) |
---|
[339] | 47 | return 1; |
---|
[524] | 48 | kk = caca_attach(qq); |
---|
| 49 | if(!kk) |
---|
| 50 | return 1; |
---|
[339] | 51 | |
---|
[524] | 52 | caca_set_delay(kk, 20000); |
---|
[339] | 53 | |
---|
| 54 | /* Fill various tables */ |
---|
| 55 | for(i = 0 ; i < 256; i++) |
---|
| 56 | red[i] = green[i] = blue[i] = alpha[i] = 0; |
---|
| 57 | |
---|
| 58 | red[0] = green[0] = blue[0] = 0x777; |
---|
| 59 | red[1] = green[1] = blue[1] = 0xfff; |
---|
| 60 | |
---|
| 61 | /* Fill the circle */ |
---|
| 62 | for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS) |
---|
| 63 | draw_disc(i, (i / DISCTHICKNESS) % 2); |
---|
| 64 | |
---|
[524] | 65 | /* Create a libcucul bitmap */ |
---|
[666] | 66 | bitmap = cucul_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0); |
---|
[339] | 67 | |
---|
| 68 | /* Main loop */ |
---|
[377] | 69 | for(;;) |
---|
[339] | 70 | { |
---|
[681] | 71 | struct caca_event ev; |
---|
| 72 | if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev)) |
---|
[377] | 73 | { |
---|
[681] | 74 | switch(ev.data.key.c) |
---|
| 75 | { |
---|
| 76 | case CACA_KEY_ESCAPE: goto end; |
---|
| 77 | case ' ': pause = !pause; |
---|
| 78 | } |
---|
[377] | 79 | } |
---|
| 80 | |
---|
| 81 | if(pause) |
---|
| 82 | goto paused; |
---|
| 83 | |
---|
[339] | 84 | memset(screen, 0, XSIZ * YSIZ); |
---|
| 85 | |
---|
| 86 | /* Set the palette */ |
---|
| 87 | red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff; |
---|
| 88 | green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff; |
---|
| 89 | blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff; |
---|
| 90 | |
---|
| 91 | red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff; |
---|
| 92 | green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff; |
---|
| 93 | blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff; |
---|
| 94 | |
---|
[653] | 95 | cucul_set_bitmap_palette(bitmap, red, green, blue, alpha); |
---|
[339] | 96 | |
---|
| 97 | /* Draw circles */ |
---|
| 98 | x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2); |
---|
| 99 | y = sin(0.11 * frame) * 128.0 + (YSIZ / 2); |
---|
| 100 | put_disc(x, y); |
---|
| 101 | |
---|
| 102 | x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2); |
---|
| 103 | y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2); |
---|
| 104 | put_disc(x, y); |
---|
| 105 | |
---|
[377] | 106 | frame++; |
---|
| 107 | |
---|
| 108 | paused: |
---|
[524] | 109 | cucul_draw_bitmap(qq, 0, 0, |
---|
| 110 | cucul_get_width(qq) - 1, cucul_get_height(qq) - 1, |
---|
| 111 | bitmap, screen); |
---|
[527] | 112 | caca_display(kk); |
---|
[339] | 113 | } |
---|
| 114 | |
---|
[377] | 115 | end: |
---|
[653] | 116 | cucul_free_bitmap(bitmap); |
---|
[524] | 117 | caca_detach(kk); |
---|
[677] | 118 | cucul_free(qq); |
---|
[339] | 119 | |
---|
| 120 | return 0; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | static void put_disc(int x, int y) |
---|
| 124 | { |
---|
[514] | 125 | char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ; |
---|
[339] | 126 | int i, j; |
---|
| 127 | |
---|
| 128 | for(j = 0; j < YSIZ; j++) |
---|
| 129 | for(i = 0; i < XSIZ; i++) |
---|
| 130 | { |
---|
| 131 | screen[i + XSIZ * j] ^= src[i + DISCSIZ * j]; |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | static void draw_disc(int r, char color) |
---|
| 136 | { |
---|
| 137 | int t, dx, dy; |
---|
| 138 | |
---|
| 139 | for(t = 0, dx = 0, dy = r; dx <= dy; dx++) |
---|
| 140 | { |
---|
| 141 | draw_line(dx / 3, dy / 3, color); |
---|
| 142 | draw_line(dy / 3, dx / 3, color); |
---|
| 143 | |
---|
| 144 | t += t > 0 ? dx - dy-- : dx; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | static void draw_line(int x, int y, char color) |
---|
| 149 | { |
---|
| 150 | if(x == 0 || y == 0 || y > DISCSIZ / 2) |
---|
| 151 | return; |
---|
| 152 | |
---|
| 153 | if(x > DISCSIZ / 2) |
---|
| 154 | x = DISCSIZ / 2; |
---|
| 155 | |
---|
| 156 | memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y), |
---|
| 157 | color, 2 * x - 1); |
---|
| 158 | memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1), |
---|
| 159 | color, 2 * x - 1); |
---|
| 160 | } |
---|
| 161 | |
---|