-- Sprite batch emulator for Love (tested on 0.8.0, but I think it compatible with 0.7.2) -- -- The motivation was: my Dingoo A320 can't find SpriteBatch, so I decided to emulate it. -- Of course this emulator inefficient in comparsion with original SpriteBatch. The emulator will be created -- only if SpriteBatch is missing (you can simulate with love.graphics.newSpriteBatch = nil). So if the SpriteBatch -- is present there will be zero overhead. -- -- Restrictions: -- SpriteBatch.add not implemented (I actually don't know the purpose of this method :-)) -- No rotation, no scaling of batch local maxSize = 1000 local newQuad, drawq = love.graphics.newQuad, love.graphics.drawq local oldDraw = love.graphics.draw -- Tiny OOP local class = function(base) local cls = {} cls.__index = cls local cls_mt = { __index = base, __call = function (c, ...) local instance = setmetatable({}, cls) if cls.init then cls.init(instance, ...) end return instance end} return setmetatable(cls, cls_mt) end -- For Lua 5.2 (maybe someday LOVE will use it^^) if table.unpack then local unpack = table.unpack end SpriteBatchEmulator = class() SpriteBatchEmulator.init = function(self, image, size) self.image = image self.size = size or maxSize self.currentSize = 0 self.spritebatchemulator = true end SpriteBatchEmulator.type = function(self) return 'SpriteBatchEmulator' end SpriteBatchEmulator.typeOf = function(self, type_) return type_ == 'SpriteBatchEmulator' or type_ == 'Drawable' end SpriteBatchEmulator.add = function(self, x, y, r, sx, sy, ox, oy) error('Not implemented') end SpriteBatchEmulator.addq = function(self, quad, x, y, r, sx, sy, ox, oy) local i = self.currentSize + 1 if i <= maxSize then self.quads[i] = {quad, x, y, r, sx, sy, ox, oy} self.currentSize = i end end SpriteBatchEmulator.clear = function(self) self.quads = {} self.currentSize = 0 end SpriteBatchEmulator.setImage = function(self, image) self.image = image end SpriteBatchEmulator.draw = function(self, xc, yc, rc, sxc, syc, oxc, oyc) for i = 1, self.currentSize do local quad, x, y, r, sx, sy, ox, oy = unpack(self.quads[i]) drawq(self.image, quad, x + xc - (oxc or 0), y + yc - (oyc or 0), r, sx, sy, ox, oy) end end -- Main hack if not love.graphics.newSpriteBatch then -- If we can't use real one we will use emulator love.graphics.newSpriteBatch = SpriteBatchEmulator -- And specific draw procedure love.graphics.draw = function(drawable, x, y, r, sx, sy, ox, oy ) if type(drawable) == 'table' and (drawable.spritebatchemulator) then drawable:draw(x, y, r, sx, sy, ox, oy) else oldDraw(drawable, x, y, r, sx, sy, ox, oy ) end end end