This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ElmuKelmuZ's profile picture

Paintutils help

Started by ElmuKelmuZ, 19 July 2014 - 11:13 PM
ElmuKelmuZ #1
Posted 20 July 2014 - 01:13 AM
I'm trying to write a function that would use the paintutils to draw a box of a certain size at a certain position.
However this is what happens with my code:





local Vector2 = {}
Vector2.new = function(xv,yv)
return {x = (xv or 0), y = (yv or 0)}
end

function win.drawBox(pos, size, c)
local xp = pos.x
local yp = pos.y
local xs = size.x
local ys = size.y
local bgc = termbgc
for currentX = xp, xs do
  local currentY = yp
  paintutils.drawPixel(
   currentX,
   currentY,
   c
  )
  for cy = currentY, ys do
   paintutils.drawPixel(
    currentX,
    cy,
    c
   )
  end
end
return {
  bgc,
  xp,
  yp,
  xs,
  ys,
  hide = function(this)
   for currentX = this.xp, this.xs do
    local currentY = this.yp
    paintutils.drawPixel(
    currentX,
    currentY,
    bgc
   )
   for cy = currentY, this.ys do
	 paintutils.drawPixel(
	 currentX,
	 this.cy,
	 bgc
    )
   end
  end
}
end

local bp = Vector2.new(1,1)
local bs = Vector2.new(5,5)
win.drawBox(bp, bs, colors.white)
Bomb Bloke #2
Posted 20 July 2014 - 01:48 AM
At a glance, the logic looks a bit messy, but it should indeed produce a filled rectangle. But it looks like you've chopped this out of a longer script - there's not declaration of the "win" table nor the "termbgc" variable in there - so I'm wagering the stuff you've omitted is causing your issue.

But just for kicks, here's another version of the box drawing function:

function win.drawBox(pos, size, c)
	local boxStats = {["bgc"] = termbgc,["xp"] = pos.x,["yp"] = pos.y,["xs"] = size.x,["ys"] = size.y}
	for i = boxStats.xp, boxStats.xs do paintutils.drawLine(i,boxStats.yp,i,boxStats.ys,c) end
	return {["hide"] = function() for i = boxStats.xp, boxStats.xs do paintutils.drawLine(i,boxStats.yp,i,boxStats.ys,boxStats.bgc) end end}
end
ElmuKelmuZ #3
Posted 20 July 2014 - 02:51 AM
Thanks, it works. however I just found out about the window API, would I be better off with that instead?
Bomb Bloke #4
Posted 20 July 2014 - 08:22 AM
Yes and no.

The window API allows you to easily create display buffers which can be redrawn on demand and even moved around the screen. While it's pretty convenient for a lot of tasks, it executes slowly and there are some limits as to what it can do (there's no "undraw" functionality, for example: the best you can do is redraw another window over the top of the one you want to hide, like in the example here).

So, ultimately you're better off writing your own code to suit your needs - but the window API will let you get "up and running" relatively quickly, and odds are it'll suit your needs just fine. It's not difficult to move away from it if you later find you need to.