Advertisement
BombBloke

Blit (ComputerCraft)

Jun 11th, 2017
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. -- pastebin get bvx6Tipk blit
  2.  
  3. -- Plays animations.
  4. -- After loading a GIF and processing it, saves the result so as to take
  5. -- less disk space and reduce load times for subsequent playbacks.
  6.  
  7. local timePerImage = 30
  8.  
  9. local function loadAPI(API, paste)
  10.     if not _G[API] then
  11.         if not (fs.exists(API) or fs.exists(shell.resolve(API))) then
  12.             shell.run("pastebin get " .. paste .. " " .. API)
  13.             os.loadAPI(shell.resolve(API))
  14.         else os.loadAPI(fs.exists(API) and API or shell.resolve(API)) end
  15.     end
  16. end
  17.  
  18. loadAPI("bbpack",  "cUYTGbpb")
  19. loadAPI("GIF",     "5uk9uRjC")
  20. loadAPI("blittle", "ujchRSnU")
  21.  
  22. local GIFlist, BLTlist = fs.list(shell.resolve("")), {}
  23.  
  24. for i = #GIFlist, 1, -1 do
  25.     local ext = GIFlist[i]:sub(-4):lower()
  26.     if ext == ".blt" then
  27.         BLTlist[#BLTlist + 1] = table.remove(GIFlist, i)
  28.     elseif ext ~= ".gif" then
  29.         table.remove(GIFlist, i)
  30.     end
  31. end
  32.  
  33. local mon = peripheral.find("monitor") or term
  34. if mon.setTextScale then mon.setTextScale(0.5) end
  35. local MonSizeX, MonSizeY = mon.getSize()
  36. MonSizeX, MonSizeY = MonSizeX * 2, MonSizeY * 3
  37.  
  38. parallel.waitForAll(
  39.     function()
  40.         if #BLTlist == 0 then os.pullEvent("madeBLT") end
  41.        
  42.         while true do
  43.             local img = blittle.load(shell.resolve(BLTlist[math.random(#BLTlist)]))
  44.            
  45.             if img.pal and GIF.applyPalette then GIF.applyPalette(img, mon) end
  46.             mon.clear()
  47.  
  48.             local x, y = math.floor((MonSizeX / 2 - img[1].width) / 2) + 1, math.floor((MonSizeY / 3 - img[1].height) / 2) + 1
  49.             if #img == 1 then img[1].delay = timePerImage end
  50.  
  51.             repeat
  52.                 local frameTimer, nextTimer, curFrame = os.startTimer(0), os.startTimer(timePerImage), 1
  53.  
  54.                 while true do
  55.                     local event, par1 = os.pullEvent()
  56.  
  57.                     if event == "timer" then
  58.                         if par1 == frameTimer then
  59.                             local frame = img[curFrame]
  60.                             blittle.draw(type(frame[1]) == "number" and img[frame[1]] or frame, x, y, mon)
  61.                             frameTimer = os.startTimer(img[curFrame].delay)
  62.  
  63.                             if curFrame == #img then
  64.                                 if not nextTimer then break end
  65.                                 curFrame = 1
  66.                             else curFrame = curFrame + 1 end
  67.                         elseif par1 == nextTimer then
  68.                             nextTimer = false
  69.                         end
  70.                     elseif event == "char" or event == "mouse_click" or event == "monitor_touch" then
  71.                         break
  72.                     end
  73.                 end
  74.             until #BLTlist > 1
  75.         end
  76.     end,
  77.  
  78.     function()
  79.         for i = 1, #GIFlist do
  80.             local file = GIFlist[i]
  81.  
  82.             write(file)
  83.  
  84.             local ok, img = pcall(GIF.loadGIF, shell.resolve(file), term.setPaletteColour and true)
  85.  
  86.             if ok then
  87.                 if MonSizeX < img.width or MonSizeY < img.height then
  88.                     if MonSizeX < math.floor(MonSizeY / img.height * img.width) then
  89.                         img = GIF.resizeGIF(img, MonSizeX)
  90.                     else
  91.                         img = GIF.resizeGIF(img, nil, MonSizeY)
  92.                     end
  93.                 end
  94.  
  95.                 img = blittle.shrinkGIF(img)
  96.                
  97.                 print(" - writing...")
  98.                 fs.delete(shell.resolve(file))
  99.                 file = file:sub(1, -5) .. ".blt"
  100.                 blittle.save(img, shell.resolve(file))
  101.                 os.queueEvent("madeBLT")
  102.                 BLTlist[#BLTlist + 1] = file
  103.             else
  104.                 printError(img)
  105.             end
  106.         end
  107.        
  108.         print("All GIFs processed.")
  109.         GIFlist = nil
  110.     end
  111. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement