From 7446451b1b022ee440ebe3932fa79c04fc6d6ac9 Mon Sep 17 00:00:00 2001 From: cheapie Date: Fri, 26 Feb 2021 22:58:58 -0600 Subject: [PATCH] Add "drawline" command to GPU --- docs/gpu.txt | 16 +++++++++++++++- gpu.lua | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/docs/gpu.txt b/docs/gpu.txt index 80427ad..a2ea3c7 100644 --- a/docs/gpu.txt +++ b/docs/gpu.txt @@ -23,7 +23,7 @@ Parameters: buffer [integer 0-7]: The slot number to create the new buffer in. If the slot is already occupied, the existing contents will be erased. xsize [integer 1-64]: The width of the new buffer in pixels. ysize [integer 1-64]: The height of the new buffer in pixels. -color [hex color, default "000000"]: A color to fill the new buffer with. +fill [hex color, default "000000"]: A color to fill the new buffer with. Command: send ------------- @@ -133,3 +133,17 @@ x [integer 1-64]: The X position of the left side of the text. y [integer 1-64]: The Y position of the right side of the text. color [hex color, default "ff6600"]: The color of the text. text: The text string to draw. + +Command: drawline +----------------- + +Draws a line on a buffer. + +Parameters: +buffer [integer 0-7]: The buffer to draw the line on. +x1 [integer 1-64]: The X position of the start of the line. +x2 [integer 1-64]: The X position of the end of the line. +y1 [integer 1-64]: The Y position of the start of the line. +y2 [integer 1-64]: The Y position of the end of the line. +color [hex color, default "000000"]: The nominal color of the line (may not be the color of every pixel, see the "antialias" setting) +antialias [boolean, default false]: Whether to apply a (very) crude smoothing algorithm to the line to reduce jagged edges at the expense of making the line slightly blurry. diff --git a/gpu.lua b/gpu.lua index 9529002..4d0e345 100644 --- a/gpu.lua +++ b/gpu.lua @@ -268,6 +268,41 @@ local function runcommand(pos,meta,command) end end meta:set_string("buffer"..bufnum,minetest.serialize(buffer)) + elseif command.command == "drawline" then + if type(command.buffer) ~= "number" or type(command.x1) ~= "number" or type(command.y1) ~= "number" or type(command.x2) ~= "number" or type(command.y2) ~= "number" then return end + local bufnum = math.floor(command.buffer) + if bufnum < 0 or bufnum > 7 then return end + local x1 = math.min(64,math.floor(command.x1)) + local y1 = math.min(64,math.floor(command.y1)) + local x2 = math.min(64,math.floor(command.x2)) + local y2 = math.min(64,math.floor(command.y2)) + if x1 < 1 or y1 < 1 or x2 < 1 or y2 < 1 then return end + local buffer = meta:get_string("buffer"..bufnum) + if string.len(buffer) == 0 then return end + buffer = minetest.deserialize(buffer) + if type(buffer) ~= "table" then return end + x2 = math.min(x2,buffer.xsize) + y2 = math.min(y2,buffer.ysize) + local color = command.color + if type(color) ~= "string" or string.len(color) > 7 or string.len(color) < 6 then color = "000000" end + if string.sub(color,1,1) == "#" then color = string.sub(color,2,7) end + if not tonumber(color,16) then color = "000000" end + local p1 = vector.new(x1,y1,0) + local p2 = vector.new(x2,y2,0) + local length = vector.distance(p1,p2) + local dir = vector.direction(p1,p2) + if length > 0 then + for i=0,length,0.3 do + local point = vector.add(p1,vector.multiply(dir,i)) + point = vector.floor(point) + if command.antialias then + buffer[point.y][point.x] = blend(buffer[point.y][point.x],color,"average") + else + buffer[point.y][point.x] = color + end + end + end + meta:set_string("buffer"..bufnum,minetest.serialize(buffer)) elseif command.command == "drawpoint" then if type(command.buffer) ~= "number" or type(command.x) ~= "number" or type(command.y) ~= "number" then return end local bufnum = math.floor(command.buffer)