Load And Play GIFS with GSLIB

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Load And Play GIFS with GSLIB

Post by xFade »

I created a gif loading library with the help of DaedalusYoung.

Code: Select all


print("GSLIB LOADED...")

local gs = {}
gs.__index = gs

local i = ""

function gs:Init(x)
  i = x
  print(x.." Initialized!")
end

setmetatable(gs, {
    _call = function(cls,...)
      return cls.new(...)
    end,
})

function gs.NewGif(init)
  local self = setmetatable({},gs)
  local frames = love.filesystem.getDirectoryItems(init)
  print("Loading Frames From..."..init)
  self.image = {}
  self.index = 1
  self.timer = 0
  self.frames = 0
  for i = 1, #frames do
    print("Frame: "..i.." "..frames[i])
    self.image[i] = love.graphics.newImage(init..frames[i])
    self.frames = i
  end
  return self
end

function gs.UpdateGif(self,dt)
  self.timer = self.timer + dt
  if self.timer >= dt then
    if self.index >= self.frames then
      self.index = 1
      print(self.index)
    elseif self.index <= self.frames then
      self.index = self.index + 1
    end
    self.timer = 0
  end
end

function gs.DrawGif(self,x,y)
  love.graphics.draw(self.image[self.index],x,y)
end

return gs
How To Use It:
- Create a Folder in your games main folder and call if GIFS
- Upload and split your gif into this amazing website. http://ezgif.com/split
- Download all the frames
- Create a new folder in the folder GIFS
- Place all your frames into the New Folder you created

Set Up:

In your main.lua type this:

GS = require"GSLIB"

Then in love.load type this:

GS:Init("GSLIB")

And Done!

How To Create A Gif(love.load()):

dir1 = "GIFS/MyGif/"

myGif = GS.NewGif(dir1)


How To Draw And Update It:

Update:
myGif:UpdateGif(dt,speed)

Draw:
myGif:DrawGif(x,y)

Thats It!

Example:
Attachments
GIF Animator.zip
Have fun :)
(3.44 MiB) Downloaded 121 times
(ง'̀-'́)ง
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Load And Play GIFS with GSLIB

Post by xFade »

Small Update :P

Code: Select all


print("GSLIB LOADED...")

local gs = {}
gs.__index = gs

local i = ""

function gs:Init(x)
  i = x
  print(x.." Initialized!")
end

setmetatable(gs, {
    _call = function(cls,...)
      return cls.new(...)
    end,
})

function gs.NewGif(init,loop)
  local self = setmetatable({},gs)
  local frames = love.filesystem.getDirectoryItems(init)
  print("Loading Frames From..."..init)
  self.image = {}
  self.index = 1
  self.timer = 0
  self.frames = 0
  self.loop = loop
  for i = 1, #frames do
    print("Frame: "..i.." "..frames[i])
    self.image[i] = love.graphics.newImage(init..frames[i])
    self.frames = i
  end
  return self
end

function gs.UpdateGif(self,dt,speed)
  self.timer = self.timer + dt
  if self.timer >= speed then
    if self.loop then
      if self.index >= self.frames then
        self.index = 1
      elseif self.index <= self.frames then
        self.index = self.index + 1
      end
    elseif not self.loop then
      if self.index >= self.frames then
        self.index = self.frames
      elseif self.index <= self.frames then
        self.index = self.index + 1
      end
    end
    self.timer = 0
  end
end

function gs.DrawGif(self,x,y,rt,sx,sy)
  love.graphics.draw(self.image[self.index],x,y,rt,sx,sy)
end

return gs
(ง'̀-'́)ง
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Load And Play GIFS with GSLIB

Post by xFade »

Another Neat Example:
Attachments
LightSpeed.zip
Wow Much Speed
(356 KiB) Downloaded 117 times
(ง'̀-'́)ง
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Load And Play GIFS with GSLIB

Post by jjmafiae »

Is this faster than running a spritesheet, comrade?
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Load And Play GIFS with GSLIB

Post by xFade »

jjmafiae wrote:Is this faster than running a spritesheet, comrade?
I couldn't tell you. I have never used sprite sheets.
(ง'̀-'́)ง
User avatar
slime
Solid Snayke
Posts: 3160
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Load And Play GIFS with GSLIB

Post by slime »

jjmafiae wrote:Is this faster than running a spritesheet, comrade?
A sprite sheet with a SpriteBatch (and proper use of [wiki]SpriteBatch:bind[/wiki] and [wiki]SpriteBatch:unbind[/wiki]) is always the most performant option.
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Load And Play GIFS with GSLIB

Post by jjmafiae »

Why use it then ?
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests