Advertisement
incinirate

kk

Jun 18th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1. Display = {}
  2. do
  3.   local Display = _G.Display
  4.   local math = _G.math
  5.  
  6.   function Display.__init__()
  7.     local self={canvas=0}
  8.    
  9.     self.canvas = Canvas.create()
  10.     self.blwin = blittle.createWindow()
  11.    
  12.     setmetatable(self, {__index=Display})
  13.     return self
  14.   end
  15.  
  16.   setmetatable(Display, {__call=Display.__init__})
  17.  
  18.   function Display:drawLine(x1,y1,x2,y2,bg,fg) --Basically copied from paintutils
  19.     bg = bg or colors.black
  20.     fg = fg or colors.white
  21.    
  22.     x1,y1 = math.floor(x1),math.floor(y1)
  23.     x2,y2 = math.floor(x2),math.floor(y2)
  24.    
  25.     if x1==x2 and y1==y2 then
  26.       self.canvas:setPixel(x1,y1,true,bg,fg)
  27.     end
  28.    
  29.     local minX = x1 > x2 and x2 or x1
  30.     local minY,maxX,maxY
  31.     if minX == x1 then
  32.       minY = y1
  33.       maxX = x2
  34.       maxY = y2
  35.     else
  36.       minY = y2
  37.       maxX = x1
  38.       maxY = y1
  39.     end
  40.    
  41.     local xDiff = x2 - x1
  42.     local yDiff = y2 - y1
  43.    
  44.     if xDiff > ( yDiff < 0 and -yDiff or yDiff ) then --optimization bruh
  45.       local y = minY
  46.       local dy = yDiff / xDiff
  47.       for x=minX,maxX do
  48.         self.canvas:setPixel(x, math.floor( y + 0.5), true,bg,fg )
  49.         y = y + dy
  50.       end
  51.     else
  52.       local x = minX
  53.       local dx = xDiff / yDiff
  54.       if maxY >= minY then
  55.         for y=minY,maxY do
  56.           self.canvas:setPixel( math.floor( x + 0.5 ), y, true,bg,fg )
  57.           x = x + dx
  58.         end
  59.       else
  60.         for y=minY,maxY,-1 do
  61.           self.canvas:setPixel( math.floor( x + 0.5 ), y, true,bg,fg )
  62.           x = x - dx
  63.         end
  64.       end
  65.     end
  66.   end
  67.  
  68.   function Display:setSize(w,h)
  69.     self.canvas:setSize(w,h)
  70.   end
  71.  
  72.   --TODO: Add scaling for drawImage?
  73.  
  74.   --MAJOR TODO: REWORK FOR USE WITH TERM.BLIT
  75.   -- OR SOME OPTIMIZATION, TOO SLOW
  76.   function Display:drawImage(bitmap, x, y)
  77.     local compo = bitmap.components
  78.     local bwid = bitmap.width
  79.     local canv = self.canvas
  80.     local blwin = self.blwin
  81.     local bg = bitmap.bg
  82.     local fromRGBf = colors.fromRGB
  83.     local offy = -3
  84.    
  85.     local currentT = term.redirect(blwin)
  86.     for i=1,bitmap.height do
  87.       --local off = (i-1) * 3
  88.       for j=1,bitmap.width do
  89.         offy = offy + 3
  90.         local r = compo[offy + 1]
  91.         local g = compo[offy + 2]
  92.         local b = compo[offy + 3]
  93.         --print(i..";"..j..";"..tostring(offy)..g..b)
  94.         local color = colors.white
  95.         if not (r==255 and g==255 and b==255) then
  96.           color = fromRGBf(r,g,b)
  97.         end
  98.         --[[if color == bitmap.bg then
  99.           --canv:setPixel( j+x, i+y, false, bg)
  100.         else
  101.           --canv:setPixel( j+x, i+y, true, bg, color)
  102.         end]]
  103.         term.setCursorPos(j+x, i+y)
  104.         term.setBackgroundColor(color)
  105.         --term.setTextColor(colors.red)
  106.         term.write("x")
  107.       end
  108.     end
  109.     term.redirect(currentT)
  110.     blwin.setVisible(true)
  111.     blwin.setVisible(false)
  112.   end
  113.  
  114.   function Display:flush()
  115.     --Deprecated
  116.     --self.canvas:draw()
  117.   end
  118. end
  119.  
  120. --local canvas = Canvas.create()
  121. --canvas:setPixel(1,1,true)
  122. --canvas:setPixel(3,3,true)
  123. --canvas:setPixel(3,2,true, colors.red, colors.green)
  124. --canvas:draw()
  125. --os.pullEvent("char")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement