| 1 |
(***********************************************************************) |
| 2 |
(* *) |
| 3 |
(* Objective Caml *) |
| 4 |
(* *) |
| 5 |
(* Jun Furuse, projet Cristal, INRIA Rocquencourt *) |
| 6 |
(* *) |
| 7 |
(* Copyright 1999-2004, *) |
| 8 |
(* Institut National de Recherche en Informatique et en Automatique. *) |
| 9 |
(* Distributed only by permission. *) |
| 10 |
(* *) |
| 11 |
(***********************************************************************) |
| 12 |
|
| 13 |
(* $Id: gifanim.ml,v 1.12.2.1 2010/05/13 13:36:09 furuse Exp $ *) |
| 14 |
|
| 15 |
open Images |
| 16 |
open Ximage |
| 17 |
|
| 18 |
open Guienv |
| 19 |
|
| 20 |
open Gdk |
| 21 |
open GMain |
| 22 |
|
| 23 |
open Gif |
| 24 |
|
| 25 |
let () = |
| 26 |
let file = ref "" in |
| 27 |
|
| 28 |
Arg.parse [] (fun s -> file := s) "gifanim file"; |
| 29 |
|
| 30 |
let gifseq = Gif.load !file [] in |
| 31 |
|
| 32 |
let darea = |
| 33 |
(* new GMisc.drawing_area :width :height show: true packing: window#add *) |
| 34 |
GMisc.drawing_area ~show: true ~packing: window#add |
| 35 |
~width: gifseq.screen_width ~height: gifseq.screen_height () in |
| 36 |
|
| 37 |
window#show (); |
| 38 |
let drawing = darea#misc#window in |
| 39 |
prerr_endline "GUI done"; |
| 40 |
|
| 41 |
let frames = |
| 42 |
Array.of_list (List.map (fun frame -> |
| 43 |
frame, Ximage.of_image visual None (Index8 frame.frame_bitmap), |
| 44 |
Ximage.mask_of_image window#misc#window |
| 45 |
(Index8 frame.frame_bitmap) ) gifseq.frames) in |
| 46 |
prerr_endline "Image load done"; |
| 47 |
|
| 48 |
let len = Array.length frames in |
| 49 |
|
| 50 |
let gc = GC.create drawing in |
| 51 |
let rect = Rectangle.create ~x:0 ~y:0 ~width: gifseq.screen_width |
| 52 |
~height: gifseq.screen_height in |
| 53 |
|
| 54 |
let pos = ref 0 in |
| 55 |
|
| 56 |
let rec disp_frame () = |
| 57 |
let frame, image, mask = frames.(!pos) in |
| 58 |
begin match mask with |
| 59 |
| Some bmp -> |
| 60 |
Gdk.GC.set_clip_origin gc ~x: frame.frame_left ~y: frame.frame_top; |
| 61 |
Gdk.GC.set_clip_mask gc bmp |
| 62 |
| None -> |
| 63 |
Gdk.GC.set_clip_origin gc ~x: 0 ~y: 0; |
| 64 |
Gdk.GC.set_clip_rectangle gc rect |
| 65 |
end; |
| 66 |
Gdk.Draw.image drawing gc image.data ~xsrc: 0 ~ysrc:0 |
| 67 |
~xdest: frame.frame_left ~ydest: frame.frame_top |
| 68 |
~width: frame.frame_bitmap.Index8.width |
| 69 |
~height: frame.frame_bitmap.Index8.height; |
| 70 |
Gdk.X.flush (); |
| 71 |
if len = 1 then false |
| 72 |
else begin |
| 73 |
incr pos; |
| 74 |
if !pos = len then pos := 0; |
| 75 |
ignore (Timeout.add ~ms:(frame.frame_delay*10) ~callback: disp_frame); |
| 76 |
false |
| 77 |
end in |
| 78 |
|
| 79 |
prerr_endline "first call"; |
| 80 |
|
| 81 |
let id = ref None in |
| 82 |
id := Some (window#event#connect#configure ~callback: (fun _ev -> |
| 83 |
begin match !id with |
| 84 |
| Some id -> window#misc#disconnect id |
| 85 |
| None -> () |
| 86 |
end; |
| 87 |
disp_frame ())); |
| 88 |
|
| 89 |
prerr_endline "Entered main loop"; |
| 90 |
|
| 91 |
Main.main () |
| 92 |
|