From c63b1e049c9da2c095281b3e5dd012cc4fc4b6fc Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Wed, 11 Mar 2026 22:36:18 -0400 Subject: Add loading for aseprite files; animated sprites --- src/textures.lua | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 3 deletions(-) (limited to 'src/textures.lua') diff --git a/src/textures.lua b/src/textures.lua index a263052..0738912 100644 --- a/src/textures.lua +++ b/src/textures.lua @@ -1,8 +1,69 @@ require "string" +local load_ase_data = require "src.lovease" + +local default_anim = { + frame_count = 1, + default_tag = "default", + tags = { + default = {from=1, to=1, duration=1}, + }, +} + local img_bank = {} +local anim_bank = {} + +local function load_ase(path) + local ase = load_ase_data(path) + + local width = ase.header.width + local height = ase.header.height + + local frames = {} + local tags = {} + local default_tag + + for _, frame in ipairs(ase.header.frames) do + for _, chunk in ipairs(frame.chunks) do + if chunk.type == 0x2005 then + local cel = chunk.data + local buf = love.data.decompress("data", "zlib", cel.data) + local data = love.image.newImageData(cel.width, cel.height, "rgba8", buf) + local img = lg.newImage(data) + table.insert(frames, {img=img, x=cel.x, y=cel.y}) + elseif chunk.type == 0x2018 then + for i, tag in ipairs(chunk.data.tags) do + if i == 1 then + default_tag = tag.name + end + + tags[tag.name] = { + from = tag.from + 1, + to = tag.to + 1, + -- this is placed wrong but idc + duration = (tag.frame_duration or 100) / 1000, + } + end + end + end + end + + local sheet = lg.newCanvas(width * #frames, height) + lg.setCanvas(sheet) + for i, frame in ipairs(frames) do + lg.draw(frame.img, (i - 1) * width + frame.x, frame.y) + end + lg.setCanvas() + + img_bank[path] = sheet + anim_bank[path] = { + frame_count = #frames, + default_tag = default_tag, + tags = tags, + } +end function load_textures_from(path) - path = path or "assets/images" + path = path or "res/img" local files = lf.getDirectoryItems(path) for _, file in ipairs(files) do @@ -11,10 +72,12 @@ function load_textures_from(path) if lf.getInfo(filepath).type == "directory" then load_textures_from(filepath) else - if string.find(filepath, ".png") then + if filepath:match("%.png$") then local name = string.gsub(filepath, ".png", "") name = string.gsub(name, "assets/images/", "") - img_bank[name] = love.graphics.newImage(filepath) + img_bank[name] = lg.newImage(filepath) + elseif filepath:match("%.ase$") then + load_ase(filepath) end end end @@ -23,3 +86,7 @@ end function get_tex(name) return img_bank[name] end + +function get_anim(name) + return anim_bank[name] or default_anim +end -- cgit v1.3-2-g0d8e