Drawing to an image within the draw method of a sprite crashes the simulator if a stencil is set

SDK 2.7.3, macOS 15.5.

This minimal lua example consistently crashes the simulator. I’m not sure what happens on device – I’m afraid to test that because it somehow leaves the simulator in a broken state where it hangs on subsequent launches, and I have to force quit and reopen it several times before it starts working again.

import "CoreLibs/graphics"
import "CoreLibs/sprites"

local gfx <const> = playdate.graphics

local testSprite = gfx.sprite.new()
testSprite:setSize(10, 10)
function testSprite:draw(x, y, width, height)
	local testImg = gfx.image.new(10, 10, gfx.kColorWhite)
	gfx.pushContext(testImg)
	gfx.setColor(gfx.kColorBlack)
	gfx.fillRect(0, 0, 10, 10)
	gfx.popContext()
	testImg:draw(0, 0)
end
testSprite:setStencilPattern(0.5) -- this in combo with the above draw method makes it crash
testSprite:add()

function playdate.update()
	gfx.sprite.update()
end

I know the way this code creates a new image every frame is silly, it’s just a quick example that triggers the crash. In my actual project I have a sprite that creates new images on the fly in its draw method, but it caches them so it isn’t doing it every frame. This works fine unless I set a stencil on the sprite, at which point it crashes.

The crash still occurs if push/popContext are swapped for lock/unlockFocus.

I believe this is already fixed for 2.7.4, scheduled for release in the next day or two. :slight_smile:

1 Like

This still crashes the sim on 2.7.4 :grimacing:

It does look like whatever was causing it to hang on subsequent launches is fixed, at least.