## Love-Imation - a sprite animation library

AlexanDDOS
Prole
Posts: 7
Joined: Mon Apr 16, 2018 10:44 am
Location: Russia, Barnaul
Contact:

### Love-Imation - a sprite animation library

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.
Last edited by AlexanDDOS on Wed May 02, 2018 4:53 am, edited 1 time in total.
Games: Phibonacci, TriPONG
Libraries: Love-Imation

grump
Party member
Posts: 587
Joined: Sat Jul 22, 2017 7:43 pm

### 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:
repectively
an new
initialzation
love.grpahics
...

ivan
Party member
Posts: 1530
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### 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'}).

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
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)

AlexanDDOS
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.
Games: Phibonacci, TriPONG
Libraries: Love-Imation

ivan
Party member
Posts: 1530
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### 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:

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
}
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.

AlexanDDOS
Prole
Posts: 7
Joined: Mon Apr 16, 2018 10:44 am
Location: Russia, Barnaul
Contact:

### Re: Love-Imation - a sprite animation library

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:

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
}
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.
What do you think about such code?

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
}

Games: Phibonacci, TriPONG
Libraries: Love-Imation

grump
Party member
Posts: 587
Joined: Sat Jul 22, 2017 7:43 pm

### 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

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
Both 0 + <number> and 1 * <number> always equal <number>, right?

ivan
Party member
Posts: 1530
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### 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.

AlexanDDOS
Prole
Posts: 7
Joined: Mon Apr 16, 2018 10:44 am
Location: Russia, Barnaul
Contact:

### Re: Love-Imation - a sprite animation library

ivan wrote:
Tue May 01, 2018 8:28 pm
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.
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.
Games: Phibonacci, TriPONG
Libraries: Love-Imation

AlexanDDOS
Prole
Posts: 7
Joined: Mon Apr 16, 2018 10:44 am
Location: Russia, Barnaul
Contact:

### Re: Love-Imation - a sprite animation library

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 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
Both 0 + <number> and 1 * <number> always equal <number>, right?
I just forgot about the operator order, when I was writing this code. I've already fixed it.
Games: Phibonacci, TriPONG
Libraries: Love-Imation

### Who is online

Users browsing this forum: No registered users and 5 guests