(* ===================================================== *)
(*      Apprentissage de la programmation avec OCaml     *)
(*      Catherine Dubois & Valérie Ménissier-Morain      *)
(*                Éditions Hermès Sciences               *)
(*                        Mars 2004                      *)
(* ===================================================== *)
(* Fichier MLSRC/CC/CC_ameliorations/affichage_jeu.ml    *)
(* ===================================================== *)

open Outils;;
open Representation;;
open Circuits;;
open Graphics;;
open Affichage_base;;

let espace_pièces = 8;;

let largeur_zone_de_jeu () =
  état.zone_de_jeu.colonnes*taille_pièce+(état.zone_de_jeu.colonnes-1)*espace_pièces
and hauteur_zone_de_jeu () =
  état.zone_de_jeu.lignes*taille_pièce+(état.zone_de_jeu.lignes-1)*espace_pièces;;

let largeur_caractère = 11
and hauteur_caractère = 18;;

let largeur_cartouche =
  largeur_caractère*(String.length " PAS DE CIRCUIT ");;

let espace_horizontal () =
  (size_x()-largeur_cartouche-largeur_zone_de_jeu())/2;;

let espace_vertical () = (size_y()-hauteur_zone_de_jeu())/2;;

let dessiner_zone_de_jeu () =
  let x = ref (espace_horizontal ())
  and y = ref (espace_vertical ()) in
  (* où placer le coin en bas à gauche de la zone de jeu *)
   for c = état.zone_de_jeu.débutc to état.zone_de_jeu.finc do
     for
 l = état.zone_de_jeu.débutl to état.zone_de_jeu.finl do
       begin match
 état.zone_de_jeu.tableau.(c).(l) with
       | Vide -> ()
       | Occupée couleur -> dessiner_pièce (!x, !y) couleur
       end;
       y := !y+taille_pièce+espace_pièces;
    done;
    y := espace_vertical ();
    x := !x+taille_pièce+espace_pièces
  done;;

let écrire_à_gauche_cartouche haut s =
  set_color foreground;
  set_font "-*-courier-medium-r-*-*-18-*";
  let (w, h) = text_size s in
  moveto (size_x()-largeur_cartouche+largeur_caractère) (haut-h/2);
  draw_string s;;

let écrire_à_droite_cartouche haut s =
  set_font "-*-courier-medium-r-*-*-18-*";
  let (w, h) = text_size s in
  set_color background;
  fill_rect (size_x()-largeur_caractère-w) (haut-h/2) w h;
  moveto (size_x()-largeur_caractère-w) (haut-h/2);
  set_color foreground;
  draw_string s;;

let écrire_centré_fenêtre haut s =
  let (w,h)= text_size s in
  moveto ((size_x()-w)/2) (haut-h/2);
  draw_string s;;

let écrire_touche c descr =
  set_font "-*-courier-bold-r-*-*-14-*";
  draw_char '('; draw_char (Char.uppercase c); draw_char ')';
  rmoveto 2 0;
  set_font "-*-courier-medium-r-*-*-14-*";
  draw_string descr;;

let écrire_description_touches () =
  let g = (size_x()-largeur_cartouche+largeur_caractère) in
  moveto g (size_y()*18/20);
  écrire_touche 'b' "acktrack";
  moveto g (size_y()*17/20);
  set_font "-*-helvetica-bold-o-*-*-14-*";
  écrire_touche 'c' "ouleur";
  moveto g (size_y()*16/20);
  écrire_touche 'h' "ighscores";
  moveto g (size_y()*15/20);
  écrire_touche 'm' "uet";
  moveto g (size_y()*14/20);
  écrire_touche 'n' "ouveau jeu";
  moveto g (size_y()*13/20);
  écrire_touche 'p' "ause";
  moveto g (size_y()*12/20);
  écrire_touche 'q' "uitter";
  moveto g (size_y()*11/20);
  écrire_touche 's' "uggestion";
  moveto g (size_y()*10/20);
  écrire_touche 't' "ouches";;

let effacer_description_touches () =
  let g = (size_x()-largeur_cartouche+largeur_caractère) in
  set_color background;
  fill_rect g (size_y()*10/20) largeur_cartouche (size_y()*9/20);
  set_color foreground;;

let dessiner_cartouche () =
  let gauche_cartouche =
    size_x() - largeur_cartouche+largeur_caractère in
  set_color foreground;
  set_font "-*-courier-medium-r-*-*-18-*";
  moveto (size_x() - largeur_cartouche) 0;
  rlineto 0 (size_y());
  écrire_à_gauche_cartouche (size_y()*4/20) "NIVEAU";
  écrire_à_droite_cartouche (size_y()*4/20) (string_of_int état.niveau);
  écrire_à_gauche_cartouche (size_y()*6/20) "SCORE";
  écrire_à_droite_cartouche (size_y()*6/20) (string_of_int état.score);
  if état.touches
  then écrire_description_touches ()
  else effacer_description_touches ();;

let affichage_score () =
  écrire_à_droite_cartouche (size_y()*6/20) (string_of_int état.score);;

let coordonnées_centre_pièce (c,l) =
  (espace_horizontal ()+(c-1)*taille_pièce+
   (c-1)*espace_pièces+taille_pièce/2,
   espace_vertical ()+(l-1)*taille_pièce+
   (l-1)*espace_pièces+taille_pièce/2);;

let dessiner_circuit circuit =
  match List.map coordonnées_centre_pièce circuit with
  | [] -> ()
  | (x,y)::circuit' ->
    set_line_width 3; moveto x y;
    List.iter (function (x,y) -> lineto x y) circuit';
    set_line_width 1;;

let coordonnées_pièce (c,l) =
  (espace_horizontal ()+(c-1)*taille_pièce+(c-1)*espace_pièces,
   espace_vertical ()+(l-1)*taille_pièce+(l-1)*espace_pièces);;

let effacer_pièce_sélectionnée (c,l) =
  let (x, y) = coordonnées_pièce (c,l) in
  set_color background;
  fill_rect x y taille_pièce taille_pièce;
  dessiner_sélection (x, y);
  set_color foreground;;

let effacer_circuit circuit =
  set_color background;
  dessiner_circuit circuit;
  set_color foreground;;

let écrire_pas_de_circuit () =
  let gauche_cartouche =
    size_x() - largeur_cartouche+largeur_caractère in
  let
 affiche couleur =
    set_color couleur;
    set_font "-*-courier-bold-r-*-*-18-*";
    moveto gauche_cartouche (size_y()*8/20-hauteur_caractère/2);
    draw_string "PAS DE CIRCUIT" in
  affiche foreground; sleep 0.1;
  affiche background; sleep 0.1;
  affiche foreground; sleep 0.1;
  affiche background; sleep 0.1;
  affiche foreground; sleep 0.1;
  affiche background;;

Ce document a été traduit de LATEX par HEVEA.