Skip to content

Create fifvm-buffer-microptimized.lua #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions fifvm-buffer-microptimized.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
local gpu = require('component').gpu

local gpu_set, gpu_setbg, gpu_setfg, gpu_fill,gpu_bitblt = gpu.set, gpu.setBackground, gpu.setForeground, gpu.fill, gpu.bitblt
local unicode_char = require('unicode').char
local string_sub, string_byte = string.sub, string.byte
local table_concat = table.concat

local handle = io.open((...) or (function() print("No input file provided") return os.exit() end)(), "rb")

handle:read(6)
local w,h = string_byte(handle:read(1)), string_byte(handle:read(1))
gpu.setResolution(w,h)
local buff = gpu.allocateBuffer(w, h)
gpu.setActiveBuffer(buff)

local i = 1
local data = {}
while true do
local s = handle:read(1024)
if s ~= nil then
data[i] = s
i = i + 1
else break end
end
data = table_concat(data)

local offset = 1
local gpustring = {} -- reusage
local function generatePI(pixelInfo)
local len = #pixelInfo
local gpustring = gpustring
for i=1,len do
gpustring[i] = unicode_char(string_byte(string_sub(pixelInfo, i, i)) + 0x2800)
end
return table_concat(gpustring,"",1,len)
end

while true do
local instruction = string_byte(string_sub(data, offset, offset), 1)
offset = offset + 1
if instruction == 0x01 then
local data = string_sub(data, offset, offset + 2)
offset = offset + 3
gpu_setbg(
string_byte(data, 1) * 0x10000 + -- r
string_byte(data, 2) * 0x100 + -- g
string_byte(data, 3) ) -- b
elseif instruction == 0x02 then
local data = string_sub(data, offset, offset + 2)
offset = offset + 3
gpu_setfg(
string_byte(data, 1) * 0x10000 + -- r
string_byte(data, 2) * 0x100 + -- g
string_byte(data, 3) ) -- b
elseif instruction == 0x10 then
local cords = string_sub(data, offset, offset + 1)
offset = offset + 2
local size = string_byte(string_sub(data, offset, offset))
offset = offset + 1
gpu_set(
string_byte(cords, 1) + 1, -- x
string_byte(cords, 2) + 1, -- y
generatePI(string_sub(data, offset, offset + size - 1))) -- string
offset = offset + size
elseif instruction == 0x11 then
local cords = string_sub(data, offset, offset + 4)
offset = offset + 5
gpu_fill(
string_byte(cords, 1) + 1, -- x
string_byte(cords, 2) + 1, -- y
string_byte(cords, 3), -- w
string_byte(cords, 4), -- h
unicode_char(0x2800 + string_byte(cords, 5))) -- filler
elseif instruction == 0x12 then
offset = offset + 1
gpu_bitblt(0, 1, 1, w, h, buff)
elseif instruction == 0x13 then
local cords = string_sub(data, offset ,offset + 1)
offset = offset + 2
local size = string_byte(string_sub(data,offset,offset))
offset = offset + 1
gpu.set(
string_byte(cords, 1) + 1, -- x
string_byte(cords, 2) + 1, -- y
generatePI(string_sub(data, offset, offset + size - 1)), true) -- string, isVertical
offset = offset + size
elseif instruction == 0x20 then break end
end

gpu.setActiveBuffer(0)
gpu_bitblt(0, 1, 1, w, h, buff)
gpu.freeBuffer(buff)