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

[TUTORIAL] A fix for the flickering effect.

Started by IliasHDZ, 31 July 2018 - 11:34 AM
IliasHDZ #1
Posted 31 July 2018 - 01:34 PM
Fixing the flickering effect!

Introduction
This is a common thing that happens when you are making a GUI program.

Today, i will show you how to fix this effect.
This is a method i found that uses the Window (API)
- Window API
- This is why you should use Computercraft 1.6 or higher


How it works
This is very easy to understand.

First, we make a Window that is the same size as the monitor.
Then at the beginning of rendering, we will redirect to the window.
Then at the end, we will redirect back to the main terminal and we will show the window.

The code
Here we have an example code:

function render()
	paintutils.drawFilledBox(5, 5, 30, 16, colors.green)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.red)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.blue)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.black)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.blue)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.white)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.lightBlue)
end
while true do
	os.pullEvent()
  
	render()
end
If you trigger an event (for example pressing a key).
The rectangles wil start to draw and you can see the flickering effect.

To fix this, we will first add a "getSize" function at the begin of the code. This is needed to give the window the size of the monitor.
I added the "term.current" function to redirect back to the main terminal.

local w, h = term.getSize()
local currTerm = term.current()

After that, we will make the window:

local window = window.create(currTerm, 1, 1, w, h, false)

Then at the beginning of the render function, we will add this:
This is to redirect to the window (This means if we draw something now on the screen. It will be drawn in the window)

term.redirect(window)

At the end of the render function, we will add this:
This is to redirect back to the main terminal (If we draw something now. It will be drawn normally) and
to show and hide the window.

term.redirect(currTerm)
window.setVisible(true)
window.setVisible(false)

You will end up like this:

local w, h = term.getSize()

local currTerm = term.current()
local window = window.create(currTerm, 1, 1, w, h, false)

function render()
	term.redirect(window)

	paintutils.drawFilledBox(5, 5, 30, 16, colors.green)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.red)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.blue)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.black)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.blue)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.white)
	paintutils.drawFilledBox(5, 5, 30, 16, colors.lightBlue)
  
	term.redirect(currTerm)

	window.setVisible(true)
	window.setVisible(false)
end
while true do
	os.pullEvent()

	render()
end

If you now test it out.
It would normally work.

If you are having problems.
You can post everything in the comment section below.
That is it for the fix.
Edited on 01 August 2018 - 08:56 AM
magiczocker #2
Posted 01 August 2018 - 05:21 AM
You could also write a draw function like this:

local w,h=term.getSize()
for y=1,h do
  term.setCursorPos(1,y)
  for x=1,w do
	term.setBackgroundColor()
	term.setTextColor()
	term.write()
  end
end

I use this method in all of my programs to make it compatible with mostly all cc versions and not cc1.6 and higher only
Edited on 01 August 2018 - 12:27 PM