I made a simple sprite animation library writen in Lua for LÖVE. It works with LÖVE drawable objects, supports looping animations, animation pausing, automatic loading frames from image files by strings with their names, frame transforms and creating sprites from spritesheets (by a special sub-module).
The source code, issue section and full manual are on the library's GitHub page:
https://github.com/AlexanDDOS/love-imation
UPD: It's my first published project, so it doesn't seem to be perfect. I often get advises, how to make its code cleaner and faster, so it changes often too. I have already solved some problems stated below, so they aren't actual now.
Love-Imation - a sprite animation library
-
- Prole
- Posts: 7
- Joined: Mon Apr 16, 2018 10:44 am
- Location: Russia, Barnaul
- Contact:
Love-Imation - a sprite animation library
Last edited by AlexanDDOS on Wed May 02, 2018 4:53 am, edited 1 time in total.
Re: Love-Imation - a sprite animation library
Nice.
You might want to consider using ImageData:paste in place of mapPixel for cropping. It'll be faster, shorter and cleaner.
Documentation has lots of typos:
You might want to consider using ImageData:paste in place of mapPixel for cropping. It'll be faster, shorter and cleaner.
Documentation has lots of typos:
repectively
an new
initialzation
love.grpahics
...
Re: Love-Imation - a sprite animation library
I see where you are going with this lib, but the code needs a lot of work.
You are creating tables left and right, some of which are not used at all (for example: letters={'x','y'}).
The use metatables/rawset in your case is not very appropriate IMO.
Minus points for not using quads.
No offence, but the code is all of the place.
Consider the following tutorial on how to clean up/simplify your lib:
https://2dengine.com/?p=animation
(I know this is not a perfect example, but note how much less code is involved during update/draw)
You are creating tables left and right, some of which are not used at all (for example: letters={'x','y'}).
Code: Select all
local frame = self:getCurrentFrame()
local args = {frame[1], ...}
local letters = {'x', 'y'}
for i = 2, 10 do
if i < 5 or i > 6 then
args[i] = args[i] or frame_defaults[i] + frame[i]
else
args[i] = args[i] or frame_defaults[i] * frame[i]
end
end
love.graphics.draw(unpack(args))
end
Minus points for not using quads.
No offence, but the code is all of the place.
Consider the following tutorial on how to clean up/simplify your lib:
https://2dengine.com/?p=animation
(I know this is not a perfect example, but note how much less code is involved during update/draw)
-
- Prole
- Posts: 7
- Joined: Mon Apr 16, 2018 10:44 am
- Location: Russia, Barnaul
- Contact:
Re: Love-Imation - a sprite animation library
Thanks, the upper commenters!
I've already optimized the code and fixed some bugs.
I've already optimized the code and fixed some bugs.
Re: Love-Imation - a sprite animation library
Looks better. Your use of metatables is not very good, in fact I highly recommend avoiding metatables in this case.
If you insist on using metatables (which is pointless in your case) then how about:
I think the code would be much cleaner if removed this "key to index" business altogether.
It doesn't add anything useful to an animation library.
If you insist on using metatables (which is pointless in your case) then how about:
Code: Select all
local alias = {drawable=1, x=2, y=3, r=4, sx=5, sy=6, ox=7, oy=8, kx=9, ky=10}
-- untested but much cleaner IMO
local frame_mt = {
__index = function(t, k)
return rawget(t, alias[k] or k)
end,
__newindex = function(t, k, v)
rawset(t, alias[k] or k, v)
end
}
It doesn't add anything useful to an animation library.
-
- Prole
- Posts: 7
- Joined: Mon Apr 16, 2018 10:44 am
- Location: Russia, Barnaul
- Contact:
Re: Love-Imation - a sprite animation library
What do you think about such code?ivan wrote: ↑Tue May 01, 2018 4:31 pm Looks better. Your use of metatables is not very good, in fact I highly recommend avoiding metatables in this case.
If you insist on using metatables (which is pointless in your case) then how about:I think the code would be much cleaner if removed this "key to index" business altogether.Code: Select all
local alias = {drawable=1, x=2, y=3, r=4, sx=5, sy=6, ox=7, oy=8, kx=9, ky=10} -- untested but much cleaner IMO local frame_mt = { __index = function(t, k) return rawget(t, alias[k] or k) end, __newindex = function(t, k, v) rawset(t, alias[k] or k, v) end }
It doesn't add anything useful to an animation library.
Code: Select all
local frame_alias = {drawable = 1, x = 2, y = 3, r = 4, sx = 5, sy = 6, ox = 7, oy = 8, kx = 9, ky = 10}
local frame_mt = {
__index = function(t, k)
local r = rawget(t, k)
if r == nil then
k = frame_alias[k] or tonumber(k)
r = rawget(t, k)
if k == 6 then
r = r or rawget(t, 5) or 1
elseif k == 5 then
r = r or 1
elseif k then
r = r or 0
end
end
return r
end,
__newindex = function(t, k, v)
rawset(t, frame_alias[k] or k, v)
end
}
Re: Love-Imation - a sprite animation library
What are you doing with this frame_alias/frame_mt code? Like, what problem does it solve? I honestly don't get it.
li:draw is a complete mystery to me
Both 0 + <number> and 1 * <number> always equal <number>, right?
li:draw is a complete mystery to me
Code: Select all
for i = 2, 10 do
if i < 5 or i > 6 then
args[i] = args[i] or 0 + frame[i]
else
args[i] = args[i] or 1 * frame[i]
end
end
Re: Love-Imation - a sprite animation library
Yes, what Grump said. Just focus your code on doing 1 thing really well, namely animation. No need for sneaky metatables trying to fill in the default values.
-
- Prole
- Posts: 7
- Joined: Mon Apr 16, 2018 10:44 am
- Location: Russia, Barnaul
- Contact:
Re: Love-Imation - a sprite animation library
I tried to make avoiding li:draw(), if it was needed, easier, because I thought it would be better. But now, I think you're right about fallback values in metatable. People, who really want to avoid it, don't need returning them by the library, so it will be really better to move it from matatables to the drawing function.
Last edited by AlexanDDOS on Wed May 02, 2018 4:58 am, edited 2 times in total.
-
- Prole
- Posts: 7
- Joined: Mon Apr 16, 2018 10:44 am
- Location: Russia, Barnaul
- Contact:
Re: Love-Imation - a sprite animation library
I just forgot about the operator order, when I was writing this code. I've already fixed it.grump wrote: ↑Tue May 01, 2018 8:01 pm What are you doing with this frame_alias/frame_mt code? Like, what problem does it solve? I honestly don't get it.
li:draw is a complete mystery to meBoth 0 + <number> and 1 * <number> always equal <number>, right?Code: Select all
for i = 2, 10 do if i < 5 or i > 6 then args[i] = args[i] or 0 + frame[i] else args[i] = args[i] or 1 * frame[i] end end
Who is online
Users browsing this forum: pablobalieiro and 3 guests