| 1 |
--[[ Copyright (c) 2009 Peter "Corsix" Cawley |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
-- Creates a dat and tab file from a given specification file to combine multiple BMP |
| 22 |
-- images into a single spritesheet for use with Graphics:loadSpriteTable |
| 23 |
-- file: the spec to instruct what the spritesheet will contain. See example.spec |
| 24 |
-- for a template |
| 25 |
|
| 26 |
-- FIXME: BMP images and the output files must be located at the same directory of the |
| 27 |
-- program used to execute this. Ideally, they should all be in the location of the |
| 28 |
-- .spec file |
| 29 |
function makeSheet(file) |
| 30 |
if not file then |
| 31 |
print "Usage: lua mksheet.lua <spec-file>" |
| 32 |
print "Converts a number of bitmaps into a sprite sheet using the given spec" |
| 33 |
return |
| 34 |
end |
| 35 |
|
| 36 |
package.path = (debug.getinfo(1, "S").source:match("@(.*[" .. package.config |
| 37 |
:sub(1, 1) .. "])") or "") .. "lib_" .. package.config:sub(5, 5) |
| 38 |
.. ".lua" .. package.config:sub(3, 3) .. package.path |
| 39 |
local bmp = require("lib_bmp") |
| 40 |
local spritesheet = require("lib_spritesheet") |
| 41 |
|
| 42 |
local specfile = file |
| 43 |
specfile = assert(dofile(specfile)) |
| 44 |
local spec = specfile |
| 45 |
|
| 46 |
assert(type(spec.sprites) == "table", "spec is missing sprite list") |
| 47 |
local required_fields = { |
| 48 |
palette = { |
| 49 |
["from bitmap"] = true, |
| 50 |
}, |
| 51 |
complex = { |
| 52 |
[true] = true, |
| 53 |
-- [false] = true, -- Not yet supported |
| 54 |
}, |
| 55 |
rnc = { |
| 56 |
-- [true] = true, -- Not yet supported |
| 57 |
[false] = true, |
| 58 |
}, |
| 59 |
output_tab = true, |
| 60 |
output_dat = true, |
| 61 |
} |
| 62 |
for field, options in pairs(required_fields) do |
| 63 |
assert(spec[field] ~= nil, "spec is missing '" .. field .. "' field") |
| 64 |
if options ~= true then |
| 65 |
assert(options[spec[field]], "spec field '" .. field .. "' is invalid") |
| 66 |
end |
| 67 |
end |
| 68 |
|
| 69 |
local ss = spritesheet.open(spec.output_tab, spec.output_dat, spec.complex) |
| 70 |
|
| 71 |
for i = 0, #spec.sprites do |
| 72 |
local filename = spec.sprites[i] |
| 73 |
if not filename then |
| 74 |
spritesheet.writeDummy(ss) |
| 75 |
else |
| 76 |
local function err(msg, ...) |
| 77 |
error("Error processing " .. filename .. ":\n" .. msg:format(...)) |
| 78 |
end |
| 79 |
local bitmap, e = bmp.open(filename) |
| 80 |
if not bitmap then |
| 81 |
err(e) |
| 82 |
end |
| 83 |
local width, height = bitmap.width, bitmap.height |
| 84 |
if width > 0xFF or height > 0xFF then |
| 85 |
err "Image too big (maximum size is 255x255)" |
| 86 |
end |
| 87 |
spritesheet.write(ss, width, height, bmp.getPixels(bitmap)) |
| 88 |
end |
| 89 |
end |
| 90 |
spritesheet.close(ss) |
| 91 |
end |