/* sines.c Author Jani Kniivilä Compile with: gcc -o sines sines.c -lSDL -lpthread -ltSGGL -lm */ #include #include #include #include #include SDL_Surface *surface; gfx_image_t *output; gfx_image_t *background; #define WIDTH 640 #define HEIGHT 480 #define SINE_HEIGHT 60 gfx_vline_t *vlines[3]; gfx_color_t *red, *green, *blue; const float my_pi = 3.141593; int fx_init() { int i, x, y; 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" */ vlines[0] = (gfx_vline_t *) calloc(output->width, sizeof(gfx_vline_t)); if (!vlines[0]) return -4; vlines[1] = (gfx_vline_t *) calloc(output->width, sizeof(gfx_vline_t)); if (!vlines[1]) return -5; vlines[2] = (gfx_vline_t *) calloc(output->width, sizeof(gfx_vline_t)); if (!vlines[2]) return -6; red = gfx_alloc_color(192, 32, 32); green = gfx_alloc_color(32, 192, 32); blue = gfx_alloc_color(32, 32, 192); for (i = 0; i < output->width; i++) { vlines[0][i].x = i; vlines[1][i].x = i; vlines[2][i].x = i; vlines[0][i].color = red; vlines[1][i].color = green; vlines[2][i].color = blue; } background = gfx_create_empty_image(WIDTH, HEIGHT); if (!background) return -10; gfx_set_color(&color, 8, 8, 8); for (i = 0; i < 10000; i++) { x = -10 + 5 * (rand() % ((output->width + 20)/5)); y = -10 + 5 * (rand() % ((output->height + 20)/5)); gfx_slow_addrect(x, y, x+29, y+29, &color, background); } return 0; } void fx_shutdown() { free(vlines[0]); free(vlines[1]); free(vlines[2]); gfx_dispose_color(red); gfx_dispose_color(green); gfx_dispose_color(blue); gfx_dispose_image(background); free(output); } int fx_draw_frame() { static float angle = 0; int i; for (i = 0; i < output->width; i++) { vlines[0][i].y1 = 200 + sin(angle + i*my_pi/233.0) * (75 + cos(angle + i*my_pi/131.3) * 150); vlines[0][i].y2 = vlines[0][i].y1 + SINE_HEIGHT-1; vlines[1][i].y1 = 250 + sin(angle + i*my_pi/129.0) * (75 + cos(angle + i*my_pi/300.3) * 150); vlines[1][i].y2 = vlines[1][i].y1 + SINE_HEIGHT-1; vlines[2][i].y1 = 300 + sin(angle + i*my_pi/500.1) * (75 + cos(angle + i*my_pi/400.3) * 150); vlines[2][i].y2 = vlines[2][i].y1 + SINE_HEIGHT-1; } if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface); gfx_copy(output, background); gfx_slow_addvlines(vlines[0], output->width, output); gfx_slow_addvlines(vlines[1], output->width, output); gfx_slow_addvlines(vlines[2], output->width, output); if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); SDL_Flip(surface); angle += my_pi / 45.0; return 0; } int main() { SDL_Event event; int i, done = 0; i = fx_init(); if (i) return i; 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; }