Elegant SpriteBatch Usage

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
laxoxal
Prole
Posts: 2
Joined: Tue Dec 19, 2017 8:21 am

Elegant SpriteBatch Usage

Post 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
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Elegant SpriteBatch Usage

Post 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)
laxoxal
Prole
Posts: 2
Joined: Tue Dec 19, 2017 8:21 am

Re: Elegant SpriteBatch Usage

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests