#!/usr/bin/env ocaml
#load "unix.cma"
#load "ming.cma"
open Ming ;;

let _ =
  let mlen = 144.0 in            (* Size of movie  *)
  let slen = mlen *. 2.0/.3.0 in (* Size of sprite *)
  setScale 20.0;

  (* Draw the shape *)
  let s = SWFShape.new' () in
  let fill = SWFShape.addSolidFill s 0x00 0xff 0x80 0xff in
  SWFShape.setRightFill ~shape:s ~fill;
  SWFShape.movePenTo s (-.slen/.2.0) (-.slen/.2.0);
  SWFShape.drawLine s slen 0.0;
  SWFShape.drawLine s 0.0 slen;
  SWFShape.drawLine s (-.slen) 0.0;
  SWFShape.drawLine s 0.0 (-.slen);

  (* Create the sprite *)
  let p = SWFMovieClip.new' () in
  let i = SWFMovieClip.add p s in

  for j=0 to 23 do
    let jf = (float) j in
    SWFDisplayItem.multColor i  1. 1. 1. (jf/.24.0);
    SWFDisplayItem.rotateTo i (-.360./.4./.24.*.jf);
    SWFMovieClip.nextFrame p;
  done;
  for j=0 to 23 do
    let jf = (float) j in
    SWFDisplayItem.multColor i  1. 1. 1. ((24.-.jf)/.24.0);
    SWFDisplayItem.rotateTo i (-.360./.4./.24.*.jf);
    SWFMovieClip.nextFrame p;
  done;

  (* Make the movie *)
  let m = SWFMovie.new' () in
  let i' = SWFMovie.add m (SWFMovie.SWFMovieClip p) in

  (* The next commented line produces a Segfaults *)
  (* SWFDisplayItem.moveTo i' (mlen/.2.) (mlen/.2.); *)
  SWFMovie.setBackground m  0xff 0xff 0xff;
  SWFMovie.setDimension m  mlen mlen;

  let filesize = SWFMovie.save m "blink_ml.swf" in
  Printf.printf " .swf size = %d\n" filesize;

  (*
  let cmd = "mozilla-firefox `pwd`/blink_ml.swf" in
  let ret = Unix.system cmd in ();
  *)