OpenComputerScripts/libs/libgpu.lua

104 lines
2.1 KiB
Lua
Raw Normal View History

2018-03-13 08:25:49 +08:00
-- LibGPU - A library help operating GPU.
2018-03-10 00:19:42 +08:00
-- Author: Github/Kiritow
local component=require("component")
2018-11-09 23:38:54 +08:00
-- y is line, x is col
2018-03-10 00:19:42 +08:00
local function GPUClear(t)
local w,h=t.gpu.getResolution()
t.gpu.fill(1,1,w,h," ")
end
2018-11-09 23:38:54 +08:00
local function GPUFill(t,x,y,w,h,char_str)
if(string.len(char_str)>1) then
char_str=string.sub(char_str,1,1)
end
t.gpu.fill(x,y,w,h,char_str)
end
2018-03-10 00:19:42 +08:00
local function GPUSet(t,line,col,str)
t.gpu.set(col,line,str)
end
local function GPUGet(t,line,col)
return t.gpu.get(col,line)
end
2018-03-10 00:33:40 +08:00
local function GPUSetColorFG(t,rgb)
t.gpu.setForeground(rgb)
end
local function GPUSetColorBG(t,rgb)
t.gpu.setBackground(rgb)
end
local function GPUGetColorFG(t)
return t.gpu.getForeground()
end
local function GPUGetColorBG(t)
return t.gpu.getBackground()
end
2018-11-09 23:38:54 +08:00
-- Store current fg, then set fg to rgb
2018-03-10 00:33:40 +08:00
local function GPUPushFG(t,rgb)
t.fgstk[t.fgstk.n+1]=t:getfg()
t.fgstk.n=t.fgstk.n+1
t:setfg(rgb)
end
local function GPUPopFG(t)
t:setfg(t.fgstk[t.fgstk.n])
t.fgstk[t.fgstk.n]=nil
t.fgstk.n=t.fgstk.n-1
end
local function GPUPushBG(t,rgb)
t.bgstk[t.bgstk.n+1]=t:getbg()
t.bgstk.n=t.bgstk.n+1
t:setbg(rgb)
end
local function GPUPopBG(t)
t:setbg(t.bgstk[t.bgstk.n])
t.bgstk[t.bgstk.n]=nil
t.bgstk.n=t.bgstk.n-1
end
2018-11-09 23:38:54 +08:00
local function GPUPushAll(t,fg_rgb,bg_rgb)
GPUPushFG(t,fg_rgb)
GPUPushBG(t,bg_rgb)
end
local function GPUPopAll(t)
GPUPopBG(t)
GPUPopFG(t)
end
2018-03-10 00:19:42 +08:00
-- API
2018-11-09 23:38:54 +08:00
function GetGPU(addr)
2018-06-26 00:15:05 +08:00
if(component.list("gpu")==nil) then
2018-03-10 00:19:42 +08:00
error("No GPU Found.")
else
local t={}
2018-06-26 00:15:05 +08:00
t.gpu=component.proxy(component.list("gpu")())
2018-03-10 00:19:42 +08:00
t.clear=GPUClear
t.set=GPUSet
t.get=GPUGet
2018-11-09 23:38:54 +08:00
t.fill=GPUFill
2018-03-10 00:33:40 +08:00
t.setfg=GPUSetColorFG
t.getfg=GPUGetColorFG
t.setbg=GPUSetColorBG
t.getbg=GPUGetColorBG
t.fgstk={n=0}
t.bgstk={n=0}
t.pushfg=GPUPushFG
t.popfg=GPUPopFG
t.pushbg=GPUPushBG
t.popbg=GPUPopBG
2018-11-09 23:38:54 +08:00
t.pushall=GPUPushAll
t.popall=GPUPopAll
2018-03-10 00:19:42 +08:00
return t
end
end