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

[1.6][SMP] Screens flickering badly

Started by Sukasa, 02 July 2014 - 12:54 AM
Sukasa #1
Posted 02 July 2014 - 02:54 AM
I'm playing SMP, and the server is on the same LAN as my client. I have a program that does some charting on a large advanced monitor which uses term.clear() once every tick, followed by a large number of single-character draws, and in 1.5 it worked fine - the monitor animated smoothly, there was no flicker, etc. However in CC 1.6 the monitor flickers incredibly badly, rendering the chart nearly unreadable. It appears like the draws are not all completed, so that it renders with the clear() having occurred and only a few of the writes to the screen after that having been completed before the texture is rendered.

The code running on the computer (both in 1.5 and in 1.6) is the following:


historyLength = 50
sampleInterval = 0.05
drawTarget = peripheral.wrap("top")
drawTarget.setTextScale(0.5)
local x,y = drawTarget.getSize()
historyLength = x
boilers = {}
loadHistory = {}
-- Scans the network for any Railcraft boiler and adds it to the Boilers list
function getBoilers()
  boilers = {}
  local hosts = peripheral.getNames()
  for index, hostName in pairs(hosts) do
    if hostName:find("rcboilertank") == 1 then
	  table.insert(boilers, peripheral.wrap(hostName))
    end
  end
end
function getInstantaneousLoad()
  local steamLoad = 0
  local numTanks = 0
 
  for index, boiler in pairs(boilers) do
   local steamTank = boiler.getTankInfo("top")[2]
   local steamAdjust = steamTank.capacity / 2
  
   if (steamTank.amount > steamAdjust) then
    local tankLoad = (steamTank.amount - steamAdjust) / steamAdjust
   
    steamLoad = steamLoad + (1 - tankLoad)
    numTanks = numTanks + 1
   end
  end
  --print("Load:")
  --print(steamLoad / numTanks)
  return steamLoad / numTanks
end
function trackLoadHistory(newLoad)
  if #loadHistory > historyLength then
    table.remove(loadHistory, 1)
  end
  --table.insert(loadHistory, newLoad)
  loadHistory[#loadHistory + 1] = newLoad
end
function drawLoadChart(target)
  target.clear()
  targetWidth, targetHeight = target.getSize()
 
 
  for x = 1,targetWidth do
    if #loadHistory >= (x) then
	  local maxY = math.floor(loadHistory[x] * targetHeight)
	 
	  for barY = 1, maxY do
	    local drawColor
	   
	    if (target.isColor()) then
		  if (barY < .2 * targetHeight) then drawColor = colors.green
		  elseif barY < .4 * targetHeight then drawColor = colors.lime
		  elseif barY < .6 * targetHeight then drawColor = colors.yellow
		  elseif barY < .8 * targetHeight then drawColor = colors.orange
		  else drawColor = colors.red
		  end
	    else
		  drawColor = colors.white
	    end
	   
	    target.setBackgroundColor(drawColor)
	    target.setCursorPos(x, targetHeight - barY + 1)
	    target.write(" ")
	   
	  end
    end
  end
 
  local load = math.floor(loadHistory[#loadHistory] * 100)
  target.setCursorPos(1, 1)
  target.setBackgroundColor(colors.black)
  target.write("Current steam load: "..load.."%")
end

sleep(1) -- Initialization delay for chunk loading
getBoilers()
while true do
  sleep(sampleInterval)
  trackLoadHistory(getInstantaneousLoad())
  drawLoadChart(drawTarget)
end
Cranium #2
Posted 02 July 2014 - 02:57 AM
This is actually something that's not easily fixed on the back end, but easily remedied by your own code. Instead of clearing the entire screen on every tick, you can only redraw the information that changes.
Sukasa #3
Posted 02 July 2014 - 03:15 AM
I'll have to come up with a different UI, then. This tiny-height screen works for now, but.. it misses a lot of the info a larger screen showed.


Thanks.
Edited on 02 July 2014 - 01:16 AM
theoriginalbit #4
Posted 02 July 2014 - 03:43 AM
Alternatively you could just search for a screen buffer API and use that, it will only render what is needed and will remove your screen flicker.
Bomb Bloke #5
Posted 02 July 2014 - 02:22 PM
I'd skip the screen clear, and draw black vertical lines from the top of each coloured bar to the top of the screen.