Commits

vfiack  committed cce9c5b

sfx in a submodule, load only once

  • Participants
  • Parent commits 0f6fa47

Comments (0)

Files changed (1)

File maze/sdlmaze.ml

 exception Escape
 
 (* sounds *)
-
-let sfx_play sfx = Sdlmixer.play_channel ~channel:0 sfx
-
-let sfx_step () =
-  let sfx = Sdlmixer.loadWAV "data/sfx/step.wav" in
-  sfx_play sfx
-
-let sfx_error () =
-  let sfx = Sdlmixer.loadWAV "data/sfx/failure.wav" in
-  sfx_play sfx
-
-let sfx_success () =
-  let sfx = Sdlmixer.loadWAV "data/sfx/success.wav" in
-  sfx_play sfx
+module Sfx = struct
+  open Sdlmixer
+  
+  let step = ref None
+  let error = ref None
+  let success = ref None
+  
+  let load () =
+    step := Some (loadWAV "data/sfx/step.wav");
+    error := Some (loadWAV "data/sfx/failure.wav");
+    success := Some (loadWAV "data/sfx/success.wav")
+  
+  let play sfxref = match !sfxref with
+    | None -> ()
+    | Some sound -> Sdlmixer.play_channel ~channel:0 sound
+end
 
 (* images *)
 
   in
   if next = current then interact screen maze color goal start current
   else if not (Maze.check maze next) || Maze.get maze next = '#' then begin
-    sfx_error ();
+    Sfx.play Sfx.error;
     interact screen maze color goal start current
   end
   else begin
     draw_bullet screen maze color current;
-    if next = goal then (sfx_success(); raise Break)
-    else (sfx_step(); interact screen maze color goal start next)
+    if next = goal then (Sfx.play Sfx.success; raise Break)
+    else (Sfx.play Sfx.step; interact screen maze color goal start next)
   end
 
 (* main *)
   Arg.parse args ignore "usage";
   Random.self_init ();
   let screen = init_sdl !width !height in
+  Sfx.load ();
   mainloop screen !generator !width !height