Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.git/
.vscode/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 unreal79

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

This repo hosts `animX` version with memory leak patch.

Proof-of-concept demonstrating that `animX` (a LÖVE animation library) leaks memory is there: https://github.com/unreal79/animX-memory-leak


# animX

*Animation in Love2d has never been so easy!!* Now I hate to look like a soap salesman... but animX has [all the features](#features-of-animx) you'd expect from an animation library plus some extra features of its own! I suggest you head over to [A quick walkthrough](#a-quick-walkthrough) if you are short on time!
Expand Down
55 changes: 49 additions & 6 deletions actor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
]]

local Actor={
animations, --list of all the animations in the actor
current, --the current animation (by it's name)
p_onAnimSwitch, --handler called when an animation is switched [internal]
animations = {}, --list of all the animations in the actor
current = nil, --the current animation (by it's name)
p_onAnimSwitch = function(...) end, --handler called when an animation is switched [internal]
}

--an internal function which just sets the default values
Expand Down Expand Up @@ -42,7 +42,7 @@ end

--Starts the animation for the actor
function Actor:startAnimation()
if self:getCurrentAnimation() then
if not self:isActive() and self:getCurrentAnimation() then
self:getCurrentAnimation():start()
end
return self
Expand Down Expand Up @@ -94,7 +94,7 @@ function Actor:getWidth() return self:getCurrentAnimation():getWidth() end
function Actor:getHeight() return self:getCurrentAnimation():getHeight() end

--Sets the style of the image
function Actor:setStyle(val) self:getCurrentAnimation():setStyle(val) return self end
function Actor:setStyle(val) self:getCurrentAnimation():setStyle(val) return self end

--Flips the current animation
function Actor:flip(...) self:getCurrentAnimation():flip(...) return self end
Expand All @@ -105,6 +105,49 @@ function Actor:render(...)
self:getCurrentAnimation():render(...)
end

--Update the current animation
function Actor:update(dt)
if self:getCurrentAnimation() then
self:getCurrentAnimation():update(dt)
end
return self
end

--Cleanup resources to prevent memory leaks
function Actor:destroy()
-- Stop current animation
if self:isActive() then
self:stopAnimation()
end

-- Get reference to animx module (should be available as global)
local animx = _G.animx

-- Destroy all animations and remove them from global list
for name, anim in pairs(self.animations) do
if anim then
-- Remove from global animx.animObjs list
if animx and animx.removeAnimation then
animx.removeAnimation(anim)
end

-- Destroy the animation
if anim.destroy then
anim:destroy()
end
end
end

-- Clear animations table
self.animations = {}
self.current = nil

-- Clear callback
self.p_onAnimSwitch = nil

return self
end

--==Aliases==--

Actor.getCurrentAnim=Actor.getCurrentAnimation
Expand All @@ -113,4 +156,4 @@ Actor.changeAnim=Actor.switch
Actor.set=Actor.switch
Actor.draw=Actor.render

return Actor
return Actor
72 changes: 48 additions & 24 deletions animation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@

--Unlike iffy we use metatables
local Animation={
texture, --the spritesheet for the animation
frames, --the frames in the animation
duration, --duration for each sprite- a smart table (idea stolen from Walt)
cache, --an internal variable to account for same duration across multiple frames
mode, --the mode of the animation - {'loop'/'rewind'/'once'/'bounce',times}
direction, --the sense of the animation
curFrame, --the current frame that's being rendered
active, --whether the animation is being played or not
curTimes, --keeps count of number of times animation executed
timer, --an internal timer variable
p_flipX, --whether to flip along x-axis [internal]
p_flipY, --whether to flip along y-axis [internal]
p_onCycleOver, --handler called when an animation cycle is complete [internal]
p_onAnimOver, --handler called when the entire animation is over [internal]
p_onAnimStart, --handler called when the animation is started [internal]
p_onAnimRestart, --handler called whenever the animation is restarted [internal]
p_onChange, --handler called whenever current frame is changed [internal]
p_onUpdate, --called at every frame regardless of it's active property [internal]
texture = {}, --the spritesheet for the animation
frames = {}, --the frames in the animation
duration = {}, --duration for each sprite- a smart table (idea stolen from Walt)
cache = {}, --an internal variable to account for same duration across multiple frames
mode = {}, --the mode of the animation - {'loop'/'rewind'/'once'/'bounce',times}
direction = 1, --the sense of the animation
curFrame = 1, --the current frame that's being rendered
active = false, --whether the animation is being played or not
curTimes = 0, --keeps count of number of times animation executed
timer = 0, --an internal timer variable
p_flipX = false, --whether to flip along x-axis [internal]
p_flipY = false, --whether to flip along y-axis [internal]
p_onCycleOver = function(...) end, --handler called when an animation cycle is complete [internal]
p_onAnimOver = function(...) end, --handler called when the entire animation is over [internal]
p_onAnimStart = function(...) end, --handler called when the animation is started [internal]
p_onAnimRestart = function(...) end, --handler called whenever the animation is restarted [internal]
p_onChange = function(...) end, --handler called whenever current frame is changed [internal]
p_onUpdate = function(...) end, --called at every frame regardless of it's active property [internal]
}

--an internal function which just sets the default values
function Animation:init(startingFrame,delay)
self:setDelay(delay)
self.startingFrame=startingFrame
self:start()
self:start()
self.mode={'loop',1}
end

Expand Down Expand Up @@ -205,7 +205,7 @@ end
function Animation:stop()
--It is important that active is first set to false and then handler is called
self.active=false
self.p_onAnimOver(self)
self.p_onAnimOver(self)
end

--gets the duration of the given frame or current frame if provided nil
Expand All @@ -227,14 +227,14 @@ end
function Animation:setDelay(frame,delay)
if not delay then
--Set delay for all frames
delay,frame=frame
delay,frame=frame, nil
self.duration={delay}
self.cache={self:getSize()}
return self
end
--Set delay for only one frame
assert(frame>=1 and frame<=self:getSize(),"animX Error: Frame is out of bounds!")

local u,v=1,1

for i=1,self:getSize() do
Expand Down Expand Up @@ -296,7 +296,7 @@ function Animation:update(dt)
if self.timer>delay then
self.timer=self.timer%delay
self:change()
if self.curFrame>self:getSize() then
if self.curFrame>self:getSize() then
self:cycle()
if self:getMode()=='bounce' then
--If we are bouncing and we are done then stop
Expand Down Expand Up @@ -326,7 +326,7 @@ function Animation:update(dt)

elseif self.curFrame<1 then
self.curFrame=1

if self:getMode()=='bounce' then
self:cycle()
end
Expand Down Expand Up @@ -373,6 +373,30 @@ end
--This function is not defined here but on the main library file!
Animation.exportToXML=nil

--Cleanup resources to prevent memory leaks
function Animation:destroy()
-- Clear all callbacks
self.p_onAnimOver = nil
self.p_onCycleOver = nil
self.p_onAnimStart = nil
self.p_onAnimRestart = nil
self.p_onChange = nil
self.p_onUpdate = nil

-- Clear frames (quads will be garbage collected)
self.frames = {}
self.duration = {}
self.cache = {}

-- Clear texture reference
self.texture = nil

-- Mark as inactive
self.active = false

return self
end

--Just setting some aliases- kinda my speciality

Animation.getTexture=Animation.getAtlas
Expand Down
Loading