/* Kääntäminen: gcc -o metaballs metaballs.c -O3 -ggdb -lSDL -lm */ #include #include #include #define RES_X 800 #define RES_Y 800 typedef struct metaball { int x, y; unsigned char r, g, b; float vx, vy; float fx, fy; float mass; } metaball; typedef union { struct {unsigned char b, g, r;} rgb; Uint32 sdl; } unioncolor; metaball pallot[3] = { {0,0,255,0,0,0.0,-8.0,100.0,400.0,5.0}, {0,0,0,255,0,0.0,8.0,600.0,400.0,1.0}, {400,400,0,0,255,0,0,400.0,400.0,400.0} }; const int pallot_count = 3; inline Uint32 rgb(int r, int g, int b){ return (r << 16) | (g << 8) | b; } inline void putpixel(SDL_Surface *screen, int x, int y, Uint32 color){ Uint8 *p = (Uint8 *)screen->pixels; *(Uint32 *)(p + y * screen->pitch + (x << 2)) = color; } void draw(SDL_Surface *screen) { int i, x, y, dx, dy; long factor; long total; long r, g, b; unioncolor *pixel; for (x=0; x 10000) factor = 10000; if (factor < 10) continue; r += (pallot[i].r * factor) >> 10; g += (pallot[i].g * factor) >> 10; b += (pallot[i].b * factor) >> 10; total += factor; } // if (total > 5000 && total < 5500) // total = 0; r = (total * r) >> 10; g = (total * g) >> 10; b = (total * b) >> 10; if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255; pixel = (Uint8 *)screen->pixels + y * screen->pitch + (x << 2); pixel->rgb.r = r; pixel->rgb.g = g; pixel->rgb.b = b; } } SDL_Flip(screen); } void reposition() { int i, j; float fx, fy, ft; int dx, dy; float fdist, vx, vy; for (i=0; i 0) SDL_Delay(ticks); } return 0; }