From 7c931f4cf553be07c837c1739843ce2421e680f6 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Sat, 9 Dec 2023 14:35:04 -0500 Subject: [PATCH] broken SDL integration, failed attempt to fix it with nix --- SDL.fnl | 20 +++++++++++++++ boot.t | 3 +-- flake.lock | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 20 +++++++++++++++ go.fnl | 31 +++++++++++----------- terra.fnl | 11 +++++--- 6 files changed, 139 insertions(+), 21 deletions(-) create mode 100644 SDL.fnl create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/SDL.fnl b/SDL.fnl new file mode 100644 index 0000000..4aee29b --- /dev/null +++ b/SDL.fnl @@ -0,0 +1,20 @@ +; todo: can we implement portable logic for finding dynamic libraries? +(terralib.linklibrary "/nix/store/hzfvp1pay5sqq96l20i4ahi7yrajjgfy-SDL2-2.28.4/lib/libSDL2.so") + +(fn load-sdl-headers [headers] + (let [includes (table.concat (icollect [_ h (ipairs headers)] + (.. "#include \n"))) + env (terralib.includecstring includes) + SDL (collect [k v (pairs env)] (k:gsub "^SDL_" "") v)] + (set SDL.INIT_EVERYTHING (bit.bor SDL.INIT_TIMER SDL.INIT_AUDIO SDL.INIT_VIDEO + SDL.INIT_JOYSTICK SDL.INIT_HAPTIC + SDL.INIT_GAMECONTROLLER SDL.INIT_EVENTS)) + SDL)) + +(load-sdl-headers [:SDL :SDL_hints :SDL_error :SDL_video :SDL_render + :SDL_pixels :SDL_rect :SDL_surface :SDL_clipboard + :SDL_events :SDL_keyboard :SDL_keycode :SDL_scancode + :SDL_mouse :SDL_joystick :SDL_gamecontroller :SDL_sensor + :SDL_haptic :SDL_audio :SDL_timer :SDL_filesystem + :SDL_rwops :SDL_messagebox]) + diff --git a/boot.t b/boot.t index f6801fc..b1eeefa 100644 --- a/boot.t +++ b/boot.t @@ -11,7 +11,6 @@ _G._3 = nil debug.traceback = fennel.traceback table.insert(package.loaders, fennel.searcher) -if arg[0] == "boot.t" then +if rawget(_G, "arg") and arg[0] == "boot.t" then fennel.repl() end - diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..f8fab9a --- /dev/null +++ b/flake.lock @@ -0,0 +1,75 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixgl": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1685908677, + "narHash": "sha256-E4zUPEUFyVWjVm45zICaHRpfGepfkE9Z2OECV9HXfA4=", + "owner": "guibou", + "repo": "nixGL", + "rev": "489d6b095ab9d289fe11af0219a9ff00fe87c7c5", + "type": "github" + }, + "original": { + "owner": "guibou", + "repo": "nixGL", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1660551188, + "narHash": "sha256-a1LARMMYQ8DPx1BgoI/UN4bXe12hhZkCNqdxNi6uS0g=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "441dc5d512153039f19ef198e662e4f3dbb9fd65", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1701861573, + "narHash": "sha256-9mZL4N+G/iAADDdR6vKDFwiweYLO8hAmjnDHvfVhYCY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "325eb628b89b9a8183256f62d017bfb499b19bd9", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixgl": "nixgl", + "nixpkgs": "nixpkgs_2" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..1fae8ca --- /dev/null +++ b/flake.nix @@ -0,0 +1,20 @@ +{ + description = "Garden"; + + inputs = { + nixgl.url = "github:guibou/nixGL"; + }; + + outputs = { self, nixgl, nixpkgs }: + let + pkgs = import nixpkgs { + system = "x86_64-linux"; + overlays = [ nixgl.overlay ]; + }; + in + { + devShells."x86_64-linux".default = pkgs.mkShell { + buildInputs = [ pkgs.terra pkgs.SDL2 pkgs.nixgl.auto.nixGLDefault ]; + }; + }; +} diff --git a/go.fnl b/go.fnl index efcd62a..e08cbe8 100644 --- a/go.fnl +++ b/go.fnl @@ -1,20 +1,19 @@ +; export LIBGL_ALWAYS_INDIRECT=1 (local fennel (require :fennel)) (import-macros {: def : q : ttype : static : printform} :terra) +(local SDL (require :SDL)) -(print "define do-thing") -(fn do-thing [f x] -(printform - (q (var z (+ x 1)) - (f z) - ($))) -) +(local CreateWindowAndRenderer (printform (def [w int h int flags uint32] + (var window [SDL.Window] nil) + (var renderer [SDL.Renderer] nil) + (var result (SDL.CreateWindowAndRenderer w h flags (& window) (& renderer))) + (return result window renderer)))) -(print "define thing-doer") -(local thing-doer (def [x int] (return))) -; (print thing-doer) -; (printform -(def [] - (var z 10) - ,(do-thing thing-doer `z)) -; ) -nil \ No newline at end of file +(fn rect [x y w h] + (SDL.Rect {: x : y : w : h})) + +(SDL.Init SDL.INIT_EVERYTHING) +(let [windowflags SDL.WINDOW_RESIZABLE ;(bit.bor SDL.WINDOW_RESIZABLE SDL.WINDOW_OPENGL) + (window renderer) (CreateWindowAndRenderer 640 480 windowflags)] + (SDL.RenderFillRect renderer (rect 0 0 640 480)) + {: window : renderer}) diff --git a/terra.fnl b/terra.fnl index fceba10..b050463 100644 --- a/terra.fnl +++ b/terra.fnl @@ -62,9 +62,9 @@ (self:pop) result) :expr (fn [self expr] - (if (and (sym? expr) (not (multi-sym? expr))) (self:env-ref expr) + (if (= expr (sym :nil)) :nil + (and (sym? expr) (not (multi-sym? expr))) (self:env-ref expr) (or (= (type expr) :number) (= (type expr) :boolean)) (tostring expr) - (= expr nil) :nil (let [name (safesym :inline-expr) arglist (sequence) expr (extract.quotes expr arglist self.locals) @@ -114,6 +114,9 @@ (where multisym (multi-sym? multisym)) (string.gsub (tostring multisym) "[^.:]+" #(scope:reference $1) 1) + (where _ (= form (sym :nil))) + :nil + (where symbol (sym? symbol)) (scope:reference symbol) @@ -281,4 +284,6 @@ compiler (. comp (tostring compiler-name))] (compiler expr scope))) -{: ttype :q tquote : def : static : compile : uncompile} +(fn printform [form] `(do (print ,(view (macroexpand form))) ,form)) + +{: ttype :q tquote : def : static : compile : uncompile : printform}