Page 1 of 1

Elegant SpriteBatch Usage

Posted: Tue Dec 19, 2017 9:02 am
by laxoxal
Hello,

I'm trying to make a 2D platformer that uses one spritesheet and spritebatch for all the foreground objects. However, I don't know how best to structure my code around Spritebatches. I have a Lua table that contains tables of quads like this:

Code: Select all

local spritedata = {
    player = {
        {0,0,8,16},
        {8,0,8,16}
    },
    brick = {
        {8,48,8,8}
    },
    ...
}
Which get turned into tables of quads in a helper function and then put into my player object for example, but setting the sprite like this seems kinda ugly. Also, if a wrote a function like updateSprite that takes a single Entity (player or enemy) as an argument, I couldn't use a static class variable for the animation quads because I don't know how to access it from an instance, like if there is a Player.quads/Enemy.quads that I set once, and I want to get at that value from within updateSprite(player/enemy).

Code: Select all

player:update(dt) 
sprites:set(player.sprite_id, player.quads[player.current_frame],player.x, player.y)
-- would like to have something like updateSprite(GameEntity)
Am I severely overthinking this? How do you abstract spritebatches elegantly?

Thanks

Re: Elegant SpriteBatch Usage

Posted: Tue Dec 19, 2017 10:15 am
by Sasha264
Hello! :awesome:
Maybe I didn't understand your question correctly, so, please correct me :3

How do you implement objects and classes? There are several ways to do it in lua.
For example (using metatables) for me is not a problem to access static class variable from an instance.
http://lua-users.org/wiki/SimpleLuaClasses
Just like that:

Code: Select all

Player.quads = ... --once set up quads
local player1 = Player.create()
local player2 = Player.create()

function updateSprite(player)
    --use player.quads here
end

updateSprite(player1)

Re: Elegant SpriteBatch Usage

Posted: Fri Dec 22, 2017 5:18 am
by laxoxal
Thanks for the response!

I use metatables as well. Sorry I'm just being dumb. For some reason I thought you couldn't do that outside of a method you call in the main love methods or it wouldn't get done. But I guess that gets done when you require the file.