/* plasma.c - Dotted Plasma Author Jani Kniivilä compile with: gcc -o plasma plasma.c -lSDL -lpthread -ltSGGL */ #include #include #include #include #define WIDTH 640 #define HEIGHT 480 SDL_Surface *surface; gfx_image_t *output; gfx_image_t *dot; const float my_pi = 3.141593; int fx_init() { gfx_color_t color; if (SDL_Init(SDL_INIT_VIDEO) < 0) return -1; atexit(SDL_Quit); surface = SDL_SetVideoMode(WIDTH, HEIGHT, 16, SDL_SWSURFACE); if (!surface) return -2; output = gfx_create_null_image(WIDTH, HEIGHT); /* create an image without pixel-data */ if (!output) return -3; output->data = (Uint16 *) surface->pixels; /* redirect the pixels to "screen" */ /* * Generate "dot" */ dot = gfx_create_image(63, 63); if (!dot) return -1; gfx_clear(dot); gfx_set_color(&color, 75, 75, 192); gfx_fcircle(31, 31, 30, &color, dot); gfx_set_color(&color, 92, 92, 224); gfx_fcircle(31, 31, 23, &color, dot); return 0; } void fx_shutdown() { gfx_dispose_image(dot); free(output); } int fx_draw_frame() { float dH, dV; static float angle = 0; int xx, yy, size; if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface); gfx_clear(output); dV = cos(angle/2.5)*(my_pi/2.0+sin(angle/5.0)*my_pi/2.0)+my_pi; for (yy = 0; yy < 15; yy++) { dH = sin(angle/2.5) * cos(angle/2) * my_pi + my_pi; for (xx = 0; xx < 20; xx++) { size = 16+(int)(cos(dH + xx*my_pi/20.0)*sin(dV + yy*my_pi/14.0)*15); gfx_put_scaled_image( 16 + xx*32 - (size>>1), 16 + yy*32 - (size>>1), dot, size, size, output); } } if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); SDL_Flip(surface); angle += my_pi/40.0; return 0; } int main() { SDL_Event event; int done = 0; if (fx_init()) return -1; while (!done) { fx_draw_frame(); while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: case SDL_KEYDOWN: case SDL_MOUSEBUTTONDOWN: done = 1; } } } fx_shutdown(); return 0; }