Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local byte = string.byte --chr, index
- local char = string.char
- local concat = table.concat --table, separator, [i], [j]
- local min = math.min
- local unpack = table.unpack
- local mon = peripheral.find("monitor")
- assert(mon, "no monitor found")
- local function resolveInvLine(s)
- local chars = {}
- for i=1, #s do
- chars[#chars+1] = char(319-byte(s,i))
- end
- return concat(chars,"")
- end
- local function writeWrap(monitor, str, col, row, w)
- local f = 1
- local len = str:len()
- local diff = w-col+1
- local len2 = min(diff,len)
- local line = str:sub(f,f+len2-1)
- monitor.write(line)
- f=f+len2
- col=col+len2
- if len-len2 ~= 0 then
- for i=f, len, w do
- col=1
- row=row+1
- monitor.setCursorPos(1,row)
- line = str:sub(i,i+w-1)
- monitor.write(line)
- col=col+line:len()
- end
- if col > w then
- col=1
- row=row+1
- monitor.setCursorPos(1,row)
- end
- end
- return col, row
- end
- local function drawFrame(monitor, inputFile, _w, _h)
- if monitor.setTextScale then
- monitor.setTextScale(0.5)
- end
- local w, h = monitor.getSize()
- if _w then w = _w end
- if _h then h = _h end
- local str = inputFile:read(w*h)
- if not str then return false end
- local length = str:len()
- local inverted = byte(str,1) >= 160
- local firstIsInverted = inverted
- local i, j = 0, 1
- local chunks = {}
- while i <= length do
- while not inverted do
- i=i+1
- if i > length then break end
- if byte(str,i) >= 160 then
- chunks[#chunks+1] = str:sub(j,i-1)
- j=i
- inverted = true
- end
- end
- while inverted do
- i=i+1
- if i > length then break end
- if byte(str,i) < 160 then
- chunks[#chunks+1] = resolveInvLine(str:sub(j,i-1))
- j=i
- inverted = false
- end
- end
- end
- if i ~= j then
- if not inverted then
- chunks[#chunks+1] = str:sub(j,i-1)
- else
- chunks[#chunks+1] = resolveInvLine(str:sub(j,i-1))
- end
- end
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- --causes bottom-side screen artifacts
- --in the event of render lag
- --monitor.clear()
- inverted = false
- if firstIsInverted then
- monitor.setBackgroundColor(colors.white)
- monitor.setTextColor(colors.black)
- inverted = true
- end
- local col, row = 1, 1
- monitor.setCursorPos(1,1)
- for c=1, #chunks do
- col, row = writeWrap(monitor, chunks[c], col, row, w)
- inverted = not inverted
- if not inverted then
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- else
- monitor.setBackgroundColor(colors.white)
- monitor.setTextColor(colors.black)
- end
- end
- return true
- end
- local argv = {...}
- if not argv[1] then argv[1] = "konosuba_20.kv3" end
- local file = io.open(argv[1], "rb")
- assert(file, "failed to open file")
- local nolag = true
- while true do
- local timeStart = os.time()
- if not drawFrame(mon, file) then break end
- local timeDiff = os.time()-timeStart
- print(nolag, timeDiff)
- if timeDiff < 0.000001 then
- os.sleep(0.05) --1 tick
- else
- nolag = false
- end
- end
- file:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement