local sdl = require("SDL") sdl.init({sdl.flags.video}) local windows = {} local renderers = {} local function drawrect(renderer,x,y,color,scale) x = x - 1 y = y - 1 local rect = {x=x*scale,y=y*scale,w=scale,h=scale} if string.sub(color,1,1) == "#" then color = string.sub(color,2,-1) end renderer:setDrawColor(tonumber(color,16)) renderer:fillRect(rect) end minetest.register_node("sdl_display:display",{ description = "SDL Display", groups = {dig_immediate=2}, tiles = {"sdl_display_sides.png"}, digiline = { receptor = {}, effector = { action = function(pos,node,channel,msg) local meta = minetest.get_meta(pos) if (not msg) or channel ~= meta:get_string("channel") then return end local w = meta:get_int("width") local h = meta:get_int("height") local scale = meta:get_int("scale") local hash = minetest.hash_node_position(pos) if not windows[hash] then windows[hash] = sdl.createWindow({ title = meta:get_string("windowtitle"), width = w*scale, height = h*scale, }) renderers[hash] = sdl.createRenderer(windows[hash],0,0) end renderers[hash]:clear() for y=1,h,1 do if type(msg[y]) == "table" then for x=1,w,1 do if type(msg[y][x]) =="string" and string.len(msg[y][x]) >= 6 then drawrect(renderers[hash],x,y,msg[y][x],scale) end end end end renderers[hash]:present() end, }, }, on_destruct = function(pos) local hash = minetest.hash_node_position(pos) if windows[hash] then windows[hash]:hide() end windows[hash] = nil renderers[hash] = nil end, after_place_node = function(pos) local meta = minetest.get_meta(pos) meta:set_string("channel","display") meta:set_int("width",10) meta:set_int("height",10) meta:set_string("windowtitle","Display") meta:set_int("scale",40) local fs = "size[3,6.5]" fs = fs.."field[0,0.5;3,1;channel;Channel;${channel}]" fs = fs.."field[0,1.5;3,1;title;Window Title;${windowtitle}]" fs = fs.."field[0,2.5;3,1;width;Width;${width}]" fs = fs.."field[0,3.5;3,1;height;Height;${height}]" fs = fs.."field[0,4.5;3,1;scale;Scale;${scale}]" fs = fs.."button[0,5.5;3,1;save;Save]" meta:set_string("formspec",fs) meta:set_string("infotext","SDL Display (\"Display\")") end, on_receive_fields = function(pos,_,fields) if not fields.save then return end local meta = minetest.get_meta(pos) meta:set_string("channel",fields.channel) meta:set_string("windowtitle",fields.title) meta:set_int("width",fields.width) meta:set_int("height",fields.height) meta:set_int("scale",fields.scale) meta:set_string("infotext",string.format("SDL Display (\"%s\")",fields.title)) local hash = minetest.hash_node_position(pos) if windows[hash] then windows[hash]:hide() end windows[hash] = nil renderers[hash] = nil end, })