-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPausableTimer.ml
More file actions
48 lines (39 loc) · 1.08 KB
/
Copy pathPausableTimer.ml
File metadata and controls
48 lines (39 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(* PausableTimer - A pausable timer for use with OCamlSDL2
Copyright (C) 2020 Florent Monnier
This software is provided "AS-IS", without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely.
*)
type t = {
mutable t1: int;
mutable time: int;
mutable paused: bool
}
let create () = {
t1 = 0;
time = 0;
paused = false;
}
let is_paused timer =
timer.paused
let get_ticks timer =
if timer.paused
then timer.t1 - timer.time
else (Sdltimer.get_ticks ()) - timer.time
let pause timer =
if not timer.paused then begin
timer.t1 <- Sdltimer.get_ticks ();
timer.paused <- true;
end
let restart timer =
if timer.paused then begin
let t2 = Sdltimer.get_ticks () in
timer.time <- timer.time + (t2 - timer.t1);
timer.paused <- false;
end
let toggle_pause timer =
if not timer.paused
then pause timer
else restart timer