Skip to content

Commit

Permalink
new version, added rotation support
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavostuff committed Nov 24, 2018
1 parent d20988c commit 8a10bfb
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 44 deletions.
3 changes: 1 addition & 2 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Copyright (c) 2015-2017 Gustavo Alberto Lara Gómez

Copyright (c) 2015-2018 Gustavo Lara
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.
Expand Down
68 changes: 57 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
### Katsudö is a small and simple animation library for [LÖVE](https://love2d.org/)
## Katsudö is a small and simple animation library for [LÖVE](https://love2d.org/)

[![License](http://img.shields.io/:license-MIT-blue.svg)](https://github.com/tavuntu/katsudo/blob/master/LICENSE.md)
[![Version](http://img.shields.io/:version-0.0.1-green.svg)](https://github.com/tavuntu/katsudo/blob/master/README.md)
[![Version](http://img.shields.io/:version-0.0.2-green.svg)](https://github.com/tavuntu/katsudo/blob/master/README.md)

Here's an example:
### Example 1:

```lua
local k = require "katsudo"

function love.load()
gr = love.graphics
gr.setBackgroundColor(213, 65, 0)
gr.setBackgroundColor(1, 1, 1)
local imgDir = "imgs/tc.png"
-- 18 frames of 30x55 at 25 FPS:
tc = k.new(imgDir, 30, 55, 18, 0.04)
Expand All @@ -31,23 +31,23 @@ function love.draw()
end
```

And the result is something like this:
Result:

![Katsudö gif](http://s32.postimg.org/5lokvncjp/image.gif)
[![test2.gif](https://i.postimg.cc/KcNLh2nS/test2.gif)](https://postimg.cc/t7s7F8S2)

Using funtion setDelay() will set a different time on the given frame(s):
### Example 2, setDelay():

```lua
local k = require "katsudo"

function love.load()
gr = love.graphics
gr.setBackgroundColor(213, 65, 0)
gr.setBackgroundColor(1, 1, 1)
imgDir = "imgs/tc.png"
-- 18 frames of 30x55 at 25 FPS:
tc = k.new(imgDir, 30, 55, 18, 1):setDelay(0.5) -- change to half second for all frames
tc2 = k.new(imgDir, 30, 55, 18, 1):setDelay(0.5, 2) -- half second just for 2nd frame
tc3 = k.new(imgDir, 30, 55, 18, 1):setDelay(0.5, 2, true) -- starting with 2nd frame
tc = k.new(imgDir, 30, 55, 18, .1):setDelay(0.5) -- change to half second for all frames
tc2 = k.new(imgDir, 30, 55, 18, .1):setDelay(0.5, 2) -- half second just for 2nd frame
tc3 = k.new(imgDir, 30, 55, 18, .1):setDelay(0.5, 2, true) -- starting with 2nd frame
end

function love.update(dt)
Expand All @@ -60,3 +60,49 @@ function love.draw()
tc3:draw(350, 50, 0, 5, 5)
end
```

Result:

[![test3.gif](https://i.postimg.cc/gk2hYTDw/test3.gif)](https://postimg.cc/mhKkjVKR)

### Example 3, rotation:

```lua
local k = require "katsudo"

function love.load()
gr = love.graphics
gr.setBackgroundColor(1, 1, 1)
rotatingDounts = {}
-- spin clockwise at 60 RPM (a spin in 1 sec):
table.insert(rotatingDounts, k.rotate('imgs/donut.png'))
-- 30 RPM:
table.insert(rotatingDounts, k.rotate('imgs/donut.png', .5))
-- 30 RPM counter clockwise:
table.insert(rotatingDounts, k.rotate('imgs/donut.png', .5, true))
-- 30 RPM spinning in a random direction:
table.insert(rotatingDounts, k.rotate('imgs/donut.png', .5, 'random'))
end

function love.update(dt)
k.update(dt)
end

function love.draw()
for i = 1, #rotatingDounts do
local donut = rotatingDounts[i]
donut:draw(
i * donut.w,
200,
donut:r(),
1,
1,
donut.w / 2,
donut.h / 2)
end
end
```

Result:

[![test.gif](https://i.postimg.cc/mkbH1XW5/test.gif)](https://postimg.cc/CBQ1W41G)
49 changes: 49 additions & 0 deletions conf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function love.conf(t)
t.identity = nil -- The name of the save directory (string)
t.appendidentity = false -- Search files in source directory before save directory (boolean)
t.version = "11.0" -- The LÖVE version this game was made for (string)
t.console = false -- Attach a console (boolean, Windows only)
t.accelerometerjoystick = true -- Enable the accelerometer on iOS and Android by exposing it as a Joystick (boolean)
t.externalstorage = false -- True to save files (and read from the save directory) in external storage on Android (boolean)
t.gammacorrect = false -- Enable gamma-correct rendering, when supported by the system (boolean)

t.audio.mixwithsystem = true -- Keep background music playing when opening LOVE (boolean, iOS and Android only)

t.window.title = "Katsudö" -- The window title (string)
t.window.icon = nil -- Filepath to an image to use as the window's icon (string)
t.window.width = 800 -- The window width (number)
t.window.height = 600 -- The window height (number)
t.window.borderless = false -- Remove all border visuals from the window (boolean)
t.window.resizable = false -- Let the window be user-resizable (boolean)
t.window.minwidth = 1 -- Minimum window width if the window is resizable (number)
t.window.minheight = 1 -- Minimum window height if the window is resizable (number)
t.window.fullscreen = false -- Enable fullscreen (boolean)
t.window.fullscreentype = "desktop" -- Choose between "desktop" fullscreen or "exclusive" fullscreen mode (string)
t.window.vsync = 1 -- Vertical sync mode (number)
t.window.msaa = 0 -- The number of samples to use with multi-sampled antialiasing (number)
t.window.depth = nil -- The number of bits per sample in the depth buffer
t.window.stencil = nil -- The number of bits per sample in the stencil buffer
t.window.display = 1 -- Index of the monitor to show the window in (number)
t.window.highdpi = false -- Enable high-dpi mode for the window on a Retina display (boolean)
t.window.x = nil -- The x-coordinate of the window's position in the specified display (number)
t.window.y = nil -- The y-coordinate of the window's position in the specified display (number)

t.modules.audio = true -- Enable the audio module (boolean)
t.modules.data = true -- Enable the data module (boolean)
t.modules.event = true -- Enable the event module (boolean)
t.modules.font = true -- Enable the font module (boolean)
t.modules.graphics = true -- Enable the graphics module (boolean)
t.modules.image = true -- Enable the image module (boolean)
t.modules.joystick = true -- Enable the joystick module (boolean)
t.modules.keyboard = true -- Enable the keyboard module (boolean)
t.modules.math = true -- Enable the math module (boolean)
t.modules.mouse = true -- Enable the mouse module (boolean)
t.modules.physics = true -- Enable the physics module (boolean)
t.modules.sound = true -- Enable the sound module (boolean)
t.modules.system = true -- Enable the system module (boolean)
t.modules.thread = true -- Enable the thread module (boolean)
t.modules.timer = true -- Enable the timer module (boolean), Disabling it will result 0 delta time in love.update
t.modules.touch = true -- Enable the touch module (boolean)
t.modules.video = true -- Enable the video module (boolean)
t.modules.window = true -- Enable the window module (boolean)
end
Binary file added imgs/donut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed katsudo.love
Binary file not shown.
156 changes: 126 additions & 30 deletions katsudo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function katsudo.new(img, quadWidth, quadHeight, numberOfQuads, millis, style)

newAnim.numberOfQuads = numberOfQuads or automaticNumberOfQuads
newAnim.items = {}
--newAnim.millis = millis or 0.1 -- Milliseconds for each frame.
newAnim.mode = "repeat"
newAnim.animType = 'frames'

-- Generate frames (quads):
local x, y = 0, 0
Expand All @@ -60,46 +60,80 @@ function katsudo.new(img, quadWidth, quadHeight, numberOfQuads, millis, style)
return setmetatable(newAnim, katsudo)
end

function katsudo:rewind()
self.mode = "rewind"
return self
end

function katsudo:once()
self.mode = "once"
return self
end
function katsudo.rotate(img, speed, reverse)
if not img then
error("Error in katsudo.rotate() parameter #1, please provide an image (string or Image)")
end

function katsudo:setDelay(millis, index, theRestAlso)
if index then
if self.items[index] then
self.items[index].delay = millis
if type(img) == "string" then
img = love.graphics.newImage(img)
end

if theRestAlso then
for i = index + 1, #self.items do
self.items[i].delay = millis
end
end
local newAnim = {}
newAnim.img = img
newAnim.w = img:getWidth()
newAnim.h = img:getHeight()
newAnim.animType = 'rotation'
newAnim.speed = speed or 1
newAnim.rotationAmount = 0
newAnim.rotationCounterClockwise = reverse

-- random rotation sense:
if reverse == 'random' then
if love.math.random(1, 2) == 1 then
newAnim.rotationCounterClockwise = true
else
error("error in setDelay(), no frame at index "..index.."")
end
else
for i = 1, #self.items do
self.items[i].delay = millis
newAnim.rotationCounterClockwise = false
end
end
return self
end

function katsudo:draw(...)
local q = self.items[self.index].quad
love.graphics.draw(self.img, q, ...)
table.insert(katsudo.anims, newAnim)
return setmetatable(newAnim, katsudo)
end

function katsudo.update(dt)
for i = 1, #katsudo.anims do
local a = katsudo.anims[i]

if a.animType == 'frames' then
if not a.finished then
a.timer = a.timer + dt
if a.timer >= a.items[a.index].delay then
a.timer = 0
a.index = a.index + 1 * a.sense

if a.index > #a.items or a.index < 1 then
if a.mode == "repeat" then
a.index = 1
elseif a.mode == "rewind" then
a.sense = a.sense * -1
if a.sense < 0 then
a.index = a.index - 1
end
if a.sense > 0 then
a.index = a.index + 1
end
elseif a.mode == "once" then
a.finished = true
a.index = a.index - 1
end
end
end
end
elseif a.animType == 'rotation' then
a.rotationAmount = a.rotationAmount + dt * a.speed * 360

if a.rotationAmount >= 360 then
a.rotationAmount = 0
end
end
end
end

function katsudo:selfUpdate(dt)
local a = self

if a.animType == 'frames' then
if not a.finished then
a.timer = a.timer + dt
if a.timer >= a.items[a.index].delay then
Expand All @@ -124,7 +158,69 @@ function katsudo.update(dt)
end
end
end
elseif a.animType == 'rotation' then
a.rotationAmount = a.rotationAmount + dt * a.speed * 360

if a.rotationAmount >= 360 then
a.rotationAmount = 0
end
end
end

function katsudo:r()
if self.rotationCounterClockwise then
return math.rad(360 - self.rotationAmount)
end

return math.rad(self.rotationAmount)
end

function katsudo:rewind()
if self.animType == 'frames' then
self.mode = "rewind"
end
return self
end

function katsudo:once()
if self.animType == 'frames' then
self.mode = "once"
end
return self
end

function katsudo:setDelay(millis, index, theRestAlso)
if self.animType == 'frames' then
if index then
if self.items[index] then
self.items[index].delay = millis

if theRestAlso then
for i = index + 1, #self.items do
self.items[i].delay = millis
end
end
else
error("error in setDelay(), no frame at index "..index.."")
end
else
for i = 1, #self.items do
self.items[i].delay = millis
end
end
elseif self.animType == 'rotation' then
self.speed = millis
end
return self
end

function katsudo:draw(...)
if self.animType == 'frames' then
local q = self.items[self.index].quad
love.graphics.draw(self.img, q, ...)
elseif self.animType == 'rotation' then
love.graphics.draw(self.img, ...)
end
end

return katsudo
return katsudo
10 changes: 9 additions & 1 deletion main.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
local k = require "katsudo"

local k = require "katsudo"

function love.load()
gr = love.graphics
gr.setBackgroundColor(213, 65, 0)
gr.setBackgroundColor(1, 1, 1)
local imgDir = "imgs/tc.png"
-- 18 frames of 30x55 at 25 FPS:
tc = k.new(imgDir, 30, 55, 18, 0.04)
Expand All @@ -20,4 +22,10 @@ function love.draw()
tc2:draw(200, 50, 0, 5, 5)
tc3:draw(350, 50, 0, 5, 5)
tc4:draw(500, 50, 0, 5, 5)
end

function love.keypressed(k)
if k == 'escape' then
love.event.quit()
end
end

0 comments on commit 8a10bfb

Please sign in to comment.