Reputation: 897
I'm making a sudoku game with in OCaml with the Graphics library.
I'm trying to add a timer at the top so that the player can see how long he's taking to solve the sudoku. I've looked around and the Lwt library seems to be what I'm looking for.
To draw the timer I wrote a function draw_time, the content of the function is too long and not important, but it has this kind of structure:
let rec hello () =
Lwt.bind (Lwt_unix.sleep 14400.)
(fun () -> print_endline "Hello, world !"; hello ())
Lwt.async (hello)
I've tested it and the function does work as it's supposed to.
The main loop of my game looks like this:
let main_loop gs f_init f_end f_key f_mouse =
f_init;
let timer = draw_time () in
try
while true do
try
if gs.completed <> true then
let event = Graphics.wait_next_event event_types in
if event.Graphics.keypressed then
f_key event.Graphics.key
else if event.Graphics.button then
f_mouse event.Graphics.mouse_x event.Graphics.mouse_y
with
| _ -> raise End
done
with
| End -> f_end ()
;;
This doesn't seem to work. The program only executes draw_time when I close the window (and the program) and not when the window is open and the timer is supposed to be drawn.
What am I doing wrong exactly?
Upvotes: 0
Views: 340
Reputation: 35270
In order to run Lwt program, you need to start the main loop, that will process your threads,
let () = Lwt_main.run (Lwt_io.printl "Hello, world!")
Lwt has code that will integrate Lwt main loop into the Glib. But, as far as I know, there is no such integration for Graphics module.
You may find async_graphics interesting, it is an integration of the Async library, that is quite near to Lwt with Graphics module. It was even mentioned in Real World OCaml
Upvotes: 2