diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 08243aa..9e9e9b1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -29,6 +29,9 @@ jobs: with: ocaml-compiler: ${{ matrix.ocaml-compiler }} + - name: Update apt + run: sudo apt update + - name: Install requirements run: sudo apt install -y libsdl2-dev diff --git a/day20/bin/main.ml b/day20/bin/main.ml index 52347c3..3f803a5 100644 --- a/day20/bin/main.ml +++ b/day20/bin/main.ml @@ -9,20 +9,14 @@ let tick t s fb _i = | 255 -> if special then 255 else 254 | p -> p - 1 ) fb; - match (Screen.font s) with - | None -> fb; - | Some (font) -> ( - let x = Random.int 8 and y = Random.int 8 in - let ch = char_of_int (Random.int 256) in - let col = if (((t / 200) mod 8) == x) then 255 else 254 in - let _ = Framebuffer.draw_char ((x * 22) + 20) ((y * 22) + 15) font ch col fb in (); - ); fb + let font = Screen.font s in + let x = Random.int 8 and y = Random.int 8 in + let ch = char_of_int (Random.int 256) in + let col = if (((t / 200) mod 8) == x) then 255 else 254 in + let _ = Framebuffer.draw_char ((x * 22) + 20) ((y * 22) + 15) font ch col fb in (); + fb let () = - match Font.load_psf_font "thirdparty/tamzen-font/psf/TamzenForPowerline10x20.psf" with - | Error (reason) -> Printf.printf "Failed to read: %s" reason - | Ok font -> ( - Palette.of_list (List.rev (0xff0000 :: (Palette.to_list (Palette.generate_mono_palette 255)))) |> - Screen.create_with_font 200 200 2 font |> - Base.run "Genuary 20/23: Generative Typography and 8x8" None tick - ) + Palette.of_list (List.rev (0xff0000 :: (Palette.to_list (Palette.generate_mono_palette 255)))) |> + Screen.create 200 200 2 |> + Base.run "Genuary 20/23: Generative Typography and 8x8" None tick diff --git a/filedrop/bin/dune b/filedrop/bin/dune new file mode 100644 index 0000000..845a3b6 --- /dev/null +++ b/filedrop/bin/dune @@ -0,0 +1,4 @@ +(executable + (public_name filedrop) + (name main) + (libraries filedrop claudius giflib)) diff --git a/filedrop/bin/main.ml b/filedrop/bin/main.ml new file mode 100644 index 0000000..a740425 --- /dev/null +++ b/filedrop/bin/main.ml @@ -0,0 +1,77 @@ +open Claudius +open Giflib + +let image : GIF.t option ref = ref None + +let draw_gif t s gif fb = + let c = GIF.image_count gif in + let frame = ((t / 2) mod c) in + let i = GIF.get_image gif frame in + + let new_pal = Palette.of_list ( 0x000000 :: 0xFFFFFF :: List.map (fun (r, g, b) -> + b + (g * 256) + (r * 256 * 256) + ) (Array.to_list (Image.palette i))) in + Screen.update_palette s new_pal; + + let sw, sh = Screen.dimensions s + and gw, gh = GIF.dimensions gif + and iw, ih = Image.dimensions i + and ixoff, iyoff = Image.offset i + and pixels = Image.pixels i in + let transparent = match (Image.transparent i) with + | None -> -1 + | Some x -> x + in + let sxoff = (sw - gw) / 2 + and syoff = (sh - gh) / 2 in + for x = 0 to (iw - 1) do + for y = 0 to (ih - 1) do + let v = pixels.(x + (y * iw)) in + if (v != transparent) then + Framebuffer.pixel_write (x + sxoff + ixoff) (y + syoff + iyoff) (v + 2) fb + done + done; + + fb + +let boot s = + let fb = Framebuffer.init (Screen.dimensions s) (fun _ _ -> 0) in + let font = Screen.font s in + ignore (Framebuffer.draw_string 10 10 font "Drop a GIF here" 1 fb); + fb + +let tick t s prev (inputs : Base.input_state) = + + let filename = List.fold_left (fun acc ev -> + match ev with + | Event.DropFile pth -> Some pth + | _ -> acc + ) None inputs.events in + + let updated = match filename with + | None -> false + | Some filename -> ( + (match Filename.extension filename with + | ".gif" -> image := Some (GIF.from_file filename) + | _ -> image := None); + true + ) + in + + match !image, updated with + | None, true -> boot s + | None, false -> prev + | Some img, false -> ( + match GIF.image_count img with + | 1 -> prev + | _ -> draw_gif t s img prev + ) + | Some img, true -> ( + Framebuffer.init (Screen.dimensions s) (fun _ _ -> 0) |> + draw_gif t s img + ) + +let () = + Palette.of_list (0x000000 :: 0xFFFFFF :: (Palette.to_list (Palette.generate_plasma_palette 256))) |> + Screen.create 640 480 1 |> + Base.run "File drop test" (Some boot) tick diff --git a/filedrop/dune-project b/filedrop/dune-project new file mode 100644 index 0000000..fb12a8e --- /dev/null +++ b/filedrop/dune-project @@ -0,0 +1,26 @@ +(lang dune 3.17) + +(name filedrop) + +(generate_opam_files true) + +(source + (github username/reponame)) + +(authors "Author Name ") + +(maintainers "Maintainer Name ") + +(license LICENSE) + +(documentation https://url/to/documentation) + +(package + (name filedrop) + (synopsis "A short synopsis") + (description "A longer description") + (depends ocaml) + (tags + ("add topics" "to describe" your project))) + +; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html diff --git a/filedrop/filedrop.opam b/filedrop/filedrop.opam new file mode 100644 index 0000000..aed4d6e --- /dev/null +++ b/filedrop/filedrop.opam @@ -0,0 +1,31 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +synopsis: "A short synopsis" +description: "A longer description" +maintainer: ["Maintainer Name "] +authors: ["Author Name "] +license: "LICENSE" +tags: ["add topics" "to describe" "your" "project"] +homepage: "https://github.com/username/reponame" +doc: "https://url/to/documentation" +bug-reports: "https://github.com/username/reponame/issues" +depends: [ + "dune" {>= "3.17"} + "ocaml" + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +] +dev-repo: "git+https://github.com/username/reponame.git" diff --git a/filedrop/lib/dune b/filedrop/lib/dune new file mode 100644 index 0000000..ca2552f --- /dev/null +++ b/filedrop/lib/dune @@ -0,0 +1,2 @@ +(library + (name filedrop)) diff --git a/filedrop/test/dune b/filedrop/test/dune new file mode 100644 index 0000000..ad210db --- /dev/null +++ b/filedrop/test/dune @@ -0,0 +1,2 @@ +(test + (name test_filedrop)) diff --git a/filedrop/test/test_filedrop.ml b/filedrop/test/test_filedrop.ml new file mode 100644 index 0000000..e69de29 diff --git a/fonts/bin/main.ml b/fonts/bin/main.ml index e4f3623..220e52f 100644 --- a/fonts/bin/main.ml +++ b/fonts/bin/main.ml @@ -6,27 +6,20 @@ let prose2 = "Hello to FieldFX, thanks for the inspirations and encouragement!" let tick t s fb _i = let width, height = Screen.dimensions s in Framebuffer.map_inplace (fun _ -> 0) fb; - match (Screen.font s) with - | None -> fb - | Some font -> ( - let prose1_width = Framebuffer.draw_string 0 0 font prose1 0 fb in - (* let prose2_width = Framebuffer.draw_string 0 0 font prose2 0 fb in *) - let slow_t = t / 20 in - let pos1 = (slow_t mod (width + (1 * prose1_width))) - prose1_width in - let _ = Framebuffer.draw_string pos1 10 font prose1 (1 + ((slow_t / 10) mod 8)) fb in (); - for i = 0 to ((String.length prose2) - 1) do - let ft = (Float.of_int t) /. 100. in - let c = String.get prose2 i in - let _ = Framebuffer.draw_char (width + ((width - slow_t + (i * 15)) mod width)) ((Int.of_float (40. *. sin ((ft +. Float.of_int(i)) *. 0.1))) + (height / 2)) font c 3 fb in (); - done; - fb - ) + let font = Screen.font s in + let prose1_width = Framebuffer.draw_string 0 0 font prose1 0 fb in + (* let prose2_width = Framebuffer.draw_string 0 0 font prose2 0 fb in *) + let slow_t = t / 2 in + let pos1 = (slow_t mod (width + (1 * prose1_width))) - prose1_width in + let _ = Framebuffer.draw_string pos1 10 font prose1 (1 + ((slow_t / 10) mod 8)) fb in (); + for i = 0 to ((String.length prose2) - 1) do + let ft = (Float.of_int t) /. 100. in + let c = String.get prose2 i in + let _ = Framebuffer.draw_char (width + ((width - slow_t + (i * 15)) mod width)) ((Int.of_float (40. *. sin ((ft +. Float.of_int(i)) *. 0.1))) + (height / 2)) font c 3 fb in (); + done; + fb let () = - match Font.load_psf_font "thirdparty/tamzen-font/psf/TamzenForPowerline10x20.psf" with - | Error (reason) -> Printf.printf "Failed to read: %s" reason - | Ok font -> ( - Palette.load_tic80_palette tic80_palette |> - Screen.create_with_font 240 136 3 font |> - Base.run "Font testing" None tick - ) + Palette.load_tic80_palette tic80_palette |> + Screen.create 240 136 3 |> + Base.run "Font testing" None tick diff --git a/keytest/bin/main.ml b/keytest/bin/main.ml index 584586d..95c9e6b 100644 --- a/keytest/bin/main.ml +++ b/keytest/bin/main.ml @@ -4,15 +4,11 @@ let pos = ref (0, 0) let tick _t s _prev (inputs : Base.input_state) = let fb = Framebuffer.init (Screen.dimensions s) (fun _x _y -> 0) in - ( - match (Screen.font s) with - | None -> () - | Some font -> ( - List.iteri (fun i c -> - let s = Printf.sprintf "0x%08x" (Base.PlatformKey.to_backend_keycode c) in - ignore(Framebuffer.draw_string 5 (i * 12) font s 8 fb) - ) (Base.KeyCodeSet.to_list inputs.keys) - )); + let font = Screen.font s in + List.iteri (fun i c -> + let s = Printf.sprintf "0x%08x" (Base.PlatformKey.to_backend_keycode c) in + ignore(Framebuffer.draw_string 5 (i * 12) font s 8 fb) + ) (Base.KeyCodeSet.to_list inputs.keys); let dx, dy = List.fold_right ( fun c (x, y) -> @@ -33,10 +29,6 @@ let tick _t s _prev (inputs : Base.input_state) = (* ----- *) let () = - match Font.load_psf_font "thirdparty/tamzen-font/psf/TamzenForPowerline10x20.psf" with - | Error (reason) -> Printf.printf "Failed to read: %s" reason - | Ok font -> ( - Palette.of_list (List.rev (Palette.to_list (Palette.generate_plasma_palette 16))) |> - Screen.create_with_font 640 480 1 font |> - Base.run "Keyboard test" None tick - ) + Palette.of_list (List.rev (Palette.to_list (Palette.generate_plasma_palette 16))) |> + Screen.create 640 480 1 |> + Base.run "Keyboard test" None tick diff --git a/paint/bin/main.ml b/paint/bin/main.ml index 5746334..9027c6d 100644 --- a/paint/bin/main.ml +++ b/paint/bin/main.ml @@ -35,16 +35,16 @@ let tick _ s buffer (inputs : Base.input_state) : Framebuffer.t = List.iter (fun e -> match e with - | Mouse.Button_up (Left, _) -> ( + | Event.MouseButtonUp (Left, _) -> ( last_coord := None; ) - | Mouse.Button_down (Left, (x, y)) -> ( + | Event.MouseButtonDown (Left, (x, y)) -> ( if (x >= (w - size)) then ( col := y / size ); last_coord := Some (x, y) ) - | Mouse.Drag (Left, (x, y)) -> ( + | Event.MouseDrag (Left, (x, y)) -> ( match !last_coord with | None -> ( @@ -55,7 +55,7 @@ let tick _ s buffer (inputs : Base.input_state) : Framebuffer.t = ) ) | _ -> () - ) ( Mouse.get_events inputs.mouse); + ) inputs.events; buffer let () = diff --git a/triangles/bin/main.ml b/triangles/bin/main.ml index 3377416..ff73686 100644 --- a/triangles/bin/main.ml +++ b/triangles/bin/main.ml @@ -71,25 +71,18 @@ let tick t s fb _i = Framebuffer.draw_circle (width / 2) (height / 2) 100. (col / 3) fb; Framebuffer.draw_circle (width / 2) (height / 2) 50. (col / 2) fb; - match (Screen.font s) with - | None -> fb - | Some font -> ( - filled_triangle - ((Int.of_float (50. *. (sin (ft)))) + (width / 2)) - ((Int.of_float (50. *. (cos (ft)))) + (height / 2)) - ((Int.of_float (150. *. (sin (ft +. (Float.pi *. 1.5))))) + (width / 2)) - ((Int.of_float (150. *. (cos (ft +. (Float.pi *. 1.5))))) + (height / 2)) - ((Int.of_float (100. *. (sin (ft +. 2.)))) + (width / 2)) - ((Int.of_float (100. *. (cos (ft +. 2.)))) + (height / 2)) - col fb font; - fb - ) + let font = Screen.font s in + filled_triangle + ((Int.of_float (50. *. (sin (ft)))) + (width / 2)) + ((Int.of_float (50. *. (cos (ft)))) + (height / 2)) + ((Int.of_float (150. *. (sin (ft +. (Float.pi *. 1.5))))) + (width / 2)) + ((Int.of_float (150. *. (cos (ft +. (Float.pi *. 1.5))))) + (height / 2)) + ((Int.of_float (100. *. (sin (ft +. 2.)))) + (width / 2)) + ((Int.of_float (100. *. (cos (ft +. 2.)))) + (height / 2)) + col fb font; + fb let () = - match Font.load_psf_font "thirdparty/tamzen-font/psf/TamzenForPowerline10x20.psf" with - | Error (reason) -> Printf.printf "Failed to read: %s" reason - | Ok font -> ( - Palette.of_list (List.rev (Palette.to_list (Palette.generate_mono_palette 16))) |> - Screen.create_with_font 640 480 1 font |> - Base.run "Triangle testing" None tick - ) + Palette.of_list (List.rev (Palette.to_list (Palette.generate_mono_palette 16))) |> + Screen.create 640 480 1 |> + Base.run "Triangle testing" None tick