Last active
February 27, 2020 23:50
-
-
Save Hammster/46f77dbc430a015a1c27a55999bf6f90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Description: | |
Ham utility script for creating ARGB8888 images | |
Made by Hammster. | |
- Twitter: @hammster1911 | |
--]] | |
function writebytes(f,x) | |
local r=string.char(x%256) x=(x-x%256)/256 | |
local g=string.char(x%256) x=(x-x%256)/256 | |
local b=string.char(x%256) x=(x-x%256)/256 | |
local a=string.char(x%256) x=(x-x%256)/256 | |
f:write(a,r,g,b) | |
end | |
-- Identify current sprite. | |
local sprite = app.activeSprite | |
if not sprite then return print('No active sprite') end | |
if sprite.colorMode ~= ColorMode.RGB then | |
print("Color mode of the sprite needs to be RGB") | |
return | |
end | |
-- Confirmation | |
local path,title = sprite.filename:match("^(.+[/\\])(.-).([^.]*)$") | |
local msg = { "Do you want to export/overwrite the following files?" } | |
local targetFolder = "C:\\workspace\\c++\\Retroham\\resources\\" | |
-- Ask for user input | |
local dialog = | |
Dialog():entry { id="targetFolder", label="Target Folder: ", text=targetFolder } | |
:button{ id="ok", text="&OK", focus=true } | |
:button{ id="cancel", text="&Cancel" } | |
local bounds = dialog.bounds | |
dialog.bounds = Rectangle(dialog.bounds.x, dialog.bounds.y, 400, bounds.height) | |
dialog:show() | |
if not dialog.data.ok then return end | |
targetFolder = dialog.data.targetFolder | |
if not targetFolder then return end | |
-------------------------------------------------------------------------------- | |
-- Logic | |
-------------------------------------------------------------------------------- | |
layerVisible = {} | |
-- Hide all layers. | |
for i,layer in ipairs(sprite.layers) do | |
layerVisible[i] = layer.isVisible | |
-- Avoid group layers. | |
if (not layer.isGroup) then | |
layer.isVisible = false | |
end | |
end | |
-- Save the layers as individual sprites. | |
for i,layer in ipairs(sprite.layers) do | |
-- Avoid group layers. | |
if (not layer.isGroup) then | |
layer.isVisible = true | |
local image = layer:cel(1).image | |
local size = (image.width * image.height) - 1 | |
local filename = targetFolder .. layer.name .. ".bin" | |
print("create: " .. filename ) | |
file = io.open(filename, "wb") | |
for j = 1, #layer.cels do | |
for k = 0, size do | |
image = layer:cel(j).image | |
writebytes(file, image:getPixel(k % image.width, k / image.width)) | |
end | |
print("added frame " .. j) | |
end | |
file:close() | |
layer.isVisible = false | |
end | |
end | |
-- Restore layers visibility. | |
for i,layer in ipairs(sprite.layers) do | |
-- Avoid group layers. | |
if (not layer.isGroup) then | |
layer.isVisible = layerVisible[i] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment