#include #include #include #include static ALLEGRO_COLOR aa_color(ALLEGRO_COLOR c, ALLEGRO_COLOR bg, float percent) { ALLEGRO_COLOR ret; ret = al_map_rgba( (int)(c.r*(1-percent)*255),// + (int)(bg.r*(1-percent)*255), (int)(c.g*(1-percent)*255),// + (int)(bg.g*(1-percent)*255), (int)(c.b*(1-percent)*255),// + (int)(bg.b*(1-percent)*255), (int)(c.a*255) ); return ret; } static void m_put_aa_pixel(float x, float y, ALLEGRO_COLOR color) { ALLEGRO_BITMAP *target = al_get_target_bitmap(); float frac1 = x - (int)x; float frac2 = y - (int)y; if (frac1 != 0.0f && frac2 != 0.0f) { // Touching 4 pixels float p1 = (1 - frac1) * (1 - frac2); float p2 = frac1 * (1 - frac2); float p3 = frac2 * (1 - frac1); float p4 = frac2 * frac1; int x1 = (int)x; int x2 = (int)x + 1; int y1 = (int)y; int y2 = (int)y + 1; al_draw_pixel(x1, y1, aa_color(color, al_get_pixel(target, x1, y1), p1)); al_draw_pixel(x2, y1, aa_color(color, al_get_pixel(target, x2, y1), p2)); al_draw_pixel(x1, y2, aa_color(color, al_get_pixel(target, x1, y2), p3)); al_draw_pixel(x2, y2, aa_color(color, al_get_pixel(target, x2, y2), p4)); } else if (frac1 != 0.0f) { // Touching two pixels, horizontal float p1 = (1 - frac1); float p2 = frac1; int x1 = (int)x; int x2 = (int)x + 1; al_draw_pixel(x1, y, aa_color(color, al_get_pixel(target, x1, y), p1)); al_draw_pixel(x2, y, aa_color(color, al_get_pixel(target, x2, y), p2)); } else if (frac2 != 0.0f) { // Touching two pixel, vertical float p1 = (1 - frac2); float p2 = frac2; int y1 = (int)y; int y2 = (int)y + 1; al_draw_pixel(x, y1, aa_color(color, al_get_pixel(target, x, y1), p1)); al_draw_pixel(x, y2, aa_color(color, al_get_pixel(target, x, y2), p2)); } else { // Touching one pixel al_draw_pixel(x, y, color); } } float interpolate(float f1, float f2, float p) { return (f1 * (1 - p)) + (f2 * p); } void draw_bolt_1(float a) { int radius = 5; int dest_x = 120; int dest_y = 110; float px = dest_x, py = dest_y; float dx = cos(a); float dy = sin(a); float lx = cos(a+M_PI/2); float ly = -sin(a+M_PI/2); int i = 0; float step = 1.0f / radius; while (py > 0 && px > 0 && px < 240) { for (float f = 0; f < 1.0f; f += step) { ALLEGRO_COLOR color; if (f < 0.5f) { color = al_map_rgba(interpolate(255, 0, f*2), interpolate(255, 0, f), 255, 255); } else { color = al_map_rgba(0, 0, 255, 255*(1-(f-0.5f)*2)); } float a = lx * radius * f; float b = ly * radius * f; m_put_aa_pixel(px+a, py+b, color); m_put_aa_pixel(px-a, py-b, color); } px += (rand() % 5 - 2) + dx; py -= dy; } } int main(void) { al_init(); al_iio_init(); ALLEGRO_DISPLAY *display = al_create_display(720, 480); const int FRAMES = 16; ALLEGRO_BITMAP *b = al_create_bitmap(240, 110); for (int i = 0; i < FRAMES; i++) { float diff = M_PI/2; float a = (M_PI/2+M_PI/4) - ((float)i/FRAMES*diff); al_set_target_bitmap(b); al_clear(al_map_rgba(0, 100, 0, 255)); draw_bolt_1(a); char name[100]; sprintf(name, "Bolt2_%d.png", i); al_iio_save(name, b); printf("i=%d done\n", i); } return 0; } END_OF_MAIN()