Created
October 23, 2021 02:40
-
-
Save Andre-LA/b95d5340251961968f6cc8f281a4b546 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-- add modules compiled with luarocks on lua path (see the pallene file for details) | |
-- just replace YOUR_USER_HERE with your username | |
package.cpath = package.cpath .. ';/home/dreunix/.luarocks/lib/lua/5.4/?.so' | |
local teste = require 'codes.teste' | |
-- below the actual experiment, this is a port from Lua-SDL2 tutorial: | |
-- https://github.com/Tangent128/luasdl2/blob/master/tutorials/02-window/tutorial.lua | |
-- | |
-- tutorial.lua -- 02 opening a window | |
-- | |
-- import lua binding | |
local SDL = require 'SDL' | |
-- give error function and SDL bindings to pallene side | |
teste.bind_stdlib(error) | |
teste.bind_sdl(SDL.init, SDL.createWindow, SDL.delay) | |
-- run the "game" here on the Lua side | |
local ret, err = teste.init { SDL.flags.Video } | |
if not ret then | |
error(err) | |
end | |
local win, err = teste.create_window { | |
title = "02 - Opening a window", -- optional | |
width = 320, -- optional | |
height = 320, -- optional | |
flags = { SDL.window.Resizable } -- optional | |
} | |
if not win then | |
error(err) | |
end | |
-- Let the window opened a bit | |
teste.delay(1000) | |
-- now, re-run the tutorial, but from the pallene side | |
teste.run_sdl_test({ SDL.flags.Video }, { SDL.window.Resizable }) |
This file contains hidden or 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
local m = {} | |
-- std types and bindings | |
-- this is how it works in general, Pallene can't import C functions (but it's planned I guess), | |
-- however, we can: | |
-- * give Lua values through functions, including Lua functions | |
-- * declare module-local variables and overrides them with functions | |
-- in short, what is done is that first a prototype is declared as a module-local function with a placeholder value, | |
-- then the value is override with a function, which should receive the actual binding implementation from Lua side. | |
typealias int_arr = {integer} | |
local error_impl: string -> () = function(string) end | |
function m.bind_stdlib(errorimpl: string -> ()) | |
error_impl = errorimpl | |
end | |
-- SDL type bindings | |
typealias createwin_args = { | |
title: string, | |
width: integer, | |
height: integer, | |
flags: int_arr | |
} | |
-- the SDL functions comes from the Lua side, and these functions comes from a Lua binding of SDL | |
-- called Lua-SDL2: https://luarocks.org/modules/tangent128/lua-sdl2 | |
-- | |
-- then, I build this binding with luarocks using pallene's Lua on --with-lua (and --with-version=5.4): | |
-- $ luarocks install --local --lua-dir=$PWD/vm/src/ --lua-version=5.4 lua-sdl2 | |
-- | |
-- finally, I add this bindings on the lua path on the lua script | |
local sdl_impl_delay: integer -> () = function (integer) end | |
local sdl_impl_init: int_arr -> (any, any) = function (int_arr) return false, 'binding not initialized' end | |
local sdl_impl_create_window: (createwin_args) -> (any, any) = function(createwin_args) return false, nil end | |
function m.bind_sdl( | |
sdl_init: int_arr -> (any, any), | |
sdl_create_window: (createwin_args) -> (any, any), | |
sdl_delay: integer -> () | |
) | |
sdl_impl_init = sdl_init | |
sdl_impl_create_window = sdl_create_window | |
sdl_impl_delay = sdl_delay | |
end | |
-- function wrappers :) | |
function m.init(flags: int_arr): (any, any) | |
local ok, err = sdl_impl_init(flags) | |
return ok, err | |
end | |
function m.create_window(args: createwin_args): (any, any) | |
local ok, err = sdl_impl_create_window(args) | |
return ok, err | |
end | |
function m.delay(ms: integer) | |
sdl_impl_delay(ms) | |
end | |
-- and yes! we can make our "games" in pallene too :] | |
function m.run_sdl_test(init_flags: {integer}, create_window_flags: {integer}) | |
local ret, err = m.init(init_flags) | |
if not ret then | |
error_impl(err) | |
end | |
local win, err = m.create_window { | |
title = "02 - Opening a window", -- optional | |
width = 320, -- optional | |
height = 320, -- optional | |
flags = create_window_flags -- optional | |
} | |
if not win then | |
error_impl(err) | |
end | |
-- Let the window opened a bit | |
m.delay(1000) | |
end | |
return m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment