Changing draw mode for primitives within a sprite's draw method

So I'm using the Windows SDK and I'm trying to draw some filled polys, I decided to do this by utilizing sprites. I ran into an issue when trying to draw one of these polys with a dither pattern. Here is how I make the sprites in my draw loop:

local polySprite = gfx.sprite.new()
polySprite:setCenter(0, 0)
polySprite:setSize(400, 240)
polySprite:setZIndex(0)

function polySprite:draw()
	-- Set pattern
	gfx.setPattern(ditherPattern)

	-- Add road poly
	gfx.fillPolygon(poly)

	-- Re-set color
	gfx.setColor(gfx.kColorXOR)
end

When I run this code, the dither pattern is not applied to the poly. This only seems to happen when I set the color/pattern and call fillPolygon inside the sprite's draw method. If I take it out of polySprite:draw, it works fine. Can anyone explain why this happens and/or any workarounds? It seems like its just a limitation of sprite drawing and I'd like to continue using sprites if possible.

Did you forget to add the sprite, maybe? This is working as expected for me:

local gfx = playdate.graphics
local polySprite = gfx.sprite.new()

polySprite:setCenter(0, 0)
polySprite:setSize(400, 240)
polySprite:setZIndex(0)

ditherPattern = { 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa }
poly = playdate.geometry.polygon.new(10,10, 390,10, 390,230, 10,230, 10,10)

function polySprite:draw(x,y,w,h)
	gfx.setPattern(ditherPattern)
	gfx.fillPolygon(poly)
	gfx.setColor(gfx.kColorXOR)
end

polySprite:add()

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

Tangentially, I'm pretty sure resetting the color at the end isn't necessary, it should restore the previous graphics context after drawing the spring. If it doesn't, that's a bug.

1 Like

Thanks for the input, I went through where I added the sprites more closely and I realized that I actually added another sprite within the polySprite:draw() method, which was causing some issues. I think you're also correct about not needing to reset the color. All sorted now, appreciate it!

2 Likes