Crash when generating imagetable

Platform
Mac, SDK 1.11.1

Issue
In a project I try to generate my own image table but it leads to a lot of unexpected behaviours. In the worst case the simulator crashes.

What I try to do is to create a new image table with playdate.graphics.imagetable.new() and after to set the different indexes with setImage(). I encountered the following issues:

  • playdate.graphics.imagetable:getSize() doesn't take into account the cellsWide argument.
  • It seems the first index has to have an image.
  • If I try to set an image to multiples indexes, the Simulator will crash.

Code

import("CoreLibs/graphics")
import("CoreLibs/sprites")

local tile = playdate.graphics.image.new( "box" )
local iw, ih = tile:getSize()

local cw, ch = 2, 2
local tileset = playdate.graphics.imagetable.new( cw*ch, cw )

-- Issue #1: return the wrong size, [4, 1] instead of [2,2] 
print( "Tileset size", tileset:getSize() )

-- Issue #2
-- if the first index is set, it is rendering but getImage() doesn't return nil for indexes that are not set, size can be incorrect
tileset:setImage(1, tile)

-- Issue #3
-- if the first index is not set, getImage() always returns nil
-- tileset:setImage(2, tile)
-- tileset:setImage(3, tile)
-- tileset:setImage(4, tile)

-- Issue #4, if the first index is set and another one is also set, crash might happens
-- tileset:setImage(1, tile)
-- tileset:setImage(2, tile)

function playdate.update()
	playdate.graphics.clear()

	for cx = 1, cw do
		for cy = 1, ch do
			local tile = tileset:getImage(cx, cy)
			print("tileset coordinates:", cx, cy)
			if tile then
				print("image size:", tile:getSize())
				tile:draw( cx*iw, cy*ih )
			end
		end
	end
end

I guess I'm not too surprised there are bugs lurking in there, it's not a heavily trafficked part of the API. I've filed that, and I'll take a look at it once I can find the time. Thanks!

2 Likes

I revive this old thread because I tried again to generate image tables at runtime but it still crash at runtime.

I checked the old example from the first post and some issues seems to have been fixed in the meantime.

What I tried to do is when I load an imaginable used by a tilemap, I want to generate flipped version of the tiles. To do that I load the original imagetable, allocate a new one and want for each tile generate every flipped versions.

However in the example crash the simulator as soon as I try to allocate the new imaginable. In simpler example the allocation seems to work.

@dave You can check the example in this branch

The piece of code that crashed the game is here