Skip to content

Instantly share code, notes, and snippets.

@drhayes
Last active March 5, 2024 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drhayes/374524e77c0f108e85e4b2003fc14c41 to your computer and use it in GitHub Desktop.
Save drhayes/374524e77c0f108e85e4b2003fc14c41 to your computer and use it in GitHub Desktop.
A lightweight REXPaint .xp file reader for LOVE2D.
--
-- A lightweight REXPaint .xp reader for LOVE2D.
-- By Vriska Serket / @arachonteur
-- Adapted by drhayes to not use self, make constants uppercase,
-- and updated to work with love 11.2.
-- From https://gist.github.com/vriska-serket/334bfcfa7dfe7265ddbe089e4a51e522
--
-- Output table looks like this.
--[[
{
version = 0,
layerCount = 0,
layers = {
[1] = {
width = 20,
height = 20,
index = 1,
cells = {
[1] = {
x = 0,
y = 0,
char = 0,
fg = {255, 255, 255},
bg = {0, 0, 0}
},
[...]
}
},
[...]
}
}
]]
--
--
local VERSION_BYTES = 4
local LAYER_COUNT_BYTES = 4
local LAYER_WIDTH_BYTES = 4
local LAYER_HEIGHT_BYTES = 4
local LAYER_KEYCODE_BYTES = 4
local LAYER_FOREGROUND_RGB_BYTES = 3
local LAYER_BACKGROUND_RGB_BYTES = 3
local function read(file)
local fileString = love.data.decompress('string', 'gzip', file)
local xp = {
version = 0,
layerCount = 0,
layers = {}
}
local offset = 0
local version = { fileString:byte(offset, VERSION_BYTES) }
offset = offset + VERSION_BYTES + 1
local layer_count = { fileString:byte(offset, offset + LAYER_COUNT_BYTES) }
offset = offset + LAYER_COUNT_BYTES
xp.version = version
xp.layerCount = layer_count[1]
for i = 1, layer_count[1] do
local layerWidth = fileString:byte(offset, offset + LAYER_WIDTH_BYTES)
offset = offset + LAYER_WIDTH_BYTES
local layerHeight = fileString:byte(offset, offset + LAYER_HEIGHT_BYTES)
offset = offset + LAYER_HEIGHT_BYTES
local layer = {
index = i,
width = layerWidth,
height = layerHeight,
cells = {}
}
for j = 0, (layerWidth * layerHeight - 1) do
local cell = {
x = 0,
y = 0,
char = 0,
fg = {
r = 255,
g = 255,
b = 255
},
bg = {
r = 255,
g = 255,
b = 255
}
}
cell.y = j % layer.height
cell.x = (j - cell.y) / layer.height
local tempOffset = offset
local keycode = { fileString:byte(tempOffset, tempOffset + LAYER_KEYCODE_BYTES - 1) }
cell.char = string.char(keycode[1])
tempOffset = tempOffset + LAYER_KEYCODE_BYTES
local fg = { fileString:byte(tempOffset, tempOffset + LAYER_FOREGROUND_RGB_BYTES - 1) }
cell.fg = fg
tempOffset = tempOffset + LAYER_FOREGROUND_RGB_BYTES
local bg = { fileString:byte(tempOffset, tempOffset + LAYER_BACKGROUND_RGB_BYTES - 1) }
cell.bg = bg
tempOffset = tempOffset + LAYER_BACKGROUND_RGB_BYTES
offset = tempOffset
table.insert(layer.cells, cell)
end
table.insert(xp.layers, layer)
end
return xp
end
return read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment