-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ml
executable file
·51 lines (43 loc) · 1.64 KB
/
main.ml
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
49
50
51
open Types
(* js_of_ocaml helper declarations *)
module Html = Dom_html
let js = Js.string
let document = Html.document
(************************ DOM HELPERS ************************)
(* [fail] is a failure/exception handler *)
let fail = fun _ -> assert false
(* [get_element_by_id id] gets a DOM element by its id *)
let get_element_by_id id =
Js.Opt.get (Html.document##getElementById (js id)) fail
(* [append_text e s] appends string s to element e *)
let append_text e s = Dom.appendChild e (document##createTextNode (js s))
(************************ START GAME ************************)
(* [main ()] is the main method of the entire game. *)
let main () =
let gui = get_element_by_id "gui" in
gui##style##cssText <- js "font-family:Triforce";
let h1 = Html.createH1 document in
let p = Html.createP document in
let audio = Html.createAudio document in
audio##src <- js "zolda.mp3";
audio##play ();
append_text h1 "The Legend of Zolda";
gui##style##cssText <- js "font-family:Triforce font-size:10px";
append_text p "You are Lonk. Save Zolda!";
Dom.appendChild gui h1;
Dom.appendChild gui p;
let canvas = Html.createCanvas document in
canvas##width <- int_of_float Gui.canvas_width;
canvas##height <- int_of_float Gui.canvas_height;
Dom.appendChild gui canvas;
let context = canvas##getContext (Html._2d_) in
(* add event listeners *)
let _ = Html.addEventListener
document Html.Event.keydown (Html.handler Game.keydown)
Js._true in
let _ = Html.addEventListener
document Html.Event.keydown (Html.handler Game.keyup)
Js._true in
Game.game_loop context false
(* start the game *)
let _ = main ()