#include #include #include #include void create_pipes (BITMAP *pipes[16], int size) { for (int i = 0; i < 16; ++i) { pipes[i] = create_video_bitmap(size,size); clear_to_color (pipes[i], pipes[i]->vtable->mask_color); int max_step = size/8; for (int step = 0; step < max_step; ++step) { float h = 1 - (max_step-step)*(max_step-step)/(float)(max_step*max_step); int col = makecol(0,(int)(128+127*h)*size/64,(int)(128+127*h)*size/64); if (i&1) rectfill (pipes[i], size/2 - max_step + step, size/2 - max_step + step, size-1, size/2 + max_step - step, col); if (i&2) rectfill (pipes[i], 0, size/2 - max_step + step, size/2 + max_step - step, size/2 + max_step - step, col); if (i&4) rectfill (pipes[i], size/2 - max_step + step, size/2 - max_step + step, size/2 + max_step - step, size-1, col); if (i&8) rectfill (pipes[i], size/2 - max_step + step, 0, size/2 + max_step - step, size/2 + max_step - step, col); } } } volatile int fps = 0; volatile int frames_this_second = 0; void fps_timer(void) { fps = frames_this_second; frames_this_second = 0; } int main(void) { TRACE("1\n"); allegro_init(); TRACE("2\n"); install_timer(); TRACE("3\n"); install_joystick(JOY_TYPE_AUTODETECT); TRACE("4\n"); set_color_depth(8); TRACE("5\n"); set_gfx_mode (GFX_AUTODETECT, 320, 240, 320, 1024); TRACE("6\n"); PALETTE pal; memcpy (pal, default_palette, sizeof pal); for (int i = 0; i < 63; ++i) { pal[i+16].r = 0; pal[i+16].g = i; pal[i+16].b = i; } set_palette(pal); TRACE("7\n"); BITMAP *screen_page[2]; screen_page[0] = create_video_bitmap (SCREEN_W, SCREEN_H); screen_page[1] = create_video_bitmap (SCREEN_W, SCREEN_H); BITMAP *active_page = screen_page[0]; clear_to_color(active_page, makecol(0,0,0)); show_video_bitmap(active_page); TRACE("8\n"); install_int (fps_timer, 1000); enum { MAX_LAYERS=8 }; BITMAP *pipes[MAX_LAYERS][16]; int size = 64; for (int i = 0; i < MAX_LAYERS; ++i) { create_pipes(pipes[i], size); size = (size * 3) / 4; } enum { MAP_W = 32, MAP_H = 32, JOIN_PERCENT = 30, MOVE_RATE = 16 }; char pipemap[MAX_LAYERS][MAP_W][MAP_H]; for (int i = 0; i < MAX_LAYERS; ++i) { memset(pipemap[i],0,sizeof pipemap[i]); int num_joins = MAP_W*MAP_H*2*JOIN_PERCENT/100; for (int j = 0; j < num_joins; ++j) { bool done = false; do { int x = rand()%MAP_W; int y = rand()%MAP_H; int dir = 1 << ((rand()>>4)&2); if ((pipemap[i][x][y] & dir) == 0) { pipemap[i][x][y] |= dir; if (dir&1) x = (x+1)%MAP_W; else if (dir&4) y = (y+1)%MAP_H; pipemap[i][x][y] |= dir<<1; done = true; } } while (!done); } } int offset_x = 0; int offset_y = 0; do { poll_joystick(); if (joy[0].stick[0].axis[0].d1) --offset_x; if (joy[0].stick[0].axis[0].d2) ++offset_x; if (joy[0].stick[0].axis[1].d1) --offset_y; if (joy[0].stick[0].axis[1].d2) ++offset_y; while (offset_x < 0) offset_x += MAP_W*MOVE_RATE; while (offset_y < 0) offset_y += MAP_H*MOVE_RATE; while (offset_x >= MAP_W*MOVE_RATE) offset_x -= MAP_W*MOVE_RATE; while (offset_y >= MAP_H*MOVE_RATE) offset_y -= MAP_H*MOVE_RATE; int dx = offset_x / MOVE_RATE; int dy = offset_y / MOVE_RATE; int remx = offset_x - dx*MOVE_RATE; int remy = offset_y - dy*MOVE_RATE; clear_to_color (active_page, makecol(0,0,0)); textprintf_ex (active_page, font, 0, 0, makecol(255,255,255), -1, "%d fps %d/%d,%d/%d", fps, dx, remx, dy, remy); set_clip_rect (active_page, 0, 10, active_page->w-1, active_page->h-1); for (int layer = MAX_LAYERS-1; layer >= 0; --layer) { int size = pipes[layer][0]->w; int disp_w = (SCREEN_W+size-1)/size + 1; int disp_h = (SCREEN_H-10+size-1)/size + 1; for (int x = 0; x < disp_w; ++x) { for (int y = 0; y < disp_h; ++y) { int pipe = pipemap[layer][(x+dx)%MAP_W][(y+dy)%MAP_H]; draw_sprite (active_page, pipes[layer][pipe], x*size - (remx*size)/MOVE_RATE, 10 + y*size - (remy*size)/MOVE_RATE); } } } set_clip_rect (active_page, 0, 0, active_page->w-1, active_page->h-1); ++frames_this_second; show_video_bitmap(active_page); if (active_page == screen_page[0]) active_page = screen_page[1]; else active_page = screen_page[0]; } while (!joy[0].button[1].b); return 0; } END_OF_MAIN()