-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement browser UI via js_of_ocaml (using Brr). Runs!! But is super…
… slow..
- Loading branch information
Showing
6 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
(executables | ||
(names main) | ||
(libraries brr bigstringaf js_of_ocaml-lwt camlboy_lib) | ||
(link_flags (:standard -no-check-prims)) | ||
(modes js)) | ||
|
||
(rule | ||
(targets main.js) | ||
(deps main.bc.js) | ||
(action (run cp %{deps} %{targets}))) | ||
|
||
(alias | ||
(name app) | ||
(deps main.js main.html)) |
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,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width,initial-scale=1.0"> | ||
<script type="text/javascript" defer="defer" src="main.js"></script> | ||
<title>CAMLBOY</title> | ||
</head> | ||
<body> | ||
<canvas class="game" width="160" height="144"></canvas> | ||
<noscript>Sorry, you need to enable JavaScript to see this page.</noscript> | ||
</body> | ||
</html> |
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,89 @@ | ||
open Camlboy_lib | ||
open Brr | ||
open Brr_canvas | ||
|
||
let gb_w = 160 | ||
let gb_h = 144 | ||
|
||
let canvas_id = "screen" | ||
|
||
let draw_framebuffer ctx image_data fb = | ||
let d = C2d.Image_data.data image_data in | ||
for y = 0 to gb_h - 1 do | ||
for x = 0 to gb_w - 1 do | ||
let off = 4 * (y * gb_w + x) in | ||
match fb.(y).(x) with | ||
| `White -> | ||
Tarray.set d (off ) 0xFF; | ||
Tarray.set d (off + 1) 0xFF; | ||
Tarray.set d (off + 2) 0xFF; | ||
Tarray.set d (off + 3) 0xFF; | ||
| `Light_gray -> | ||
Tarray.set d (off ) 0xAA; | ||
Tarray.set d (off + 1) 0xAA; | ||
Tarray.set d (off + 2) 0xAA; | ||
Tarray.set d (off + 3) 0xFF; | ||
| `Dark_gray -> | ||
Tarray.set d (off ) 0x77; | ||
Tarray.set d (off + 1) 0x77; | ||
Tarray.set d (off + 2) 0x77; | ||
Tarray.set d (off + 3) 0xFF; | ||
| `Black -> | ||
Tarray.set d (off ) 0x00; | ||
Tarray.set d (off + 1) 0x00; | ||
Tarray.set d (off + 2) 0x00; | ||
Tarray.set d (off + 3) 0xFF; | ||
done | ||
done; | ||
C2d.put_image_data ctx image_data ~x:0 ~y:0 | ||
|
||
let load_rom_button ctx image_data = | ||
let on_change i = | ||
let file = El.Input.files i |> List.hd in | ||
let blob = File.as_blob file in | ||
let buf_fut = Blob.array_buffer blob in | ||
Fut.await buf_fut (function | ||
| Ok buf -> | ||
let rom_bytes = | ||
Tarray.of_buffer Uint8 buf | ||
|> Tarray.to_bigarray1 | ||
(* Convert uint8 bigarray to char bigarray *) | ||
|> Obj.magic | ||
in | ||
let cartridge = Detect_cartridge.f ~rom_bytes in | ||
let module C = Camlboy.Make(val cartridge) in | ||
let t = C.create_with_rom ~print_serial_port:true ~rom_bytes in | ||
Console.profile (Jstr.v "foo"); | ||
let rec run_instr () = | ||
begin match C.run_instruction t with | ||
| In_frame -> | ||
run_instr () | ||
| Frame_ended fb -> | ||
draw_framebuffer ctx image_data fb; | ||
end; | ||
in | ||
ignore @@ G.set_interval ~ms:10 run_instr | ||
| Error e -> | ||
Console.(log [Jv.Error.message e]) | ||
) | ||
in | ||
let i = El.input ~at:At.[type' (Jstr.v "file")] () in | ||
let b = El.button [ El.txt' "Load Rom" ] in | ||
El.set_inline_style El.Style.display (Jstr.v "none") i; | ||
Ev.listen Ev.click (fun _ -> El.click i) (El.as_target b); | ||
Ev.listen Ev.change (fun _ -> on_change i) (El.as_target i); | ||
El.span [i; b] | ||
|
||
let () = | ||
let cnv = Canvas.create ~w:gb_w ~h:gb_h ~at:At.[id (Jstr.v canvas_id)] [] in | ||
let ctx = C2d.create cnv in | ||
let image_data = C2d.create_image_data ctx ~w:gb_w ~h:gb_h in | ||
let fb = Array.make_matrix gb_h gb_w `Dark_gray in | ||
let fb2 = Array.make_matrix gb_h gb_w `Black in | ||
draw_framebuffer ctx image_data fb; | ||
draw_framebuffer ctx image_data fb2; | ||
draw_framebuffer ctx image_data fb; | ||
El.set_children (Document.body G.document) [ | ||
Canvas.to_el cnv; | ||
load_rom_button ctx image_data; | ||
] |
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