Page 1 of 1

Load And Play GIFS with GSLIB

Posted: Sun Jun 15, 2014 10:29 pm
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:

Re: Load And Play GIFS with GSLIB

Posted: Sun Jun 15, 2014 10:41 pm
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

Re: Load And Play GIFS with GSLIB

Posted: Sun Jun 15, 2014 11:03 pm
by xFade
Another Neat Example:

Re: Load And Play GIFS with GSLIB

Posted: Mon Jun 16, 2014 5:01 am
by jjmafiae
Is this faster than running a spritesheet, comrade?

Re: Load And Play GIFS with GSLIB

Posted: Mon Jun 16, 2014 9:09 pm
by xFade
jjmafiae wrote:Is this faster than running a spritesheet, comrade?
I couldn't tell you. I have never used sprite sheets.

Re: Load And Play GIFS with GSLIB

Posted: Mon Jun 16, 2014 9:22 pm
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.

Re: Load And Play GIFS with GSLIB

Posted: Tue Jun 17, 2014 4:10 pm
by jjmafiae
Why use it then ?