Skip to content

Instantly share code, notes, and snippets.

@pingbird
Created March 6, 2014 14:45
Show Gist options
  • Save pingbird/9391271 to your computer and use it in GitHub Desktop.
Save pingbird/9391271 to your computer and use it in GitHub Desktop.
OC framebuffer API
local component=require("component")
local unicode=require("unicode")
local function new(mx,my,gpu)
gpu=gpu or component.gpu
local d={
mx=mx,
my=my,
t={},
b={},
f={},
bg=0x000000,
fg=0xFFFFFF,
live=false,
maxdepth=gpu.maxDepth(),
depth=gpu.getDepth(),
}
for x=1,d.mx do
d.t[x]={}
d.b[x]={}
d.f[x]={}
for y=1,d.my do
d.t[x][y]=" "
d.b[x][y]=0x000000
d.f[x][y]=0x000000
end
end
local out
out={
data=d,
write=function(x,y,txt)
for x=1,unicode.len(txt) do
d.t[x][y]=unicode.sub(txt,x,x)
d.b[x][y]=d.bg
d.f[x][y]=d.fg
end
local live=d.live
if live then
gpu.set(x+live[1]-1,y+live[2]-1,txt)
end
end,
dump=function(ox,oy)
ox,oy=ox-1,oy-1
local lfg=getForeground()
local lbg=getBackground()
local ofg,obg=lfg,lbg
local fb,st
for y=1,d.my do
bf=""
st=1
for x=1,d.my do
if lbg~=d.b[x][y] or lfg~=d.f[x][y] then
gpu.set(ox+st,oy+y,bf)
bf=""
st=x
lbg=d.b[x][y]
lfg=d.f[x][y]
gpu.setForeground(lfg)
gpu.setBackground(lbg)
end
bf=bf..d.t[x][y]
end
gpu.set(ox+st,oy+y,bf)
end
if lfg~=ofg then
gpu.setForeground(ofg)
end
if lbg~=obg then
gpu.setBackground(obg)
end
end,
setFG=function(fg)
d.fg=fg
end,
setBG=function(bg)
d.bg=bg
end,
setLive=function(live)
d.live=live
end,
wrap={
set=function(x,y,txt)
write(x,y,txt)
end,
getBackground=function()
return d.bg
end,
setBackground=function(bg)
d.bg=bg
end,
getForeground=function()
return d.fg
end,
setForground=function(fg)
d.fg=fg
end,
maxDepth=function()
return d.maxdepth
end,
getDepth=function()
return d.depth
end,
setDepth=function(num)
d.depth=num
end,
maxResolution=function()
return d.mx,d.my
end,
getResolution=function()
return d.mx,d.my
end,
get=function(x,y)
return d.t[x][y]
end,
copy=function(x,y,w,h,tx,ty)
for wx=1,w do
ot[wx],of[wx],ob[wx]={},{},{}
for wy=1,h do
if d.t[wx+tx-1][wy+ty-1] and d.t[x+wx-1][y+wy-1] then
ot[wx+tx-1][wy+ty-1]=d.t[x+wx-1][y+wy-1]
of[wx+tx-1][wy+ty-1]=d.f[x+wx-1][y+wy-1]
og[wx+tx-1][wy+ty-1]=d.g[x+wx-1][y+wy-1]
end
end
end
end,
fill=function(x,y,w,h,txt)
for wx=x,w+x-1 do
for wy=y,h+y-1 do
ot[wx][wy]=txt
of[wx][wy]=d.fg
ob[wx][wy]=d.bg
end
end
end,
},
}
return out
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment