

gfx_init("space",640,480);

function enemylist.init(buf, rows, cols, maxwid) global()
(
  this.buf = buf;
  this.rows=rows;
  this.cols=cols;
  this.maxwid = maxwid;
  loop(rows*cols, buf[]=1; buf += 1; );
);


function enemylist.draw(pos, vpos, destextent*) 
                  local(rad,buf,xpos,ypos,dxpos) 
                  global(gfx_w,gfx_h,gfx_r,gfx_g,gfx_b,gfx_a)
(
  gfx_r=gfx_a=1;
  gfx_g=gfx_b=0;
  dxpos = this.maxwid/this.cols*gfx_w;
  buf=this.buf;
  rad = dxpos*0.3;
  destextent.right = 0;
  destextent.bottom = 0;
  destextent.left=2^20;
  destextent.top=2^20;

  ypos=dxpos*0.5 + gfx_h*vpos;
  loop(this.rows,
    xpos=pos*gfx_w + dxpos*0.5;
    loop(this.cols,
      buf[] ? (
        destextent.left = min(destextent.left,xpos-rad);
        destextent.right = max(destextent.right,xpos+rad);
        destextent.top = min(destextent.top,ypos-rad);
        destextent.bottom = max(destextent.bottom,ypos+rad);
        gfx_circle(xpos,ypos,rad);
      );
      xpos += dxpos;
      buf += 1;
    );
    ypos += dxpos;
  );
);


enemylist.init(0, 4, 8, 0.8);


last_time = time_precise();
enemy_pos = 0;
enemy_vpos = 0;
enemy_vel = 0.05;
while ((curchar = gfx_getchar()) != 27)
(
  now = time_precise();
  enemylist.draw(enemy_pos, enemy_vpos, area);
  enemy_vel > 0 ? (
   area.right >= gfx_w ? ( enemy_vel = -enemy_vel; enemy_vpos += abs(enemy_vel); enemy_vel *= 1.3; );
  ) : (
   area.left <= 0 ? ( enemy_vel = -enemy_vel; enemy_vpos += abs(enemy_vel); enemy_vel *= 1.3; );
  );

  enemy_pos += (now-last_time)*enemy_vel;
  
  


  last_time = now;
  sleep(33);
  gfx_update();
);
