NielsUtrecht

hydraGfxApi

Sep 9th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | None | 0 0
  1. function createBuffer(width, height)
  2.     local buffer = {}
  3.     buffer["width"] = width
  4.     buffer["height"] = height
  5.     buffer["pixels"] = {}
  6.    
  7.     for i = 1, width * height, 1 do
  8.         buffer["pixels"][i] = colors.black
  9.     end
  10.    
  11.     return buffer
  12. end
  13.  
  14. function clearBuffer(buffer, clearColor)
  15.     for y = 1, buffer["height"], 1 do
  16.         for x = 1, buffer["width"], 1 do
  17.             setPixel(buffer, x, y, clearColor)
  18.         end
  19.     end
  20. end
  21.  
  22. local function isOutOfBounds(buffer, x, y)
  23.     return x < 0 or y < 0 or x > buffer["width"] or y > buffer["height"]
  24. end
  25.  
  26. local function getBufferIndex(buffer, x, y)
  27.     return y * buffer["width"] + x
  28. end
  29.  
  30. function setPixel(buffer, x, y, color)
  31.     if(isOutOfBounds(buffer, x, y)) then
  32.         print("OOB! " .. tostring(x) .. "," .. tostring(y))
  33.         return
  34.     end
  35.    
  36.     local i = y * buffer["width"] + x
  37.     buffer["pixels"][getBufferIndex(buffer, x, y)] = color
  38. end
  39.  
  40. function getPixel(buffer, x, y)
  41.     if(isOutOfBounds(buffer, x, y)) then
  42.         print("OOB! " .. tostring(x) .. "," .. tostring(y))
  43.         return nil
  44.     end
  45.    
  46.     return buffer["pixels"][getBufferIndex(buffer, x, y)]
  47. end
  48.  
  49. function drawLine(buffer, x1, y1, x2, y2, color)
  50.     delta_x = x2 - x1
  51.     ix = delta_x > 0 and 1 or -1
  52.     delta_x = 2 * math.abs(delta_x)
  53.  
  54.     delta_y = y2 - y1
  55.     iy = delta_y > 0 and 1 or -1
  56.     delta_y = 2 * math.abs(delta_y)
  57.  
  58.     setPixel(buffer, x1, y1, color)
  59.  
  60.     if delta_x >= delta_y then
  61.         error = delta_y - delta_x / 2
  62.  
  63.         while x1 ~= x2 do
  64.             if (error >= 0) and ((error ~= 0) or (ix > 0)) then
  65.                 error = error - delta_x
  66.                 y1 = y1 + iy
  67.             end
  68.      
  69.             error = error + delta_y
  70.             x1 = x1 + ix
  71.      
  72.             setPixel(buffer, x1, y1, color)
  73.         end
  74.     else
  75.         error = delta_x - delta_y / 2
  76.  
  77.         while y1 ~= y2 do
  78.             if (error >= 0) and ((error ~= 0) or (iy > 0)) then
  79.                 error = error - delta_y
  80.                 x1 = x1 + ix
  81.             end
  82.      
  83.             error = error + delta_x
  84.             y1 = y1 + iy
  85.      
  86.             setPixel(buffer, x1, y1, color)
  87.         end
  88.     end
  89. end
  90.  
  91. function drawToMonitor(buffer, monitor)
  92.     local col = colors.black
  93.     local prevCol = col
  94.     for y = 1, buffer["height"], 1 do
  95.         for x = 1, buffer["width"], 1 do
  96.             col = getPixel(buffer, x, y)
  97.             print(tostring(x) .. "," .. tostring(y) .. ":" .. tostring(col))
  98.             if(col ~= prevCol) then
  99.                 monitor.setBackgroundColor(col)
  100.                 prevCol = col
  101.             end
  102.             monitor.setCursorPos(x, y)
  103.             monitor.write(" ")
  104.         end
  105.     end
  106. end
Advertisement
Add Comment
Please, Sign In to add comment