broken SDL integration, failed attempt to fix it with nix

This commit is contained in:
Jeremy Penner 2023-12-09 14:35:04 -05:00
parent f8ab3c4621
commit 7c931f4cf5
6 changed files with 139 additions and 21 deletions

20
SDL.fnl Normal file
View file

@ -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 <SDL2/" h ".h>\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])

3
boot.t
View file

@ -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

75
flake.lock Normal file
View file

@ -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
}

20
flake.nix Normal file
View file

@ -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 ];
};
};
}

31
go.fnl
View file

@ -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
(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})

View file

@ -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}