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

Loading Screen not working

Started by 132ikl, 06 February 2015 - 12:50 AM
132ikl #1
Posted 06 February 2015 - 01:50 AM
I am making a program to connect to tny.cz (like pastebin) using the http API. But the reason I'm asking about this is my loading screen. When I start the program, the loading screen flashes. I want the loading circle to sort of spin.

loading = 5
loadStage = 0
printActive = false
rednet.open("top")

function refreshLink()
	h = http.get("http://tny.cz/57e18bf1")
	y = h.readAll()
end

function checkPaste()
	sleep(0)
	if string.find(y,"ason") then
		 autoSpawner = true
	else
	autoSpawner = false
	end
end

function getPrintableVars()
	if autoSpawner == true then
		autoSpawnerPrint = "on"
		autoSpawnColor = colors.lime
	else
	autoSpawnerPrint = "off"
	autoSpawnColor = colors.red
	end
end

function printState()
	printActive = true
	term.clear()
	term.setCursorPos(1,1)
	term.setTextColor(autoSpawnColor)
	print("Auto Spawner is ".. autoSpawnerPrint)
	sleep(0)
	printActive = false
end

function loading()
   loading = 5
   repeat
		refreshLink()
		checkPaste()
		getPrintableVars()
		screenX, screenY = term.getSize()
		screenX = screenX/2
		screenY = screenY/2
		term.clear()
		term.setCursorPos(screenX-14,screenY-1)
		term.setTextColor(colors.yellow)
		print("InternetCraft Version Beta 1.0")
		term.setCursorPos(screenX-3,screenY)
		.setTextColor(colors.red)
		print("Loading...")
		stage()
		loadingCircle()
		loading = loading - 1
	until loading == 0
end

function stage()
	sleep(0)
	loadStage = loadStage + 1
	if loadStage == 5 then
		loadStage = 1
	end
	loadColor1 = colors.white
	loadColor2 = colors.white
	loadColor3 = colors.white
	loadColor4 = colors.white
	if loadStage == 1 then
		loadColor1 = colors.gray
		elseif loadStage == 2 then
		loadColor2 = colors.gray
		elseif loadStage == 3 then
		loadColor3 = colors.gray
		elseif loadStage == 4 then
		loadColor4 = colors.gray
	end
end

function loadingCircle()
	stage()
	term.setBackgroundColor(loadColor1)
	term.setCursorPos(screenX-1,screenY+2)
	print(" ")
	term.setBackgroundColor(loadColor2)
	term.setCursorPos(screenX+1,screenY+2)
	print(" ")
	term.setBackgroundColor(loadColor4)
	term.setCursorPos(screenX-1,screenY+4)
	print(" ")
	term.setBackgroundColor(loadColor3)
	term.setCursorPos(screenX+1,screenY+4)
	print(" ")
	term.setBackgroundColor(colors.black)
end

loading()

while true do
	if loading == 0 then
		checkPaste()
		getPrintableVars()
		sleep(1)
		refreshLink()
			if printActive == false then
				printState()
			end
	  end
end

Bomb Bloke #2
Posted 06 February 2015 - 03:41 AM
Long story short, executing code takes time. The more operations you have your script perform in between clearing the screen and getting your page re-drawn, the more time the screen will spend blank. This leads to a noticeable "flash" effect.

I get the impression that all you really need to do is get rid of the sleep(0) in your stage() function. sleep works by rigging up a timer and waiting for it to expire - the minimum timer duration is a twentieth of a second (or one MineCraft server tick).

I'm not sure why you're calling stage() directly before loadingCircle(), then calling it again first thing once loadingCircle() starts. Together, that adds up to a tenth of a second delay.

If you must sleep, do so directly before clearing the screen.