removed maps, tilesets, and all utilities except the packer

This commit is contained in:
Andre Leiradella 2015-08-26 00:22:11 -03:00
parent f7781b7c70
commit f7276abc98
14 changed files with 0 additions and 1504 deletions

View File

@ -1,16 +0,0 @@
local writer = require 'writer'
local mkrle = require 'mkrle'
return function( images, limit )
local out = writer()
out:add16( #images )
for _, png in ipairs( images ) do
local rle = mkrle( png, limit )
out:add32( rle:getsize() )
out:addwriter( rle )
end
return out
end

View File

@ -1,115 +0,0 @@
local image = require 'image'
local rgbto16 = require 'rgbto16'
local writer = require 'writer'
local function getpixel( png, x, y, transp )
local r, g, b, a = image.split( png:getPixel( x, y ) )
local c = rgbto16( r, g, b )
if c == transp then
r, g, b, a = 0, 0, 0, 0
end
if a >= 0 and a <= 31 then
a = 0 -- transparent
elseif a >= 32 and a <= 95 then
a = 1 -- 25%
elseif a >= 96 and a <= 159 then
a = 2 -- 50%
elseif a >= 160 and a <= 223 then
a = 3 -- 75%
else
a = 4 -- opaque
end
return rgbto16( r, g, b ), a
end
local function rlerow( png, y, limit, transp )
local rle = {}
local width = png:getWidth()
local used = 0
local numcols = ( width + ( limit - 1 ) ) // limit
for i = 1, numcols do
rle[ #rle + 1 ] = 0
end
for xx = 0, width - 1, limit do
rle[ xx // limit + 1 ] = #rle
local x = xx
rle[ #rle + 1 ] = 0
local runs = #rle
while x < ( xx + limit ) and x < width do
local _, a = getpixel( png, x, y, transp )
local count = 0
local xsave = x
while x < ( xx + limit ) and x < width do
local c, aa = getpixel( png, x, y, transp )
if aa ~= a then
break
end
count = count + 1
x = x + 1
end
rle[ #rle + 1 ] = ( a << 13 | count )
rle[ runs ] = rle[ runs ] + 1
if a ~= 0 then
for i = 0, count - 1 do
local c = getpixel( png, xsave + i, y, transp )
rle[ #rle + 1 ] = c
used = used + 1
end
end
end
end
local final = writer()
for i = 1, #rle do
final:add16( rle[ i ] )
end
return final, used
end
return function( png, limit, transp )
local width, height = png:getSize()
local rows = {}
local used = 0
limit = limit or 1e10
for y = 0, height - 1 do
local row, u = rlerow( png, y, limit, transp )
rows[ y ] = row
used = used + u
end
local rle = writer()
rle:add16( width )
rle:add16( height )
rle:add32( used )
local count = 0
for y = 0, height - 1 do
rle:add32( count )
count = count + rows[ y ]:getsize()
end
for y = 0, height - 1 do
rle:addwriter( rows[ y ] )
end
return rle
end

View File

@ -1,19 +0,0 @@
local writer = require 'writer'
return function( images )
local res = writer()
res:add16( images[ 1 ]:getWidth() )
res:add16( images[ 1 ]:getHeight() )
res:add16( #images )
for _, img in pairs( images ) do
for y = 0, img:getHeight() - 1 do
for x = 0, img:getWidth() - 1 do
res:add16( getpixel( img, x, y ) )
end
end
end
return res
end

View File

@ -1,7 +0,0 @@
return function( r, g, b )
r = r * 32 // 256
g = g * 64 // 256
b = b * 32 // 256
return ( r << 11 ) | ( g << 5 ) | b
end

View File

@ -1,170 +0,0 @@
local image = require 'image'
local xml = require 'xml'
local path = require 'path'
return {
load = function( filename )
local dir = path.split( filename ) .. path.separator
local file = assert( io.open( filename ) )
file:read( '*l' ) -- skip <?xml ... ?>
local contents = file:read( '*a' )
file:close()
local tmx = xml.parse( contents )
local map = {}
map.version = xml.findAttr( tmx, 'version' )
map.orientation = xml.findAttr( tmx, 'orientation' )
map.width = tonumber( xml.findAttr( tmx, 'width' ) )
map.height = tonumber( xml.findAttr( tmx, 'height' ) )
map.tilewidth = tonumber( xml.findAttr( tmx, 'tilewidth' ) )
map.tileheight = tonumber( xml.findAttr( tmx, 'tileheight' ) )
map.widthpixels = map.width * map.tilewidth
map.heightpixels = map.height * map.tileheight
map.backgroundcolor = image.color( 0, 0, 0 )
local backgroundcolor = xml.findAttr( tmx, 'backgroundcolor' )
if backgroundcolor then
backgroundcolor = tonumber( backgroundcolor, 16 )
local r = backgroundcolor >> 16
local g = backgroundcolor >> 8 & 255
local b = backgroundcolor & 255
map.backgroundcolor = image.color( r, g, b )
end
local tilesets = {}
map.tilesets = tilesets
local gids = {}
map.gids = gids
for _, child in ipairs( tmx ) do
if child.label == 'tileset' then
local tileset =
{
firstgid = tonumber( xml.findAttr( child, 'firstgid' ) ),
name = xml.findAttr( child, 'name' ),
tilewidth = tonumber( xml.findAttr( child, 'tilewidth' ) ),
tileheight = tonumber( xml.findAttr( child, 'tileheight' ) )
}
if map.tilewidth ~= tileset.tilewidth or map.tileheight ~= tileset.tileheight then
error( string.format( 'tile dimensions in %s are different from tile dimensions in map', tileset.name ) )
end
for _, child2 in ipairs( child ) do
if child2.label == 'image' then
local filename = path.realpath( dir .. xml.findAttr( child2, 'source' ) )
tileset.image = image.load( filename )
local trans = xml.findAttr( child2, 'trans' )
if trans then
trans = tonumber( trans, 16 )
local r = trans >> 16
local g = trans >> 8 & 255
local b = trans & 255
trans = image.color( r, g, b )
tileset.image:colorToAlpha( trans )
end
end
end
tileset.lastgid = tileset.firstgid + ( tileset.image:getWidth() // tileset.tilewidth ) * ( tileset.image:getHeight() // tileset.tileheight ) - 1
local imagewidth = tileset.image:getWidth()
local tilewidth = tileset.tilewidth
local tileheight = tileset.tileheight
for i = tileset.firstgid, tileset.lastgid do
local id = i - tileset.firstgid
local j = id * tileset.tilewidth
local x = j % imagewidth
local y = math.floor( j / imagewidth ) * tileset.tileheight
gids[ i ] =
{
tileset = tileset,
id = id,
x = x,
y = y,
width = tilewidth,
height = tileheight,
image = tileset.image:sub( x, y, x + tilewidth - 1, y + tileheight - 1 )
}
end
tilesets[ #tilesets + 1 ] = tileset
end
end
local layers = {}
map.layers = layers
for _, child in ipairs( tmx ) do
if child.label == 'layer' then
local layer =
{
name = xml.findAttr( child, 'name' ),
width = tonumber( xml.findAttr( child, 'width' ) ),
height = tonumber( xml.findAttr( child, 'height' ) ),
tiles = {}
}
for _, child2 in ipairs( child ) do
if child2.label == 'data' then
local index = 1
for y = 1, layer.height do
local row = {}
layer.tiles[ y ] = row
for x = 1, layer.width do
local tile = child2[ index ]
index = index + 1
row[ x ] = tonumber( xml.findAttr( tile, 'gid' ) )
end
end
end
end
layers[ #layers + 1 ] = layer
end
end
return map
end,
render = function( map, layers )
local png = image.create( map.widthpixels, map.heightpixels, image.color( 0, 0, 0, 0 ) )
for _, layer in ipairs( map.layers ) do
if layers[ layer.name ] then
local yy = 0
for y = 1, layer.height do
local row = layer.tiles[ y ]
local xx = 0
for x = 1, layer.width do
if row[ x ] ~= 0 then
local tile = map.gids[ row[ x ] ]
if not tile then
error( 'Unknown gid ' .. row[ x ] .. ' in layer ' .. layer.name )
end
tile.image:blit( png, xx, yy )
end
xx = xx + map.tilewidth
end
yy = yy + map.tileheight
end
end
end
return png
end
}

View File

@ -1,105 +0,0 @@
local function prettyPrint( node, file, ident )
file = file or io.stdout
ident = ident or 0
if type( node ) == 'table' then
file:write( ( ' ' ):rep( ident ), '<', node.label )
for attr, value in pairs( node.xarg ) do
file:write( ' ', attr, '="', value, '"' )
end
if node.empty then
file:write( '/>\n' )
else
file:write( '>\n' )
for _, child in ipairs( node ) do
prettyPrint( child, file, ident + 2 )
end
file:write( ( ' ' ):rep( ident ), '</', node.label, '>\n' )
end
else
file:write( ( ' ' ):rep( ident ), node, '\n' )
end
end
local function findNode( node, label )
if type( node ) == 'table' then
if node.label == label then
return node
end
for i = 1, #node do
local res = findNode( node[ i ], label )
if res then
return res
end
end
end
end
local function parseargs(s)
local arg = {}
string.gsub(s, "([%w:]+)=([\"'])(.-)%2", function (w, _, a)
arg[w] = a
end)
return arg
end
return {
parse = function(s)
local stack = {}
local top = {}
table.insert(stack, top)
local ni,c,label,xarg, empty
local i, j = 1, 1
while true do
ni,j,c,label,xarg, empty = string.find(s, "<(%/?)([%w:]+)(.-)(%/?)>", i)
if not ni then break end
local text = string.sub(s, i, ni-1)
if not string.find(text, "^%s*$") then
table.insert(top, text)
end
if empty == "/" then -- empty element tag
table.insert(top, {label=label, xarg=parseargs(xarg), empty=1})
elseif c == "" then -- start tag
top = {label=label, xarg=parseargs(xarg)}
table.insert(stack, top) -- new level
else -- end tag
local toclose = table.remove(stack) -- remove top
top = stack[#stack]
if #stack < 1 then
error("nothing to close with "..label)
end
if toclose.label ~= label then
error("trying to close "..toclose.label.." with "..label)
end
table.insert(top, toclose)
end
i = j+1
end
local text = string.sub(s, i)
if not string.find(text, "^%s*$") then
table.insert(stack[#stack], text)
end
if #stack > 1 then
error("unclosed "..stack[#stack].label)
end
return stack[1][1]
end,
findNode = findNode,
findAttr = function( node, name )
for key, value in pairs( node.xarg ) do
if key == name then
return value
end
end
end,
prettyPrint = prettyPrint
}

View File

@ -1,45 +0,0 @@
local image = require 'image'
local mkis = require 'mkis'
return function( args )
if #args == 0 then
io.write[[
rlimageset.lua creates an imageset containing all files given on the command
line.
Usage: luai rlimageset.lua [ options ] images...
--output <file> writes the imageset to the given file
--margin x sets the pixel limit on RLE runs on a row (must be equal to
RL_BACKGRND_MARGIN)
]]
return 0
end
local output, limit
local images = {}
do
local i = 1
while i <= #args do
if args[ i ] == '--output' then
i = i + 1
output = args[ i ]
elseif args[ i ] == '--margin' then
i = i + 1
limit = tonumber( args[ i ] )
else
images[ #images + 1 ] = image.load( args[ i ] )
end
i = i + 1
end
end
local out = mkis( images, limit )
out:save( output )
return 0
end

View File

@ -1,365 +0,0 @@
local image = require 'image'
local path = require 'path'
local tmx = require 'tmx'
local mkrle = require 'mkrle'
local writer = require 'writer'
local function dump( t, i )
i = i or 0
local s = string.rep( ' ', i * 2 )
if type( t ) == 'table' then
io.write( s, '{\n' )
for k, v in pairs( t ) do
io.write( s, ' ', tostring( k ), ' = ' )
dump( v, i + 1 )
end
io.write( s, '}\n' )
elseif type( t ) == 'string' then
io.write( s, string.format( '%q', t ), '\n' )
else
io.write( s, tostring( t ), '\n' )
end
end
local function split( str, sep )
sep = sep or ' '
local res = {}
local i = 1
while #str ~= 0 do
local j = str:find( sep, i, true )
if not j then
j = #str + 1
end
res[ #res + 1 ] = str:sub( i, j - 1 )
str = str:sub( j + 1 )
end
return res
end
local function list_cmd( args )
local map = tmx.load( args[ 1 ] )
local layers = false
local tilesets = false
for i = 3, #args do
if args[ i ] == '--layers' then
layers = true
elseif args[ i ] == '--tilesets' then
tilesets = true
else
error( 'unknown argument to list: ' .. args[ i ] )
end
end
if not layers and not tilesets then
layers, tilesets = true, true
end
if layers then
for i, layer in ipairs( map.layers ) do
io.write( string.format( 'Layer %d: %s\n', i, layer.name ) )
end
end
if tilesets then
for i, tileset in ipairs( map.tilesets ) do
io.write( string.format( 'Tileset %d: %s\n', i, tileset.name ) )
end
end
end
local function render_cmd( args )
local map = tmx.load( args[ 1 ] )
local layers = {}
for i = 3, #args do
layers[ args[ i ] ] = true
end
if not next( layers ) then
for i, layer in ipairs( map.layers ) do
layers[ layer.name ] = true
end
end
local dir, name, ext = path.split( args[ 1 ] )
tmx.render( map, layers ):save( dir .. path.separator .. name .. '.png' )
end
local function compile_cmd( args )
local map = tmx.load( args[ 1 ] )
local layers = {}
local coll, limit
do
local i = 3
while i <= #args do
if args[ i ] == '--coll' then
coll = split( args[ i + 1 ], '+' )
i = i + 2
elseif args[ i ] == '--margin' then
limit = tonumber( args[ i + 1 ] )
i = i + 2
else
layers[ #layers + 1 ] = split( args[ i ], '+' )
i = i + 1
end
end
end
if #layers == 0 then
error( 'the built map must have at least one layer' )
end
local built = { tiles = {}, images = {}, layer0 = {}, layers = {} }
-- build layer 0 and the tileset
do
local names = {}
for i = 1, #layers[ 1 ] do
for _, layer in ipairs( map.layers ) do
if layer.name == layers[ 1 ][ i ] then
names[ layer.name ] = layer
end
end
end
local png = tmx.render( map, names )
local tileset = {}
local tw, th = map.tilewidth, map.tileheight
for y = 0, png:getHeight() - 1, th do
built.layer0[ y // th + 1 ] = {}
for x = 0, png:getWidth() - 1, tw do
local sub = png:sub( x, y, x + tw - 1, y + th - 1 )
if not tileset[ sub:getHash() ] then
built.tiles[ #built.tiles + 1 ] = sub
tileset[ sub:getHash() ] = #built.tiles - 1
end
built.layer0[ y // th + 1 ][ x // tw + 1 ] = tileset[ sub:getHash() ]
end
end
end
-- build the other layers and the imageset
do
for l = 2, #layers do
local names = {}
for i = 1, #layers[ l ] do
for _, layer in ipairs( map.layers ) do
if layer.name == layers[ l ][ i ] then
names[ layer.name ] = layer
end
end
end
local png = tmx.render( map, names )
local imageset = {}
local tw, th = map.tilewidth, map.tileheight
local indices = {}
built.layers[ l - 1 ] = indices
for y = 0, png:getHeight() - 1, th do
indices[ y // th + 1 ] = {}
for x = 0, png:getWidth() - 1, tw do
local sub = png:sub( x, y, x + tw - 1, y + th - 1 )
if not sub:invisible() then
if not imageset[ sub:getHash() ] then
built.images[ #built.images + 1 ] = sub
imageset[ sub:getHash() ] = #built.images
end
indices[ y // th + 1 ][ x // tw + 1 ] = imageset[ sub:getHash() ]
else
indices[ y // th + 1 ][ x // tw + 1 ] = 0
end
end
end
end
end
-- output file name
local filename
do
local dir, name, ext = path.split( args[ 1 ] )
filename = dir .. path.separator .. name
end
-- rl_tileset_t
do
local out = writer()
out:add16( map.tilewidth )
out:add16( map.tileheight )
out:add16( #built.tiles )
for _, tile in ipairs( built.tiles ) do
for y = 0, map.tileheight - 1 do
for x = 0, map.tilewidth - 1 do
local r, g, b = image.split( tile:getPixel( x, y ) )
r, g, b = r * 31 // 255, g * 63 // 255, b * 31 // 255
out:add16( ( r << 11 ) | ( g << 5 ) | b )
end
end
end
out:save( filename .. '.tls' )
end
-- rl_imageset_t
do
local out = writer()
out:add16( #built.images )
for _, image in ipairs( built.images ) do
local res = mkrle( image, limit )
out:add32( res:getsize() )
out:addwriter( res )
end
out:save( filename .. '.ims' )
end
-- rl_map_t
do
local out = writer()
out:add16( map.width )
out:add16( map.height )
out:add16( 1 + #built.layers ) -- layer count
-- map flags
local flags = 0
flags = flags | ( coll and 1 or 0 ) -- has collision bits
out:add16( flags )
-- rl_layer0
for y = 1, map.height do
for x = 1, map.width do
out:add16( built.layer0[ y ][ x ] )
end
end
-- rl_layern
for _, layer in ipairs( built.layers ) do
for y = 1, map.height do
for x = 1, map.width do
out:add16( layer[ y ][ x ] )
end
end
end
-- collision bits
if coll then
local bits, bit = 0, 1
for y = 1, map.height do
for x = 1, map.width do
for i = 1, #coll do
for _, layer in ipairs( map.layers ) do
if layer.name == coll[ i ] then
if layer.tiles[ y ][ x ] ~= 0 then
bits = bits | bit
break
end
end
end
end
if bit == 0x80000000 then
out:add32( bits )
bits, bit = 0, 1
else
bit = bit << 1
end
end
end
if bit ~= 1 then
out:add32( bits )
end
end
out:save( filename .. '.map' )
end
end
return function( args )
if #args < 2 then
io.write[[
An utility to work with Tiled maps and convert them to retroluxury.
rlmap understands the following commands:
* list: Lists the layers and/or tilesets in a map.
* render: Renders the map as a PNG image. The command accepts a list of layer
names and will render only them if the list is given.
* compile: Converts the map into a format ready to be used with rl_map_create.
If a collision layer is given with --coll, all non-zero tiles
represent unpassable tiles. The map resulting layers are a
combination of one or more map layers, they can be compined with
the + operator. Ex.: floor+flobjs fences+walls will create a map
with two layers, the first with the combined floor and flobjs
layers and the other with the fences and walls layers combined.
Usage: rlmap <mapname.tmx> command args...
Commands:
list lists the map\'s layers and/or tilesets
[--layers] lists only layers
[--tilesets] lists only tilesets
render renders the map as a PNG image
[layername...] names of the layers to render
compile compiles the map as a .map file
--margin x sets the pixel limit on RLE runs on a row
(must be equal to RL_BACKGRND_MARGIN)
--coll layermame sets the layer that contains collision
data (any tile means block)
layername[+layername]... first set of layers, that will be
merged into layer 0
layername[+layername]... second set of layers, that will be
merged into layer 1 (and so forth)
]]
return 0
end
args[ 1 ] = path.realpath( args[ 1 ] )
local commands = {
list = list_cmd,
render = render_cmd,
compile = compile_cmd
}
local cmd = commands[ args[ 2 ] ]
if cmd then
cmd( args )
else
error( 'unknown command: ' .. args[ 2 ] )
end
end

View File

@ -1,63 +0,0 @@
local image = require 'image'
local path = require 'path'
local rgbto16 = require 'rgbto16'
local mkrle = require 'mkrle'
return function( args )
if #args == 0 then
io.write[[
RLE-encodes an image to a format ready to be used with rl_image_create. The
--margin argument should be given, and must be the same as the value of the
RL_BACKGRND_MARGIN when retroluxury was compiled. If it's not given, the image
is still valid, but it must be entirely contained within the background
boundaries when blit.
Usage: luai rlrle.lua [ options ] <image>
--transp r g b makes the given color transparent
--tl makes the color at (0,0) transparent
--bl makes the color at (0,height-1) transparent
--tr makes the color at (width-1,0) transparent
--br makes the color at (width-1,height-1) transparent
--margin x sets the pixel limit on RLE runs on a row
(must be equal to RL_BACKGRND_MARGIN)
]]
return 0
end
local name, limit, transp
for i = 1, #args do
if args[ i ] == '--transp' then
transp = rgbto16( tonumber( args[ i + 1 ] ), tonumber( args[ i + 2 ] ), tonumber( args[ i + 3 ] ) )
i = i + 3
elseif args[ i ] == '--tl' or args[ i ] == '--bl' or args[ i ] == '--tr' or args[ i ] == '--br' then
transp = args[ i ]
elseif args[ i ] == '--margin' then
limit = tonumber( args[ i + 1 ] )
i = i + 1
else
name = args[ i ]
end
end
name = path.realpath( name )
local png = image.load( name )
if transp == '--tl' then
transp = rgbto16( image.split( png:getPixel( 0, 0 ) ) )
elseif transp == '--bl' then
transp = rgbto16( image.split( png:getPixel( 0, png:getHeight() - 1 ) ) )
elseif transp == '--tr' then
transp = rgbto16( image.split( png:getPixel( png:getWidth() - 1, 0 ) ) )
elseif transp == '--br' then
transp = rgbto16( image.split( png:getPixel( png:getWidth() - 1, png:getHeight() - 1 ) ) )
end
local res = mkrle( png, limit, transp )
local dir, name, ext = path.split( name )
res:save( dir .. path.separator .. name .. '.rle' )
return 0
end

View File

@ -1,34 +0,0 @@
local image = require 'image'
local path = require 'path'
return function( args )
if #args == 0 then
io.write[[
Usage: luai rlslice.lua <width> <height> <image>
]]
return 0
end
local width = tonumber( args[ 1 ] )
local height = tonumber( args[ 2 ] )
local name = path.realpath( args[ 3 ] )
local png = image.load( name )
local dir, name, ext = path.split( name )
local index = 1
for y = 0, png:getHeight() - 1, height do
for x = 0, png:getWidth() - 1, width do
local sub = png:sub( x, y, x + width - 1, y + width - 1 )
local fn = dir .. path.separator .. name .. '_' .. index .. ext
sub:save( fn )
io.write( fn, '\n' )
index = index + 1
end
end
return 0
end

View File

@ -1,60 +0,0 @@
local image = require 'image'
local path = require 'path'
local rgbto16 = require 'rgbto16'
return function( args )
if #args == 0 then
io.write[[
Creates a .h header file with the tile ready to be used in tile functions
rl_tile_blit_nobg, rl_tile_blit and rl_tile_unblit. The header file will have
a pixel array with the same name as the input image with extension, and two
macros with the tile's width and height will be defined.
Ex. "luai rltile.lua grass.png" will create a grass.h file containing the
grass_png pixel array and the grass_png_width and grass_png_height macros.
Usage: luai rltile.lua <image>
]]
return 0
end
local name = path.realpath( args[ 1 ] )
local png = image.load( name )
local width = png:getWidth()
local height = png:getHeight()
local pixels = {}
for y = 0, height - 1 do
for x = 0, width - 1 do
local c = rgbto16( image.split( png:getPixel( x, y ) ) )
pixels[ #pixels + 1 ] = c
end
end
local dir, name, ext = path.split( name )
local file = assert( io.open( dir .. path.separator .. name .. '.h', 'w' ) )
local array = string.gsub( name .. ext, '[^a-zA-Z0-9_]', '_' )
file:write( 'const uint16_t ', array, '[] = {\n' )
for i = 1, #pixels, 8 do
local line = {}
table.move( pixels, i, i + 7, 1, line )
file:write( ' ' )
for j = 1, #line do
file:write( string.format( '0x%04x, ', line[ j ] ) )
end
file:write( '\n' )
end
file:write( '};\n\n' )
file:write( '#define ', array, '_width ', tostring( width ), '\n' )
file:write( '#define ', array, '_height ', tostring( height ), '\n' )
file:close()
return 0
end

View File

@ -1,75 +0,0 @@
local image = require 'image'
local mkts = require 'mkts'
return function( args )
if #args == 0 then
io.write[[
rltileset.lua reads all images in the given directory and writes tileset data
that can be directly fed to rl_tileset_create. The first image it finds in the
given directory will dictate the width and height of all tiles in the tileset.
Subsequent images with different dimensions won't be written to the tileset.
Usage: luai rltileset.lua [ options ] <directory>
--output <file> writes the tileset to the given file
(the default is <directory>.tls)
]]
return 0
end
local dir, output
for i = 1, #args do
if args[ i ] == '--output' then
output = args[ i + 1 ]
i = i + 1
else
dir = args[ i ]
end
end
dir = path.realpath( dir )
local files = path.scandir( dir )
local width, height
local images = {}
for _, file in ipairs( files ) do
local stat = path.stat( file )
if stat.file then
local ok, png = pcall( image.load, file )
if ok then
if not width then
width, height = png:getSize()
images[ #images + 1 ] = png
io.write( string.format( '%s set the tileset dimensions to %dx%d\n', file, width, height ) )
elseif width == png:getWidth() and height == png:getHeight() then
images[ #images + 1 ] = png
else
io.write( string.format( '%s doesn\'t have the required dimensions\n', file ) )
end
else
io.write( string.format( '%s could not be read as an image\n', file ) )
end
end
end
if #images ~= 0 then
local out = mkts( images )
if not output then
local dir, name, ext = path.split( dir )
output = dir .. path.separator .. name .. '.tls'
end
out:save( output )
else
io.write( 'no images were found\n' )
end
return 0
end

View File

@ -1,203 +0,0 @@
#include <rl_map.h>
#include <rl_memory.h>
#include <rl_backgrnd.h>
#include <string.h>
#include <rl_endian.c>
static rl_map_t* destroy( const rl_map_t* map )
{
if ( map )
{
for ( int i = map->num_layers - 1; i >= 0; --i )
{
rl_free( (void*)map->layers[ i ] );
}
if ( map->layer0 )
{
rl_free( (void*)map->layer0 );
}
rl_free( (void*)map );
}
return NULL;
}
static void* alloc_zero( size_t size )
{
void* ptr = rl_malloc( size );
if ( ptr )
{
memset( ptr, 0, size );
}
return ptr;
}
rl_map_t* rl_map_create( const void* data, size_t size, const rl_tileset_t* tileset, const rl_imageset_t* imageset )
{
union
{
const void* restrict v;
const uint8_t* restrict u8;
const uint16_t* restrict u16;
const uint32_t* restrict u32;
}
ptr;
ptr.v = data;
int width = ne16( *ptr.u16++ );
int height = ne16( *ptr.u16++ );
int num_layers = ne16( *ptr.u16++ );
int flags = ne16( *ptr.u16++ );
rl_map_t* map = (rl_map_t*)alloc_zero( sizeof( rl_map_t ) + ( num_layers - 1 ) * sizeof( rl_layern_t* ) );
if ( !map )
{
return NULL;
}
map->width = width;
map->height = height;
map->num_layers = num_layers;
map->flags = flags;
map->tileset = tileset;
map->imageset = imageset;
map->layer0 = (rl_layer0_t*)alloc_zero( width * height * sizeof( uint16_t ) );
if ( !map->layer0 )
{
return destroy( map );
}
uint16_t* restrict ndx = (uint16_t*)( (uint8_t*)map->layer0 + sizeof( rl_layer0_t ) );
const uint16_t* restrict end = ndx + width * height;
while ( ndx < end )
{
*ndx++ = ne16( *ptr.u16++ );
}
for ( int i = 1; i < num_layers; i++ )
{
map->layers[ i - 1 ] = (rl_layern_t*)alloc_zero( width * height * sizeof( uint16_t ) );
if ( !map->layers[ i - 1 ] )
{
return destroy( map );
}
ndx = (uint16_t*)( (uint8_t*)map->layers[ i - 1 ] + sizeof( rl_layern_t ) );
end = ndx + width * height;
while ( ndx < end )
{
*ndx++ = ne16( *ptr.u16++ );
}
}
int numqw = ( width * height + 31 ) / 32;
uint32_t* restrict collision = (uint32_t*)rl_malloc( numqw * sizeof( uint32_t ) );
if ( !collision )
{
return destroy( map );
}
map->collision = collision;
const uint32_t* restrict coll_end = collision + numqw;
while ( collision < coll_end )
{
*collision++ = ne32( *ptr.u32++ );
}
return map;
}
void rl_map_destroy( const rl_map_t* map )
{
destroy( map );
}
void rl_map_blit0_nobg( const rl_map_t* map, int x, int y )
{
int bg_width, bg_height;
rl_backgrnd_fb( &bg_width, &bg_height );
const rl_tileset_t* tileset = map->tileset;
int ts_width = tileset->width;
int ts_height = tileset->height;
int dx = -( x % ts_width );
int dy = -( y % ts_height );
int max_x = dx + bg_width + ts_width;
int max_y = dy + bg_height + ts_height;
x /= ts_width;
y /= ts_height;
int pitch = map->width;
const uint16_t* restrict ndx = map->layer0->indices + y * pitch + x;
for ( y = dy; y < max_y; y += ts_height )
{
const uint16_t* restrict next = ndx + pitch;
for ( x = dx; x < max_x; x += ts_width )
{
rl_tileset_blit_nobg( tileset, *ndx++, x, y );
}
ndx = next;
}
}
void rl_map_blitn_nobg( const rl_map_t* map, int index, int x, int y )
{
int bg_width, bg_height;
rl_backgrnd_fb( &bg_width, &bg_height );
const rl_imageset_t* imageset = map->imageset;
int ts_width = map->tileset->width;
int ts_height = map->tileset->height;
int dx = -( x % ts_width );
int dy = -( y % ts_height );
int max_x = dx + bg_width + ts_width;
int max_y = dy + bg_height + ts_height;
x /= ts_width;
y /= ts_height;
int pitch = map->width;
const uint16_t* restrict ndx = map->layers[ --index ]->indices + y * pitch + x;
for ( y = dy; y < max_y; y += ts_height )
{
const uint16_t* restrict next = ndx + pitch;
for ( x = dx; x < max_x; x += ts_width )
{
index = *ndx++;
if ( index )
{
rl_image_blit_nobg( imageset->images[ index - 1 ], x, y );
}
}
ndx = next;
}
}

View File

@ -1,227 +0,0 @@
#include <rl_tile.h>
#include <rl_memory.h>
#include <rl_backgrnd.h>
#include <string.h>
#include <rl_endian.c>
rl_tileset_t* rl_tileset_create( const void* data, size_t size )
{
union
{
const void* restrict v;
const uint16_t* restrict u16;
}
ptr;
ptr.v = data;
int width = ne16( *ptr.u16++ );
int height = ne16( *ptr.u16++ );
int num_tiles = ne16( *ptr.u16++ );
size -= 3 * sizeof( uint16_t );
rl_tileset_t* tileset = (rl_tileset_t*)rl_malloc( sizeof( rl_tileset_t ) + size );
if ( tileset )
{
tileset->width = width;
tileset->height = height;
tileset->size = width * height;
tileset->num_tiles = num_tiles;
uint16_t* restrict pixel = (uint16_t*)( (uint8_t*)tileset + sizeof( rl_tileset_t ) );
const uint16_t* restrict end = pixel + size / 2;
while ( pixel < end )
{
*pixel++ = ne16( *ptr.u16++ );
}
return tileset;
}
return NULL;
}
void rl_tileset_blit_nobg( const rl_tileset_t* tileset, int index, int x, int y )
{
int width = tileset->width;
int height = tileset->height;
int size = tileset->size;
const uint16_t* pixels = tileset->data + size * index;
rl_tile_blit_nobg( width, height, pixels, x, y );
}
uint16_t* rl_tileset_blit( const rl_tileset_t* tileset, int index, int x, int y, uint16_t* bg )
{
int width = tileset->width;
int height = tileset->height;
int size = tileset->size;
const uint16_t* pixels = tileset->data + size * index;
return rl_tile_blit( width, height, pixels, x, y, bg );
}
void rl_tileset_unblit( const rl_tileset_t* tileset, int x, int y, const uint16_t* bg )
{
int width = tileset->width;
int height = tileset->height;
rl_tile_unblit( width, height, x, y, bg );
}
void rl_tile_blit_nobg( int width, int height, const uint16_t* pixels, int x, int y )
{
int d_width, d_height;
uint16_t* restrict dest = rl_backgrnd_fb( &d_width, &d_height );
const uint16_t* restrict source = pixels;
int pitch = width;
int d_pitch = d_width + RL_BACKGRND_MARGIN;
if ( x < 0 )
{
width += x;
source -= x;
x = 0;
}
if ( x + width > d_width )
{
int out = x + width - d_width;
width -= out;
}
if ( y < 0 )
{
height += y;
source -= y * pitch;
y = 0;
}
if ( y + height > d_height )
{
int out = y + height - d_height;
height -= out;
}
if ( width > 0 && height > 0 )
{
dest += y * d_pitch + x;
width *= 2;
for ( int y = height; y > 0; --y )
{
memcpy( (void*)dest, (void*)source, width );
source += pitch;
dest += d_pitch;
}
}
}
uint16_t* rl_tile_blit( int width, int height, const uint16_t* pixels, int x, int y, uint16_t* bg )
{
int d_width, d_height;
uint16_t* restrict dest = rl_backgrnd_fb( &d_width, &d_height );
const uint16_t* restrict source = pixels;
int pitch = width;
int d_pitch = d_width + RL_BACKGRND_MARGIN;
if ( x < 0 )
{
width += x;
source -= x;
x = 0;
}
if ( x + width > d_width )
{
int out = x + width - d_width;
width -= out;
}
if ( y < 0 )
{
height += y;
source -= y * pitch;
y = 0;
}
if ( y + height > d_height )
{
int out = y + height - d_height;
height -= out;
}
if ( width > 0 && height > 0 )
{
dest += y * d_pitch + x;
for ( int y = height; y > 0; --y )
{
memcpy( (void*)bg, (void*)dest, width * 2 );
memcpy( (void*)dest, (void*)source, width * 2 );
source += pitch;
dest += d_pitch;
}
}
return bg;
}
void rl_tile_unblit( int width, int height, int x, int y, const uint16_t* bg )
{
int d_width, d_height;
uint16_t* restrict dest = rl_backgrnd_fb( &d_width, &d_height );
int pitch = width;
int d_pitch = d_width + RL_BACKGRND_MARGIN;
if ( x < 0 )
{
width += x;
x = 0;
}
if ( x + width > d_width )
{
int out = x + width - d_width;
width -= out;
}
if ( y < 0 )
{
height += y;
y = 0;
}
if ( y + height > d_height )
{
int out = y + height - d_height;
height -= out;
}
if ( width > 0 && height > 0 )
{
dest += y * d_pitch + x;
width *= 2;
for ( int y = height; y > 0; --y )
{
memcpy( (void*)dest, (void*)bg, width );
dest += d_pitch;
bg += width;
}
}
}