Changeset 319 for libcaca/trunk/examples/cacaball.c
- Timestamp:
- Jan 6, 2004, 1:46:25 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/examples/cacaball.c
r317 r319 1 1 /* 2 * cacaball Metaballs effect usinglibcaca3 * 4 * 2 * cacaball metaballs effect for libcaca 3 * Copyright (c) 2003-2004 Jean-Yves Lamoureux <jylam@lnxscene.org> 4 * All Rights Reserved 5 5 * 6 * $Id$ 6 7 * 7 * 8 * 9 * 10 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2 of the License, or (at your option) any later version. 11 12 * 12 * 13 * 14 * 15 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 16 17 * 17 * 18 * 19 * 20 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 21 * 02111-1307 USA 21 22 */ 22 23 23 #include " ../config.h"24 #include "config.h" 24 25 25 #include <math.h>26 #include <string.h>27 26 #include <stdio.h> 28 27 #include <stdlib.h> 29 28 #include <string.h> 29 #include <math.h> 30 30 31 31 #include "caca.h" 32 32 33 33 /* Virtual buffer size */ 34 #define XSIZ 12835 #define YSIZ 12834 #define XSIZ 256 35 #define YSIZ 256 36 36 37 #define METASIZE 64 38 #define METACENTER (METASIZE/2) 37 #define METASIZE 128 39 38 40 static struct caca_bitmap *caca_bitmap; 41 static unsigned char *bitmap; 42 unsigned char MetaBoule[METASIZE*METASIZE]; 39 static void draw_ball(unsigned int, unsigned int); 40 static void generate_ball(void); 43 41 44 45 46 void drawBalls(unsigned int bx, unsigned int by); 47 void cutBalls(unsigned int bx, unsigned int by); 48 void generateBalls(void); 49 50 /* Screen palette */ 51 const static int pal[] = 52 { 53 0, 0, 0, 0, 0, 6, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 8, 0, 0, 9, 0, 0, 10, 54 2, 0, 10, 4, 0, 9, 6, 0, 9, 8, 0, 8, 10, 0, 7, 12, 0, 7, 14, 0, 6, 16, 0, 5, 55 18, 0, 5, 20, 0, 4, 22, 0, 4, 24, 0, 3, 26, 0, 2, 28, 0, 2, 30, 0, 1, 32, 0, 0, 56 32, 0, 0, 33, 0, 0, 34, 0, 0, 35, 0, 0, 36, 0, 0, 36, 0, 0, 37, 0, 0, 38, 0, 0, 57 39, 0, 0, 40, 0, 0, 40, 0, 0, 41, 0, 0, 42, 0, 0, 43, 0, 0, 44, 0, 0, 45, 0, 0, 58 46, 1, 0, 47, 1, 0, 48, 2, 0, 49, 2, 0, 50, 3, 0, 51, 3, 0, 52, 4, 0, 53, 4, 0, 59 54, 5, 0, 55, 5, 0, 56, 6, 0, 57, 6, 0, 58, 7, 0, 59, 7, 0, 60, 8, 0, 61, 8, 0, 60 63, 9, 0, 63, 9, 0, 63, 10, 0, 63, 10, 0, 63, 11, 0, 63, 11, 0, 63, 12, 0, 63, 12, 0, 61 63, 13, 0, 63, 13, 0, 63, 14, 0, 63, 14, 0, 63, 15, 0, 63, 15, 0, 63, 16, 0, 63, 16, 0, 62 63, 17, 0, 63, 17, 0, 63, 18, 0, 63, 18, 0, 63, 19, 0, 63, 19, 0, 63, 20, 0, 63, 20, 0, 63 63, 21, 0, 63, 21, 0, 63, 22, 0, 63, 22, 0, 63, 23, 0, 63, 24, 0, 63, 24, 0, 63, 25, 0, 64 63, 25, 0, 63, 26, 0, 63, 26, 0, 63, 27, 0, 63, 27, 0, 63, 28, 0, 63, 28, 0, 63, 29, 0, 65 63, 29, 0, 63, 30, 0, 63, 30, 0, 63, 31, 0, 63, 31, 0, 63, 32, 0, 63, 32, 0, 63, 33, 0, 66 63, 33, 0, 63, 34, 0, 63, 34, 0, 63, 35, 0, 63, 35, 0, 63, 36, 0, 63, 36, 0, 63, 37, 0, 67 63, 38, 0, 63, 38, 0, 63, 39, 0, 63, 39, 0, 63, 40, 0, 63, 40, 0, 63, 41, 0, 63, 41, 0, 68 63, 42, 0, 63, 42, 0, 63, 43, 0, 63, 43, 0, 63, 44, 0, 63, 44, 0, 63, 45, 0, 63, 45, 0, 69 63, 46, 0, 63, 46, 0, 63, 47, 0, 63, 47, 0, 63, 48, 0, 63, 48, 0, 63, 49, 0, 63, 49, 0, 70 63, 50, 0, 63, 50, 0, 63, 51, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 71 63, 53, 0, 63, 53, 0, 63, 53, 0, 63, 53, 0, 63, 54, 0, 63, 54, 0, 63, 54, 0, 63, 54, 0, 72 63, 54, 0, 63, 55, 0, 63, 55, 0, 63, 55, 0, 63, 55, 0, 63, 56, 0, 63, 56, 0, 63, 56, 0, 73 63, 56, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 58, 0, 63, 58, 0, 74 63, 58, 0, 63, 58, 0, 63, 59, 0, 63, 59, 0, 63, 59, 0, 63, 59, 0, 63, 60, 0, 63, 60, 0, 75 63, 60, 0, 63, 60, 0, 63, 60, 0, 63, 61, 0, 63, 61, 0, 63, 61, 0, 63, 61, 0, 63, 62, 0, 76 63, 62, 0, 63, 62, 0, 63, 62, 0, 63, 63, 0, 63, 63, 1, 63, 63, 2, 63, 63, 3, 63, 63, 4, 77 63, 63, 5, 63, 63, 6, 63, 63, 7, 63, 63, 8, 63, 63, 9, 63, 63, 10, 63, 63, 10, 63, 63, 11, 78 63, 63, 12, 63, 63, 13, 63, 63, 14, 63, 63, 15, 63, 63, 16, 63, 63, 17, 63, 63, 18, 63, 63, 19, 79 63, 63, 20, 63, 63, 21, 63, 63, 21, 63, 63, 22, 63, 63, 23, 63, 63, 24, 63, 63, 25, 63, 63, 26, 80 63, 63, 27, 63, 63, 28, 63, 63, 29, 63, 63, 30, 63, 63, 31, 63, 63, 31, 63, 63, 32, 63, 63, 33, 81 63, 63, 34, 63, 63, 35, 63, 63, 36, 63, 63, 37, 63, 63, 38, 63, 63, 39, 63, 63, 40, 63, 63, 41, 82 63, 63, 42, 63, 63, 42, 63, 63, 43, 63, 63, 44, 63, 63, 45, 63, 63, 46, 63, 63, 47, 63, 63, 48, 83 63, 63, 49, 63, 63, 50, 63, 63, 51, 63, 63, 52, 63, 63, 52, 63, 63, 53, 63, 63, 54, 63, 63, 55, 84 63, 63, 56, 63, 63, 57, 63, 63, 58, 63, 63, 59, 63, 63, 60, 63, 63, 61, 63, 63, 62, 63, 63, 63}; 85 42 static unsigned char pixels[XSIZ * YSIZ]; 43 static unsigned char metaball[METASIZE * METASIZE]; 86 44 87 45 int main(int argc, char **argv) 88 46 { 89 int quit = 0; 90 float i=0, j=0, k=0; 91 int p; 92 unsigned int x1=0, yy1=0; 93 unsigned int x2=20, y2=70; 94 unsigned int x3=30, y3=20; 95 47 struct caca_bitmap *caca_bitmap; 48 float i = 0, j = 0, k = 0; 49 int p; 50 unsigned int x[5], y[5]; 51 int r[256], g[256], b[256], a[256]; 96 52 97 int r[256], g[256], b[256], a[256]; 98 if(caca_init()) 99 return 1; 100 101 caca_set_delay(40000); 102 103 /* Make the palette eatable by libcaca */ 104 for (p = 0; p < 256; p++) 53 if(caca_init()) 54 return 1; 55 56 caca_set_delay(0); 57 58 /* Make the palette eatable by libcaca */ 59 for(p = 0; p < 256; p++) 105 60 { 106 b[p] = pal[p * 3] * 64;107 g[p] = pal[p * 3 + 1] * 64;108 r[p] = pal[p * 3 + 2] * 64;109 a[p] = 0xfff;61 r[p] = p < 0xc0 ? 0 : (p - 0xc0) * 0x40; 62 g[p] = p < 0x40 ? 0 : p < 0xc0 ? (p - 0x40) * 0x20 : 0xfff; 63 b[p] = p < 0x40 ? p * 0x40 : 0xfff; 64 a[p] = 0x0; 110 65 } 111 112 /* Create the bitmap */113 caca_bitmap = caca_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);114 66 115 /* Set the palette*/116 caca_set_bitmap_palette(caca_bitmap, r, g, b, a);67 /* Create the bitmap */ 68 caca_bitmap = caca_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0); 117 69 118 /* Our virtual buffer */ 119 bitmap = malloc(XSIZ * YSIZ * sizeof(char)); 120 121 122 /* Generate ball sprite */ 123 generateBalls(); 70 /* Set the palette */ 71 caca_set_bitmap_palette(caca_bitmap, r, g, b, a); 124 72 73 /* Generate ball sprite */ 74 generate_ball(); 125 75 126 /* Go ! */127 while(!quit)76 /* Go ! */ 77 while(!caca_get_event(CACA_EVENT_KEY_PRESS)) 128 78 { 129 /* Get event and test it for a key pressure */ 130 int event = caca_get_event(CACA_EVENT_KEY_PRESS); 131 132 if((event & CACA_EVENT_KEY_PRESS)) 133 { 134 quit = 1; 135 } 79 /* Silly paths for our balls */ 80 x[0] = (1 + sin(i + k)) * (XSIZ-METASIZE) / 2; 81 y[0] = (1 + cos(1 + j)) * (YSIZ-METASIZE) / 2; 82 x[1] = (1 + cos(2 + j * 2 + k)) * (XSIZ-METASIZE) / 2; 83 y[1] = (1 + cos(3 + i / 2 + j)) * (YSIZ-METASIZE) / 2; 84 x[2] = (1 + cos(4 + k * 2)) * (XSIZ-METASIZE) / 2; 85 y[2] = (1 + cos(i + j / 2)) * (YSIZ-METASIZE) / 2; 86 x[3] = (1 + sin(6 + j * 2)) * (XSIZ-METASIZE) / 2; 87 y[3] = (1 + cos(7 + i / 2)) * (YSIZ-METASIZE) / 2; 88 x[4] = (1 + cos(i - k / 2)) * (XSIZ-METASIZE) / 2; 89 y[4] = (1 + sin(i + k / 2)) * (YSIZ-METASIZE) / 2; 136 90 137 /* Silly paths for our balls */138 x1 = abs(sin(i)*(XSIZ-METASIZE));139 yy1 = abs(cos(j)*(YSIZ-METASIZE));91 i += 0.011; 92 j += 0.021; 93 k += 0.029; 140 94 141 x2 = abs(cos(j*2)*(XSIZ-METASIZE)); 142 y2 = abs(cos(i/2)*(YSIZ-METASIZE)); 95 memset(pixels, 0, XSIZ * YSIZ); 143 96 144 x3 = abs(cos(k*2)*(XSIZ-METASIZE)); 145 y3 = abs(cos(i+j/2)*(YSIZ-METASIZE)); 97 /* Here is all the trick. Maybe if you're that 98 * clever you'll understand. */ 99 for(p = 0; p < 5; p++) 100 draw_ball(x[p], y[p]); 146 101 102 /* Draw our virtual buffer to screen, letting libcaca resize it */ 103 caca_draw_bitmap(-10, -10, caca_get_width() + 9, caca_get_height() + 9, 104 caca_bitmap, pixels); 105 caca_refresh(); 106 } 147 107 148 /* Here is all the trick. Maybe if you're that 149 clever you'll understand. 150 */ 151 drawBalls(x1,x2); 152 drawBalls(x2,y2); 153 drawBalls(x3,y3); 108 /* End, bye folks */ 109 caca_end(); 154 110 155 156 157 158 i+=0.01; 159 j+=0.02; 160 k+=0.03; 161 162 /* Draw our virtual buffer to screen, letting libcaca resize it */ 163 caca_draw_bitmap(0, 0, caca_get_width() , caca_get_height(), 164 caca_bitmap, bitmap); 165 caca_refresh(); 166 167 memset(bitmap, 0, XSIZ*YSIZ); 168 } 169 170 /* End, bye folks */ 171 caca_end(); 172 173 return 0; 174 111 return 0; 175 112 } 176 113 114 /* Generate ball sprite 115 * You should read the comments, I already wrote that before ... */ 116 static void generate_ball(void) 117 { 118 int x, y; 119 float distance; 177 120 178 /* You missed the trick ? */ 179 void drawBalls(unsigned int bx, unsigned int by) 180 { 181 unsigned int color; 182 unsigned int i, e=0; 183 unsigned int b = (by*XSIZ)+bx; 184 185 for(i=0;i<METASIZE*METASIZE;i++){ 186 color = bitmap[b] + MetaBoule[i]; 187 if(color>255) color = 255; 188 bitmap[b] = color; 189 if(e==METASIZE){e=0;b+=(XSIZ-(METASIZE));} 190 b++; 191 e++; 121 for(y = 0; y < METASIZE; y++) 122 for(x = 0; x < METASIZE; x++) 123 { 124 distance = ((METASIZE/2) - x) * ((METASIZE/2) - x) 125 + ((METASIZE/2) - y) * ((METASIZE/2) - y); 126 distance = sqrt(distance) * 64 / METASIZE; 127 metaball[x + y * METASIZE] = distance > 15 ? 0 : (255 - distance) * 15; 192 128 } 193 129 } 194 130 195 /* 'Limit' the color to 100, to have the cropped effect*/196 void cutBalls(unsigned int bx, unsigned int by)131 /* You missed the trick ? */ 132 static void draw_ball(unsigned int bx, unsigned int by) 197 133 { 198 unsigned int i, e=0; 199 unsigned int b = (by*XSIZ)+bx; 200 for(i=0;i<METASIZE*METASIZE;i++){ 201 if(bitmap[b]<100) bitmap[b] = 0; 202 if(e==METASIZE){e=0;b+=(XSIZ-(METASIZE));} 203 b++; 204 e++; 134 unsigned int color; 135 unsigned int i, e = 0; 136 unsigned int b = (by * XSIZ) + bx; 137 138 for( i = 0; i < METASIZE * METASIZE; i++) 139 { 140 color = pixels[b] + metaball[i]; 141 142 if(color > 255) 143 color = 255; 144 145 pixels[b] = color; 146 if(e == METASIZE) 147 { 148 e = 0; 149 b += XSIZ - METASIZE; 150 } 151 b++; 152 e++; 205 153 } 206 154 } 207 208 209 210 /* Generate ball sprite211 You should read the comments, I already wrote that before ...212 */213 void generateBalls(void)214 {215 int x,y,distance;216 for(y=0;y<METASIZE;y++)217 for(x=0;x<METASIZE;x++)218 {219 distance = sqrt( ((METACENTER-x)*(METACENTER-x)) + ((METACENTER-y)*(METACENTER-y)));220 MetaBoule[x+y*METASIZE]= (255-(distance))*15;221 if(distance > 15)222 MetaBoule[x+y*METASIZE] = 0;223 }224 }
Note: See TracChangeset
for help on using the changeset viewer.