jtmm2/levels/generate_c.lua

63 lines
1.5 KiB
Lua
Executable File

#!/usr/bin/env luajit
local function read_level(id)
local file = io.open(id..".jtmm2")
if not file then
return nil
else
local content = {width = nil, height = nil, layers = {{}}}
local line_nb = 0
local layer_nb = 1
for line in file:lines() do
line_nb = line_nb + 1
if line_nb < 3 then
if content.width then
content.height = tonumber(line)
else
content.width = tonumber(line)
end
else
if line == "n" then
layer_nb = layer_nb + 1
table.insert(content.layers, {})
else
table.insert(content.layers[layer_nb], line)
end
end
end
file:close()
return content
end
end
local function create_structure_c(id)
local content = read_level(id)
if content then
-- layers
for i, layer in ipairs(content.layers) do
io.write("const unsigned char tiles_", id, "_", i, "[] = {\n\t")
for i, v in ipairs(layer) do
io.write(v, ", ")
if i % 14 == 0 and i ~= #layer then
io.write("\n\t")
end
end
io.write("\n};\n");
end
-- array
io.write("const unsigned char *layers_", id, "[] = {")
for i = 1, #content.layers, 1 do
io.write("tiles_", id, "_", i, ", ")
end
io.write("};\n")
-- structure
io.write("const Level level_", id, " = {\n")
io.write("\t.width = ", content.width, ",\n")
io.write("\t.height = ", content.height, ",\n")
io.write("\t.layers = &layers_", id, "\n")
io.write("};\n")
end
end
create_structure_c(0)