Added error checking and offsets for flipping sprites

This commit is contained in:
Micaiah Parker 2020-11-04 12:01:44 -05:00
parent d57c1bdfee
commit 7c0b15d602
1 changed files with 17 additions and 9 deletions

View File

@ -1,25 +1,33 @@
local module = {}
function module.create(file, rows, columns)
local spritesheet = {
image=love.graphics.newImage(image),
quads={}
}
if columns == nil then columns = 1 end
if image == nil then
if love.filesystem.getInfo(file) == nil then
love.errorhandler("file: "..file.." was not found.")
end
image = love.graphics.newImage(file)
local spritesheet = {
image=image,
quads={}
}
local width, height = image:getWidth()/rows, image:getHeight()/columns
spritesheet.width = width
spritesheet.height = height
for r = 0, rows-1 do
for c = 0, rows-1 do
table.insert(spritesheet.quads, love.graphics.newQuad(width*(r-1),height*(c-1),width,height,image:getDimensions()))
for c = 0, columns-1 do
table.insert(spritesheet.quads, love.graphics.newQuad(width*r,height*c,width,height,image:getDimensions()))
end
end
function spritesheet:draw(frame, x, y)
love.graphics.draw(self.quads[frame], x, y)
function spritesheet:draw(frame, x, y, sx, sy, ox, oy)
if sx == -1 then ox = self.width end
if sy == -1 then oy = self.height end
love.graphics.draw(self.image, self.quads[frame], x, y, 0, sx, sy, ox, oy)
end
return spritesheet