Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function createBuffer(width, height)
- local buffer = {}
- buffer["width"] = width
- buffer["height"] = height
- buffer["pixels"] = {}
- for i = 1, width * height, 1 do
- buffer["pixels"][i] = colors.black
- end
- return buffer
- end
- function clearBuffer(buffer, clearColor)
- for y = 1, buffer["height"], 1 do
- for x = 1, buffer["width"], 1 do
- setPixel(buffer, x, y, clearColor)
- end
- end
- end
- local function isOutOfBounds(buffer, x, y)
- return x < 0 or y < 0 or x > buffer["width"] or y > buffer["height"]
- end
- local function getBufferIndex(buffer, x, y)
- return y * buffer["width"] + x
- end
- function setPixel(buffer, x, y, color)
- if(isOutOfBounds(buffer, x, y)) then
- print("OOB! " .. tostring(x) .. "," .. tostring(y))
- return
- end
- local i = y * buffer["width"] + x
- buffer["pixels"][getBufferIndex(buffer, x, y)] = color
- end
- function getPixel(buffer, x, y)
- if(isOutOfBounds(buffer, x, y)) then
- print("OOB! " .. tostring(x) .. "," .. tostring(y))
- return nil
- end
- return buffer["pixels"][getBufferIndex(buffer, x, y)]
- end
- function drawLine(buffer, x1, y1, x2, y2, color)
- delta_x = x2 - x1
- ix = delta_x > 0 and 1 or -1
- delta_x = 2 * math.abs(delta_x)
- delta_y = y2 - y1
- iy = delta_y > 0 and 1 or -1
- delta_y = 2 * math.abs(delta_y)
- setPixel(buffer, x1, y1, color)
- if delta_x >= delta_y then
- error = delta_y - delta_x / 2
- while x1 ~= x2 do
- if (error >= 0) and ((error ~= 0) or (ix > 0)) then
- error = error - delta_x
- y1 = y1 + iy
- end
- error = error + delta_y
- x1 = x1 + ix
- setPixel(buffer, x1, y1, color)
- end
- else
- error = delta_x - delta_y / 2
- while y1 ~= y2 do
- if (error >= 0) and ((error ~= 0) or (iy > 0)) then
- error = error - delta_y
- x1 = x1 + ix
- end
- error = error + delta_x
- y1 = y1 + iy
- setPixel(buffer, x1, y1, color)
- end
- end
- end
- function drawToMonitor(buffer, monitor)
- local col = colors.black
- local prevCol = col
- for y = 1, buffer["height"], 1 do
- for x = 1, buffer["width"], 1 do
- col = getPixel(buffer, x, y)
- print(tostring(x) .. "," .. tostring(y) .. ":" .. tostring(col))
- if(col ~= prevCol) then
- monitor.setBackgroundColor(col)
- prevCol = col
- end
- monitor.setCursorPos(x, y)
- monitor.write(" ")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment