Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(+1)

I have a bunch of tiles stored in a single label (like a spritesheet). I'm using loops and ID numbers to identify and retrieve sprite information for each tile from the label, like so:

# ve is the ID of the tile that I want
i := tileset
vc := sprite_size
vd := 0
loop if vd < ve begin
  i += vc
  vd += 1
  again
end

I was just wondering if there might be a more efficient solution that I could be using. Especially if the data I want is further down the list.

(1 edit) (+1)

You could calculate the index you need. This will require some consideration of the tile size:

Be careful of overflow. If you have lots of tiles, you might need to add to i multiple times. This technique also gets a bit fiddlier if you aren't using a power-of-two size, but it's still doable.

Another option is to just use pre-multiplied values as your tile IDs. If you don't have many tiles, this makes looking one up trivial!

In some situations, you could consider using self-modifying code to rewrite the address in an `i := NNN` or `i := long NNNN` instruction. This gets a bit fiddly, but if you have lots of tiles, or tiles that take up a lot of memory (like full-color tiles) it might be the most efficient option. There are some examples of these sorts of techniques in the FAQ, when discussing pointers.

Edit: Oh, and one other thing- while writing a loop like you did there does work, it would be more idiomatic to do something like:

Using == or != instead of < is more efficient when you can get away with it.