Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Graphics = {version=6}
- local gpu = require'component'.gpu
- local unicode = require'unicode'
- local function expect(v,...)
- local exp = {...}
- local got = type(v)
- local tx = ''
- local ok = false
- for i,v in ipairs(exp) do
- tx=tx..v
- if not(i>=#exp) then
- tx=tx..' or '
- end
- if got==v then
- ok=true
- end
- end
- return ok , ('Expected '..tx..', but got '..got)
- end
- local function getIndex(x,y,w)
- return w*(y-1)+x
- end
- local function getXY(i,w)
- local x=(i-1)%w+1
- return x,(i-x)/w+1
- end
- --local y=math.floor(i/(w+1))
- --return ((i-1)%w)+1,y+1 --OLD CODE
- local function cn(v)
- if type(v)=='number' then
- return v
- elseif v then
- return 1
- else
- return 0
- end
- end
- local function toBits(num,bits)
- local t={}
- for b = bits, 1, -1 do
- t[b] = math.fmod(num, 2)
- num = math.floor((num - t[b]) / 2)
- end
- return t
- end
- local cstack={}
- local function pushColor()
- table.insert(cstack,{gpu.getBackground(),gpu.getForeground()})
- end
- local function popColor()
- local bg,fg=table.unpack(table.remove(cstack))
- gpu.setBackground(bg)
- gpu.setForeground(fg)
- end
- function Graphics:getWidth()
- assert(expect(self,'table'))
- return self.w*2
- end
- function Graphics:getHeight()
- assert(expect(self,'table'))
- return self.h*4
- end
- function Graphics:getDimensions()
- assert(expect(self,'table'))
- return self:getWidth(),self:getHeight()
- end
- function Graphics:_px(x,y,v)
- assert(expect(self,'table'))
- assert(expect(x,'number'))
- assert(expect(y,'number'))
- --assert(expect(v,'number','boolean','nil'))
- local w,h=self:getDimensions()
- if not(x>w or x<1 or y>h or y<1)then
- local ew,eh=2,4
- local fx,fy=math.ceil(x/ew),math.ceil(y/eh)
- local lx,ly=x-(fx-1)*ew,y-(fy-1)*eh
- local iA,iB=getIndex(fx,fy,self.w),getIndex(ly,lx,eh)
- if v~=nil then
- local oldv=self.s[iA][iB]
- local newv=cn(v)
- if not(oldv==newv) then
- self.s[iA][iB]=newv
- self.u[iA]=true
- end
- else
- return self.s[iA][iB]
- end
- end
- end
- function Graphics:setPixel(x,y,v)
- if v==nil then v=false end
- self:_px(x,y,v)
- end
- function Graphics:getPixel(x,y)
- return self:_px(x,y,nil)
- end
- function Graphics:fastLine(x,y,vx,vy,l,rem)
- assert(expect(self,'table'))
- assert(expect(x ,'number')) assert(expect(y ,'number'))
- assert(expect(vx,'number')) assert(expect(vy,'number'))
- assert(expect(l ,'number'))
- for i=0,l do
- self:setPixel(
- math.ceil(x+i*vx),
- math.ceil(y+i*vy),
- not(rem)
- )
- end
- end
- function Graphics:oldLine(x1,y1,x2,y2,rem)
- assert(expect(self,'table'))
- assert(expect(x1,'number')) assert(expect(x2,'number'))
- assert(expect(y1,'number')) assert(expect(y2,'number'))
- local a=math.atan2(x1-x2,y1-y2)
- local vx,vy=-math.sin(a),-math.cos(a)
- local l=math.sqrt((x2-x1)^2+(y2-y1)^2)
- self:fastLine(x1,y1,vx,vy,l,rem)
- end
- function Graphics:drawLine(x1,y1,x2,y2,rem)
- assert(expect(self,'table'))
- assert(expect(x1,'number')) assert(expect(x2,'number'))
- assert(expect(y1,'number')) assert(expect(y2,'number'))
- y1,y2=y1+1,y2+1
- local dx = math.abs(x2-x1)
- local dy = -math.abs(y2-y1)
- local sx,sy
- if x1<x2 then
- sx=1
- else
- sx=-1
- end
- if y1<y2 then
- sy=1
- else
- sy=-1
- end
- local err = dx+dy
- while true do
- self:setPixel(x1,y1,not(rem))
- if x1==x2 and y1==y2 then
- break
- end
- local e2 = 2*err
- if e2 >= dy then
- err = err+dy
- x1 = x1+sx
- end
- if e2 <= dx then
- err = err+dx
- y1 = y1+sy
- end
- end
- end
- local function fixRect(x,y,w,h)
- if w<0 then
- x,w=x+w,math.abs(w)
- end
- if h<0 then
- y,h=y+h,math.abs(h)
- end
- return x,y,w,h
- end
- function Graphics:drawRect(x,y,w,h,fill,rem)
- assert(expect(self,'table'))
- assert(expect(x,'number')) assert(expect(y,'number'))
- assert(expect(w,'number')) assert(expect(h,'number'))
- x,y,w,h=fixRect(x,y,w,h)
- if fill then
- for i=0,w-1 do
- for j=0,h-1 do
- self:setPixel(x+i,y+j,not(rem))
- end
- end
- else
- self:fastLine(x,y,1,0,w-1,rem)--top
- self:fastLine(x,y+h-1,1,0,w-1,rem)--bottom
- self:fastLine(x,y,0,1,h-1,rem)--left
- self:fastLine(x+w-1,y,0,1,h-1,rem)--right
- end
- end
- local Sprite={}
- function Sprite:getWidth()
- assert(expect(self,'table'))
- return self.w
- end
- function Sprite:getHeight()
- assert(expect(self,'table'))
- return self.h
- end
- function Sprite:getDimensions()
- assert(expect(self,'table'))
- return Sprite:getWidth(),Sprite:getHeight()
- end
- --TODO SPRITE SET PIX/GET PIX
- function Graphics:newSprite(spriteData)
- assert(expect(spriteData,'string'))
- local t={
- w=spriteData:byte(1),
- h=spriteData:byte(2),
- data={}
- }
- local d={spriteData:byte(3,#spriteData)}
- for i=1,#d do
- local b=toBits(d[i],8)
- for j=1,#b do
- if #t.data<=t.w*t.h then
- t.data[#t.data+1]=(b[j]==1)
- else
- break
- end
- end
- end
- setmetatable(t,{__index=Sprite})
- return t
- end
- function Graphics:drawSprite(sprite,x,y)
- assert(expect(self,'table'))
- assert(expect(x,'number'))
- assert(expect(y,'number'))
- local px,py=0,0
- local d=sprite.data
- for i=1,#d do
- self:setPixel(x+px,y+py,d[i])
- px=px+1
- if px>=sprite.w then
- px=0
- py=py+1
- end
- end
- end
- function Graphics:_draw(i,x,y,shd)
- x=x or 0
- y=y or 0
- pushColor()
- local v=self.s[i]
- local xp,yp=getXY(i,self.w)
- local sxp,syp=xp+x,yp+y
- local gw,gh=gpu.getResolution()
- local r=false
- if sxp>=1 and syp>=1 and sxp<=gw and syp<=gh then
- local schr,sfg,sbg=gpu.get(sxp,syp)
- local chr=self:char(v)
- local fg,bg=table.unpack(v.color)
- if chr~=schr or sfg~=fg or sbg~=bg then --:gsub(' ','')
- gpu.setForeground(fg)
- gpu.setBackground(bg)
- gpu.set(sxp,syp,chr)
- r=true
- end
- end
- popColor()
- return r
- end
- function Graphics:redraw(x,y,shd)
- assert(expect(self,'table'))
- assert(expect(x,'number','nil'))
- assert(expect(y,'number','nil'))
- assert(expect(shd,'function','nil'))
- for i=1,self.w*self.h do --old
- self:_draw(i,x,y,shd)
- end
- end
- function Graphics:draw(x,y)
- assert(expect(self,'table'))
- assert(expect(x,'number','nil'))
- assert(expect(y,'number','nil'))
- for i,v in next,self.u do
- self:_draw(i,x,y)
- end
- self.u={}
- end
- function Graphics:_reset(UPUSH)
- self.u={}
- for i=1,self.w*self.h do
- if UPUSH and self:char(self.s[i])~=' ' then
- self.u[i]=true
- end
- self.s[i]={
- 0,0,0,0,0,0,0,0,
- color={0xFFFFFF,0x000000}
- }
- end
- end
- function Graphics:reset()
- assert(expect(self,'table'))
- self:_reset(true)
- end
- function Graphics:char(s)
- assert(expect(s,'table'))
- local n=s[1]+s[2]*2+s[3]*4+s[4]*64+s[5]*8+s[6]*16+s[7]*32+s[8]*128
- --local n=tonumber(table.concat(s):reverse(),2)
- if n>0 then
- return unicode.char(10240+n)
- else
- return ' '
- end
- end
- function Graphics:new(w,h)
- assert(expect(self,'table'))
- assert(expect(w,'number','nil'))
- assert(expect(h,'number','nil'))
- local gpuw,gpuh=gpu.getResolution()
- w,h = w or gpuw, h or gpuh
- local g={
- w=w,
- h=h,
- u={},
- s={}
- }
- setmetatable(g,{__index=Graphics})
- g:_reset()
- return g
- end
- return setmetatable(
- Graphics,
- {
- __call=Graphics.new,
- __newindex=function()end
- }
- )
Advertisement
Add Comment
Please, Sign In to add comment