Advertisement
Ewgeniy

Untitled

Feb 7th, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.09 KB | None | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local gpu = component.gpu
  4. local computer = require("computer")
  5. local tape = component.proxy("tape_drive")
  6.  
  7. COLMAP = {}
  8. local i
  9. for i=0,15 do
  10.     local w = i*255/15
  11.     COLMAP[i] = (w<<16)|(w<<8)|w
  12.     gpu.setPaletteColor(i, COLMAP[i])
  13. end
  14. for i=0,240-1 do
  15.     local r = i%6
  16.     local g = (i//6)%8
  17.     local b = (i//(6*8))
  18.  
  19.     r = (r*255+2)//5
  20.     g = (g*255+3)//7
  21.     b = (b*255+2)//4
  22.  
  23.     COLMAP[i+16] = (r<<16)|(g<<8)|b
  24. end
  25.  
  26. if tape and tape.isReady() then
  27.     tape.stop()
  28.     while tape.seek(-tape.getSize()) ~= 0 do
  29.         os.sleep(0.05)
  30.         tape.stop()
  31.     end
  32.     tape.stop()
  33. end
  34.  
  35. local fname = ...
  36. local fp = io.open(fname, "rb")
  37.  
  38. local W = fp:read(1):byte()
  39. local H = fp:read(1):byte()
  40. gpu.setResolution(W, H)
  41. gpu.setBackground(0x000000)
  42. gpu.setForeground(0xFFFFFF)
  43. term.clear()
  44.  
  45. if sysnative then
  46.     tlast = os.clock()
  47. else
  48.     os.sleep(0.05)
  49.     if tape and tape.isReady() then
  50.         tape.play()
  51.         os.sleep(1.0) -- deal to sound latency
  52.     end
  53.     tlast = computer.uptime()
  54. end
  55.  
  56. local delay_acc = 0.0
  57. local function delay(d)
  58.     assert(d >= 0.0)
  59.     delay_acc = delay_acc + d
  60.     local dquo = math.floor(delay_acc / 0.05) * 0.05
  61.     delay_acc = delay_acc - dquo
  62.     os.sleep(dquo)
  63. end
  64.  
  65. while true do
  66.     local s = fp:read(1)
  67.     if s == "" or s == nil then
  68.         break
  69.     end
  70.     local c = s:byte()
  71.  
  72.     if c == 0xFF then
  73.         tnow = computer.uptime()
  74.         tlast = tlast + 0.05
  75.         while tnow < tlast do
  76.             --delay(tlast-tnow)
  77.             os.sleep(tlast-tnow)
  78.             tnow = computer.uptime()
  79.         end
  80.     elseif c == 0x00 then
  81.         local col = COLMAP[fp:read(1):byte()]
  82.         gpu.setBackground(col)
  83.     else
  84.         local by = fp:read(1):byte()
  85.         local bx = fp:read(1):byte()
  86.         local bw = fp:read(1):byte()
  87.         local bh = c & 0x3F
  88.         local type = c & 0x40
  89.         if type == 0x40 then
  90.             gpu.fill(bx, by, bw, bh, " ")
  91.         else
  92.             if bh < bw then
  93.                 for i = 0, bh-1 do
  94.                     gpu.set(bx, by + i, string.rep(" ", bw), false)
  95.                 end
  96.             else
  97.                 for i = 0, bw - 1 do
  98.                     gpu.set(bx + i, by, string.rep(" ", bh), true)
  99.                 end
  100.             end
  101.         end
  102.     end
  103. end
  104.  
  105. gpu.setBackground(0x000000)
  106. gpu.setForeground(0xFFFFFF)
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement