From d6d18de137d8455456a3ea53beacfc2d4b04e058 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= Date: Fri, 13 Dec 2024 00:51:04 +0100 Subject: [PATCH] =?utf8?q?Interface=20graphique=20tr=C3=A8s=20basique?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- bin/main.ml | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 350e3d0..b0e5404 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -13,9 +13,44 @@ * with this program. If not, see . *) +let render (type t) (m : (module Automata.Automaton with type t = t)) (board : t Automata.board) = + let module M = (val m) in + for x = 0 to 63 do + for y = 0 to 63 do + Graphics.set_color + (let r, g, b = M.color (Automata.get x y board) in + Graphics.rgb (int_of_char r) (int_of_char g) (int_of_char b)); + Graphics.fill_rect (x * 16) (y * 16) 16 16; + Graphics.set_color Graphics.black; + Graphics.draw_rect (x * 16) (y * 16) 16 16 + done + done + +let inputs (type t) (m : (module Automata.Automaton with type t = t)) (board : t Automata.board) = + let module M = (val m) in + if Graphics.button_down () + then ( + let mx, my = Graphics.mouse_pos () in + let x = mx / 16 + and y = my / 16 in + while Graphics.button_down () do + () + done; + Automata.set x y (M.next (Automata.get x y board)) board) + +let run (type t) (m : (module Automata.Automaton with type t = t)) = + let rec aux board = + render m board; + Graphics.synchronize (); + inputs m board; + aux (if Graphics.key_pressed () && Graphics.read_key () = '\t' then Automata.update m board else board) + in + aux (Automata.initial m) + let () = - List.iter - (fun m -> - let module Automaton = (val m : Automata.Automaton) in - Printf.printf "%s\n" Automaton.name) - Automata.automata + Graphics.open_graph ""; + Graphics.set_window_title "Automaton"; + Graphics.auto_synchronize false; + let m = List.nth Automata.automata 0 in + let module M = (val m) in + run (module M) -- 2.46.0