From c492499402f22343b7769e9c8bae5b55099dea33 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Mon, 7 Apr 2025 17:00:20 -0700 Subject: [PATCH] Basic window open based on https://github.com/xyproto/sdl2-examples/blob/0c24a39748d17c0205e6f857959d482951bbb9d0/ocaml/main.ml --- bin/dune | 2 +- bin/main.ml | 32 +++++++++++++++++++++++++++++++- dune-project | 5 ++++- flake.nix | 1 + ocaml_pong.opam | 1 + 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/bin/dune b/bin/dune index 98bb3b6..1d114f2 100644 --- a/bin/dune +++ b/bin/dune @@ -1,4 +1,4 @@ (executable (public_name ocaml_pong) (name main) - (libraries ocaml_pong)) + (libraries ocaml_pong tsdl)) diff --git a/bin/main.ml b/bin/main.ml index 7bf6048..8154cfe 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1 +1,31 @@ -let () = print_endline "Hello, World!" +open Tsdl + +let run () = + let (let*) = Result.bind in + let* _ = Sdl.init Sdl.Init.everything in + let* w = Sdl.create_window "Hello World!" + ~x:Sdl.Window.pos_centered + ~y:Sdl.Window.pos_centered + ~w:620 + ~h:387 + Sdl.Window.shown in + let* r = Sdl.create_renderer w + ~index:(-1) + ~flags:Sdl.Renderer.software in + for _ = 0 to 20 do + ignore @@ Sdl.render_clear r; + Sdl.render_present r; + Sdl.delay (Int32.of_int 100); + done; + + Sdl.destroy_renderer r; + Sdl.destroy_window w; + Sdl.quit (); + Ok () + +let () = + match run () with + | Ok _ -> exit 0 + | Error (`Msg e) -> + Sdl.log "Error: %s" e; + exit 1 \ No newline at end of file diff --git a/dune-project b/dune-project index 6b38643..538383c 100644 --- a/dune-project +++ b/dune-project @@ -17,7 +17,10 @@ (name ocaml_pong) (synopsis "An OCaml pong clone") (description "An pong clone written in OCaml, using the TSDL SDL bindings") - (depends ocaml) + (depends + ocaml + tsdl + ) (tags ("game"))) diff --git a/flake.nix b/flake.nix index eac512d..8e0fece 100644 --- a/flake.nix +++ b/flake.nix @@ -32,6 +32,7 @@ pname = "ocaml_pong"; version = "0.1.0"; src = ./.; + buildInputs = [ tsdl ]; inherit meta; }; }; diff --git a/ocaml_pong.opam b/ocaml_pong.opam index 70cdf33..859ca7f 100644 --- a/ocaml_pong.opam +++ b/ocaml_pong.opam @@ -9,6 +9,7 @@ tags: ["game"] depends: [ "dune" {>= "3.18"} "ocaml" + "tsdl" "odoc" {with-doc} ] build: [