-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved Bitmap, Controls, and GameLoop over to Reason
- Loading branch information
Jeremy Morrell
committed
Jul 17, 2017
1 parent
d88b237
commit 994c1cf
Showing
6 changed files
with
127 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
type t = { | ||
image: Image.t, | ||
width: int, | ||
height: int, | ||
}; | ||
|
||
let make src width height => { | ||
let image = Image.make width height; | ||
Image.setSrc image src; | ||
[%bs.obj { image, width, height }]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
|
||
open Bs_webapi.Dom; | ||
|
||
/* Magic event translation functions */ | ||
external eventToKeyboardEvent : Dom.event => KeyboardEvent.t = "%identity"; | ||
external eventToTouchEvent : Dom.event => TouchEvent.t = "%identity"; | ||
|
||
type states = { | ||
left: bool, | ||
right: bool, | ||
forward: bool, | ||
backward: bool, | ||
}; | ||
|
||
type t = ref states; | ||
|
||
type direction = Left | Right | Forward | Backward; | ||
|
||
let empty = { left: false, right: false, forward: false, backward: false }; | ||
|
||
let getCode keyCode => { | ||
switch (keyCode) { | ||
| "ArrowUp" => Some Forward | ||
| "ArrowLeft" => Some Left | ||
| "ArrowRight" => Some Right | ||
| "ArrowDown" => Some Backward | ||
| _ => None | ||
}; | ||
}; | ||
|
||
let updateState controls value direction => { | ||
switch (direction) { | ||
| Some Left => controls := {...(!controls), left: value } | ||
| Some Forward => controls := {...(!controls), forward: value } | ||
| Some Backward => controls := {...(!controls), backward: value } | ||
| Some Right => controls := {...(!controls), right: value } | ||
| None => () | ||
}; | ||
}; | ||
|
||
let onKey controls value e => { | ||
Event.preventDefault e; | ||
Event.stopPropagation e; | ||
let direction = e |> eventToKeyboardEvent |> KeyboardEvent.code |> getCode; | ||
updateState controls value direction; | ||
}; | ||
|
||
let onTouch controls e => { | ||
controls := empty; | ||
Event.preventDefault e; | ||
Event.stopPropagation e; | ||
/* TODO: Bs_webapi doesn't have support for getting touch event info yet */ | ||
let _touches = TouchEvent.touches (eventToTouchEvent e); | ||
}; | ||
|
||
let onTouchEnd controls e => { | ||
controls := empty; | ||
Event.preventDefault e; | ||
Event.stopPropagation e; | ||
}; | ||
|
||
let make _ => { | ||
let controls = ref empty; | ||
Document.addEventListener "keydown" (onKey controls true) document; | ||
Document.addEventListener "keyup" (onKey controls false) document; | ||
Document.addEventListener "touchstart" (onTouch controls) document; | ||
Document.addEventListener "touchmove" (onTouch controls) document; | ||
Document.addEventListener "touchend" (onTouchEnd controls) document; | ||
controls; | ||
}; | ||
|
||
let getStates controls => { | ||
[%bs.obj { | ||
left: (!controls).left, | ||
right: (!controls).right, | ||
forward: (!controls).forward, | ||
backward: (!controls).backward, | ||
} ]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
open Bs_webapi; | ||
|
||
type t = { | ||
lastTime: float, | ||
callback: (float => unit), | ||
}; | ||
|
||
let make lastTime callback => { lastTime, callback }; | ||
|
||
let rec frame loop time => { | ||
let seconds = (time -. loop.lastTime) /. 1000.; | ||
if (seconds < 0.2) { | ||
loop.callback seconds; | ||
}; | ||
let newLoop = make time loop.callback; | ||
requestAnimationFrame (frame newLoop); | ||
}; | ||
|
||
let start callback => { | ||
let loop = make 0. callback; | ||
requestAnimationFrame (frame loop); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type t; | ||
|
||
external make: int => int => t = "Image" [@@bs.new]; | ||
external setSrc : t => string => unit = "src" [@@bs.set]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
[%bs.raw {|require('./app.js')|}]; | ||
|
||
/* Let's do this! */ |